
    YfH                         d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	 ddl
mZ  e	d          ddlmZ ndZ G d	 d
ej                  ZdS )z?
Tests for the inotify wrapper in L{twisted.internet.inotify}.
    N)deferreactor)filepathruntime)requireModule)unittestztwisted.python._inotify)inotifyc                       e Zd ZdZej                                        sdZd Zd Z	d dZ
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#dS )!INotifyTestsz]
    Define all the tests for the basic functionality exposed by
    L{inotify.INotify}.
    z&This platform doesn't support INotify.c                 .   t          j        |                                           | _        | j                                         t          j                    | _        | j                                         |                     | j        j	                   d S N)
r   FilePathmktempdirnamecreateDirectoryr	   INotifystartReading
addCleanuploseConnectionselfs    T/var/www/html/env/lib/python3.11/site-packages/twisted/internet/test/test_inotify.pysetUpzINotifyTests.setUp   sp    (77$$&&&((!!###344444    c                      G d d          }|                      t          j        d |                       |                     t          j        t          j                   dS )a  
        L{inotify.INotify} emits a C{RuntimeError} when initialized
        in an environment that doesn't support inotify as we expect it.

        We just try to raise an exception for every possible case in
        the for loop in L{inotify.INotify._inotify__init__}.
        c                       e Zd Zd ZdS );INotifyTests.test_initializationErrors.<locals>.FakeINotifyc                 (    t          j                    r   )r	   INotifyErrorr   s    r   initz@INotifyTests.test_initializationErrors.<locals>.FakeINotify.init.   s    *,,,r   N)__name__
__module____qualname__r     r   r   FakeINotifyr   -   s#        - - - - -r   r%   _inotifyN)patchr	   r   assertRaisesr   )r   r%   s     r   test_initializationErrorsz&INotifyTests.test_initializationErrors$   sk    	- 	- 	- 	- 	- 	- 	- 	- 	

7?J>>>'.@@@@@r   Nc                      j                             d          t          j                     fd}                    |            j                             j         fdg            |           S )a  
        Test notification from some filesystem operation.

        @param mask: The event mask to use when setting up the watch.

        @param operation: A function which will be called with the
            name of a file in the watched directory and which should
            trigger the event.

        @param expectedPath: Optionally, the name of the path which is
            expected to come back in the notification event; this will
            also be passed to C{operation} (primarily useful when the
            operation is being done to the directory itself, not a
            file in it).

        @return: A L{Deferred} which fires successfully when the
            expected event has been received or fails otherwise.
        Nzfoo.barc                     | \  }}}                     |                                                                                               |z             d S r   )assertEqualasBytesMode
