
    hG                        S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
Jr  S SKJr   " S S\5      r " S S	\
R                  5      r " S
 S\5      rg)    )annotationsN)skipIf)logfileruntime)TestCasec                     \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSS jr\" \R*                  R-                  5       S5      SS j5       rSS jrSS jrSS jrSS jrSrg)LogFileTests   z
Test the rotating log file.
c                    U R                  5       U l        [        R                  " U R                  5        SU l        [        R
                  R                  U R                  U R                  5      U l        g )Nztest.logmktempdirosmakedirsnamepathjoinselfs    `/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_logfile.pysetUpLogFileTests.setUp   sD    ;;=
DHH	GGLL4995	    c                    [         R                  " U R                  S5        [         R                  R	                  U R                  5      (       a"  [         R                  " U R                  S5        gg)z
Restore back write rights on created paths: if tests modified the
rights, that will allow the paths to be removed easily afterwards.
  N)r   chmodr   r   existsr   s    r   tearDownLogFileTests.tearDown   sD    
 	5!77>>$))$$HHTYY& %r   c                    [         R                  " U R                  U R                  5      nU R	                  UR
                  5        U R                  [        UR                  5        g)zN
L{BaseLogFile.shouldRotate} is abstract and must be implemented by
subclass.
N)	r   BaseLogFiler   r   
addCleanupcloseassertRaisesNotImplementedErrorshouldRotater   logs     r   test_abstractShouldRotate&LogFileTests.test_abstractShouldRotate$   sE    
 !!$))TXX6		"-s/?/?@r   c                   [         R                  " [        R                  " U R                  U R
                  5      5       nUR                  S5        UR                  S5        UR                  5         UR                  S5        SSS5        [        U R                  5       nU R                  UR                  5       S5        SSS5        g! , (       d  f       NM= f! , (       d  f       g= f)zV
Log files can be written to, flushed and closed. Closing a log file
also flushes it.
1234567890N
1234567890)
contextlibclosingr   LogFiler   r   writeflushopenr   assertEqualreadr   r(   fs      r   test_writingLogFileTests.test_writing-   s    
 		488 DEIIeIIeIIKIIf	 F $))_QVVX|4 _ FE _s    AC!!C
C
C*c                r   [         R                  " [        R                  " U R                  U R
                  SS95       nUR                  S5        UR                  S5        UR                  S5        U R                  [        R                  R                  U R                   S35      5        U R                  [        R                  R                  U R                   S35      5        UR                  S5        U R                  [        R                  R                  U R                   S35      5        U R                  [        R                  R                  U R                   S35      5        U R                  [        R                  R                  U R                   S	35      5        UR                  S
5        U R                  [        R                  R                  U R                   S	35      5        UR                  5         U R                  [        R                  R                  U R                   S	35      5        U R                  [        R                  R                  U R                   S35      5        SSS5        U R                  WR                  5       / SQ5        g! , (       d  f       N1= f)zY
Rotating log files autorotate after a period of time, and can also be
manually rotated.

   )rotateLengthr,   456789011111111111.1.2 .33.4N)         )r0   r1   r   r2   r   r   r3   
assertTruer   r   r   assertFalserotater6   listLogsr'   s     r   test_rotationLogFileTests.test_rotation;   s    OODIItxxbA
IIeIIi IIhOOBGGNNdii[+;<=RWW^^tyyk,<=>IIbMOOBGGNNdii[+;<=OOBGGNNdii[+;<=RWW^^tyyk,<=>IIcNRWW^^tyyk,<=> JJLOOBGGNNdii[+;<=RWW^^tyyk,<=>'
* 	3+
 
s   H>J((
J6c                |   [         R                  " [        R                  " U R                  U R
                  5      5       nUR                  S5        SSS5        [        R                  " U R                  U R
                  5      nU R                  UR                  5        U R                  UR                  S5        U R                  UR                  R                  5       UR                  5        UR                  S5        UR                  S5        SnU R                  UR                  [        U5      5        U R                  UR                  R                  5       UR                  5        UR                  nUR                  SS5        U R                  UR                  5       U5        g! , (       d  f       GNa= f)z
Log files can be written to, closed. Their size is the number of
bytes written to them. Everything that was written to them can
be read, even if the writing happened on separate occasions,
and even if the log file was closed in between.

0123456789Nr=   abcs   defs   0123456789abcdefr   )r0   r1   r   r2   r   r   r3   r"   r#   r6   size_filetelllenseekr7   )r   r(   expectResultr9   s       r   test_appendLogFileTests.test_appendX   s!    		488 DEIIl# F oodii2		"2&)3884		%		*.3|#45)3884II	q!<0 FEs    F,,
F;c                   [         R                  " U R                  U R                  5      nU R	                  UR
                  5        UR                  S5        UR                  S5        UR                  5         UR                  S5        UR                  5         U R                  UR                  5       S/5        [        R                  " UR                  5       5       nUR                  R                  S5        U R                  UR!                  5       S/5        U R                  UR!                  5       / 5        SSS5        [        R                  " UR#                  S5      5       nU R                  UR!                  5       SS/5        U R                  UR!                  5       / 5        SSS5        U R%                  [&        UR"                  S5        U R%                  [(        UR"                  S5        UR                  5         U R                  UR                  5       SS/5        [        R                  " UR#                  S5      5       nUR                  R                  S5        U R                  UR!                  5       S/5        U R                  UR!                  5       / 5        SSS5        [        R                  " UR#                  S5      5       nU R                  UR!                  5       SS/5        U R                  UR!                  5       / 5        SSS5        g! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       g= f)	a#  
Various tests for log readers.

First of all, log readers can get logs by number and read what
was written to those log files. Getting nonexistent log files
raises C{ValueError}. Using anything other than an integer
index raises C{TypeError}. As logs get older, their log
numbers increase.
zabc
zdef
zghi
rG   r   NrH   1)r   r2   r   r   r"   r#   r3   rL   r4   r6   rM   r0   r1   getCurrentLogrT   rW   	readLinesgetLogr$   
ValueError	TypeError)r   r(   readers      r   test_logReaderLogFileTests.test_logReadero   si    oodii2		"		'		'

		'		 	!- 1 1 34LLa V--/';V--/4 5 

1.&V--/'71CDV--/4 /
 	*cjj!4)SZZ5 	

!Q0

1.&LLa V--/';V--/4 / 

1.&V--/'71CDV--/4 /.' 54 /. /. /.s4   AL#)AL5AMAM#
L25
M
M
M&c                   [        U R                  S5          SSS5        [        R                  " U R                  5      nU R	                  UR
                  5        U R                  / UR                  S5      5        g! , (       d  f       Nk= f)z2
L{LogReader.readLines} supports reading no line.
wNr   )r5   r   r   	LogReaderr"   r#   r6   r^   )r   rb   s     r   test_LogReaderReadsZeroLine(LogFileTests.test_LogReaderReadsZeroLine   sd    
 $))S! " ""499-%V--a01 "!s   A>>
Bc                @   [        U R                  S5      R                  5         [        R                  " U R                  S5        [        R
                  " U R                  5      [
        R                     n[        R                  " U R                  U R                  5      nU R                  UR                  5        UR                  S5        UR                  5         U R                  U[        R
                  " U R                  5      [
        R                     5        g)z8
Check rotated files have same permissions as original.
rf     rR   N)r5   r   r#   r   r   statST_MODEr   r2   r   r   r"   r3   rL   r6   )r   moder(   s      r   test_modePreservation"LogFileTests.test_modePreservation   s     	TYY""$
E"wwtyy!$,,/oodii2		"		%

rwwtyy1$,,?@r   c                   [         R                  " U R                  U R                  5      nU R	                  UR
                  5        UR                  S5        [        R                  " U R                  S5         [        [        R                  R                  U R                  S5      S5      nUR                  5         g! [         a     Of = fUR                  5         UR                  S5        UR                  5         UR                  nU R!                  UR#                  5       S5        UR%                  SS5        U R!                  UR'                  5       S	5        g)
z8
Check it keeps working when permission on dir changes.
rR   m  xxxrf   Ndef   r   s   abcdef)r   r2   r   r   r"   r#   r3   r   r   r5   r   r   OSErrorrL   r4   rT   r6   rU   rW   r7   r8   s      r   test_noPermissionLogFileTests.test_noPermission   s     oodii2		"		% 	5!	RWW\\$((E2C8A GGI	  		 	

		%		II1%	q!9-s   :4B? ?
CCc                Z   [         R                  " U R                  U R                  SSS9nU R	                  UR
                  5        UR                  S5        UR                  S5        U R                  [        R                  R                  U R                   S35      5        UR                  S5        U R                  [        R                  R                  U R                   S35      5        UR                  S	5        U R                  [        R                  R                  U R                   S
35      5        [        U R                   S
35       nU R                  UR                  5       S5        SSS5        UR                  S5        [        U R                   S
35       nU R                  UR                  5       S5        SSS5        U R                  [        R                  R                  U R                   S35      5        g! , (       d  f       N= f! , (       d  f       N[= f)zT
Test it respect the limit on the number of files when maxRotatedFiles
is not None.
r=   rI   )r>   maxRotatedFilesr@   22222222222rA   33333333333rB   44444444444rD   N55555555555rF   )r   r2   r   r   r"   r#   r3   rJ   r   r   r   r5   r6   r7   rK   r   r(   fps      r   test_maxNumberOfLog LogFileTests.test_maxNumberOfLog   ss   
 oodiiTUV		"		(		($))B'789		($))B'789		($))B'789TYYKr"#rRWWY1 $ 			(TYYKr"#rRWWY1 $499+R(89: $# $#s   !H&!H
H
H*c                   [         R                  " U R                  U R                  SSS9nU R	                  UR
                  5        [         R                  R                  U R                  SSS9nU R	                  UR
                  5        U R                  UR                  UR                  5        U R                  [        R                  R                  UR                  5      UR                  5        U R                  UR                  UR                  5        U R                  UR                  UR                  5        g)z
Test the fromFullPath method.
r=   r   defaultModeN)r   r2   r   r   r"   r#   fromFullPathr   r6   r   abspathr>   r   )r   log1log2s      r   test_fromFullPathLogFileTests.test_fromFullPath   s     tyy$((BEJ

#++DIIru+M

#DII.3TYY?**D,=,=>))4+;+;<r   c                f   [        U R                  S5         [        R                  " U R                  S5        [        R
                  " [        R                  " U R                  5      [        R                     5      nSSS5        [        R                  " U R                  U R                  5      nU R                  [        R
                  " [        R                  " U R                  5      [        R                     5      W5        U R                  UR                  5        g! , (       d  f       N= f)z`
