B s `f@s\dZddlmZddlmZddlmZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZy ddlZWnek rdZYnXddlmZddlmZddlmZdd lm Z dd l!m"Z"dd l#m$Z$dd l#m%Z%dd l&m'Z'ddl(Z(ddl)m*Z*ddl)m+Z+y\ddl,Z,ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mZ>Wnek rYn6Xe,j?re,@dZAe(jBre'jCjDZEnddlFZejCjDZEe(jGr,ddlmHZHn(y ddlHZHWnek rRdZHYnXejIZIejJZJejKZKejLZLejMZMejNZNe(jOe(jPfZQdZRddZSGddde"jTZUddZVddZWddZXddZYejZd eVd!dd"ej[d#eWd$dd"ej[d%eXd&dd"ej[d'd(d)dd"e[d*d(d+d,d-Z\e\e]fd.d/Z^Gd0d1d1e_Z`Gd2d3d3e_ZaGd4d5d5e'jbZbd6d7Zcd8d9Zdd:d;Zedd?Zgd@dAZhdBdCZidldDdEZjdFdGZkdHdIZldJdKZmdLdMZnGdNdOdOe_ZodPdQZpdRdSZqdTdUZrGdVdWdWejsZsdXdYZtdZd[Zud\d]Zvdmd^d_Zwd`daZxdbdcZydddeZzdfdgZ{dhdiZ|djdkZ}dS)nzBase functionality for Abseil Python tests. This module contains base classes and high-level functions for Abseil-style tests. )absolute_import)division)print_functionN)app)flags)logging)abc)enum)_pretty_print_reporter) xml_reporter)unittest3_backport)urllib)xrange)AnyAnyStrBinaryIOCallableContextManagerIOIteratorListMappingMutableMappingMutableSequenceOptionalSequenceTextTextIOTupleTypeUnion_T)mockTcCs~|r tjSddSdS)aHExpects the test to fail if the run condition is True. Example usage: @expectedFailureIf(sys.version.major == 2, "Not yet working in py2") def test_foo(self): ... Args: condition: bool, whether to expect failure or not. reason: Text, the reason to expect failure. Returns: Decorator function cSs|S)N)fr#r#9/tmp/pip-unpacked-wheel-00lyeop_/absl/testing/absltest.pyz#expectedFailureIf..N)unittestexpectedFailure) conditionreasonr#r#r%expectedFailureIfxsr,c@seZdZdZdZdZdS)TempFileCleanupalwayssuccessneverN)__name__ __module__ __qualname__ALWAYSSUCCESSOFFr#r#r#r%r-sr-cCs8d}tjdd}y t|}Wntk r2YnX|S)Ni-ZTEST_RANDOM_SEED)osenvirongetint ValueError)Z random_seedvaluer#r#r%_get_default_test_random_seeds r>cCstjddS)z Returns default test source dir.Z TEST_SRCDIRr7)r8r9r:r#r#r#r%get_default_test_srcdirsr?cCs(tjdd}|s$tjtd}|S)zReturns default test temp dir.Z TEST_TMPDIRr7Z absl_testing)r8r9r:pathjointempfile gettempdir)Ztmpdirr#r#r%get_default_test_tmpdirsrDcCstdjrtj}ndtjkr(tjd}nd}|s4dS|dkrLtddS|dkrXdSyt|}|dkrn|SWnt k rYnXt d |d S) aReturns default seed to use for randomizing test order. This function first checks the --test_randomize_ordering_seed flag, and then the TEST_RANDOMIZE_ORDERING_SEED environment variable. If the first value we find is: * (not set): disable test randomization * 0: disable test randomization * 'random': choose a random seed in [1, 4294967295] for test order randomization * positive integer: use this seed for test order randomization (The values used are patterned after https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED). In principle, it would be simpler to return None if no override is provided; however, the python random module has no `get_seed()`, only `getstate()`, which returns far more data than we want to pass via an environment variable or flag. Returns: A default value for test case randomization (int). 0 means do not randomize. Raises: ValueError: Raised when the flag or env value is not one of the options above. test_randomize_ordering_seedZTEST_RANDOMIZE_ORDERING_SEEDr7rrandoml0z)Unknown test randomization seed value: {}N) FLAGSpresentrEr8r9rFRandomrandintr;r<format)Z randomizeseedr#r#r%$_get_default_randomize_ordering_seeds&   rOZtest_random_seedzRandom seed for testing. Some test frameworks may change the default value of this flag between runs, so it is not appropriate for seeding probabilistic tests.)Zallow_override_cppZ test_srcdirz.Root of directory tree where source files live test_tmpdirz%Directory for temporary testing filesrEr7aIf positive, use this as a seed to randomize the execution order for test cases. If "random", pick a random seed to use. If 0 or not set, do not randomize test case execution order. This flag also overrides the TEST_RANDOMIZE_ORDERING_SEED environment variable.xml_output_filezFile to store XML test resultscCsLdd}t}|tdd|rH|tj_|rHtjddS)z3Workaround for .cSs4t|jt|jko.t|jko.dkSS)zTells whether or not this result was a success. Any unexpected pass is to be counted as a non-success. Args: self: The TestResult instance. Returns: Whether or not this result was a success. r)lenZfailureserrorsZunexpectedSuccesses)selfr#r#r% wasSuccessful s zF_monkey_patch_test_result_for_unexpected_passes..wasSuccessfulcSsdS)Nr#r#r#r#r%r&r'zA_monkey_patch_test_result_for_unexpected_passes..z^unittest.result.TestResult monkey patch to report unexpected passes as failures did not work. N)r(Z TestResultZaddUnexpectedSuccessZFunctionTestCaserUsysstderrwrite)rUZ test_resultr#r#r%/_monkey_patch_test_result_for_unexpected_passessrYcCs"tjr|||S|||ddSdS)aOpens a file. Like open(), but compatible with Python 2 and 3. Also ensures that we can open real files even if tests stub out open(). Args: filepath: A filepath. mode: A mode. _open_func: A built-in open() function. Returns: The opened file object. zutf-8)encodingN)sixPY2)filepathmodeZ _open_funcr#r#r%_open&s r_c@s@eZdZdZddZeddZddZdd d ZdddZ dS)_TempDiraRepresents a temporary directory for tests. Creation of this class is internal. Using its public methods is OK. This class implements the `os.PathLike` interface (specifically, `os.PathLike[str]`). This means, in Python 3, it can be directly passed to e.g. `os.path.join()`. cCs ||_dS)z2Module-private: do not instantiate outside module.N)_path)rTr@r#r#r%__init__Esz_TempDir.__init__cCs|jS)zReturns the path, as a string, for the directory. TIP: Instead of e.g. `os.path.join(temp_dir.full_path)`, you can simply do `os.path.join(temp_dir)` because `__fspath__()` is implemented. )ra)rTr#r#r% full_pathJsz_TempDir.full_pathcCs|jS)zSee os.PathLike.)rc)rTr#r#r% __fspath__Tsz_TempDir.__fspath__Nwutf8strictcCst|j|||||\}}|S)aCreate a file in the directory. NOTE: If the file already exists, it will be made writable and overwritten. Args: file_path: Optional file path for the temp file. If not given, a unique file name will be generated and used. Slashes are allowed in the name; any missing intermediate directories will be created. NOTE: This path is the path that will be cleaned up, including any directories in the path, e.g., 'foo/bar/baz.txt' will `rm -r foo` content: Optional string or bytes to initially write to the file. If not specified, then an empty file is created. mode: Mode string to use when writing content. Only used if `content` is non-empty. encoding: Encoding to use when writing string content. Only used if `content` is text. errors: How to handle text to bytes encoding errors. Only used if `content` is text. Returns: A _TempFile representing the created file. ) _TempFile_createra)rT file_pathcontentr^rZrStf_r#r#r% create_fileYs z_TempDir.create_filecCs4|rtj|j|}ntj|jd}t|t|S)zCreate a directory in the directory. Args: dir_path: Optional path to the directory to create. If not given, a unique name will be generated and used. Returns: A _TempDir representing the created directory. )dir)r8r@rArarBmkdtemp_makedirs_exist_okr`)rTZdir_pathr@r#r#r%mkdirvs z_TempDir.mkdir)NNrerfrg)N) r1r2r3__doc__rbpropertyrcrdrnrrr#r#r#r%r`;s  r`c@seZdZdZddZeddZeddZdd Z dd d Z ddZ d ddZ d!ddZ d"ddZd#ddZejd$ddZdS)%rhaRepresents a tempfile for tests. Creation of this class is internal. Using its public methods is OK. This class implements the `os.PathLike` interface (specifically, `os.PathLike[str]`). This means, in Python 3, it can be directly passed to e.g. `os.path.join()`. cCs ||_dS)zPrivate: use _create instead.N)ra)rTr@r#r#r%rbsz_TempFile.__init__c Cs|rntj|t|}tj||}ttj|tj|rt|tjst |} t || j t j Bn*t|t jt|d\} }t| |}||} |rt|tjr| j||||dq| ||n | d| |fS)z+Module-private: create a tempfile instance.)ro)r^rZrSr')r8r@rA_get_first_partrqdirnameexistsaccessW_OKstatchmodst_modeS_IWUSRrBmkstempstrclose isinstancer[ text_type write_text write_bytes) cls base_pathrjrkr^rZrS cleanup_pathr@ stat_infofdrlr#r#r%ris$    z_TempFile._createcCs|jS)zReturns the path, as a string, for the file. TIP: Instead of e.g. `os.path.join(temp_file.full_path)`, you can simply do `os.path.join(temp_file)` because `__fspath__()` is implemented. )ra)rTr#r#r%rcsz_TempFile.full_pathcCs|jS)zSee os.PathLike.)rc)rTr#r#r%rdsz_TempFile.__fspath__rfrgc Cs"|j||d }|SQRXdS)z(Return the contents of the file as text.)rZrSN) open_textread)rTrZrSfpr#r#r% read_textsz_TempFile.read_textc Cs| }|SQRXdS)z(Return the content of the file as bytes.N) open_bytesr)rTrr#r#r% read_bytess z_TempFile.read_bytesrec CsFtjrt|tr|||}|j|||d}||WdQRXdS)aWrite text to the file. Args: text: Text to write. In Python 2, it can be bytes, which will be decoded using the `encoding` arg (this is as an aid for code that is 2 and 3 compatible). mode: The mode to open the file for writing. encoding: The encoding to use when writing the text to the file. errors: The error handling strategy to use when converting text to bytes. )rZrSN)r[r\rbytesdecoderrX)rTtextr^rZrSrr#r#r%rs  z_TempFile.write_textwbc Cs$||}||WdQRXdS)zWrite bytes to the file. Args: data: bytes to write. mode: Mode to open the file for writing. The "b" flag is implicit if not already present. It must not have the "t" flag. N)rrX)rTdatar^rr#r#r%rs z_TempFile.write_bytesrtcCs8d|krtd|d|kr&|d7}||||}|S)aReturn a context manager for opening the file in text mode. Args: mode: The mode to open the file in. The "t" flag is implicit if not already present. It must not have the "b" flag. encoding: The encoding to use when opening the file. errors: How to handle decoding errors. Returns: Context manager that yields an open file. Raises: ValueError: if invalid inputs are provided. bzFInvalid mode {!r}: "b" flag not allowed when opening file in text modet)r<rMr_)rTr^rZrScmr#r#r%rsz_TempFile.open_textrbcCs:d|krtd|d|kr&|d7}|j|ddd}|S)aLReturn a context manager for opening the file in binary mode. Args: mode: The mode to open the file in. The "b" mode is implicit if not already present. It must not have the "t" flag. Returns: Context manager that yields an open file. Raises: ValueError: if invalid inputs are provided. rzHInvalid mode {!r}: "t" flag not allowed when opening file in binary moderN)rZrS)r<rMr_)rTr^rr#r#r%r sz_TempFile.open_bytesc cs*tj|j|||d }|VWdQRXdS)N)r^rZrS)ioopenrc)rTr^rZrSrr#r#r%r_&sz_TempFile._openN)rfrg)rerfrg)r)rrfrg)r)rfrg)r1r2r3rsrb classmethodrirtrcrdrrrrrr contextlibcontextmanagerr_r#r#r#r%rhs !     rhcseZdZdZejZdZdZfddZ fddZ did d Z djddZ ddZ eddZddZddZddZddZddZfddZdkd d!Zdld"d#Zdmd$d%Zdnd&d'Zdod(d)Zdpd*d+Zdqd,d-Zdrd.d/Zdsd0d1Zdtd2d3Zdud4d5Z dvfd6d7 Z!e"j#r dwfd8d9 Z$dxd:d;Z%dydd?Z'e"j#r^d@dAZ(dBdCZ)dDdEZ*d{dFdGZ+d|dIdJZ,d}dKdLZ-GdMdNdNe.Z/d~dOdPZ0ddQdRZ1ddSdTZ2ddUdVZ3ddWdXZ4dYdZZ5dd[d\Z6dd]d^Z7ddadbZ8ddcddZ9fdedfZ:dfdgdh Z;Zs zTestCase.__init__cs4tt|ttdr0t|_||jjdS)N ExitStack) rrsetUphasattrrrr addCleanupr)rT)rr#r%rEs  zTestCase.setUpNcCsj|}|r.tj||}tj|t|}nt|tj|d}|}t|t|| ||t |S)aCreate a temporary directory specific to the test. NOTE: The directory and its contents will be recursively cleared before creation. This ensures that there is no pre-existing state. This creates a named directory on disk that is isolated to this test, and will be properly cleaned up by the test. This avoids several pitfalls of creating temporary directories for test purposes, as well as makes it easier to setup directories and verify their contents. For example: def test_foo(self): out_dir = self.create_tempdir() out_log = out_dir.create_file('output.log') expected_outputs = [ os.path.join(out_dir, 'data-0.txt'), os.path.join(out_dir, 'data-1.txt'), ] code_under_test(out_dir) self.assertTrue(os.path.exists(expected_paths[0])) self.assertTrue(os.path.exists(expected_paths[1])) self.assertEqual('foo', out_log.read_text()) See also: `create_tempfile()` for creating temporary files. Args: name: Optional name of the directory. If not given, a unique name will be generated and used. cleanup: Optional cleanup policy on when/if to remove the directory (and all its contents) at the end of the test. If None, then uses `self.tempfile_cleanup`. Returns: A _TempDir representing the created directory; see _TempDir class docs for usage. )ro) _get_tempdir_path_testr8r@rArurqrBrp_rmtree_ignore_errors_maybe_add_temp_path_cleanupr`)rTnamecleanup test_pathr@rr#r#r%create_tempdirLs%  zTestCase.create_tempdirrerfrgc Cs2|}tj||||||d\}} || ||S)aCreate a temporary file specific to the test. This creates a named file on disk that is isolated to this test, and will be properly cleaned up by the test. This avoids several pitfalls of creating temporary files for test purposes, as well as makes it easier to setup files, their data, read them back, and inspect them when a test fails. For example: def test_foo(self): output = self.create_tempfile() code_under_test(output) self.assertGreater(os.path.getsize(output), 0) self.assertEqual('foo', output.read_text()) NOTE: This will zero-out the file. This ensures there is no pre-existing state. NOTE: If the file already exists, it will be made writable and overwritten. See also: `create_tempdir()` for creating temporary directories, and `_TempDir.create_file` for creating files within a temporary directory. Args: file_path: Optional file path for the temp file. If not given, a unique file name will be generated and used. Slashes are allowed in the name; any missing intermediate directories will be created. NOTE: This path is the path that will be cleaned up, including any directories in the path, e.g., 'foo/bar/baz.txt' will `rm -r foo`. content: Optional string or bytes to initially write to the file. If not specified, then an empty file is created. mode: Mode string to use when writing content. Only used if `content` is non-empty. encoding: Encoding to use when writing string content. Only used if `content` is text. errors: How to handle text to bytes encoding errors. Only used if `content` is text. cleanup: Optional cleanup policy on when/if to remove the directory (and all its contents) at the end of the test. If None, then uses `self.tempfile_cleanup`. Returns: A _TempFile representing the created file; see _TempFile class docs for usage. )rkr^rZrS)rrhrir) rTrjrkr^rZrSrrrlrr#r#r%create_tempfiles 0   zTestCase.create_tempfilecCs|jstd|j|S)aHReturns the CM's value after registering it with the exit stack. Entering a context pushes it onto a stack of contexts. The context is exited when the test completes. Contexts are are exited in the reverse order of entering. They will always be exited, regardless of test failure/success. The context stack is specific to the test being run. This is useful to eliminate per-test boilerplate when context managers are used. For example, instead of decorating every test with `@mock.patch`, simply do `self.foo = self.enter_context(mock.patch(...))' in `setUp()`. NOTE: The context managers will always be exited without any error information. This is an unfortunate implementation detail due to some internals of how unittest runs tests. Args: manager: The context manager to enter. zgself._exit_stack is not set: enter_context is Py3-only; also make sure that AbslTest.setUp() is called.)rAssertionError enter_context)rTmanagerr#r#r%rszTestCase.enter_contextcCstjtjt|S)N)r8r@rArIrP _get_qualname)rr#r#r%_get_tempdir_path_clsszTestCase._get_tempdir_path_clscCstj||jS)N)r8r@rAr_testMethodName)rTr#r#r%rszTestCase._get_tempdir_path_testcCs|dk r |S|jS)N)tempfile_cleanup)rToverrider#r#r%_get_tempfile_cleanupszTestCase._get_tempfile_cleanupcCsZ||}|tjkrdS|tjkr0|t|n&|tjkrH|t|ntd |dS)NzUnexpected cleanup value: {}) rr-r6r4rrr5_internal_cleanup_on_successrrM)rTr@rr#r#r%rs    z%TestCase._maybe_add_temp_path_cleanupcs$fdd}j|f||dS)Ncss dS||dS)N)_ran_and_passed)rr)functionrTr#r%_call_cleaner_on_successszGTestCase._internal_cleanup_on_success.._call_cleaner_on_success)r)rTrrrrr#)rrTr%rsz%TestCase._internal_cleanup_on_successcCs$|j}|}|||j|S)N)rZdefaultTestResultZ_feedErrorsToResultrSrU)rTZoutcomeresultr#r#r%rszTestCase._ran_and_passedcsJ|}|dr"|tdd}tt|}|dk rFd||f}|S)arFormats both the test method name and the first line of its docstring. If no docstring is given, only returns the method name. This method overrides unittest.TestCase.shortDescription(), which only returns the first line of the docstring, obscuring the name of the test upon failure. Returns: desc: A short description of a test method. z __main__.N )id startswithrRrrshortDescriptionrA)rTdescZdoc_first_line)rr#r%rs  zTestCase.shortDescriptioncCs"||s|d||f|dS)zAsserts that actual.startswith(expected_start) is True. Args: actual: str expected_start: str msg: Optional message to report on failure. z%r does not start with %rN)rfail)rTactualZexpected_startmsgr#r#r%assertStartsWiths zTestCase.assertStartsWithcCs"||r|d||f|dS)zAsserts that actual.startswith(unexpected_start) is False. Args: actual: str unexpected_start: str msg: Optional message to report on failure. z%r does start with %rN)rr)rTrZunexpected_startrr#r#r%assertNotStartsWith(s zTestCase.assertNotStartsWithcCs"||s|d||f|dS)zAsserts that actual.endswith(expected_end) is True. Args: actual: str expected_end: str msg: Optional message to report on failure. z%r does not end with %rN)endswithr)rTrZ expected_endrr#r#r%assertEndsWith3s zTestCase.assertEndsWithcCs"||r|d||f|dS)zAsserts that actual.endswith(unexpected_end) is False. Args: actual: str unexpected_end: str msg: Optional message to report on failure. z%r does end with %rN)rr)rTrZunexpected_endrr#r#r%assertNotEndsWith>s zTestCase.assertNotEndsWithc Csy t|}Wn"ttfk r.|g}d}YnXy t|}Wn0ttfk rl|d|t|f|YnX||kst||d||f|s|r|dt||f|y|||d||Wn(tk r|d||f|YnXdS)aAn equality assertion for the beginning of ordered sequences. If prefix is an empty sequence, it will raise an error unless whole is also an empty sequence. If prefix is not a sequence, it will raise an error if the first element of whole does not match. Args: prefix: A sequence expected at the beginning of the whole parameter. whole: The sequence in which to look for prefix. msg: Optional message to report on failure. rGz>For whole: len(%s) is not supported, it appears to be type: %sz4Prefix length (%d) is longer than whole length (%d).z-Prefix length is 0 but whole length is %d: %sNz+prefix: %s not found at start of whole: %s.)rR TypeErrorNotImplementedErrorrtyper_formatMessageZassertSequenceEqual)rTprefixZwholer prefix_lenZ whole_lenr#r#r%assertSequenceStartsWithIs,   z!TestCase.assertSequenceStartsWithcCsHt|tjs$|dt|j|t|rD|d|t||dS)zAsserts that an object has zero length. Args: container: Anything that implements the collections.abc.Sized interface. msg: Optional message to report on failure. z"Expected a Sized object, got: {!r}z{!r} has length of {}.N)rrSizedrrMrr1rR)rT containerrr#r#r% assertEmptyss  zTestCase.assertEmptycCsBt|tjs$|dt|j|t|s>|d||dS)zAsserts that an object has non-zero length. Args: container: Anything that implements the collections.abc.Sized interface. msg: Optional message to report on failure. z"Expected a Sized object, got: {!r}z{!r} has length of 0.N)rrrrrMrr1rR)rTrrr#r#r%assertNotEmptys  zTestCase.assertNotEmptycCsZt|tjs$|dt|j|t||krVtj |}|d|t|||dS)zAsserts that an object has the expected length. Args: container: Anything that implements the collections.abc.Sized interface. expected_len: The expected length of the container. msg: Optional message to report on failure. z"Expected a Sized object, got: {!r}z!{} has length of {}, expected {}.N) rrrrrMrr1rRr(util safe_repr)rTr expected_lenrZcontainer_reprr#r#r% assertLens   zTestCase.assertLenc Cst|t|kr,|dt|t||g}xntt||D]\\}\}} y|j|| |||dWq@|jk r} z|d|| Wdd} ~ XYq@Xq@W|rt|dkr|dddg}||d |}||dS)akAn approximate equality assertion for ordered sequences. Fail if the two sequences are unequal as determined by their value differences rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between each value in the two sequences is more than the given delta. Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit). If the two sequences compare equal then they will automatically compare almost equal. Args: expected_seq: A sequence containing elements we are expecting. actual_seq: The sequence that we are testing. places: The number of decimal places to compare. msg: The message to be printed if the test fails. delta: The OK difference between compared values. z Sequence size mismatch: {} vs {})placesrdeltazAt index {}: {}Nz...r) rRrrM enumeratezipZassertAlmostEqualfailureExceptionappendrrA) rT expected_seq actual_seqrrrZerr_listidxZexp_elemZact_elemerrr#r#r%assertSequenceAlmostEquals  ( z"TestCase.assertSequenceAlmostEqualcCs2t|t|}|sdS|d|||f|dS)zBChecks whether actual iterable is a superset of expected iterable.Nz+Missing elements %s Expected: %s Actual: %s)setr)rTZexpected_subsetZ actual_setrmissingr#r#r%assertContainsSubsets zTestCase.assertContainsSubsetcCs2t|t|@}|sdS|d|||f|dS)zBChecks whether actual iterable and expected iterable are disjoint.Nz*Common elements %s Expected: %s Actual: %s)rr)rTrrrcommonr#r#r%assertNoCommonElementss zTestCase.assertNoCommonElementscs4tjrtt||||ntt||||dS)aDeprecated, please use assertCountEqual instead. This is equivalent to assertCountEqual in Python 3. An implementation of assertCountEqual is also provided by absltest.TestCase for Python 2. Args: expected_seq: A sequence containing elements we are expecting. actual_seq: The sequence that we are testing. msg: The message to be printed if the test fails. N)r[PY3rrassertCountEqualassertItemsEqual)rTrrr)rr#r%rs zTestCase.assertItemsEqualcstt||||dS)aTests two sequences have the same elements regardless of order. It tests that the first sequence contains the same elements as the second, regardless of their order. When they don't, an error message listing the differences between the sequences will be generated. Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: self.assertEqual(Counter(list(expected_seq)), Counter(list(actual_seq))) but works with sequences of unhashable objects as well. Example: - [0, 1, 1] and [1, 0, 1] compare equal. - [0, 0, 1] and [0, 1] compare unequal. Args: expected_seq: A sequence containing elements we are expecting. actual_seq: The sequence that we are testing. msg: The message to be printed if the test fails. N)rrr)rTrrr)rr#r%rszTestCase.assertCountEqualcs"t|tst|tr&|d||fy\tdd|Dtdd|DfddD}fddD}||WnBtk rt|t|t\}}YnXg}|r||df|r| d||r| d ||s|r|d |d S) a/Asserts that two sequences have the same elements (in any order). This method, unlike assertCountEqual, doesn't care about any duplicates in the expected and actual sequences. >> assertSameElements([1, 1, 1, 0, 0, 0], [0, 1]) # Doesn't raise an AssertionError If possible, you should use assertCountEqual instead of assertSameElements. Args: expected_seq: A sequence containing elements we are expecting. actual_seq: The sequence that we are testing. msg: The message to be printed if the test fails. zuPassing string/bytes to assertSameElements is usually a bug. Did you mean to use assertEqual? Expected: %s Actual: %scSsg|] }|dfqS)Nr#).0elementr#r#r% 1sz/TestCase.assertSameElements..cSsg|] }|dfqS)Nr#)rrr#r#r%r2scsg|]}|kr|qSr#r#)rr)rr#r%r3scsg|]}|kr|qSr#r#)rr)expectedr#r%r4sz: zExpected, but missing: %r zUnexpected, but present: %r r7N) r_TEXT_OR_BINARY_TYPESrdictsortrlist_sorted_list_differenceextendrrA)rTrrrr unexpectedrSr#)rrr%assertSameElementss4     zTestCase.assertSameElementsc Kst|tjstd|ft|tjs4td|f|dd}|rVtdt|||krbdS|rr|dg}ndg}|r|t|7}x>t | d | d D]"}| || ds| dqW|rt||krt||}|d|}| d ||d |dS) z.Asserts that two multi-line strings are equal.z"First argument is not a string: %rz#Second argument is not a string: %r line_limitrzUnexpected keyword args {}Nz: rTz3(... and {} more delta lines omitted for brevity.) r7)rr[ string_typesrpoprrMtuplerRdifflibZndiff splitlinesrrrrA) rTfirstsecondrrrZfailure_messagelineZ n_omittedr#r#r%assertMultiLineEqualKs2         zTestCase.assertMultiLineEqualcCs:||d|||f}|||k||||k|dS)z8Asserts that value is between minv and maxv (inclusive).z+"%r" unexpectedly not between "%r" and "%r"N)r assertTrue)rTr=ZminvZmaxvrr#r#r% assertBetweenjs zTestCase.assertBetweencOs |j||S)N)ZassertRegexpMatches)rTrrr#r#r% assertRegexuszTestCase.assertRegexcOs |j||S)N)ZassertRaisesRegexp)rTrrr#r#r%assertRaisesRegexxszTestCase.assertRaisesRegexcOs |j||S)N)ZassertNotRegexpMatches)rTrrr#r#r%assertNotRegex{szTestCase.assertNotRegexcCst|tr|d||s&|d|t|d}x,|ddD]}t||k r@|d|q@W|tkrt|tjrdd|D}tj}n&|tjkrt|trd d|D}t}|tjkrd d |}n(|tkrd d |d}n |d|t ||tj s|d||f|dS)aAsserts that at least one regex in regexes matches str. If possible you should use `assertRegex`, which is a simpler version of this method. `assertRegex` takes a single regular expression (a string or re compiled object) instead of a list. Notes: 1. This function uses substring matching, i.e. the matching succeeds if *any* substring of the error message matches *any* regex in the list. This is more convenient for the user than full-string matching. 2. If regexes is the empty list, the matching will always fail. 3. Use regexes=[''] for a regex that will always pass. 4. '.' matches any single character *except* the newline. To match any character, use '(.|\n)'. 5. '^' matches the beginning of each line, not just the beginning of the string. Similarly, '$' matches the end of each line. 6. An exception will be thrown if regexes contains an invalid regex. Args: actual_str: The string we try to match with the items in regexes. regexes: The regular expressions we want to match against str. See "Notes" above for detailed notes on how this is interpreted. message: The message to be printed if the test fails. z4regexes is string or bytes; use assertRegex instead.zNo regexes specified.rrGNz'regexes list must all be the same type.cSsg|]}|dqS)zutf-8)r)rregexr#r#r%rsz-TestCase.assertRegexMatch..cSsg|]}|dqS)zutf-8)encode)rr r#r#r%rsz(?:%s)z)|(?:s(?:s)|(?:)z8Only know how to deal with unicode str or bytes regexes.z/"%s" does not contain any of these regexes: %s.) rrrrrr[rrAresearch MULTILINE)rTZ actual_strregexesmessageZ regex_typer r#r#r%assertRegexMatch~s0     zTestCase.assertRegexMatchr'c Cst|||\}}t|dtjr.dd|D}t|}||d||dt||t|f|j||||dt||t||fddS)anAsserts that a shell command succeeds (i.e. exits with code 0). Args: command: List or string representing the command to run. regexes: List of regular expression byte strings that match success. env: Dictionary of environment variable settings. If None, no environment variables will be set for the child process. This is to make tests more hermetic. NOTE: this behavior is different than the standard subprocess module. close_fds: Whether or not to close all open fd's in the child after forking. msg: Optional message to report on failure. rcSsg|]}|dqS)zutf-8)r )rr r#r#r%rsz2TestCase.assertCommandSucceeds..z;Running command %s failed with error code %s and message %szXRunning command %s failed with error code %s and message %s which matches no regex in %s)rN) get_command_stderrrr[rget_command_string assertEqualr_quote_long_stringr) rTcommandrenv close_fdsrret_codercommand_stringr#r#r%assertCommandSucceedss*zTestCase.assertCommandSucceedsc Cst|||\}}t|dtjr.dd|D}t|}||d||dt||j||||dt||t||fddS)aaAsserts a shell command fails and the error matches a regex in a list. Args: command: List or string representing the command to run. regexes: the list of regular expression strings. env: Dictionary of environment variable settings. If None, no environment variables will be set for the child process. This is to make tests more hermetic. NOTE: this behavior is different than the standard subprocess module. close_fds: Whether or not to close all open fd's in the child after forking. msg: Optional message to report on failure. rcSsg|]}|dqS)zutf-8)r )rr r#r#r%rsz/TestCase.assertCommandFails..z:The following command succeeded while expected to fail: %szXRunning command %s failed with error code %s and message %s which matches no regex in %s)rN) rrr[rrZassertNotEqualrrr) rTrrrrrrrrr#r#r%assertCommandFailss$zTestCase.assertCommandFailsc@s&eZdZdddZddZddZdS) zTestCase._AssertRaisesContextNcCs||_||_||_||_dS)N)expected_exception test_case test_funcr)rTr!r"r#rr#r#r%rbsz&TestCase._AssertRaisesContext.__init__cCs|S)Nr#)rTr#r#r% __enter__sz'TestCase._AssertRaisesContext.__enter__cCs>|dkr |j|jjd|jt||js0dS||dS)Nz not raisedFT)r"rr!r1r issubclassr#)rTexc_type exc_valuetbr#r#r%__exit__s  z&TestCase._AssertRaisesContext.__exit__)N)r1r2r3rbr$r)r#r#r#r%_AssertRaisesContexts r*c sFfdd}||}|dkr(|S||||WdQRXdS)a?Asserts that exception is thrown and predicate(exception) is true. Args: expected_exception: Exception class expected to be raised. predicate: Function of one argument that inspects the passed-in exception and returns True (success) or False (please fail the test). callable_obj: Function to be called. *args: Extra args. **kwargs: Extra keyword args. Returns: A context manager if callable_obj is None. Otherwise, None. Raises: self.failureException if callable_obj does not raise a matching exception. cs|d|fdS)Nz%r does not match predicate %r)r)r) predicaterTr#r%Check:s z6TestCase.assertRaisesWithPredicateMatch..CheckN)r*)rTr!r+ callable_objrrr,contextr#)r+rTr%assertRaisesWithPredicateMatch(s z'TestCase.assertRaisesWithPredicateMatchc sFfdd}||}|dkr(|S||||WdQRXdS)a)Asserts that the message in a raised exception equals the given string. Unlike assertRaisesRegex, this method takes a literal string, not a regular expression. with self.assertRaisesWithLiteralMatch(ExType, 'message'): DoSomething() Args: expected_exception: Exception class expected to be raised. expected_exception_message: String message expected in the raised exception. For a raise exception e, expected_exception_message must equal str(e). callable_obj: Function to be called, or None to return a context. *args: Extra args. **kwargs: Extra kwargs. Returns: A context manager if callable_obj is None. Otherwise, None. Raises: self.failureException if callable_obj does not raise a matching exception. cs$t|}|kd|fdS)Nz9Exception message does not match. Expected: %r Actual: %r)rr)rZactual_exception_message)expected_exception_messagerTr#r%r,^s  z4TestCase.assertRaisesWithLiteralMatch..CheckN)r*)rTr!r0r-rrr,r.r#)r0rTr%assertRaisesWithLiteralMatchDs z%TestCase.assertRaisesWithLiteralMatchcCst|tttkrtntfr |f}d}d}xh|D]`}|t||}|dkrh|dkrh|d||f|n|dkr|d|||f||}|}q.WdS)atAsserts that the strings provided are found in the target in order. This may be useful for checking HTML output. Args: strings: A list of strings, such as [ 'fox', 'dog' ] target: A target string in which to look for the strings, such as 'The quick brown fox jumped over the lazy dog'. msg: Optional message to report on failure. rNzDid not find '%s' in '%s'z$Did not find '%s' after '%s' in '%s')rrrunicodefindr)rTstringstargetrZ current_indexZ last_stringstringindexr#r#r%assertContainsInOrderls  zTestCase.assertContainsInOrdercCshd}tt|}t|}x,|D]$}||kr0|}Px||kr@q2WqW|dk rd|d|||f|dS)aAsserts that "container" contains "subsequence" as a subsequence. Asserts that "container" contains all the elements of "subsequence", in order, but possibly with other elements interspersed. For example, [1, 2, 3] is a subsequence of [0, 0, 1, 2, 0, 3, 0] but not of [0, 0, 1, 3, 0, 2, 0]. Args: container: the list we're testing for subsequence inclusion. subsequence: the list we hope will be a subsequence of container. msg: Optional message to report on failure. Nz:%s not a subsequence of %s. First non-matching element: %s)rreversedrr)rTr subsequencerZfirst_nonmatchingZreversed_containerer#r#r%assertContainsSubsequences   z"TestCase.assertContainsSubsequencecCst|}t|}d}xjtdt|t|D]N}|t|kr@Pd}x,|t|krp|||||krp|d7}qFWt||}q.W|t|kr|d|||d|f|dS)aAsserts that "container" contains "subsequence" as an exact subsequence. Asserts that "container" contains all the elements of "subsequence", in order, and without other elements interspersed. For example, [1, 2, 3] is an exact subsequence of [0, 0, 1, 2, 3, 0] but not of [0, 0, 1, 2, 0, 3, 0]. Args: container: the list we're testing for subsequence inclusion. subsequence: the list we hope will be an exact subsequence of container. msg: Optional message to report on failure. rrGz>%s not an exact subsequence of %s. Longest matching prefix: %sN)rrrRmaxr)rTrr;rZ longest_matchstartr8r#r#r%assertContainsExactSubsequences    z'TestCase.assertContainsExactSubsequencec sfdd}fdd}|dx\tj|D]N}t|}x@t|ddD],\}}x"||ddD]}|||qjWqPWq2WxH|D]@} x| D]} || | qWx"t| | D]\} } || | qWqWdS) aAsserts that total ordering has been implemented correctly. For example, say you have a class A that compares only on its attribute x. Comparators other than __lt__ are omitted for brevity. class A(object): def __init__(self, x, y): self.x = x self.y = y def __hash__(self): return hash(self.x) def __lt__(self, other): try: return self.x < other.x except AttributeError: return NotImplemented assertTotallyOrdered will check that instances can be ordered correctly. For example, self.assertTotallyOrdered( [None], # None should come before everything else. [1], # Integers sort earlier. [A(1, 'a')], [A(2, 'b')], # 2 is after 1. [A(3, 'c'), A(3, 'd')], # The second argument is irrelevant. [A(4, 'z')], ['foo']) # Strings sort last. Args: *groups: A list of groups of elements. Each group of elements is a list of objects that are equal. The elements in each group must be less than the elements in the group after it. For example, these groups are totally ordered: [None], [1], [2, 2], [3]. **kwargs: optional msg keyword argument can be passed. c s||kd||f||kd||f||||kd||f||||kd||f||||kd||f||||kd||fdS)z$Ensures small is ordered before big.z%r unexpectedly equals %rz%r unexpectedly less than %rz(%r unexpectedly less than or equal to %rz%r unexpectedly greater than %rz+%r unexpectedly greater than or equal to %rN) assertFalserrZ assertLessassertLessEqualZ assertGreaterassertGreaterEqual)smallbig)rrTr#r% CheckOrders.     z1TestCase.assertTotallyOrdered..CheckOrderc s4||||kd||ft|tjrxt|tjrxt|t|dt||t||f||kd||f||kd||f||||||kd||f||kd||f||||dS)zEnsures that a and b are equal.z%r unexpectedly unequals %rz5hash %d of %r unexpectedly not equal to hash %d of %rz%r unexpectedly less than %rz%r unexpectedly greater than %rN) rrArrrHashablehashrBrC)ar)rrTr#r% CheckEquals>        z1TestCase.assertTotallyOrdered..CheckEqualrNr2rG)r: itertoolsproductrr) rTgroupsrrFrJelementsr8rDrEgrouprIrr#)rrTr%assertTotallyOrdereds(&   zTestCase.assertTotallyOrderedcs||t||d||t||ddd}||kr@dS|tt|}|tt|}g}g}g} tjjfdd} d| || ||rd |nd fg} xH|D]@\} } | |kr| | | fq| || kr| | | || fqWx&|D]\}}||kr| ||fqW|rB| d d fd d |D| rj| dd fdd | D|r| dd fdd |D| d | dS)aRaises AssertionError if a and b are not equal dictionaries. Args: a: A dict, the expected value. b: A dict, the actual value. msg: An optional str, the associated message. Raises: AssertionError: if the dictionaries are not equal. z"First argument is not a dictionaryz#Second argument is not a dictionarycSs"yt|Stk r|SXdS)N)sortedr)Z list_of_itemsr#r#r%SortedNsz(TestCase.assertDictEqual..SortedNcs4tfddt|D}dddd|DS)zDeterministic repr for dict.c3s"|]\}}||fVqdS)Nr#)rkv)rr#r% csz9TestCase.assertDictEqual..Repr..z{%s}z, css|]}d|VqdS)z%s: %sNr#)rpairr#r#r%rUes)rQr[ iteritemsrA)Zdiktentries)rr#r%Repr_s z&TestCase.assertDictEqual..Reprz %s != %s%sz (%s)r7z#Unexpected, but present entries: %sc3s&|]\}}d||fVqdS)z%s: %s Nr#)rrSrT)rr#r%rUxsz+TestCase.assertDictEqual..zrepr() of differing entries: %sc3s.|]&\}}}d|||fVqdS)z %s: %s != %s Nr#)rrSa_valueb_value)rr#r%rU}szMissing entries: %sc3s&|]\}}d||fVqdS)z%s: %s Nr#)rrSrT)rr#r%rUsr) ZassertIsInstancerrrr[rWr(rrrrAr)rTrIrrrRZa_itemsZb_itemsrrZ differentrYrZa_keyrZZb_keyr[r#)rr%assertDictEqual:sN   $  zTestCase.assertDictEqualcCstj|}tj|}||j|j|||j|j|||j|j|||j|j||t|j dt|j d|| tjj |j ddtjj |j dd|dS)z?Asserts that urls are equal, ignoring ordering of query params.;T)keep_blank_valuesN)r parseurlparserschemenetlocr@fragmentrQparamssplitr\parse_qsquery)rTrIrrZparsed_aZparsed_br#r#r%assertUrlEquals  zTestCase.assertUrlEqualrIrcCsdg}t||||||jdk rJ|jd}t||krJ|d|ddg}|r`|d||dS)aAsserts that two values contain the same structural content. The two arguments should be data trees consisting of trees of dicts and lists. They will be deeply compared by walking into the contents of dicts and lists; other items will be compared using the == operator. If the two structures differ in content, the failure message will indicate the location within the structures where the first difference is found. This may be helpful when comparing large structures. Mixed Sequence and Set types are supported. Mixed Mapping types are supported, but the order of the keys will not be considered in the comparison. Args: a: The first structure to compare. b: The second structure to compare. aname: Variable name to use for the first structure in assertion messages. bname: Variable name to use for the second structure. msg: Additional text to include in the failure message. NPrrGz...z; )_walk_structure_for_problemsmaxDiffrRrrA)rTrIranamebnamerproblemsZmax_problems_to_showr#r#r%assertSameStructures   zTestCase.assertSameStructurec Csyt|}Wn:tk rH}zt||d||fWdd}~XYnXyt|}Wn:tk r}zt||d||fWdd}~XYnX|j||dd|ddS)aAsserts that the JSON objects defined in two strings are equal. A summary of the differences will be included in the failure message using assertSameStructure. Args: first: A string containing JSON to decode and compare to second. second: A string containing JSON to decode and compare to first. msg: Additional text to include in the failure message. z(could not decode first JSON value %s: %sNz)could not decode second JSON value %s: %srr)rlrmr)jsonloadsr<rro)rTrrrZfirst_structuredr<Zsecond_structuredr#r#r%assertJsonEquals ""zTestCase.assertJsonEqualcsVytt|||Stk rBt|dd}tt||YnXtt|||S)Nrr)rr_getAssertEqualityFuncAttributeErrorrrb)rTrrZ test_method)rr#r%rss  zTestCase._getAssertEqualityFunccstt||||S)z=Fail immediately with the given message, optionally prefixed.)rrrr)rTrr)rr#r%rsz TestCase.fail)NN)NNrerfrgN)N)N)N)N)N)N)N)N)NNN)N)N)N)N)N)N)N)N)rNTN)NTN)N)N)N)N)N)N)N)rIrN)N)NN)=r1r2r3rsr-r4rrkZ longMessagerbrrrrrrrrrrrrrrrrrrrrrrrrr[r\rrrrr r r rrr objectr*r/r1r9r=r@rPr\rhrorrrsr __classcell__r#r#)rr%r.sr    7 6    *    ,  8   > - )  &   z N  %  rcCs*d}}g}g}xy||}||}||kr\|||d7}x|||krX|d7}qBWn||kr|||d7}xf|||kr|d7}qxWnL|d7}zx|||kr|d7}qWWd|d7}x|||kr|d7}qWXWqtk r|||d|||dPYqXqW||fS)aNFinds elements in only one or the other of two, sorted input lists. Returns a two-element tuple of lists. The first list contains those elements in the "expected" list but not in the "actual" list, and the second contains those elements in the "actual" list but not in the "expected" list. Duplicate elements in either input list are ignored. Args: expected: The list we expected. actual: The list we actualy got. Returns: (missing, unexpected) missing: items in expected that are not in actual. unexpected: items in actual that are not in expected. rrGN)r IndexErrorr)rrijrrr<rIr#r#r%rs:   rcCst|tjot|tjS)N)rr[ integer_types)rIrr#r#r%_are_both_of_integer_typesr{cCs0t|tjo.t|tjo.t|t o.t|t S)N)rrrr)rIrr#r#r%_are_both_of_sequence_type!s r|cCst|tjot|tjS)N)rrSet)rIrr#r#r%_are_both_of_set_type(sr~cCst|tjot|tjS)N)rrr)rIrr#r#r%_are_both_of_mapping_type-src CsTt|t|krZt||sZt||sZt||sZt||sZ|d|t||t|fdSt|tjrx(|D] }||krl|d|||fqlWx(|D] }||kr|d|||fqWnt|tj r^xZ|D]R}||kr t ||||d||fd||f|q|d|||||fqWx2|D]*}||kr.|d|||||fq.Wnt|tj r0t|t s0t t|t|}x:t|D].}t ||||d||fd||f|qWx2t|t|D] }|d |||||fqWxTt|t|D] }|d |||||fq Wn ||krP|d ||||fdS) z4The recursive comparison behind assertSameStructure.z%s is a %r but %s is a %rNz%s has %r but %s does notz%s lacks %r but %s has itz%s[%r]z0%s has [%r] with value %r but it's missing in %sz)%s lacks [%r] but %s has it with value %rz%s[%d]z)%s has [%i] with value %r but %s does notz)%s lacks [%i] but %s has it with value %rz%s is %r but %s is %r)rr{r|r~rrrrr}rrjrrminrRr)rIrrlrmZ problem_listrSZminlenrxr#r#r%rj3sV            rjcCs^t|tjr|Stjdkr$d|Sd}x$|D]}|d|ddd7}q.W|ddSdS) zReturns an escaped string that can be used as a shell command. Args: command: List or string representing the command to run. Returns: A string suitable for use as a shell command. nt r7'z'"'"'z' Nr2)rr[rr8rrAreplace)rrwordr#r#r%rns    rcCs\|dkr i}tjdkrd}t|tj}tj||||tjtjd}| d}| }||fS)afRuns the given shell command and returns a tuple. Args: command: List or string representing the command to run. env: Dictionary of environment variable settings. If None, no environment variables will be set for the child process. This is to make tests more hermetic. NOTE: this behavior is different than the standard subprocess module. close_fds: Whether or not to close all open fd's in the child after forking. On Windows, this is ignored and close_fds is always False. Returns: Tuple of (exit status, text printed to stdout and stderr by the command). NrF)rrshellrWstdoutr) r8rrr[r subprocessPopenSTDOUTPIPE communicatewait)rrrZ use_shellprocessoutput exit_statusr#r#r%rs    rcCsJt|ttfr:y|d}Wntk r8t|}YnXd|ddS)zQuotes a potentially multi-line string to make the start and end obvious. Args: s: A string. Returns: The quoted string. zutf-8z8<----------- rz----------->8 )rr bytearrayrUnicodeDecodeErrorr)sr#r#r%rs  rcCs&tjdtjtjrtjnddS)Nz5Running tests under Python {0[0]}.{0[1]}.{0[2]}: {1} z embedded.)rVrWrXrM version_info executabler#r#r#r%print_python_versions rcOsttt||dS)aExecutes a set of Python unit tests. Usually this function is called without arguments, so the unittest.TestProgram instance will get created with the default settings, so it will run all test methods of all TestCase classes in the __main__ module. Args: *args: Positional arguments passed through to unittest.TestProgram.__init__. **kwargs: Keyword arguments passed through to unittest.TestProgram.__init__. N)r _run_in_app run_tests)rrr#r#r%mains rcCs4tj}x$|r.|jttjkr&dS|j}q WdS)z#Returns True iff app.run is active.TF)rV _getframef_backf_coder[get_function_coderrun)r$r#r#r%_is_in_app_mains   rc@s eZdZdZddZddZdS) _SavedFlagz3Helper class for saving and restoring a flag value.cCs||_|j|_|j|_dS)N)flagr=rJ)rTrr#r#r%rbsz_SavedFlag.__init__cCs|j|j_|j|j_dS)N)r=rrJ)rTr#r#r% restore_flags z_SavedFlag.restore_flagN)r1r2r3rsrbrr#r#r#r%rsrc Cs\trXttddrXytjtjddWn2tk rV}ztjd|Wdd}~XYnXdS)zGHave faulthandler dump stacks on SIGTERM. Useful to diagnose timeouts.registerNT)chainz4faulthandler.register(SIGTERM) failed %r; ignoring. ) faulthandlerrrsignalSIGTERM ExceptionrVrWrX)r<r#r#r%#_register_sigterm_with_faulthandlers rcstrrtddtD}tdd|D}tdd|d=ttj}xt|D] }| qTW|n(tddfdd}t j |dd S) aExecutes a set of Python unit tests, ensuring app.run. This is a private function, users should call absltest.main(). _run_in_app calculates argv to be the command-line arguments of this program (without the flags), sets the default of FLAGS.alsologtostderr to True, then it calls function(argv, args, kwargs), making sure that `function' will get called within app.run(). _run_in_app does this by checking whether it is called by app.run(), or by calling app.run() explicitly. The reason why app.run has to be ensured is to make sure that flags are parsed and stripped properly, and other initializations done by the app module are also carried out, no matter if absltest.run() is called from within or outside app.run(). If _run_in_app is called from within app.run(), then it will reparse sys.argv and pass the result without command-line flags into the argv argument of `function'. The reason why this parsing is needed is that __main__.main() calls absltest.main() without passing its argv. So the only way _run_in_app could get to know the argv without the flags is that it reparses sys.argv. _run_in_app changes the default of FLAGS.alsologtostderr to True so that the test program's stderr will contain all the log messages unless otherwise specified on the command-line. This overrides any explicit assignment to FLAGS.alsologtostderr by the test program prior to the call to _run_in_app() (e.g. in __main__.main). Please note that _run_in_app (and the function it calls) is allowed to make changes to kwargs. Args: function: absltest.run_tests or a similar function. It will be called as function(argv, args, kwargs) where argv is a list containing the elements of sys.argv without the command-line flags. args: Positional arguments passed through to unittest.TestProgram.__init__. kwargs: Keyword arguments passed through to unittest.TestProgram.__init__. css|]}t|VqdS)N)rI)rrr#r#r%rU'sz_run_in_app..css|]}|jt|fVqdS)N)rr)rr$r#r#r%rU(sZalsologtostderrTcst|dS)N)r)argv)rrrr#r% main_functionCsz"_run_in_app..main_function)rN) rrrIr set_defaultrVrr[ itervaluesrrr)rrrZ flag_objectsZ saved_flagsrZ saved_flagrr#)rrrr%rs(    rcCs|drt|dkr|drt||}t|s@t|rtjrt |}t|j dko~|j ddko~|j dko~|j dkSt |}t|j dko|j ddko|j dko|jdko|j SdS)zBReturns True if an attribute is a method named like a test method.ZTestrGrrTNF)rrRisupperrinspect isfunctionismethodr[r\ getargspecrvarargskeywordsgetfullargspecvarkw kwonlyargs) testCaseClassrattrrr#r#r%_is_suspicious_attributeJs"   rcs6eZdZdZedZfddZfddZZ S) TestLoaderaA test loader which supports common test features. Supported features include: * Banning untested methods with test-like names: methods attached to this testCase with names starting with `Test` are ignored by the test runner, and often represent mistakenly-omitted test cases. This loader will raise a TypeError when attempting to load a TestCase with such methods. * Randomization of test case execution order (optional). zMethod '%s' is named like a test case but is not one. This is often a bug. If you want it to be a test method, name it with 'test' in lowercase. If not, rename the method to not begin with 'Test'.csBtt|j||t}|r2||_t|j|_n d|_d|_dS)N)rrrbrO_randomize_ordering_seedrFrK_random)rTrkwdsrN)rr#r%rblszTestLoader.__init__cspx(t|D]}t||r ttj|q Wtt||}|jdk rlt d|jt d|j|j ||S)zFValidates and returns a (possibly randomized) list of test case names.Nz$Randomizing test order with seed: %dzFTo reproduce this order, re-run with --test_randomize_ordering_seed=%d) rorrr _ERROR_MSGrgetTestCaseNamesrrinforshuffle)rTrrnames)rr#r%rvs    zTestLoader.getTestCaseNames) r1r2r3rstextwrapdedentrrbrrvr#r#)rr%r\s   rc Cs|tjdrtjdStjdr:tjtjtjdStjdrxtjtjdtjtj t j dddSdS)NZXML_OUTPUT_FILEZRUNNING_UNDER_TEST_DAEMONztest_detail.xmlZTEST_XMLOUTPUTDIRrz.xml) r8r9r:r@rArvrIrPsplitextbasenamerVrr#r#r#r%get_default_xml_output_filenames    rcCs2tjd}|dks|sdSt||dd<dS)ahImplements the bazel test filtering protocol. The following environment variable is used in this method: TESTBRIDGE_TEST_ONLY: string, if set, is forwarded to the unittest framework to use as a test filter. Its value is split with shlex before being passed as positional arguments on argv. Args: argv: the argv to mutate in-place. ZTESTBRIDGE_TEST_ONLYNrG)r8r9r:shlexre)rZ test_filterr#r#r%_setup_filterings  rcCs2|dkr dStjddkr dSdg|dd<dS)a)Implements the bazel test fail fast protocol. The following environment variable is used in this method: TESTBRIDGE_TEST_RUNNER_FAIL_FAST=<1|0> If set to 1, --failfast is passed to the unittest framework to return upon first failure. Args: argv: the argv to mutate in-place. NZ TESTBRIDGE_TEST_RUNNER_FAIL_FAST1z --failfastrG)r8r9r:)rr#r#r%_setup_test_runner_fail_fasts rcs dtjkrzz\d}yttjdd}|dWn4tk rbtjdtjdtdYnXWd|dk rx|X|pt }dtjkr|St tjd}t tjdd ks|krtjd |ftd|j t t|fd d }||_ |S) aImplements the bazel sharding protocol. The following environment variables are used in this method: TEST_SHARD_STATUS_FILE: string, if set, points to a file. We write a blank file to tell the test runner that this test implements the test sharding protocol. TEST_TOTAL_SHARDS: int, if set, sharding is requested. TEST_SHARD_INDEX: int, must be set if TEST_TOTAL_SHARDS is set. Specifies the shard index for this instance of the test process. Must satisfy: 0 <= TEST_SHARD_INDEX < TEST_TOTAL_SHARDS. Args: custom_loader: A TestLoader to be made sharded. Returns: The test loader for shard-filtering or the standard test loader, depending on the sharding environment variables. ZTEST_SHARD_STATUS_FILENrer7z3Error opening TEST_SHARD_STATUS_FILE (%s). Exiting.rGZTEST_TOTAL_SHARDSZTEST_SHARD_INDEXrz/ERROR: Bad sharding values. index=%d, total=%d csJg|}x*t|D]}t}|kr|qWfdd|DS)Ncsg|]}|kr|qSr#r#)rx)filtered_namesr#r%r szD_setup_sharding..getShardedTestCaseNames..)rQnextr)rZ ordered_namesZtestcaseZbucket)bucket_iteratordelegate_get_names shard_index)rr%getShardedTestCaseNamessz0_setup_sharding..getShardedTestCaseNames)r8r9rrXIOErrorrVrWexitrrr;rrKcycler)Z custom_loaderr$Z base_loaderZ total_shardsrr#)rrrr%_setup_shardings4       rc Cs`t|t|t|dd|d<tjs2tt_tj}d}|rRtj |}|rtj |syt |Wn0t k r}z|j t jkrWdd}~XYnXt|dWdQRX|ddk rt|ddstjd|d||d<|ddkr||d<t}|d|t|ddd}t|ddd} |rl| rl| d |n|ddkrltj|d<tjr|d} t| trt| tjst| tjrd | _tj tjsyt tjWn4t k r}z|j t jkrWdd}~XYnX| d |zt!j"||} | j#S|rZz(t|d} | |$WdQRXWd|%XXdS) z;Executes a set of Python unit tests and returns the result.Z testLoaderNreZ testRunnerset_default_xml_streamzkWARNING: XML_OUTPUT_FILE or --xml_output_file setting overrides testRunner=%r setting (possibly from --pdb)rZset_testsuites_propertyrETr)&rrrr:rIrQrr8r@rvisdirmakedirsOSErrorerrnoEEXISTr_rrVrWrXr[StringIOrrr ZTextTestRunnerZpdb_post_mortemrrr%Zrun_for_debuggingrP setdefaultr(Z TestProgramrgetvaluer) rrrZxml_test_runner_classrQZ xml_bufferZxml_output_dirr<Zrandomize_ordering_seedsetterrunnerZ test_programr$r#r#r%_run_and_get_tests_result sl            rcCs$t|||tj}t| dS)aGExecutes a set of Python unit tests. Most users should call absltest.main() instead of run_tests. Please note that run_tests should be called from app.run. Calling absltest.main() would ensure that. Please note that run_tests is allowed to make changes to kwargs. Args: argv: sys.argv with the command-line flags removed from the front, i.e. the argv with which app.run() has called __main__.main. args: Positional arguments passed through to unittest.TestProgram.__init__. kwargs: Keyword arguments passed through to unittest.TestProgram.__init__. N)rr ZTextAndXMLTestRunnerrVrrU)rrrrr#r#r%ro srcCs*tjr|j}nd|j|j}|ddS)Nz{}.{}z __main__.r7)r[rr3rMr2r1r)rrr#r#r%r srcCsDtj|r2yt|Wq@tk r.Yq@Xntj|dddS)NT) ignore_errors)r8r@isfileunlinkrshutilrmtree)r@r#r#r%r s  rc CsZtjrtj|ddn@yt|Wn0tk rT}z|jtjkrDWdd}~XYnXdS)NT)exist_ok)r[rr8rrrr)Zdir_namer<r#r#r%rq s rqcCs|tjd}|dS)NrGr)rer8sep)r@partsr#r#r%ru sru)NT)N)~rs __future__rrrrrrgetpassrrrKrpr8rFrrrrrzrrVrBrr(r ImportErrorZabslrrrZabsl._collections_abcrZabsl._enum_moduler Z absl.testingr r Zabsl.third_partyr r[Z six.movesr rtypingrrrrrrrrrrrrrrrrrr TYPE_CHECKINGTypeVarr!r\ZcaseZ_OutcomeZ _OutcomeTypeZ unittest.caserr"skipZskipIfZ skipUnlessZSkipTestr)rIr binary_typerZ __unittestr,Enumr-r>r?rDrOZDEFINE_integerZ DEFINE_stringrYrr_rur`rhrrr{r|r~rrjrrrrrrrrrrrrrrrrrrrrqrur#r#r#r%s               T       2   Q#E2; "    P*  Le