
    g_                        S r SSKrSSKrSSKrSSKr " S S\R
                  5      rSS jrSS jrS r	\R                  r
\R                  r\" \
S	S5      c%   SSKr\R                  " 5       r
\R                  rS
rOS
rSrSrSr " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\S:X  a  \" S\R8                  \R:                  S9  gg! \ a    Sr Nkf = f)zlRun a unittest testcase reporting results as Subunit.

$ python -m samba.subunit.run mylib.tests.test_suite
    Nc                   f   ^  \ rS rSrSrS rSS jrS rU 4S jrU 4S jr	S r
S	 rS
 rS rSrU =r$ )TestProtocolClient!   a  A TestResult which generates a subunit stream for a test run.

# Get a TestSuite or TestCase to run
suite = make_suite()
# Create a stream (any object with a 'write' method). This should accept
# bytes not strings: subunit is a byte orientated protocol.
stream = open('tests.log', 'wb')
# Create a subunit result object which will output to the stream
result = subunit.TestProtocolClient(stream)
# Optionally, to get timing data for performance analysis, wrap the
# serialiser with a timing decorator
result = subunit.test_results.AutoTimingTestResultDecorator(result)
# Run the test suite reporting to the subunit result object
suite.run(result)
# Close the stream.
stream.close()
c                 \    [         R                  R                  U 5        Xl        / U l        g N)unittest
TestResult__init___stream	successes)selfstreams     3/usr/lib/python3/dist-packages/samba/subunit/run.pyr
   TestProtocolClient.__init__4   s"    $$T*    c                    U R                   R                  SU-  UR                  5       -   5        U(       a  U R                   R                  S5        U HQ  nU R                   R                  U5        UR                  S5      (       a  M6  U R                   R                  S5        MS     U R                   R                  S5        U R                   R                  S5        g)zReport an outcome of test test.

:param outcome: A string describing the outcome - used as the
    event name in the subunit stream.