Test the default permission of the log file: if the file exist, it
should keep the permission.
wbrk   N)r5   r   r   r   rl   S_IMODErm   r   r2   r   r   r6   r"   r#   )r   currentModer   s      r   test_defaultPermissions$LogFileTests.test_defaultPermissions   s    
 $))T"HHTYY&,,rwwtyy'9$,,'GHK # tyy$((3bggdii&8&FGU

# #"s   A'D""
D0c                   [         R                  " U R                  U R                  SS9nU R	                  UR
                  5        [        R                  " [        R                  " U R                  5      [        R                     5      n[        R                  R                  5       (       a  U R                  US5        gU R                  US5        g)z7
Test specifying the permissions used on the log file.
6   r   $  N)r   r2   r   r   r"   r#   rl   r   r   r   rm   r   platform	isWindowsr6   )r   r   rn   s      r   test_specifiedPermissions&LogFileTests.test_specifiedPermissions  s     tyy$((F

#||BGGDII.t||<=%%''T5)T5)r   zCan't test reopen on Windowsc                   [         R                  " [        R                  " U R                  U R
                  5      5       nUR                  S5        [        R                  R                  U R
                  S5      n[        R                  " U R                  U5        UR                  5         UR                  S5        SSS5        [        U R                  5       nU R                  UR                  5       S5        SSS5        [        W5       nU R                  UR                  5       S5        SSS5        g! , (       d  f       N= f! , (       d  f       NT= f! , (       d  f       g= f)zs
L{logfile.LogFile.reopen} allows to rename the currently used file and
make L{logfile.LogFile} create a new file.
hello1zsave.loghello2N)r0   r1   r   r2   r   r   r3   r   r   r   renamereopenr5   r6   r7   )r   r   savePathr9   s       r   test_reopenLogFileTests.test_reopen  s     		488 DEJJx ww||DHHj9HIIdii*KKMJJx  F $))_QVVXx0 (^qQVVXx0 ^ FE _^s%    A>D9!E
!E9
E

E
E)c                    U R                  [        [        R                  U R                  S5      nU R                  UR                  [        R                  5        g)zB
Specifying an invalid directory to L{LogFile} raises C{IOError}.
this_dir_does_not_existN)r$   IOErrorr   r2   r   r6   errnoENOENT)r   es     r   test_nonExistentDir LogFileTests.test_nonExistentDir"  s@     W__dii1J
 	%,,/r   c                4   [         R                  R                  5       (       a  Su  pSnOSu  pSn[        R                  " XSS9nU R                  UR                  5        U R                  UR                  U5        U R                  UR                  S5        g)zl
Opening a L{LogFile} which can be read and write but whose mode can't
be changed doesn't trigger an error.
)NULrC   r   )nullz/devz	/dev/nullrr   r   N)
r   r   r   r   r2   r"   r#   r6   r   r   )r   r   	directoryexpectedPathr(   s        r   test_cantChangeFileMode$LogFileTests.test_cantChangeFileMode+  sy    
 %%'''OD L,OD&Lood5A		"<0%0r   c                   [         R                  " U R                  U R                  5      nU R	                  UR
                  5        [        UR                   S3S5       nUR                  S5        SSS5        [        UR                   S3S5       nUR                  S5        SSS5        U R                  S/UR                  5       5        g! , (       d  f       Nc= f! , (       d  f       NA= f)zT
L{LogFile.listLogs} doesn't choke if it encounters a file with an
unexpected name.
rA   rf   r,   Nz	.bad-filerG   )r   r2   r   r   r"   r#   r5   r   r3   r6   rM   r   s      r    test_listLogsWithBadlyNamedFiles-LogFileTests.test_listLogsWithBadlyNamedFiles=  s    
 oodii2		"SXXJb/3'2HHUO (SXXJi(#."HHUO / 	!clln- ('..s    CC 
C 
C.c                   [         R                  " U R                  U R                  5      nU R	                  UR
                  5        [        SS5       H8  n[        UR                   SU 3S5       nUR                  S5        SSS5        M:     U R                  SS/UR                  5       5        g! , (       d  f       Mn  = f)	zB
L{LogFile.listLogs} ignores log files which rotated suffix is 0.
r   rI   .rf   r,   NrG   rH   )r   r2   r   r   r"   r#   ranger5   r   r3   r6   rM   )r   r(   ir   s       r   %test_listLogsIgnoresZeroSuffixedFiles2LogFileTests.test_listLogsIgnoresZeroSuffixedFilesL  s     oodii2		"q!A
!A3'- .-  	!Q0 .-s   1B22
C	r   r   r   NreturnNone)__name__
__module____qualname____firstlineno____doc__r   r   r)   r:   rN   rY   rc   rh   ro   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__ r   r   r	   r	      s    6'A54:1.)5V
2A.>;0=
$* G&&(*HI1 J1"01$.1r   r	   c                  ,    \ rS rSrSrSS jrSS jrSrg)	RiggedDailyLogFileiZ          c                l    [         R                  R                  U 5        U R                  5       U l        g )N)r   DailyLogFile	_openFiletoDatelastDater   s    r   r   RiggedDailyLogFile._openFile]  s#    &&t,r   c                    U(       a  [         R                  " U6 S S $ [         R                  " U R                  5      S S $ )NrI   )timegmtime_clock)r   argss     r   r   RiggedDailyLogFile.toDateb  s5    ;;%bq)){{4;;'++r   )r   Nr   )r   floatr   ztuple[int, int, int])r   r   r   r   r   r   r   r   r   r   r   r   r   Z  s    F&
,r   r   c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	\
" \R                  R                  5       S5      SS	 j5       rSS
 jrSS jrSS jrSS jrSrg)DailyLogFileTestsih  z
Test rotating log file.
c                    U R                  5       U l        [        R                  " U R                  5        SU l        [        R
                  R                  U R                  U R                  5      U l        g )Nztestdaily.logr   r   s    r   r   DailyLogFileTests.setUpm  sD    ;;=
DHH#	GGLL4995	r   c                   [         R                  " [        U R                  U R                  5      5       nUR                  S5        UR                  S5        UR                  5         UR                  S5        SSS5        [        U R                  5       nU R                  UR                  5       S5        SSS5        g! , (       d  f       NM= f! , (       d  f       g= f)z?
A daily log file can be written to like an ordinary log file.
r,   r-   r.   Nr/   )r0   r1   r   r   r   r3   r4   r5   r   r6   r7   r8   s      r   r:   DailyLogFileTests.test_writings  s      2499dhh GHCIIeIIeIIKIIf	 I $))_QVVX|4 _ IH _s   AC !C 
C
Cc                8   [        U R                  U R                  5      nU R                  UR                  5        [        S5       Vs/ s H&  o R                  S-   UR                  US-  5      -   PM(     nnSUl        UR                  S5        SUl        UR                  S5        SUl        UR                  S5        U R                  [        R                  R                  US	   5      5        U R                  [        R                  R                  US
   5      5        SUl        UR                  S5        U R                  [        R                  R                  US	   5      5        U R                  [        R                  R                  US
   5      5        U R                  [        R                  R                  US   5      5        SUl        UR                  S5        U R                  [        R                  R                  US   5      5        gs  snf )z
Daily log files rotate daily.
rI   r   Q r   r,   i  r?   r@   r   rG   i  rC   rH   i rE   N)r   r   r   r"   r#   r   r   suffixr   r3   rJ   r   r   rK   )r   r(   daydayss       r   rN   DailyLogFileTests.test_rotation  sq    !DHH5		"GLQxPxS3::cEk#::xP 
		%
		)
		(tAw/0Q01
		"tAw/0tAw/0Q01
		#Q01% Qs   	-Hc                   / SQn[        U R                  U R                  5      nU R                  UR                  5        U H  nUR                  U5        M     UR                  5         UR                  S5      nU R                  UR                  5        U R                  XR                  5       5        U R                  [        UR                  S5        SUl        UR	                  5         UR                  5         UR                  S5      nU R                  UR                  5        U R                  XR                  5       5        g)z8
Test retrieving log files with L{DailyLogFile.getLog}.
)z1
z2
z3
r   r   iQ r   N)r   r   r   r"   r#   r3   r4   r_   r6   r^   r$   r`   r   rL   )r   datar(   drs        r   test_getLogDailyLogFileTests.test_getLog  s     % DHH5		"AIIaL 		 JJsO {{}- 	*cjj%8
		

JJqM {{}-r   c                   [        U R                  U R                  5      nU R                  UR                  5        UR
                   SUR                  UR                  5       3n[        US5       nUR                  S5        SSS5        UR                  nUR                  5         U R                  XAR                  5        g! , (       d  f       NF= f)z]
L{DailyLogFile.rotate} doesn't do anything if they new log file already
exists on the disk.
r   rf   r,   N)r   r   r   r"   r#   r   r   r   r5   r3   rT   rL   r6   )r   r(   newFilePathr   previousFiles        r   test_rotateAlreadyExists*DailyLogFileTests.test_rotateAlreadyExists  s    
 !DHH5		" 
!CJJs||$<#=>+s#rHHUO $yy

yy1	 $#s   2C
CzVMaking read-only directories on Windows is too complex for this test to reasonably do.c                   [         R                  " U R                  U R                  5      nU R	                  UR
                  5        [        R                  " UR                  S5        U R	                  [        R                  UR                  S5        UR                  nUR                  5         U R                  X!R                  5        g)zk
L{DailyLogFile.rotate} doesn't do anything if the directory containing
the log files can't be written to.
r   i  N)r   r   r   r   r"   r#   r   r   r   rT   rL   r6   r   r(   r   s      r   #test_rotatePermissionDirectoryNotOk5DailyLogFileTests.test_rotatePermissionDirectoryNotOk  s|     ""499dhh7		"
&#--7yy

yy1r   c                @   [         R                  " U R                  U R                  5      nU R	                  UR
                  5        [        R                  " UR                  S5        UR                  nUR                  5         U R                  X!R                  5        g)zQ
L{DailyLogFile.rotate} doesn't do anything if the log file can't be
written to.
r   N)r   r   r   r   r"   r#   r   r   r   rT   rL   r6   r   s      r   test_rotatePermissionFileNotOk0DailyLogFileTests.test_rotatePermissionFileNotOk  sd    
 ""499dhh7		"
5!yy

yy1r   c                    [         R                  " U R                  U R                  5      nU R	                  UR
                  5        [        R                  " S5      nU R                  SUR                  U5      5        g)zf
Test that L{DailyLogFile.toDate} converts its timestamp argument to a
time tuple (year, month, day).
)	  rG   rG   r   r   r   r   r   r   )r   rG   rG   N)
r   r   r   r   r"   r#   r   mktimer6   r   )r   r(   	timestamps      r   test_toDateDailyLogFileTests.test_toDate  sV    
 ""499dhh7		"KK >?	szz)'<=r   c                  ^  SU 4S jjn[         R                  " T R                  T R                  5      nT R	                  UR
                  5        T R                  [        SU5        UR                  5       nT R                  / SQU5        g)a  
Test that L{DailyLogFile.toDate} returns today's date by default.

By mocking L{time.localtime}, we ensure that L{DailyLogFile.toDate}
returns the first 3 values of L{time.localtime} which is the current
date.

Note that we don't compare the *real* result of L{DailyLogFile.toDate}
to the *real* current date, as there's a slight possibility that the
date changes between the 2 function calls.
c                 R   > TR                  SU 5        [        [        SS5      5      $ )Nr   r   	   )r6   listr   )r   r   s    r   mock_localtimeADailyLogFileTests.test_toDateDefaultToday.<locals>.mock_localtime  s$    R&a$$r   	localtime)r   rG   rH   N)r   objectr   z	list[int])
r   r   r   r   r"   r#   patchr   r   r6   )r   r   r(   logDates   `   r   test_toDateDefaultToday)DailyLogFileTests.test_toDateDefaultToday  s_    	% ""499dhh7		"

4n5**,G,r   c                
   [         R                  " U R                  U R                  5      nU R	                  UR
                  5        Sn[        R                  " US-   5      nUR                  U5      nU R                  X$5        g)zK
Test that L{DailyLogFile.toDate} uses its arguments to create a new
date.
)i  r=      )r   r   r   r   r   r   N)
r   r   r   r   r"   r#   r   r   r   r6   )r   r(   datesecondsr  s        r   #test_toDateUsesArgumentsToMakeADate5DailyLogFileTests.test_toDateUsesArgumentsToMakeADate
  sa    
 ""499dhh7		"++dXo.**W%'r   r   Nr   )r   r   r   r   r   r   r:   rN   r   r   r   r   r   r   r   r   r   r  r  r   r   r   r   r   r   h  sd    6524.62" ""$	!
2
22	>-0(r   r   )
__future__r   r0   r   r   rl   r   unittestr   twisted.pythonr   r   twisted.trial.unittestr   r	   r   r   r   r   r   r   <module>r     sQ    #   	    + +G18 G1T
,-- ,n( n(r   