
    I                         S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	J
r
  S r " S S\
R                  5      rg)a  Setuptools extension for running Google-style Python tests.

Google-style Python tests differ from normal Python tests in that each test
module is intended to be executed as an independent script. In particular, the
test fixture code in basetest.main() that executes module-wide setUp() and
tearDown() depends on __main__ being the module under test. This conflicts with
the usual setuptools test style, which uses a single TestSuite to run all of a
package's tests.

This package provides a new setuptools command, google_test, that runs all of
the google-style tests found in a specified directory.

NOTE: This works by overriding sys.modules['__main__'] with the module under
test, but still runs tests in the same process. Thus it will *not* work if your
tests depend on any of the following:
  - Per-process (as opposed to per-module) initialization.
  - Any entry point that is not basetest.main().

To use the google_test command in your project, do something like the following:

In setup.py:
  setup(
      name = "mypackage",
      ...
      setup_requires = ["google-apputils>=0.2"],
      google_test_dir = "tests",
      )

Run:
  $ python setup.py google_test
    )errorsN)testc                 ~    [         R                  R                  U5      (       d  [        R                  " SU-  5      eg)z0Validate that the test directory is a directory.z%s is not a directoryN)ospathisdirr   DistutilsSetupError)unused_distunused_attrvalues      8platform/bq/third_party/google/apputils/setup_command.pyValidateGoogleTestDirr   ;   s1    	u		

$
$%<u%D
EE 
    c                   N    \ rS rSrSrSrSrSSSS\-  4S	/rS
 rS r	S r
S rSrg)
GoogleTestA   z7Command to run Google-style tests after in-place build.z+run Google-style tests after in-place buildz_(?:unit|reg)?test\.py$)z	test-dir=dz-Look for test modules in specified directory.ztest-module-pattern=pzPattern for matching test modules. Defaults to %r. Only source files (*.py) will be considered, even if more files match this pattern.)z
test-args=azjArguments to pass to basetest.main(). May only make sense if test_module_pattern matches exactly one test.c                 P    S U l         U R                  U l        SU l        SU l        g )N T)test_dir_DEFAULT_PATTERNtest_module_pattern	test_args
test_suiteselfs    r   initialize_optionsGoogleTest.initialize_optionsS   s'    DM#44DDN DOr   c                 L   U R                   cM  U R                  R                  (       a  U R                  R                  U l         O[        R                  " S5      e[
        R                  " U R                  5      U l        [        R                  " U R                  5      U l
        g )NzNo test directory specified)r   distributiongoogle_test_dirr   DistutilsOptionErrorrecompiler   shlexsplitr   r   s    r   finalize_optionsGoogleTest.finalize_options\   sk    }}				*	*))99))*GHH!zz$*B*BCD[[0DNr   c                    [         R                  R                  U5      u  p#[        R                  SS n[        R                  SS n[        R
                  R                  5       n[        R                  R                  SU5        UR                  SS5      n[        R                  " Xr/5      n[        R                  " U/UQ76 n	U	[        R
                  S'   U	R                  /U R                  -   [        l        SSKn
  [        R                  R!                  SU-  5        U
R#                  5          U[        R                  SS& U[        R                  SS& [        R
                  R%                  5         [        R
                  R'                  U5        g! [(         a  nUR*                  u  nU(       + s SnAU[        R                  SS& U[        R                  SS& [        R
                  R%                  5         [        R
                  R'                  U5        $ SnAf  [,        R.                  " 5          U[        R                  SS& U[        R                  SS& [        R
                  R%                  5         [        R
                  R'                  U5        g= f! U[        R                  SS& U[        R                  SS& [        R
                  R%                  5         [        R
                  R'                  U5        f = f)zRun a module as a test module given its path.

Args:
  module_path: The path to the module to test; must end in '.py'.

Returns:
  True if the tests in this module pass, False if not or if an error occurs.
Nr   .pyr   __main__zTesting %s
F)r   r   r(   sysargvmodulescopyinsertreplaceimpfind_moduleload_module__file__r   baseteststderrwritemainclearupdate
SystemExitargs	traceback	print_exc)r   module_pathr   filenameold_argvold_pathold_modulesmodule_nameimport_tuplemoduler8   e
returncodes                r   _RunTestModuleGoogleTest._RunTestModulef   s    WW]];/NDxx{Hxx{H++""$K HHOOAt""5"-K??;7L__[8<8F$CKK
 4>>1CH &

+56  chhqkchhqk	kk	kk%  ff
~
 chhqkchhqk	kk	kk%chhqkchhqk	kk	kk% chhqkchhqk	kk	kk%s7   2F 
J#H 7J8J"  J:J" J" "A%Lc                    Sn[         R                  " U R                  5       H{  u  p#nU Ho  nUR                  S5      (       d  M  [         R                  R                  X%5      nU R                  R                  U5      (       d  M\  XR                  U5      -  nMq     M}     [        R                  " [        U(       + 5      5        g )NTr,   )r   walkr   endswithr   joinr   searchrL   r.   exitint)r   okr   _	filenamesrC   	file_paths          r   	run_testsGoogleTest.run_tests   s    	B ggdmm4(  ''
GGLL0	##**955
##I.
."   5 HHSR[r   )r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__descriptionr   user_optionsr   r)   rL   rY   __static_attributes__ r   r   r   r   A   sN    ?=+/ Js	*	+-:	,1-&^
r   r   )r_   	distutilsr   r4   r   r%   r'   r.   r@   setuptools.commandr   r   r   rc   r   r   <module>rf      s=    @  
 	 	  
  #F^ ^r   