:param errors: A list of strings describing the errors.
z%s: z [

]N)r   writeidendswith)r   outcometesterrorserrors        r   _addOutcomeTestProtocolClient._addOutcome9   s     	FW,	9:LLv&""5)~~d++LL&&t,   LLs#4 r   c                 :    U R                   R                  U5        g)zReport a success in a test.N)r   appendr   r   s     r   
addSuccessTestProtocolClient.addSuccessJ   s    d#r   c                    > [         TU ]  U5        U R                  R                  SUR	                  5       -   S-   5        U R                  R                  5         g)z%Mark a test as starting its test run.ztest: r   N)super	startTestr   r   r   flushr   r   	__class__s     r   r%   TestProtocolClient.startTestN   sB    $8dggi/$67r   c                 F   > [         TU ]  U5        U R                  U5        g)z,Mark a test as having finished its test run.N)r$   stopTestwriteOutcomer'   s     r   r+   TestProtocolClient.stopTestT   s    $r   c                    U R                  XR                  5      u  o l        U R                  XR                  5      u  o0l        U R                  XR                  5      u  o@l        U R                  XR                  5      u  oPl        U R                  XR                  5      u  o`l        U R                  XR                  5      u  opl        U(       a  SnO4U(       a  SnO*U(       a  SnO U(       a  SnOU(       a  SnOU(       a  SnOSnU(       a  U R                  XX#-   U-   U-   S9  U R                  R                  5         g)	z)Output the overall outcome for test test.r   failureskip	uxsuccessxfail
successfulN)r   )_filterErrorsr   failuresexpectedFailuresskipped_filterSuccessesr   unexpectedSuccessesr   r   r&   )	r   r   errfailr2   r0   successr1   r   s	            r   r,   TestProtocolClient.writeOutcomeY   s   .2.@.@++.V;.2.@.@--.X =.2.@.@J_J_.`((.2.@.@,,.W<.2.C.CD...Y!>.2.C.CDJbJb.c+	+GGG!GG"GGW38D=3FGr   c                     / n/ nU H2  nUS   UL a  UR                  US   5        M!  UR                  U5        M4     X44$ )a.  Filter a list of errors by test test.

:param test: The test to filter by.
:param errors: A list of <test, error> pairs to filter.

:return: A pair whose first element is a list of strings containing
    errors that apply to test test, and whose second element is a list
    of the remaining elements.
r      r   )r   r   r   filtered
unfilteredr   s         r   r4    TestProtocolClient._filterErrorsv   sM     
EQx4a)!!%(	  %%r   c                 T    Sn/ nU H  nXQL a  SnM  UR                  U5        M     X44$ )a2  Filter a list of successes by test test.

:param test: The test to filter by.
:param successes: A list of tests to filter.

:return: A tuple whose first element is a boolean stating whether test
    test was found in the list of successes, and whose second element is
    a list of the remaining elements.
FTr@   )r   r   r   rA   rB   r<   s         r   r8   #TestProtocolClient._filterSuccesses   s;     
 G!!'*	 ! %%r   c           
      ,   UR                  [        R                  R                  5      nU R                  R                  SUR                  UR                  UR                  UR                  UR                  UR                  UR                  4-  5        g)zPInform the client of the time.

":param a_datetime: A datetime.datetime object.
z*time: %04d-%02d-%02d %02d:%02d:%02d.%06dZ
N)
astimezonedatetimetimezoneutcr   r   yearmonthdayhourminutesecondmicrosecond)r   
a_datetimetimes      r   rS   TestProtocolClient.time   sn    
 $$X%6%6%:%:;HIItzz488TYYKK))L+ + 	,r   )r   r   r6   r5   r7   r   r9   r   )__name__
__module____qualname____firstlineno____doc__r
   r   r!   r%   r+   r,   r4   r8   rS   __static_attributes____classcell__r(   s   @r   r   r   !   s:    $
!"$ 
:&*&*, ,r   r   Fc                     [        U 5      n[        U 5      [        R
                  4;   d  U(       a'  / nU H  nUR                  [        U5      5        M     U$ S n[        U 5      nU H  nUR                  5       n  O   [        U SS 5      b  U R                  5         XP4/$ ! [         a    U R                  5       U 4/s $ f = f)N
sort_tests)iter	TypeErrorr   typer   	TestSuiteextend_flatten_testsiterate_testsgetattrr^   )suite_or_caseunpack_outertestsresultr   suite_ids         r   rd   rd      s    5]# 	] 2 244DMM../   m,DwwyH  =,5A$$&)**+  5!!#]3445s   B! !C Cc                     [        XS9nUR                  5         [        R                  " U VVs/ s H  u  p4UPM	     snn5      $ s  snnf )z;Sort suite_or_case while preserving non-vanilla TestSuites.)rh   )rd   sortr   rb   )rg   rh   ri   sort_keyr   s        r   sorted_testsro      s;    =DE	JJLEBE(8tEBCCBs   A
c              #      #     [        U 5      nU H  n[        U5       H  nUv   M	     M     g! [         a    U v    gf = f7f)z>Iterate through all of the test cases in 'test_suite_or_case'.N)r_   re   r`   )test_suite_or_casesuiter   subtests       r   re   re      sK     '( D(. /   !  !s#   A/  AA AA  AdiscoverTz7  -c, --catch      Catch control-C and display results
z=  -b, --buffer     Buffer stdout and stderr during test runs
a/  Usage: %(progName)s [options] [tests]

Options:
  -h, --help       Show this message
  -v, --verbose    Verbose output
  -q, --quiet      Minimal output
  -l, --list       List tests rather than executing them.
  --load-list      Specifies a file containing test ids, only tests matching
                   those ids are executed.
%(catchbreak)s%(buffer)s
Examples:
  %(progName)s test_module               - run tests from test_module
  %(progName)s module.TestClass          - run tests from module.TestClass
  %(progName)s module.Class.test_method  - run specified test method

All options must come before [tests].  [tests] can be a list of any number of
test modules, classes and test methods.

Alternative Usage: %(progName)s discover [options]

Options:
  -v, --verbose    Verbose output
s%(catchbreak)s%(buffer)s  -s directory     Directory to start discovery ('.' default)
  -p pattern       Pattern to match test files ('test*.py' default)
  -t directory     Top level directory of project (default to
                   start directory)
  -l, --list       List tests rather than executing them.
  --load-list      Specifies a file containing test ids, only tests matching
                   those ids are executed.

