
    r@;jC&                    z   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlZddlZddlmZmZ ddlmZ erddlmZ  G d	 d
ej                  Z G d dej                  Zej        ej        hZddZ G d dej                  Z G d dej                  Z G d dej                   Z d Z!dS )zUtilities for the pywin32 tests    )annotationsN)Counter)TYPE_CHECKING)_GetGatewayCount_GetInterfaceCount)IsUserAnAdmin)
OptExcInfoc                  4    e Zd ZdZd Zd ZddZd ZddZdS )	LeakTestCasea  An 'adaptor' which takes another test.  In debug builds we execute the
    test once to remove one-off side-effects, then capture the total
    reference count, then execute the test a few times.  If the total
    refcount at the end is greater than we first captured, we have a leak!

    In release builds the test is executed just once, as normal.

    Generally used automatically by the test runner - you can safely
    ignore this.
    c                    t           j                            |            || _        d| _        d| _        t          t          d          r| xj        | j        z  c_        d S d S )N      gettotalrefcount)unittestTestCase__init__	real_testnum_test_casesnum_leak_itershasattrsys)selfr   s     hC:\Users\Administrator\AppData\Local\Temp\2\pip-target-b0z114gj\lib\python\win32/lib/pywin32_testutil.pyr   zLeakTestCase.__init__'   sj    ""4((("3*++ 	74#66	7 	7    c                    | j         S N)r   r   s    r   countTestCaseszLeakTestCase.countTestCases/   s    ""r   Nc                   t          j                     t                      }t                      }|                     |           |j        s|                                sd S |                     |           t          j                     t                      |z
  }t                      |z
  }|s|r;d||fz  }t          |          }|	                    | j        |j
        |d f           d S d S )Nz2%d interface objects and %d gateway objects leaked)gccollectr   r   r   
shouldStopwasSuccessful_do_leak_testsAssertionError
addFailure	__class__)r   resultninglost_ilost_gmsgexcs           r   __call__zLeakTestCase.__call__2   s    

!!v 	F$8$8$:$: 	FF###

#%%*!##b( 	JV 	JFJ C !%%Cdns}c4.HIIIII	J 	Jr   c                     t          d          )Nznot used)NotImplementedErrorr   s    r   runTestzLeakTestCase.runTestG   s    !*---r   c                2   	 t           j        }n# t          $ r Y d S w xY wt          j                      |            }t          | j                  D ] }|                     |           |j        r n!~t          j                      |            |z
  | j        z  }|dk     r0d| j        | fz  }|	                    | j        t          |d f           |dk    r@d| j        |fz  }t          |          }|	                    | j        |j        |d f           d S d S )Nr   z-LeakTest: %s appeared to gain %d references!!zLeakTest: %s lost %d references)r   r   AttributeErrorr    r!   ranger   r   r"   r&   r%   r'   )r   r(   gtrctrcilostr-   r.   s           r   r$   zLeakTestCase._do_leak_testsJ   sL   	'DD 	 	 	FF	 	
