o
    >hV                     @  s`  d Z ddlm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
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZmZm Z  G dd deZ!G dd deZ"G dd dZ#G dd dZ$G dd deZ%G dd deZ&G dd deZ'G dd deZ(G dd  d eZ)G d!d" d"eZ*dS )#z!
Tests for L{twisted.trial.util}
    )annotationsN)StringIO)	Generator)implementer)assert_thatequal_to)DelayedCall)IProcessTransport)filepath)Failure)util)SynchronousTestCase)DirtyReactorAggregateError_JanitoracquireAttributeexcInfoOrFailureToExcInfoopenTestLogc                   @  8   e Zd ZdZdddZdddZddd	Zdd
dZdS )MktempTestszm
    Tests for L{TestCase.mktemp}, a helper function for creating temporary file
    or directory names.
    returnNonec                 C  s8   |   }tj|tjdd }| |g d dS )z
        The path name returned by C{mktemp} is directly beneath a directory
        which identifies the test method which created the name.
        N)ztwisted.trial.test.test_utilr   	test_name)mktempospathdirnamesplitsepassertEqual)selfnamedirs r#   |/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/trial/test/test_util.pyr   )   s
   zMktempTests.test_namec                 C  s   |   }| ||    dS )zF
        Repeated calls to C{mktemp} return different values.
        N)r   assertNotEqual)r    r!   r#   r#   r$   test_unique4   s   zMktempTests.test_uniquec                 C  s<   |   }tj|}| tj| | tj| dS )zS
        The directory part of the path name returned by C{mktemp} exists.
        N)r   r   r   r   
assertTrueexistsassertFalse)r    r!   r   r#   r#   r$   test_created;   s   zMktempTests.test_createdc                 C  s(   t j|  }| |t   dS )zZ
        The path returned by C{mktemp} is beneath the current working directory.
        N)r   r   abspathr   r'   
startswithgetcwd)r    r   r#   r#   r$   test_locationD   s   zMktempTests.test_locationNr   r   )__name__
__module____qualname____doc__r   r&   r*   r.   r#   r#   r#   r$   r   #   s    


	r   c                   @  .   e Zd ZdZdddZdddZddd	Zd
S )DirtyReactorAggregateErrorTestsz6
    Tests for the L{DirtyReactorAggregateError}.
    r   r   c                 C  s    t ddg}| t|d dS )z5
        Delayed calls are formatted nicely.
        FoobarzhReactor was unclean.
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
Foo
barNr   r   strr    errorr#   r#   r$   test_formatDelayedCallQ   s
   z6DirtyReactorAggregateErrorTests.test_formatDelayedCallc                 C  s"   t g ddg}| t|d dS )z3
        Selectables are formatted nicely.
        zselectable 1zselectable 2z;Reactor was unclean.
Selectables:
selectable 1
selectable 2Nr8   r:   r#   r#   r$   test_formatSelectables_   s
   z6DirtyReactorAggregateErrorTests.test_formatSelectablesc                 C  s&   t ddgddg}| t|d dS )zR
        Both delayed calls and selectables can appear in the same error.
        bleckBoozoSel1Sel2zReactor was unclean.
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
bleck
Boozo
Selectables:
Sel1
Sel2Nr8   r:   r#   r#   r$   %test_formatDelayedCallsAndSelectablesm   s
   zEDirtyReactorAggregateErrorTests.test_formatDelayedCallsAndSelectablesNr/   )r0   r1   r2   r3   r<   r=   rB   r#   r#   r#   r$   r5   L   s
    

r5   c                   @  s>   e Zd ZdZ	ddd	d
ZddddZdddZdddZdS )StubReactora~  
    A reactor stub which contains enough functionality to be used with the
    L{_Janitor}.

    @ivar iterations: A list of the arguments passed to L{iterate}.
    @ivar removeAllCalled: Number of times that L{removeAll} was called.
    @ivar selectables: The value that will be returned from L{removeAll}.
    @ivar delayedCalls: The value to return from L{getDelayedCalls}.
    NdelayedCallslist[DelayedCall]selectableslist[object] | Noner   r   c                 C  s$   || _ g | _d| _|sg }|| _dS )z
        @param delayedCalls: See L{StubReactor.delayedCalls}.
        @param selectables: See L{StubReactor.selectables}.
        r   N)rD   
iterationsremoveAllCalledrF   )r    rD   rF   r#   r#   r$   __init__   s   
zStubReactor.__init__timeoutfloat | Nonec                 C     | j | dS )z/
        Increment C{self.iterations}.
        N)rH   append)r    rK   r#   r#   r$   iterate      zStubReactor.iteratec                 C  s   | j S )z.
        Return C{self.delayedCalls}.
        )rD   r    r#   r#   r$   getDelayedCalls   s   zStubReactor.getDelayedCallslist[object]c                 C  s   |  j d7  _ | jS )zS
        Increment C{self.removeAllCalled} and return C{self.selectables}.
           )rI   rF   rQ   r#   r#   r$   	removeAll   s   zStubReactor.removeAllN)rD   rE   rF   rG   r   r   )rK   rL   r   r   )r   rE   )r   rS   )r0   r1   r2   r3   rJ   rO   rR   rU   r#   r#   r#   r$   rC      s    
rC   c                   @  s$   e Zd ZdZdddZdd
dZdS )StubErrorReporterz
    A subset of L{twisted.trial.itrial.IReporter} which records L{addError}
    calls.

    @ivar errors: List of two-tuples of (test, error) which were passed to
        L{addError}.
    r   r   c                 C  
   g | _ d S rV   )errorsrQ   r#   r#   r$   rJ         
zStubErrorReporter.__init__testobjectr;   r   c                 C  s   | j ||f dS )z6
        Record parameters in C{self.errors}.
        N)rY   rN   )r    r[   r;   r#   r#   r$   addError   s   zStubErrorReporter.addErrorNr/   )r[   r\   r;   r   r   r   )r0   r1   r2   r3   rJ   r]   r#   r#   r#   r$   rW      s    
rW   c                   @  s~   e Zd ZdZdddZdddZddd	Zdd
dZdddZdddZ	dddZ
dddZdddZdddZdddZdS )JanitorTestsz 
    Tests for L{_Janitor}!
    r   r   c                 C  s4   t g }tdd|d}|  | |jddg dS )a  
        During pending-call cleanup, the reactor will be spun twice with an
        instant timeout. This is not a requirement, it is only a test for
        current behavior. Hopefully Trial will eventually not do this kind of
        reactor stuff.
        Nreactorr   )rC   r   _cleanPendingr   rH   r    r`   janr#   r#   r$   test_cleanPendingSpinsReactor   s   z*JanitorTests.test_cleanPendingSpinsReactorc                 C  sX   ddd}g }t d|di |jdd }t|g}td	d	|d
}|  | ||g d	S )zW
        During pending-call cleanup, the janitor cancels pending timed calls.
        r   r9   c                   S     dS )NLulzr#   r#   r#   r#   r$   func      z8JanitorTests.test_cleanPendingCancelsCalls.<locals>.func,  r#   c                 S     d S rV   r#   xr#   r#   r$   <lambda>       z<JanitorTests.test_cleanPendingCancelsCalls.<locals>.<lambda>Nr_   r   r9   )r   rN   rC   r   ra   r   )r    rg   	cancelleddelayedCallr`   rc   r#   r#   r$   test_cleanPendingCancelsCalls   s   