assertTrue)resultwatchfilenameeventsexpectedPathmaskr   s       r   
cbNotifiedz2INotifyTests._notificationTest.<locals>.cbNotifiedK   sY    (.%UHfX1133\5M5M5O5OPPPOOFTM*****r   c                  .                         |           S r   callbackargsnotifieds    r   <lambda>z0INotifyTests._notificationTest.<locals>.<lambda>S   s    h>O>OPT>U>U r   r4   	callbacks)r   childr   DeferredaddCallbackr	   r0   )r   r4   	operationr3   r5   r;   s   `` ` @r   _notificationTestzINotifyTests._notificationTest4   s    & <--i88L>##	+ 	+ 	+ 	+ 	+ 	+ 	+
 	Z(((Lt0U0U0U0U/V 	 	
 	
 	
 		,r   c                 H    d }|                      t          j        |          S )z{
        Reading from a file in a monitored directory sends an
        C{inotify.IN_ACCESS} event to the callback.
        c                 X    |                      d           |                                  d S )N   foo)
setContent
getContentpaths    r   rB   z+INotifyTests.test_access.<locals>.operation^   s*    OOF###OOr   )rC   r	   	IN_ACCESSr   rB   s     r   test_accesszINotifyTests.test_accessX   s,    	 	 	 %%g&7CCCr   c                 H    d }|                      t          j        |          S )zy
        Writing to a file in a monitored directory sends an
        C{inotify.IN_MODIFY} event to the callback.
        c                     |                      d          5 }|                    d           d d d            d S # 1 swxY w Y   d S )NwrF   )openwrite)rJ   fObjs     r   rB   z+INotifyTests.test_modify.<locals>.operationj   s    3 #4

6"""# # # # # # # # # # # # # # # # # #s   9= =)rC   r	   	IN_MODIFYrL   s     r   test_modifyzINotifyTests.test_modifyd   s,    	# 	# 	# %%g&7CCCr   c                 H    d }|                      t          j        |          S )z
        Changing the metadata of a file in a monitored directory
        sends an C{inotify.IN_ATTRIB} event to the callback.
        c                 V    |                                   |                                   d S r   )touchrI   s    r   rB   z+INotifyTests.test_attrib.<locals>.operationv   s     JJLLLJJLLLLLr   )rC   r	   	IN_ATTRIBrL   s     r   test_attribzINotifyTests.test_attribp   s,    	 	 	 %%g&7CCCr   c                 H    d }|                      t          j        |          S )z
        Closing a file which was open for writing in a monitored
        directory sends an C{inotify.IN_CLOSE_WRITE} event to the
        callback.
        c                 T    |                      d                                           d S NrP   rQ   closerI   s    r   rB   z/INotifyTests.test_closeWrite.<locals>.operation   $    IIcNN  """""r   )rC   r	   IN_CLOSE_WRITErL   s     r   test_closeWritezINotifyTests.test_closeWrite|   s,    	# 	# 	# %%g&<iHHHr   c                 H    d }|                      t          j        |          S )z
        Closing a file which was open for reading but not writing in a
        monitored directory sends an C{inotify.IN_CLOSE_NOWRITE} event
        to the callback.
        c                 |    |                                   |                     d                                           d S )Nr)rX   rQ   r_   rI   s    r   rB   z1INotifyTests.test_closeNoWrite.<locals>.operation   s1    JJLLLIIcNN  """""r   )rC   r	   IN_CLOSE_NOWRITErL   s     r   test_closeNoWritezINotifyTests.test_closeNoWrite   s,    	# 	# 	# %%g&>	JJJr   c                 H    d }|                      t          j        |          S )zt
        Opening a file in a monitored directory sends an
        C{inotify.IN_OPEN} event to the callback.
        c                 T    |                      d                                           d S r]   r^   rI   s    r   rB   z)INotifyTests.test_open.<locals>.operation   r`   r   )rC   r	   IN_OPENrL   s     r   	test_openzINotifyTests.test_open   s+    	# 	# 	# %%goyAAAr   c                 N      fd}                      t          j        |          S )z}
        Moving a file out of a monitored directory sends an
        C{inotify.IN_MOVED_FROM} event to the callback.
        c                     |                      d                                           |                     t          j                                                             d S r]   )rQ   r_   moveTor   r   r   rJ   r   s    r   rB   z.INotifyTests.test_movedFrom.<locals>.operation   sJ    IIcNN  """KK)$++--8899999r   )rC   r	   IN_MOVED_FROMrL   s   ` r   test_movedFromzINotifyTests.test_movedFrom   s7    	: 	: 	: 	: 	: %%g&;YGGGr   c                 N      fd}                      t          j        |          S )zy
        Moving a file into a monitored directory sends an
        C{inotify.IN_MOVED_TO} event to the callback.
        c                     t          j                                                  }|                                 |                    |            d S r   )r   r   r   rX   rn   )rJ   pr   s     r   rB   z,INotifyTests.test_movedTo.<locals>.operation   s;    !$++--00AGGIIIHHTNNNNNr   )rC   r	   IN_MOVED_TOrL   s   ` r   test_movedTozINotifyTests.test_movedTo   s7    	 	 	 	 	
 %%g&99EEEr   c                 H    d }|                      t          j        |          S )zw
        Creating a file in a monitored directory sends an
        C{inotify.IN_CREATE} event to the callback.
        c                 T    |                      d                                           d S r]   r^   rI   s    r   rB   z+INotifyTests.test_create.<locals>.operation   r`   r   )rC   r	   	IN_CREATErL   s     r   test_createzINotifyTests.test_create   s,    	# 	# 	# %%g&7CCCr   c                 H    d }|                      t          j        |          S )zw
        Deleting a file in a monitored directory sends an
        C{inotify.IN_DELETE} event to the callback.
        c                 V    |                                   |                                  d S r   )rX   removerI   s    r   rB   z+INotifyTests.test_delete.<locals>.operation   s     JJLLLKKMMMMMr   )rC   r	   	IN_DELETErL   s     r   test_deletezINotifyTests.test_delete   s,    	 	 	 %%g&7CCCr   c                 V    d }|                      t          j        || j                  S )z{
        Deleting the monitored directory itself sends an
        C{inotify.IN_DELETE_SELF} event to the callback.
        c                 .    |                                   d S r   )r}   rI   s    r   rB   z/INotifyTests.test_deleteSelf.<locals>.operation   s    KKMMMMMr   r3   )rC   r	   IN_DELETE_SELFr   rL   s     r   test_deleteSelfzINotifyTests.test_deleteSelf   s<    	 	 	 %%"IDL & 
 
 	
r   c                       fd} j                              j        dfdg           t          j                                        |            j                                         S )z
        Deleting the monitored directory itself sends an
        C{inotify.IN_DELETE_SELF} event to the callback
        even if the mask isn't specified by the call to watch().
        c                     | \  }}}                     |                                j                                                                       |t          j        z             d S r   )r,   r-   r   r.   r	   r   )r/   r0   r1   r2   r   s       r   r5   z6INotifyTests.test_deleteSelfForced.<locals>.cbNotified   s_    (.%UHfX1133T\5M5M5O5OPPPOOFW%;;<<<<<r   r   c                  .                         |           S r   r7   )r:   ds    r   r<   z4INotifyTests.test_deleteSelfForced.<locals>.<lambda>   s    QZZ=M=M r   r=   )r	   r0   r   r   r@   rA   r}   )r   r5   r   s   ` @r   test_deleteSelfForcedz"INotifyTests.test_deleteSelfForced   s    	= 	= 	= 	= 	=
 	Ls/M/M/M/M.N 	 	
 	
 	
 N	j!!!r   c                 \      fd}                      t          j        | j                  S )zy
        Renaming the monitored directory itself sends an
        C{inotify.IN_MOVE_SELF} event to the callback.
        c                 z    |                      t          j                                                             d S r   )rn   r   r   r   ro   s    r   rB   z-INotifyTests.test_moveSelf.<locals>.operation   s.    KK)$++--8899999r   r   )rC   r	   IN_MOVE_SELFr   rL   s   ` r   test_moveSelfzINotifyTests.test_moveSelf   sG    	: 	: 	: 	: 	: %% )$, & 
 
 	
r   c                      fd}t           j        t           j        z  } j                              j        |d|g            j                            d          t          j                                                     S )z
        L{inotify.INotify} when initialized with autoAdd==True adds
        also adds the created subdirectories to the watchlist.
        c                 @    fd}t          j        d|           d S )Nc                      	                      j                                                                      d            d S # t          $ r                                   Y d S w xY wr   )r.   r	   
_isWatchedr8   	Exceptionerrbackr   r   subdirs   r   _zIINotifyTests.test_simpleSubdirectoryAutoAdd.<locals>._callback.<locals>._	  sp     OODL$;$;F$C$CDDDJJt$$$$$       IIKKKKKK    AA A)(A)r   r   	callLater)wpr1   r4   r   r   r   r   s       r   	_callbackz>INotifyTests.test_simpleSubdirectoryAutoAdd.<locals>._callback  A                  a#####r   Tr4   autoAddr>   test	r	   IN_ISDIRry   r0   r   r?   r   r@   r   r   r   	checkMaskr   r   s   `  @@r   test_simpleSubdirectoryAutoAddz+INotifyTests.test_simpleSubdirectoryAutoAdd   s    
	$ 
	$ 
	$ 
	$ 
	$ 
	$ 
	$ $w'88	Ly$9+ 	 	
 	
 	
 ##F++N   r   c                     g  fd}t           j        t           j        z  } j                              j        |d|g            j                            d          t          j                                                     S )zz
        L{inotify.INotify} removes a directory from the watchlist when
        it's removed from the filesystem.
        c                     fd}fd}s,                     |           t          j        d|           d S t          j        d|           d S )Nc                      	                      j                                                                                  d S # t          $ r                                   Y d S w xY wr   )r.   r	   r   r}   r   r   r   s   r   r   zEINotifyTests.test_simpleDeleteDirectory.<locals>._callback.<locals>._%  si     OODL$;$;F$C$CDDDMMOOOOO       IIKKKKKK s   AA A('A(c                      	                      j                                                                      d            d S # t          $ r                                   Y d S w xY wr   assertFalser	   r   r8   r   r   r   s   r   _ebzGINotifyTests.test_simpleDeleteDirectory.<locals>._callback.<locals>._eb,  sr     $$T\%<%<V%D%DEEEJJt$$$$$       IIKKKKKK r   r   )appendr   r   )	r   r1   r4   r   r   callsr   r   r   s	        r   r   z:INotifyTests.test_simpleDeleteDirectory.<locals>._callback"  s                                *X&&&!!Q''''' !!S)))))r   Tr   r   r   )r   r   r   r   r   r   s   `  @@@r   test_simpleDeleteDirectoryz'INotifyTests.test_simpleDeleteDirectory  s    
 	* 	* 	* 	* 	* 	* 	* 	*4 $w'88	Ly$9+ 	 	
 	
 	
 ##F++N   r   c                 F     fd}                       j                             j        t          j        fdg                     t          j                                        |            j                                         t          j                    S )z
        L{inotify.INotify} closes the file descriptor after removing a
        directory from the filesystem (and therefore from the watchlist).
        c                    fd}| \  }}}                     |                                j                                                                       |t          j        z             t          j        d|           d S )Nc                      	                      j                            j                                                  j        j                                        d            d S # t          $ r                                   Y d S w xY wr   )r   r	   r   r   	connectedr8   r   r   )r   r   s   r   r   zIINotifyTests.test_deleteSelfLoseConnection.<locals>.cbNotified.<locals>._L  s     $$T\%<%<T\%J%JKKK$$T\%;<<<JJt$$$$$       IIKKKKKK s   A&A+ +BBr   )r,   r-   r   r.   r	   r   r   r   )r/   r   ignoredr1   r2   r   r   s        r   r5   z>INotifyTests.test_deleteSelfLoseConnection.<locals>.cbNotifiedK  s                +1'WhX1133T\5M5M5O5OPPPOOFW%;;<<<a#####r   c                  .                         |           S r   r7   r9   s    r   r<   z<INotifyTests.test_deleteSelfLoseConnection.<locals>.<lambda>]  s    ):):4)@)@ r   r=   )	r.   r	   r0   r   r   r   r@   rA   r}   )r   r5   r   r;   s   ` @@r   test_deleteSelfLoseConnectionz*INotifyTests.test_deleteSelfLoseConnectionE  s    	$ 	$ 	$ 	$ 	$ 	$ 	L+@@@@A   	
 	
 	
 >##Z(((Nr   c                 N   | j                             | j        d           |                     | j                             | j                             | j                             | j                   |                     | j                             | j                             dS )zR
        L{inotify.INotify.ignore} removes a directory from the watchlist
        T)r   N)r	   r0   r   r.   r   ignorer   r   s    r   test_ignoreDirectoryz!INotifyTests.test_ignoreDirectoryh  s     	4<666//==>>>DL)))00>>?????r   c                 @   t           j        D ]3\  }}|                     t          j        |          d         |           4t           j        t           j        z  t           j        z  }|                     t          t          j        |                    h d           dS )zy
        L{inotify.humaReadableMask} translates all the possible event
        masks to a human readable string.
        r   >   rQ   accessclose_writeN)r	   _FLAG_TO_HUMANr,   humanReadableMaskra   rK   rj   set)r   r4   valuer   s       r   test_humanReadableMaskz#INotifyTests.test_humanReadableMaskq  s    
 #1 	H 	HKD%W6t<<Q?GGGG*W->>P	))4455---	
 	
 	
 	
 	
r   c                    | j                             d          }|                    d          }|                    d          }|                                 |||g}| j                            | j         d           | j                            | j         d           |D ]/}|                     | j                            |                     0dS )z
        L{inotify.INotify.watch} with recursive==True will add all the
        subdirectories under the given path to the watchlist.
        r   test2test3T)	recursiveN)r   r?   makedirsr	   r0   r.   r   )r   r   subdir2subdir3dirsr   s         r   test_recursiveWatchz INotifyTests.test_recursiveWatch  s    
 ##F++,,w''--(()4<48884<4888 	8 	8AOODL33A667777	8 	8r   c                     ddl }t          j                    }|                    |j                   |                                 |                                  dS )z
        L{inotify.INotify.connectionLost} if there's a problem while closing
        the fd shouldn't raise the exception but should log the error
        r   N)osr	   r   r_   _fdr   flushLoggedErrors)r   r   in_s      r   test_connectionLostErrorz%INotifyTests.test_connectionLostError  sX    
 				o
     r   c                      fd}t           j        t           j        z  } j                              j        |d|g            j                            d          t          j                                                     S )z
        L{inotify.INotify.watch} with autoAdd==False will stop inotify
        from watching subdirectories created under the watched one.
        c                 @    fd}t          j        d|           d S )Nc                      	                      j                                                                      d            d S # t          $ r                                   Y d S w xY wr   r   r   s   r   r   zEINotifyTests.test_noAutoAddSubdirectory.<locals>._callback.<locals>._  sr     $$T\%<%<V%D%DEEEJJt$$$$$       IIKKKKKK r   r   r   )r   fpr4   r   r   r   r   s       r   r   z:INotifyTests.test_noAutoAddSubdirectory.<locals>._callback  r   r   Fr   r   r   r   s   `  @@r   test_noAutoAddSubdirectoryz'INotifyTests.test_noAutoAddSubdirectory  s    
	$ 
	$ 
	$ 
	$ 
	$ 
	$ 
	$ $w'88	Ly%I; 	 	
 	
 	
 ##F++N   r   c                       j                             d                                           t          j                     fd}fd}                      j                            |g                      j                                                             j                            t          j	        |g                     
                    |                                            S )z
        L{inotify.INotify} will watch a filepath for events even if the same
        path is repeatedly added/removed/re-added to the watchpoints.
        foo.bar2c                     | \  }}}                     |                                                                                               |t          j        z             d S r   r,   r-   r.   r	   r   )r/   r   r1   r2   r3   r   s       r   r5   z<INotifyTests.test_seriesOfWatchAndIgnore.<locals>.cbNotified  s]    *0'WhX1133\5M5M5O5OPPPOOFW%;;<<<<<r   c                  2                         |            d S r   r7   r9   s    r   callItz8INotifyTests.test_seriesOfWatchAndIgnore.<locals>.callIt      d#####r   r>   r=   )r   r?   rX   r   r@   r.   r	   r0   r   r   rA   r}   )r   r5   r   r3   r;   s   `  @@r   test_seriesOfWatchAndIgnorez(INotifyTests.test_seriesOfWatchAndIgnore  s   
 |))*55>##	= 	= 	= 	= 	= 	=
	$ 	$ 	$ 	$ 	$ 	**<F8*LLMMML)))L7#9fX   	
 	
 	
 	Z((( 	r   c                      j                             d          }|                                  j                             d                                           t          j                     fd}fd}                      j                            |t          j        |g                     	                    |                                 j                            t          j        |g                      j        
                    |           |                                                                  S )zr
        L{inotify.INotify} will ignore a filepath after it has been removed from
        the watch list.
        r   zfoo.bar3c                     | \  }}}                     |                                                                                               |t          j        z             d S r   r   )r/   r   r1   r2   expectedPath2r   s       r   r5   z4INotifyTests.test_ignoreFilePath.<locals>.cbNotified  s]    *0'WhX1133]5N5N5P5PQQQOOFW%;;<<<<<r   c                  2                         |            d S r   r7   r9   s    r   r   z0INotifyTests.test_ignoreFilePath.<locals>.callIt  r   r   r   )r   r?   rX   r   r@   r.   r	   r0   r   rA   r   r}   )r   r3   r5   r   r   r;   s   `   @@r   test_ignoreFilePathz INotifyTests.test_ignoreFilePath  sd   
 |))*55**:66>##	= 	= 	= 	= 	= 	=
	$ 	$ 	$ 	$ 	$ 	L|W-CPVxXX	
 	
 	
 	Z(((Lw5&   	
 	
 	
 	L)))r   c                     | j                             d          }|                                 |                     t          | j        j        |           dS )zf
        L{inotify.INotify} will raise KeyError if a non-watched filepath is
        ignored.
        zfoo.ignoredN)r   r?   rX   r(   KeyErrorr	   r   )r   r3   s     r   test_ignoreNonWatchedFilez&INotifyTests.test_ignoreNonWatchedFile  sN    
 |))-88(DL$7FFFFFr   c                    	
 t                       	
fd}t          j        t          j        z  } j                             j        |d|g            j                            d                              d          		                    d          
t          j                    
	                                                     d          	                    d          
                    d	          gt                    D ]C\  }}|                    |j                            t          j                                         DS )
aB  
        L{inotify.INotify} with autoAdd==True for a watched path
        generates events for every file or directory already present
        in a newly created subdirectory under the watched one.

        This tests that we solve a race condition in inotify even though
        we may generate duplicate events.
        c                                         |           t                    dk    r	                     j                                                                     j                            	                                         j                            
                     	
gz   }d |D             }                    t                    t          |                                         |                               d            d S # t          $ r                                  Y d S w xY wd S )N   c                 6    h | ]}|                                 S r$   )r-   ).0fs     r   	<setcomp>zRINotifyTests.test_complexSubdirectoryAutoAdd.<locals>._callback.<locals>.<setcomp>  s     @@@1q}}@@@r   )	addlenr.   r	   r   r,   r8   r   r   )r   r1   r4   createdr   r   r   	someFilesr   r   r   s       r   r   z?INotifyTests.test_complexSubdirectoryAutoAdd.<locals>._callback  s<   IIh5zzQ%OODL$;$;F$C$CDDDOODL$;$;G$D$DEEEOODL$;$;G$D$DEEE'67G*DDG@@@@@G$$SZZW>>>$$UG444 JJt$$$$$ !      IIKKKKKK  s   C!D$ $EETr   r   r   r   z	file1.datz	file2.datz	file3.dat)r   r	   r   ry   r0   r   r?   r   r@   r   	enumeraterG   rJ   encodesysgetfilesystemencoding)r   r   r   ir1   r   r   r   r   r   r   s   `    @@@@@@r   test_complexSubdirectoryAutoAddz,INotifyTests.test_complexSubdirectoryAutoAdd  sk    	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  $w'88	Ly$9+ 	 	
 	
 	
 ##F++,,w''--((N LL%%MM+&&MM+&&
	 %Y// 	S 	SKAx 4 4S5N5P5P Q QRRRRr   r   )$r!   r"   r#   __doc__r   platformsupportsINotifyskipr   r)   rC   rM   rU   rZ   rb   rg   rk   rq   rv   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r   r      s        
 ++-- 875 5 5A A A " " " "H
D 
D 
D
D 
D 
D
D 
D 
D
I 
I 
IK K K	B 	B 	B
H 
H 
HF F F	D 	D 	D
D 
D 
D
 
 
  ,
 
 
  6( ( (T! ! !F@ @ @
 
 
8 8 8 
! 
! 
!  6# # #J$ $ $LG G G. . . . .r   r   )r   r   twisted.internetr   r   twisted.pythonr   r   twisted.python.reflectr   twisted.trialr   r	   TestCaser   r$   r   r   <module>r      s     


 + + + + + + + + , , , , , , , , 0 0 0 0 0 0 " " " " " "=*++7(((((((Ge e e e e8$ e e e e er   