dfft*++ 	 	ANN6"""   	
!44!88AE C dn~sD.IJJJ!883t~t6LLC %%Cdns}c4.HIIIII 8s    
r   )	__name__
__module____qualname____doc__r   r   r/   r2   r$    r   r   r   r      s}        	 	7 7 7# # #J J J J*. . .J J J J J Jr   r   c                  .    e Zd Zd Zd Zd Zd ZddZdS )
TestLoaderc                    g }|                      |          D ]7} ||          }|                     |          }|                    |           8|                     |          S )z<Return a suite of all tests cases contained in testCaseClass)getTestCaseNames_getTestWrapperappend
suiteClass)r   testCaseClass
leak_testsnamer   	leak_tests         r   loadTestsFromTestCasez TestLoader.loadTestsFromTestCaseh   so    
))-88 	) 	)D%d++I,,Y77Ii((((z***r   c                     t          |t          j                  r fd|j        D             |_        |S                      |          S )Nc                :    g | ]}                     |          S r>   )fixupTestsForLeakTests).0tr   s     r   
<listcomp>z5TestLoader.fixupTestsForLeakTests.<locals>.<listcomp>s   s'    OOOa466q99OOOr   )
isinstancer   	TestSuite_testsrC   )r   tests   ` r   rM   z!TestLoader.fixupTestsForLeakTestsq   sP    dH.// 	.OOOO4;OOODKK ''---r   c                h    t          |dd          }|rt          d           |S t          |          S )Nno_leak_testsFz%Test says it doesn't want leak tests!)getattrprintr   )r   rT   rV   s      r   rC   zTestLoader._getTestWrappery   s=    ou== 	9:::KD!!!r   c                    t          |d          r|                                }n t          j                            | |          }|                     |          S )Nsuite)r   rZ   r   r@   loadTestsFromModulerM   )r   modtestss      r   r[   zTestLoader.loadTestsFromModule   sP    3   	GIIKKEE';;D#FFE**5111r   Nc                    t           j                            | ||          }t          |t           j                  rn@t          |t           j                  r|                     |          }nt          d|           |S )NzXXX - what is)r   r@   loadTestsFromNamerQ   rR   r   rC   rX   )r   rH   modulerT   s       r   r_   zTestLoader.loadTestsFromName   ss    "44T4HHdH.// 	)h/00 	)''--DD/4(((r   r   )r:   r;   r<   rJ   rM   rC   r[   r_   r>   r   r   r@   r@   g   sd        + + +. . ." " "2 2 2	 	 	 	 	 	r   r@   .c                   t           j                            t          j        d                   |dg}|D ]E}t           j                            ||           }t           j                            |          r|c S Ft           j                            t           j                            t          j        d                             }t          j
                    d d          }t          j        r|                    t          j                   |D ]^}t           j                            |          }t           j                            ||g          |k    rt          j        d| d          _t!          d| d          )Nr   ra   zCan't find test fixture '')ospathdirnamer   argvjoinisfilenormcaseabspathsitegetsitepackages	USER_SITErD   commonprefixr   SkipTestRuntimeError)basename	extra_dir
candidates	candidatefname	this_filedirs_to_checkds           r   find_test_fixturerz      sY    	$$J
   A A	Y117>>%   	LLL	
 G$$RW__SXa[%A%ABB	,..qqq1> 	1  000 	N 	NA  ##Aw##YN33q88'(LE(L(L(LMMM 9 ?u???@@@r   c                  ,     e Zd Zd	 fdZd
 fdZ xZS )
TestResultrT   unittest.TestCaseerrr	   returnNonec                L   |d         }t          |t          j                  r?|j        t          v r1t                      s#|                     |t          |                    S t          |t          j                  rM|j	        t          j
        t          j        t          j        hv r#|                     |t          |                    S t          |t                    r#|                     |t          |                    S t                                          ||           dS )zqCalled when an error has occurred.

        Translate a couple of 'well-known' exceptions into 'skipped'
        r   N)rQ   
pywintypeserrorwinerrornon_admin_error_codesr   addSkipstr	com_errorhresultCO_E_CLASSSTRINGREGDB_E_CLASSNOTREGTYPE_E_LIBNOTREGISTEREDr1   superaddError)r   rT   r~   exc_valr'   s       r   r   zTestResult.addError   s   
 a& w
 011	4 $999!OO : <<c'll333 !566 	47?%(,O
 <
 <

 <<c'll333!455 	4<<c'll333s#####r   c                    t                                                       d | j        D             }t          |                                          D ]%\  }}| j                            d| d|            &d S )Nc                    g | ]\  }}|S r>   r>   )rN   _reasons      r   rP   z*TestResult.printErrors.<locals>.<listcomp>   s    :::kq&6:::r   z	SKIPPED: z	 tests - )r   printErrorsskippedr   itemsstreamwriteln)r   reasonsr   num_skippedr'   s       r   r   zTestResult.printErrors   s    ::T\:::#*7#3#3#9#9#;#; 	L 	LFKK JK J J& J JKKKK	L 	Lr   )rT   r}   r~   r	   r   r   )r   r   )r:   r;   r<   r   r   __classcell__)r'   s   @r   r|   r|      se        $ $ $ $ $ $:L L L L L L L L L Lr   r|   c                      e Zd Zd ZdS )
TestRunnerc                B    t          | j        | j        | j                  S r   )r|   r   descriptions	verbosityr   s    r   _makeResultzTestRunner._makeResult   s    $+t'8$.IIIr   N)r:   r;   r<   r   r>   r   r   r   r      s(        J J J J Jr   r   c                      e Zd Zd ZdS )TestProgramc                x    t          | j                  | _        t          j                            |            d S )N)r   )r   r   
testRunnerr   r   runTestsr   s    r   r   zTestProgram.runTests   s3    $t~>>>%%d+++++r   N)r:   r;   r<   r   r>   r   r   r   r      s#        , , , , ,r   r   c                     |                                 }d|vrt                      |d<   |                    dt                    } || i | d S )N
testLoadertestProgram)copyr@   getr   )argskwnew_kwprogram_classs       r   testmainr      sV    WWYYF6!!)|||JJ}k::MM4"6"""""r   )ra   )"r=   
__future__r   r    rd   rl   r   r   collectionsr   typingr   r   r   	pythoncomr   r   win32com.shell.shellr   	_typeshedr	   r   r   r@   ERROR_ACCESS_DENIEDERROR_PRIVILEGE_NOT_HELDr   rz   TextTestResultr|   TextTestRunnerr   r   r   r>   r   r   <module>r      s   % % " " " " " " 				 				  



                         : : : : : : : : . . . . . . %$$$$$$IJ IJ IJ IJ IJ8$ IJ IJ IJX* * * * *$ * * *l  % A A A A<"L "L "L "L "L( "L "L "LLJ J J J J( J J J, , , , ,(& , , ,# # # # #r   