z*JanitorTests.test_cleanPendingCancelsCallsc              	   C  s`   t ddd di dd dd dd d}t|}t|g}tdd|d	}| }| ||g dS )
aK  
        The Janitor produces string representations of delayed calls from the
        delayed call cleanup method. It gets the string representations
        *before* cancelling the calls; this is important because cancelling the
        call removes critical debugging information from the string
        representation.
        ri   c                   S  rj   rV   r#   r#   r#   r#   r$   rm      rn   zIJanitorTests.test_cleanPendingReturnsDelayedCallStrings.<locals>.<lambda>r#   c                 S  rj   rV   r#   rk   r#   r#   r$   rm      rn   c                   S  re   Nr   r#   r#   r#   r#   r$   rm      rn   secondsNr_   )r   r9   rC   r   ra   r   )r    rq   delayedCallStringr`   rc   stringsr#   r#   r$   *test_cleanPendingReturnsDelayedCallStrings   s   
z7JanitorTests.test_cleanPendingReturnsDelayedCallStringsc                 C  s0   t g }tdd|d}|  | |jd dS )zM
        The Janitor will remove selectables during reactor cleanup.
        Nr_   rT   )rC   r   _cleanReactorr   rI   rb   r#   r#   r$   #test_cleanReactorRemovesSelectables   s   z0JanitorTests.test_cleanReactorRemovesSelectablesc                 C  sR   t tG dd d}| }tg |g}tdd|d}|  | |jdg dS )zI
        The Janitor will kill processes during reactor cleanup.
        c                   @  s$   e Zd ZdZdddZddd	Zd