For test discovery all test modules must be importable from the top
level directory of the project.
c                       \ rS rSrSrS rS rS rS rS r	SS	 jr
SS
 jrS rSS jrSS jrS rS r\S 5       rS r\S 5       rS rSrg)TestResultDecoratori(  zGeneral pass-through decorator.

This provides a base that other TestResults can inherit from to
gain basic forwarding functionality. It also takes care of
handling the case where the target doesn't support newer methods
or features by degrading them.
c                     Xl         g)z5Create a TestResultDecorator forwarding to decorated.N	decorated)r   ry   s     r   r
   TestResultDecorator.__init__1  s	     #r   c                 8    U R                   R                  U5      $ r   )ry   r%   r    s     r   r%   TestResultDecorator.startTest6  s    ~~''--r   c                 6    U R                   R                  5       $ r   )ry   startTestRunr   s    r   r~    TestResultDecorator.startTestRun9  s    ~~**,,r   c                 8    U R                   R                  U5      $ r   )ry   r+   r    s     r   r+   TestResultDecorator.stopTest<  s    ~~&&t,,r   c                 6    U R                   R                  5       $ r   )ry   stopTestRunr   s    r   r   TestResultDecorator.stopTestRun?  s    ~~))++r   Nc                 8    U R                   R                  X5      $ r   )ry   addErrorr   r   r:   s      r   r   TestResultDecorator.addErrorB  s    ~~&&t11r   c                 8    U R                   R                  X5      $ r   )ry   
addFailurer   s      r   r   TestResultDecorator.addFailureE  s    ~~((33r   c                 8    U R                   R                  U5      $ r   )ry   r!   r    s     r   r!   TestResultDecorator.addSuccessH  s    ~~((..r   c                 8    U R                   R                  X5      $ r   )ry   addSkipr   r   reasons      r   r   TestResultDecorator.addSkipK  s    ~~%%d33r   c                 8    U R                   R                  X5      $ r   )ry   addExpectedFailurer   s      r   r   &TestResultDecorator.addExpectedFailureN  s    ~~00;;r   c                 8    U R                   R                  U5      $ r   )ry   addUnexpectedSuccessr    s     r   r   (TestResultDecorator.addUnexpectedSuccessQ  s    ~~22488r   c                 6    U R                   R                  5       $ r   )ry   wasSuccessfulr   s    r   r   !TestResultDecorator.wasSuccessfulT  s    ~~++--r   c                 .    U R                   R                  $ r   ry   
shouldStopr   s    r   r   TestResultDecorator.shouldStopW      ~~(((r   c                 6    U R                   R                  5       $ r   )ry   stopr   s    r   r   TestResultDecorator.stop[  s    ~~""$$r   c                 .    U R                   R                  $ r   )ry   testsRunr   s    r   r   TestResultDecorator.testsRun^  s    ~~&&&r   c                 8    U R                   R                  U5      $ r   )ry   rS   r   rR   s     r   rS   TestResultDecorator.timeb  s    ~~"":..r   rx   r   )rU   rV   rW   rX   rY   r
   r%   r~   r+   r   r   r   r!   r   r   r   r   propertyr   r   r   rS   rZ    r   r   rv   rv   (  su    #
.--,24/4<9. ) )% ' '/r   rv   c                      ^  \ rS rSrSrU 4S jrS rS rS rS r	SS jr
SS	 jrS
 rSS jrSS jrS rS r\S 5       rS rS rSrU =r$ )HookedTestResultDecoratorif  z/A TestResult which calls a hook on every event.c                 Z   > [        5       U l         U R                   R                  U5        g r   )r$   r
   r   ry   r(   s     r   r
   "HookedTestResultDecorator.__init__i  s    W


I&r   c                 X    U R                  5         U R                  R                  U5      $ r   )_before_eventr$   r%   r    s     r   r%   #HookedTestResultDecorator.startTestm  s#    zz##D))r   c                 V    U R                  5         U R                  R                  5       $ r   )r   r$   r~   r   s    r   r~   &HookedTestResultDecorator.startTestRunq  s!    zz&&((r   c                 X    U R                  5         U R                  R                  U5      $ r   )r   r$   r+   r    s     r   r+   "HookedTestResultDecorator.stopTestu  s#    zz""4((r   c                 V    U R                  5         U R                  R                  5       $ r   )r   r$   r   r   s    r   r   %HookedTestResultDecorator.stopTestRuny  s!    zz%%''r   c                 X    U R                  5         U R                  R                  X5      $ r   )r   r$   r   r   s      r   r   "HookedTestResultDecorator.addError}  s#    zz""4--r   c                 X    U R                  5         U R                  R                  X5      $ r   )r   r$   r   r   s      r   r   $HookedTestResultDecorator.addFailure  s#    zz$$T//r   c                 X    U R                  5         U R                  R                  U5      $ r   )r   r$   r!   r    s     r   r!   $HookedTestResultDecorator.addSuccess  s#    zz$$T**r   c                 X    U R                  5         U R                  R                  X5      $ r   )r   r$   r   r   s      r   r   !HookedTestResultDecorator.addSkip  s#    zz!!$//r   c                 X    U R                  5         U R                  R                  X5      $ r   )r   r$   r   r   s      r   r   ,HookedTestResultDecorator.addExpectedFailure  s#    zz,,T77r   c                 X    U R                  5         U R                  R                  U5      $ r   )r   r$   r   r    s     r   r   .HookedTestResultDecorator.addUnexpectedSuccess  s#    zz..t44r   c                 V    U R                  5         U R                  R                  5       $ r   )r   r$   r   r   s    r   r   'HookedTestResultDecorator.wasSuccessful  s!    zz''))r   c                 N    U R                  5         U R                  R                  $ r   )r   r$   r   r   s    r   r   $HookedTestResultDecorator.shouldStop  s    zz$$$r   c                 V    U R                  5         U R                  R                  5       $ r   )r   r$   r   r   s    r   r   HookedTestResultDecorator.stop  s    zz  r   c                 X    U R                  5         U R                  R                  U5      $ r   )r   r$   rS   r   s     r   rS   HookedTestResultDecorator.time  s!    zzz**r   )r$   r   )rU   rV   rW   rX   rY   r
   r%   r~   r+   r   r   r   r!   r   r   r   r   r   r   r   rS   rZ   r[   r\   s   @r   r   r   f  se    9'*))(.0+085* % %!+ +r   r   c                   H   ^  \ rS rSrSrU 4S jrS r\S 5       rS r	Sr
U =r$ )AutoTimingTestResultDecoratori  zDecorate a TestResult to add time events to a test run.