S )zJJanitorTests.test_cleanReactorKillsProcesses.<locals>.StubProcessTransportz
            A stub L{IProcessTransport} provider which records signals.
            @ivar signals: The signals passed to L{signalProcess}.
            r   r   c                 S  rX   rV   )signalsrQ   r#   r#   r$   rJ     rZ   zSJanitorTests.test_cleanReactorKillsProcesses.<locals>.StubProcessTransport.__init__signal	str | intc                 S  rM   )zF
                Append C{signal} to C{self.signals}.
                N)r{   rN   )r    r|   r#   r#   r$   signalProcess  rP   zXJanitorTests.test_cleanReactorKillsProcesses.<locals>.StubProcessTransport.signalProcessNr/   )r|   r}   r   r   )r0   r1   r2   r3   rJ   r~   r#   r#   r#   r$   StubProcessTransport   s    
r   Nr_   KILL)r   r	   rC   r   ry   r   r{   )r    r   ptr`   rc   r#   r#   r$   test_cleanReactorKillsProcesses   s   z,JanitorTests.test_cleanReactorKillsProcessesc                 C  s@   G dd d}t g | g}tdd|d}| | dg dS )z
        The Janitor returns string representations of the selectables that it
        cleaned up from the reactor cleanup method.
        c                   @  s   e Zd ZdZdddZdS )zJJanitorTests.test_cleanReactorReturnsSelectableStrings.<locals>.Selectablezl
            A stub Selectable which only has an interesting string
            representation.
            r   r9   c                 S  re   )N(SELECTABLE!)r#   rQ   r#   r#   r$   __repr__   rh   zSJanitorTests.test_cleanReactorReturnsSelectableStrings.<locals>.Selectable.__repr__Nro   )r0   r1   r2   r3   r   r#   r#   r#   r$   
Selectable  s    r   Nr_   r   )rC   r   r   ry   )r    r   r`   rc   r#   r#   r$   )test_cleanReactorReturnsSelectableStrings  s   	z6JanitorTests.test_cleanReactorReturnsSelectableStringsc                 C  sB   t g }t }t }t|||d}| |  | |jg  dS )z
        The post-case cleanup method will return True and not call C{addError}
        on the result if there are no pending calls.
        r_   N)rC   r\   rW   r   r'   postCaseCleanupr   rY   r    r`   r[   reporterrc   r#   r#   r$   test_postCaseCleanupNoErrors'  s   z)JanitorTests.test_postCaseCleanupNoErrorsc              	   C  s   t ddd di dd dd dd d}t|}t|gg }t }t }t|||d}| |  | t	|j
d	 | |j
d
 d	 jj|g dS )z
        The post-case cleanup method will return False and call C{addError} on
        the result with a L{DirtyReactorAggregateError} Failure if there are
        pending calls.
        ri   c                   S  rj   rV   r#   r#   r#   r#   r$   rm   :  rn   z=JanitorTests.test_postCaseCleanupWithErrors.<locals>.<lambda>r#   c                 S  rj   rV   r#   rk   r#   r#   r$   rm   :  rn   c                   S  re   rs   r#   r#   r#   r#   r$   rm   :  rn   rt   r_   rT   r   N)r   r9   rC   r\   rW   r   r)   r   r   lenrY   valuerD   r    rq   rv   r`   r[   r   rc   r#   r#   r$   test_postCaseCleanupWithErrors3  s    z+JanitorTests.test_postCaseCleanupWithErrorsc                 C  s<   t g }t }t }t|||d}|  | |jg  dS )z
        The post-class cleanup method will not call C{addError} on the result
        if there are no pending calls or selectables.
        r_   N)rC   r\   rW   r   postClassCleanupr   rY   r   r#   r#   r$   test_postClassCleanupNoErrorsE  s   z*JanitorTests.test_postClassCleanupNoErrorsc              	   C  s   t ddd di dd dd dd d}t|}t|gg }t }t }t|||d}|  | t|j	d	 | |j	d
 d	 j