By default this will cause a time event before every test event,
but if explicit time data is being provided by the test run, then
this decorator will turn itself off to prevent causing confusion.
c                 2   > S U l         [        TU ]	  U5        g r   )_timer$   r
   r   s     r   r
   &AutoTimingTestResultDecorator.__init__  s    
#r   c                     U R                   nUb  g [        R                  R                  [        R                  R                  S9nU R
                  R                  U5        g )N)tz)r   rH   nowrI   rJ   ry   rS   )r   rS   s     r   r   +AutoTimingTestResultDecorator._before_event  sK    zz  $$(9(9(=(=$>D!r   c                 .    U R                   R                  $ r   r   r   s    r   r   (AutoTimingTestResultDecorator.shouldStop  r   r   c                 D    Xl         U R                  R                  U5      $ )a4  Provide a timestamp for the current test activity.

:param a_datetime: If None, automatically add timestamps before every
    event (this is the default behaviour if time() is not called at
    all).  If not None, pass the provided time onto the decorated
    result object and disable automatic timestamps.
)r   ry   rS   r   s     r   rS   "AutoTimingTestResultDecorator.time  s      
~~"":..r   )r   )rU   rV   rW   rX   rY   r
   r   r   r   rS   rZ   r[   r\   s   @r   r   r     s0    $" ) )	/ 	/r   r   c                   $    \ rS rSrSS jrS rSrg)SubunitTestRunneri  Nc                 @    U=(       d    [         R                  U l        g)zPCreate a SubunitTestRunner.

:param verbosity: Ignored.
:param buffer: Ignored.
N)sysstdoutr   )r   	verbositybufferr   s       r   r
   SubunitTestRunner.__init__  s     *