j|g dS )z
        The post-class cleanup method call C{addError} on the result with a
        L{DirtyReactorAggregateError} Failure if there are pending calls.
        ri   c                   S  rj   rV   r#   r#   r#   r#   r$   rm   W  rn   zIJanitorTests.test_postClassCleanupWithPendingCallErrors.<locals>.<lambda>r#   c                 S  rj   rV   r#   rk   r#   r#   r$   rm   W  rn   c                   S  re   rs   r#   r#   r#   r#   r$   rm   W  rn   rt   r_   rT   r   N)r   r9   rC   r\   rW   r   r   r   r   rY   r   rD   r   r#   r#   r$   *test_postClassCleanupWithPendingCallErrorsQ  s    z7JanitorTests.test_postClassCleanupWithPendingCallErrorsc                 C  sh   d}t g |g}t }t }t|||d}|  | t|jd | |jd d jj	t
|g dS )z
        The post-class cleanup method call C{addError} on the result with a
        L{DirtyReactorAggregateError} Failure if there are selectables.
        zSELECTABLE HEREr_   rT   r   N)rC   r\   rW   r   r   r   r   rY   r   rF   repr)r    
selectabler`   r[   r   rc   r#   r#   r$   )test_postClassCleanupWithSelectableErrorsb  s   $z6JanitorTests.test_postClassCleanupWithSelectableErrorsNr/   )r0   r1   r2   r3   rd   rr   rx   rz   r   r   r   r   r   r   r   r#   r#   r#   r$   r^      s    




	




r^   c                   @  r4   )RemoveSafelyTestsz*
    Tests for L{util._removeSafely}.
    r   r   c                 C  s8   |   d}t| t|}| tjtj	| dS )z
        If a path doesn't contain a node named C{"_trial_marker"}, that path is
        not removed by L{util._removeSafely} and a L{util._NoTrialMarker}
        exception is raised instead.
        utf-8N)
r   encoder   mkdirr
   FilePathassertRaisesr   _NoTrialMarker_removeSafely)r    	directorydirPathr#   r#   r$   test_removeSafelyNoTrialMarkerv  s   

z0RemoveSafelyTests.test_removeSafelyNoTrialMarkerc                 C  sr   d
dd}t  }| td| |  d}t| t|}|	d
  ||_t| | d|  d	S )z
        If an L{OSError} is raised while removing a path in
        L{util._removeSafely}, an attempt is made to move the path to a new
        name.
        r   r   c                   S  s   t  )
            Raise an C{OSError} to emulate the branch of L{util._removeSafely}
            in which path removal fails.
            OSErrorr#   r#   r#   r$   dummyRemove  s   zORemoveSafelyTests.test_removeSafelyRemoveFailsMoveSucceeds.<locals>.dummyRemovestdoutr      _trial_markercould not remove FilePathNr/   )r   patchsysr   r   r   r   r
   r   childtouchremover   r   assertIngetvalue)r    r   outr   r   r#   r#   r$   (test_removeSafelyRemoveFailsMoveSucceeds  s   



z:RemoveSafelyTests.test_removeSafelyRemoveFailsMoveSucceedsc                 C  s   ddd}ddd
d}t  }| td| |  d}t| t|}|	d
  ||_||_| ttj|}| t|d | d|  dS )z
        If an L{OSError} is raised while removing a path in
        L{util._removeSafely}, an attempt is made to move the path to a new
        name. If that attempt fails, the L{OSError} is re-raised.
        r   r   c                   S     t d)r   zpath removal failedr   r#   r#   r#   r$   r        zLRemoveSafelyTests.test_removeSafelyRemoveFailsMoveFails.<locals>.dummyRemoveTdestinationr\   followLinksboolc                 S  r   )z
            Raise an C{OSError} to emulate the branch of L{util._removeSafely}
            in which path movement fails.
            path movement failedr   )r   r   r#   r#   r$   dummyMoveTo  r   zLRemoveSafelyTests.test_removeSafelyRemoveFailsMoveFails.<locals>.dummyMoveTor   r   r   r   r   Nr/   )T)r   r\   r   r   r   r   )r   r   r   r   r   r   r   r
   r   r   r   r   moveTor   r   r   r   r   r9   r   r   )r    r   r   r   r   r   r;   r#   r#   r$   %test_removeSafelyRemoveFailsMoveFails  s   


z7RemoveSafelyTests.test_removeSafelyRemoveFailsMoveFailsNr/   )r0   r1   r2   r3   r   r   r   r#   r#   r#   r$   r   q  s
    

r   c                   @  $   e Zd ZdZd	ddZd	ddZdS )
ExcInfoTestsz1
    Tests for L{excInfoOrFailureToExcInfo}.
    r   r   c                 C  s$   t t ddf}| |t|u  dS )z
        L{excInfoOrFailureToExcInfo} returns exactly what it is passed, if it is
        passed a tuple like the one returned by L{sys.exc_info}.
        fooN)
ValueErrorr'   r   )r    infor#   r#   r$   test_excInfo  s   zExcInfoTests.test_excInfoc                 C  sF   zdd  W n t y   t }Y nw | |j|j|jft| dS )z
        When called with a L{Failure} instance, L{excInfoOrFailureToExcInfo}
        returns a tuple like the one returned by L{sys.exc_info}, with the
        elements taken from the type, value, and traceback of the failure.
        rT   r   N)BaseExceptionr   r   typer   tbr   )r    fr#   r#   r$   test_failure  s   
 zExcInfoTests.test_failureNr/   )r0   r1   r2   r3   r   r   r#   r#   r#   r$   r     s    
r   c                   @  r   )AcquireAttributeTestsz(
    Tests for L{acquireAttribute}.
    r   r   c                 C  s*   t   | _}| |t| t  gdu  dS )z
        The value returned by L{acquireAttribute} is the value of the requested
        attribute on the first object in the list passed in which has that
        attribute.
        r   Nr\   r   r'   r   r    r   r#   r#   r$   test_foundOnEarlierObject  s   z/AcquireAttributeTests.test_foundOnEarlierObjectc                 C  s*   t   | _}| |tt  | gdu  dS )z
        The same as L{test_foundOnEarlierObject}, but for the case where the 2nd
        element in the object list has the attribute and the first does not.
        r   Nr   r   r#   r#   r$   test_foundOnLaterObject  s   z-AcquireAttributeTests.test_foundOnLaterObjectc                 C  s   |  ttt gd dS )z
        If none of the objects passed in the list to L{acquireAttribute} have
        the requested attribute, L{AttributeError} is raised.
        r   N)r   AttributeErrorr   r\   rQ   r#   r#   r$   test_notFoundException  s   z,AcquireAttributeTests.test_notFoundExceptionc                 C  s$   t  }| |tt  gd|u  dS )z
        If none of the objects passed in the list to L{acquireAttribute} have
        the requested attribute and a default value is given, the default value
        is returned.
        r   N)r\   r'   r   )r    defaultr#   r#   r$   test_notFoundDefault  s   z*AcquireAttributeTests.test_notFoundDefaultNr/   )r0   r1   r2   r3   r   r   r   r   r#   r#   r#   r$   r     s    

	
r   c                   @  sj   e Zd ZdZdddZdddZddd	Zdd
dZdddZdddZ	dddZ
dddZdddZdS )ListToPhraseTestsz
    Input is transformed into a string representation of the list,
    with each item separated by delimiter (defaulting to a comma) and the final
    two being separated by a final delimiter.
    r   r   c                 C  s$   g }d}t |d}| || dS )zB
        If things is empty, an empty string is returned.
         andNr   _listToPhraser   r    sampleexpectedresultr#   r#   r$   
test_empty  s   zListToPhraseTests.test_emptyc                 C  s&   dg}d}t |d}| || dS )z;
        With a single item, the item is returned.
        Oner   Nr   r   r#   r#   r$   test_oneWord  s   zListToPhraseTests.test_oneWordc                 C  s(   ddg}d}t |d}| || dS )zA
        Two words are separated by the final delimiter.
        r   TwozOne and Twor   Nr   r   r#   r#   r$   test_twoWords      zListToPhraseTests.test_twoWordsc                 C  (   g d}d}t |d}| || dS )zY
        With more than two words, the first two are separated by the delimiter.
        )r   r   ThreezOne, Two, and Threer   Nr   r   r#   r#   r$   test_threeWords)  r   z!ListToPhraseTests.test_threeWordsc                 C  s,   g d}d}t j|ddd}| || dS )zW
        If a delimiter is specified, it is used instead of the default comma.
        )r   r   r   FourzOne; Two; Three; or Fourorz; )	delimiterNr   r   r#   r#   r$   test_fourWords2  s   z ListToPhraseTests.test_fourWordsc                 C  r   )zS
        If something in things is not a string, it is converted into one.
        )rT      threez1, 2, and threer   Nr   r   r#   r#   r$   test_notString;  r   z ListToPhraseTests.test_notStringc                 C  s*   d}|  ttj|d}| t|d dS )z?
        If things is a string, a TypeError is raised.
        zOne, two, threer    Things must be a list or a tupleNr   	TypeErrorr   r   r   r9   r    r   r;   r#   r#   r$   test_stringTypeErrorD  s   z&ListToPhraseTests.test_stringTypeErrorc                 C  s2   t g d}| ttj|d}| t|d dS )zB
        If things is an iterator, a TypeError is raised.
        )rT   r      r   r   N)iterr   r   r   r   r   r9   r   r#   r#   r$   test_iteratorTypeErrorL  s   z(ListToPhraseTests.test_iteratorTypeErrorc                 C  s0   ddd}|  ttj|d}| t|d dS )	zB
        If things is a generator, a TypeError is raised.
        r   Generator[int, None, None]c                   s  s    t dE d H  d S )Nr   )ranger#   r#   r#   r$   r   Y  s   z9ListToPhraseTests.test_generatorTypeError.<locals>.sampler   r   N)r   r   r   r   r#   r#   r$   test_generatorTypeErrorT  s   
z)ListToPhraseTests.test_generatorTypeErrorNr/   )r0   r1   r2   r3   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   r     s    

	
	
	
	
	
	
r   c                   @  r   )
OpenTestLogTestsz#
    Tests for C{openTestLog}.
    r   r   c                 C  s   t  }| t jt j| t t jd d}t|  }t|}|	| W d   n1 s1w   Y  t
|jd}| }W d   n1 sKw   Y  t|dt| dS )zR
        The log file is opened in text mode and uses UTF-8 for encoding.
        )Casciiu   Here comes the ☉Nrbr   )locale	getlocale
addCleanup	setlocaleLC_ALLr
   r   r   r   writeopenr   readr   r   r   )r    currentLocaletextpr   writtenr#   r#   r$   	test_utf8e  s   

zOpenTestLogTests.test_utf8c                 C  s   d}d}dt j dt j }t|  }t|}|| W d   n1 s)w   Y  t|}|| W d   n1 sBw   Y  t| 	dt
| dS )z
        The log file is opened in append mode so if runner configuration specifies
        an existing log file its contents are not wiped out.
        zHello, world.
 zGoodbye, world.
zHello, world.z Goodbye, world.Nr   )r   linesepr
   r   r   r   r   r   
getContentdecoder   )r    existingTextnewTextr   r   r   r#   r#   r$   test_append{  s   

zOpenTestLogTests.test_appendNr/   )r0   r1   r2   r3   r  r  r#   r#   r#   r$   r   `  s    
r   )+r3   
__future__r   r   r   r   ior   typingr   zope.interfacer   hamcrestr   r   twisted.internet.baser   twisted.internet.interfacesr	   twisted.pythonr
   twisted.python.failurer   twisted.trialr   twisted.trial.unittestr   twisted.trial.utilr   r   r   r   r   r   r5   rC   rW   r^   r   r   r   r   r   r#   r#   r#   r$   <module>   s6   	)3- 3U'Y