r   c                 V    [        U R                  5      n[        U5      nU" U5        U$ )z&Run the given test case or test suite.)r   r   r   )r   r   rj   s      r   runSubunitTestRunner.run  s&    #DKK0.v6Vr   )r   )NNN)rU   rV   rW   rX   r
   r   rZ   r   r   r   r   r     s    +r   r   c            
       n    \ rS rSrSr\rS=r=rr	\ SSS\
SSSSS4
S jrS rS r\4S	 jrS
 rSS jrSrg)TestProgrami  ztA command-line program that runs a set of tests; this is primarily
for making test modules conveniently executable.
NTr?   c                 `   U[         :X  a  S U l        Ob[        U[        5      (       aG  [	        U5      U l        UR                  S5      SS   H  n[        U R                  U5      U l        M      OXl        Uc  [        R                  nU
c  [        R                  n
Uc
  [        5       nX`l        Xl        Xpl        Xl        X l        SU l        S U l        X@l        XPl        US   nUR)                  S[*        R,                  R.                  -  5      (       a2  UR                  [*        R,                  R.                  5      nSUS   -  nO"[*        R,                  R1                  US   5      nXl        U R5                  U5        U R"                  (       a  [7        U R"                  S5      n UR9                  5       nUR;                  5         [=        S	 U 5       5      n[>        R@                  " 5       n[C        U RD                  5       H*  nURG                  5       U;   d  M  URI                  U5        M,     UU l"        U R                   (       d  U RK                  5         g [C        U RD                  5       H%  nU
RM                  S
URG                  5       -  5        M'     g ! UR;                  5         f = f)N.r?   Fr   z%srun.pyz%s.runrbc              3   ^   #    U  H#  oR                  5       R                  S 5      v   M%     g7f)zutf-8N)stripdecode).0lines     r   	<genexpr>'TestProgram.__init__.<locals>.<genexpr>  s"     JED::<..w77Es   +-z%s
)'rU   module
isinstancestr
__import__splitrf   r   argvr   r   exit
catchbreakr   r   defaultTest	listtests	load_list
testRunner
testLoaderr   ospathsepbasenameprogName	parseArgsopen	readlinesclosesetr   rb   re   r   r   addTestrunTestsr   )r   r   r   r   r   r   r   r   r   r   r   partr  elementssourcelinestest_idsrA   r   s                      r   r
   TestProgram.__init__  s    XDK$$$V,DKS)!"-%dkk48 . !K<88D>ZZF*,J	$"&$$7Z"''++566~~bggkk2H(2,.Hww''Q0H t>>
 $..$/F((*JEJJH))+H%dii0779($$T* 1 !DI~~MMO%dii0Vdggi/0 1 s   )J J-c                    [        U5      S:  a,  US   R                  5       S:X  a  U R                  USS  5        g SS Kn/ SQn UR                  USS  SU5      u  pEU H  u  pgUS;   a  U R	                  5         US;   a  SU l        US	;   a  SU l        US
;   a  U R                  c  SU l        US;   a  U R                  c  SU l        US;   a  SU l        US:X  d  M~  Xpl	        M     [        U5      S:X  a  U R                  c  S U l        O([        U5      S:  a  XPl        OU R                  4U l        U R                  5         g ! UR                   a*    U R	                  [        R                  " 5       S   5         g f = f)Nr?   rt      r   )helpverbosequietcatchr   listz
load-list=hHvqfcbl)z-hz-Hz--help)z-qz--quiet)-v	--verbose)-c--catchT)-b--buffer)-l--list--load-list)lenlower_do_discoverygetopt	usageExitr   r   r   r   r   r   	testNamescreateTestsr   r   exc_info)r   r   r'  	long_optsoptionsargsoptvalues           r   r  TestProgram.parseArgs  sc   t9q=T!W]]_
:tABx("		."MM$qr(J	JMG%
..NN$**%&DN,,%&DN**.*.++{{*&***%)DN-'%*N% && 4yA~$"2"2":!%TQ!%"&"2"2!4|| 	.NN3<<>!,-	.s   BE A&E 7E>=E>c                     U R                   c+  U R                  R                  U R                  5      U l        g U R                  R                  U R                   U R                  5      U l        g r   )r)  r   loadTestsFromModuler   r   loadTestsFromNamesr   s    r   r*  TestProgram.createTestsG  sJ    >>!;;DKKHDI::4>>;?;;HDIr   c           	      @   [         (       d  [        S5      eSU R                  -  U l        SS KnUR	                  5       nU R                  Ul        UR                  SSSSSS	S
9  U R                  SLa  UR                  SSSSSS	S
9  U R                  SLa  UR                  SSSSSS	S
9  UR                  SSSSSS9  UR                  SSSSSS9  UR                  SSS S S!S9  UR                  S"S#S$SS	S%S&9  UR                  S'S(S S)S9  UR                  U5      u  pV[        U5      S*:  a  U R                  5         [        S+U5       H  u  px[        XWU5        M     U R                  c  UR                  U l        U R                  c  UR                  U l        UR                  U l        UR                  U l        UR                   (       a  S,U l        UR$                  n	UR&                  n
UR(                  nU" 5       nUR+                  XU5      n[-        U5      U l        g )-NzYUnable to use discovery, must use python 2.7 or greater, or install the discover package.z%s discoverr   r  r  r  FzVerbose output
store_true)destdefaultr  actionr  r  r   z'Catch ctrl-C and display results so farr  r   r   z%Buffer stdout and stderr during testsz-sz--start-directorystartr   z*Directory to start discovery ('.' default))r8  r9  r  z-pz	--patternpatternztest*.pyz+Pattern to match tests ('test*.py' default)z-tz--top-level-directorytopz<Top level directory of project (defaults to start directory)r!  r"  r   z$List tests rather than running them.)r8  r9  r:  r  r#  r   z2Specify a filename containing the test ids to use.   )r;  r<  r=  r  )have_discoverAssertionErrorr  optparseOptionParserprog
add_optionr   r   
parse_argsr$  r(  zipsetattrr   r   r  r   r;  r<  r=  rt   ro   r   )r   r   LoaderrA  parserr-  r.  namer0  	start_dirr<  top_level_dirloaderloadeds                 r   r&  TestProgram._do_discoveryN  sC   }  "C D D%5&&(mm$)U/ 	 	F??%'dIL%#L%1  3 ;;e#dJXu#J%1  3 	$ 3'3K 	 	M$)ZL 	 	N$ 7eT] 	 	_${ER^E 	 	G-k4S 	 	U ))$/t9q=NN:DAKDG5) B
 ??"%00DO;;!..DK ** **??DNMM	// ]C (	r   c                 R   U R                   (       a'  [        [        SS 5      b  [        R                  " 5         U R                  R                  U R                  5      U l        U R                  (       a4  [        R                  " U R                  R                  5       (       + 5        g g )NinstallHandler)r   rf   r   rQ  r   r   r   rj   r   r   r   r   s    r   r  TestProgram.runTests  sg    OO"2D9E##%oo))$))499HH22445 r   c                    U(       a  [        U5        U R                  SSS.nU R                  SLa	  [        US'   U R                  SLa	  [
        US'   U R                  U-  nUR                  S5      nUR                  SS5        UR                  S	S5        [        SR                  U5      5        [        R                  " S5        g )
N )r  r   r   Fr   r   r   r  z)Run a test suite with a subunit reporter.r>  )printr  r   
CATCHBREAKr   BUFFEROUTPUTUSAGEr   insertjoinr   r   )r   msgusage
usage_textusage_liness        r   r(  TestProgram.usageExit  s    3K!]]"??%'",E,;;e#*E(OZZ%'
 &&t,1IJ1b!dii$%r   )r   r   r   r   r   r   r   r  rj   r   r   r)  r   r   r   )rU   rV   rW   rX   rY   USAGE_AS_MAINrX  r   r   r  defaultTestLoaderr
   r  r*  defaultTestLoaderClsr&  r  r(  rZ   r   r   r   r   r     s`     E &*)J)(&Dt#0At81t&.PH *> @)D6r   r   __main__)r   r   r   )F)rY   rH   r  r   r   r	   r   rd   ro   re   ra  
TestLoaderrb  rf   rt   DiscoveringTestLoaderr?  ImportErrorrV  rW  r`  objectrv   r   r   r   r   rU   r   r   r   r   r   <module>rh     s)  ,
  	 
 G,,, G,T+6D	 .. ** 
j$/7$::<'== M4 J
O!L;/& ;/|>+ 3 >+B /$=  /F $J& JZ zt#((3::> O  s   #C   C+*C+