1jwdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z ddl m Z dZ dZGddejZd Zed krejdSdS) zETest that FakeFilesystem calls work identically to a real filesystem.N)fake_filesystemfake_os fake_open)skip_if_symlink_not_supportedcpt|tr |dtjS|S)zBConverts slashes in the path to the architecture's path separator./) isinstancestrreplaceosseppaths }/builddir/build/BUILD/cloudlinux-venv-1.0.11/venv/lib/python3.11/site-packages/pyfakefs/tests/fake_filesystem_vs_real_test.pyr r s/$)||C((( Kc<| |jS#t$rYdSwxYwdSN)errnoAttributeError) raised_errors r _get_errnor$s@ % %    DD  s  ceZdZejdZerdndZdZd7dZ dZ dZ d8d Z e d Ze d Z d8d Z d9dZ d8dZ d8dZd7dZdZdZe dZdZd9dZd9dZdZdZdZdZdZdZ dZ!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.Z0d/Z1d0Z2d1Z3d2Z4d3Z5d4Z6d5Z7d6Z8dS):FakeFilesystemVsRealTestwinz C:\fakefsz/fakefsc|sdStj|j|tj|j|fS)z@For a given path, return paths in the real and fake filesystems.)NN)r rjoin real_base fake_baseselfrs r_pathszFakeFilesystemVsRealTest._paths0sC : GLL . . GLL . .  rNct|}|j|||g||\}}|dkr.t j||j||dkrt|dd}||pd| | |dd}||pd| |dkr|t|d}||pd| | |d}||pd| |d vrt|}||}}| tjr ||d d \}}|d kr2t j |||j ||d S|d kr4t j |||j ||d Sd Sd S)z=Create a dir, file, or link in both the real fs and the fake.dfwutf8encodingbwb)lhNr,r-)r _created_filesappendr!r mkdirropenwritecloser startswithsymlinklink) r file_typercontents real_path fake_pathfh real_target fake_targets r_create_test_filez*FakeFilesystemVsRealTest._create_test_file9s?4yy ""ItX#>???#{{400 9    HY    L  y ) ) )   iv666B HHX^ $ $ $ HHJJJ 3@@B HHX^ $ $ $ HHJJJ   i&&B HHX^ $ $ $ HHJJJ 400B HHX^ $ $ $ HHJJJ  " "8}}H(0(K""26** E+/;;x|+D+D( [C ; 222 $$[)<<<<<c!! Y/// !!+y99999 # ""!rcdtjz}tj|_tjtjtj||_ tj tjtj |j rtj |j tj|j |tj|j tjt%j|_|j|jt-j|j|_t1j|j|_g|_tj |j |j |jdS)Nz fakefs.%s)timer getcwdcwdrrealpathrtempfile gettempdirrchdirisdirshutilrmtreer1 assertTrueaccessW_OKrFakeFilesystem create_dirrr FakeOsModuler FakeFileOpenr/)r tsnames rsetUpzFakeFilesystemVsRealTest.setUp_sVty{{*9;;))"',,x7J7L7Lf*U*UVV $&&''' 7== ( ( * M$. ) ) )      $."'::;;;.=?? ''777+D,@AA "/0DEE      4>*****rc Ptjtj |jdd}||D]}||d\}}|ddkr} tj|nM#t$r@}d|vr1| d|d|dtj |nYd}~nd}~wwxYw|j ||ddks |dd kr.tj ||j | tj|jtj|jdS#tj|jtj|jwxYw) Nr.rr#zDirectory not emptyz Real path z not empty: z : r$r,)r rGrErFr/reverser!rmdirOSErrorfaillistdirrremoverIrJrrC)r rev_filesinfor:r;es rtearDownz!FakeFilesystemVsRealTest.tearDownxs $&&''' +AAA.I      ! 3 3'+{{47';';$ 97c>> "++++""""0A55 III#,99aaaI1F1F1F!H " "L&&y1117c>>T!W^^Ii(((L'' 222! 3$ M$. ) ) ) HTX      M$. ) ) ) HTX    s7AE19B E1 C6CE1CA$E114F%FcHd}||||\}}||||\} } |} | |dkrdndz } |||| krm| d| ||| S| d| ||| Sd| |||| St|} t| } | | kr"d ||||| | S|rp|rn| rl||jrR| |jr8|t|jd}| t|jd} || krd | || SdS) aInvoke an os method in both real and fake contexts and compare results. Invoke a real filesystem method with a path to a real file and invoke a fake filesystem method with a path to a fake file and compare the results. We expect some calls to throw Exceptions, so we catch those and compare them. Args: method_name: Name of method being tested, for use in error messages. path: potential path to a file in the real and fake file systems, passing an empty tuple indicates that no arguments to pass to method. real: built-in system library or method from the built-in system library which takes a path as an arg and returns some value. fake: fake_filesystem object or method from a fake_filesystem class which takes a path as an arg and returns some value. method_returns_path: True if the method returns a path, and thus we must compensate for expected difference between real and fake. Returns: A description of the difference in behavior, or None. cp|r3t|dr|jjd|jdS|jjSdS)Nr()None)hasattr __class____name__r)excs r _error_classzAFakeFilesystemVsRealTest._compare_behaviors.._error_classsK .3((D!m4CCsyCCCC}--6rz()z({path})Nz,{}: real version returned {}, fake raised {}z,{}: real version raised {}, fake returned {}z*{}: real version raised {}, fake raised {}z4{}({}): both raised {}, real errno {}, fake errno {}z${}: real return {}, fake returned {})_get_real_value_get_fake_valueformatrr5rrlen)r method_namerrealfakemethod_returns_pathrhreal_err real_valuefake_err fake_value method_call real_errno fake_errnos r_compare_behaviorsz+FakeFilesystemVsRealTest._compare_behaviorss5:    $33KtLL*#33KtLL*$& trzzttz9  < ! !\\(%;%; ; ;ELL L** ELL L** @FF X&& X&&   )) ))  # #IPP X&&    ? ? ?))$.99 ?))$.99  ? (DN(;(;(=(=> 'DN(;(;(=(=>  # #9@@  trcd}d} |}t|st||}|dkrgn|g}||}t|tr|}nt |}n#t $r }|}Yd}~nd}~wwxYw||fSNricallablegetattrr bytesdecoder Exception) rnrrprurt fake_methodargsresultr]s rrkz(FakeFilesystemVsRealTest._get_fake_values  KD>> 9%dK88 22$D [$'F&%(( )#]]__  [[    HHHHHH ##A*A11 B;BBcd}d} |dkrgn|g}|}t|st||}||}t|tr|}nt |}n#t $r }|}Yd}~nd}~wwxYw||fSr{r|) rnrrorsrrr real_methodrr]s rrjz(FakeFilesystemVsRealTest._get_real_values  22$DKD>> 9%dK88  [$'F&%(( )#]]__  [[    HHHHHH ##rcft|}|||t|j|S)aInvoke an os method in both real and fake contexts and compare. For a given method name (from the os module) and a path, compare the behavior of the system provided module against the fake_filesystem module. We expect results and/or Exceptions raised to be identical. Args: method_name: Name of method being tested. path: potential path to a file in the real and fake file systems. method_returns_path: True if the method returns a path, and thus we must compensate for expected difference between real and fake. Returns: A description of the difference in behavior, or None. )r ryr rr rnrrqs rassertOsMethodBehaviorMatchesz6FakeFilesystemVsRealTest.assertOsMethodBehaviorMatchess5&4yy&& r4<1D   rTc i}d|vrd|d<t||fi|5}|j||fi|5}||||||cdddcdddS#1swxYwY ddddS#1swxYwYdS)aInvoke an open method in both real and fake contexts and compare. Args: method_name: Name of method being tested. path: potential path to a file in the real and fake file systems. mode: how to open the file. data: any data to pass to the method. method_returns_data: True if a method returns some sort of data. For a given method name (from builtin open) and a path, compare the behavior of the system provided module against the fake_filesystem module. We expect results and/or Exceptions raised to be identical. Returns: A description of the difference in behavior, or None. r*r&r(N)r2rry) r rnrmodedatamethod_returns_datakwargsreal_fhfake_fhs rdiff_open_method_behaviorz2FakeFilesystemVsRealTest.diff_open_method_behavior+sO( d??!'F:  $ ' ' ' ' 7d55f55 ..w9L                                          s4A5A A5A A5#A $A55A9<A9c\|||tj|jj|S)aInvoke an os.path method in both real and fake contexts and compare. For a given method name (from the os.path module) and a path, compare the behavior of the system provided module against the fake_filesystem module. We expect results and/or Exceptions raised to be identical. Args: method_name: Name of method being tested. path: potential path to a file in the real and fake file systems. method_returns_path: True if the method returns a path, and thus we must compensate for expected difference between real and fake. Returns: A description of the difference in behavior, or None. )ryr rrrs rdiff_os_path_method_behaviorz5FakeFilesystemVsRealTest.diff_os_path_method_behaviorHs/&&& rw (9;N   rct|}||||}|r||dSdS)aGAssert that an os.path behaves the same in both real and fake contexts. Wraps DiffOsPathMethodBehavior, raising AssertionError if any differences are reported. Args: method_name: Name of method being tested. path: potential path to a file in the real and fake file systems. method_returns_path: True if the method returns a path, and thus we must compensate for expected difference between real and fake. Raises: AssertionError if there is any difference in behavior. N)r rrX)r rnrrqdiffs r!assertOsPathMethodBehaviorMatchesz:FakeFilesystemVsRealTest.assertOsPathMethodBehaviorMatches_sL$4yy00dDWXX   IIdOOOOO  rcpgt|}dg}dg}hdtz }djjgdfdfdgdfd fd gd jjgg}fd |D}g}|D]/}||} | r|| 0|D]1}|d d} | r|| 2|D]/}||} | r|| 0|D]C} | d|| d| d} | r|| D|r0 d|dd |dSdS)NreadlinkrB>isabsrHexistsisfileislinklexistsrLz stat.sizec2|ddSNst_sizeF_stat_result_realpr s rzDFakeFilesystemVsRealTest.assertAllOsBehaviorsMatch..$00IuEErc2|ddSr_stat_result_fakers rrzDFakeFilesystemVsRealTest.assertAllOsBehaviorsMatch..rr stat.modec0|dSNst_moderrs rrzDFakeFilesystemVsRealTest.assertAllOsBehaviorsMatch..$00I>>rc0|dSrrrs rrzDFakeFilesystemVsRealTest.assertAllOsBehaviorsMatch..rrz lstat.sizec(g|]}|dv |S)rri).0mexcludess r zFFakeFilesystemVsRealTest.assertAllOsBehaviorsMatch..s'NNN1X9M9M19M9M9MrriT)rqrr.Behaviors do not match for :  ) r set _access_real _access_fake_lstat_size_real_lstat_size_fakerr0rryrXr) r rros_method_namesos_method_names_no_argsos_path_method_nameswrapped_methods differencesrnrrs ` ` rassertAllOsBehaviorsMatchz2FakeFilesystemVsRealTest.assertAllOsBehaviorsMatchvsJ>r4yy%,#+*    MM t($*; <EEEEEEEE  >>>>>>>>  40$2G H  ONNNoNNN * ) )K55k4HHD )""4(((2 ) )K55RT6D )""4(((/ ) )K44[$GGD )""4(((  ) )A**1Q4qtQqTBBD )""4(((   III44{3335       rczt|}ddg}ddg}gd}g}|D]1}|||||} | r|| 2||zD]1}||||d} | r|| 2|r0|d|dd |dSdS) Nr3 writelinesread readlines)truncateflushr4rirrr)r rr0rXr) r rrrwrite_method_namesread_method_namesother_method_namesrrnrs rassertFileHandleBehaviorsMatchz7FakeFilesystemVsRealTest.assertFileHandleBehaviorsMatchs4yy%|4#[1;;; - ) )K11+tT4PPD )""4(((,/AA ) )K11+tT2NND )""4(((   III44{3335       rc2d}d} t|i|5 dddn #1swxYwYn#t$r }|}Yd}~nd}~wwxYw |j|i|5 dddn #1swxYwYn#t$r }|}Yd}~nd}~wwxYw||k}|r0|r.t|t|uo|j|jk}|sWd||}dt |z}dt |z} |||z| zdSdS)awCompare open() function invocation between real and fake. Runs open(*args, **kwargs) on both real and fake. Args: *args: args to pass through to open() **kwargs: kwargs to pass through to open(). Returns: None. Raises: AssertionError if underlying open() behavior differs from fake. Nz8Behaviors don't match on open with args {} & kwargs {}. zReal open results in: %s zFake open results in: %s )r2rrtyperrlreprrX) r rrrrrtr]is_exception_equalmsg real_err_msg fake_err_msgs r"assertFileHandleOpenBehaviorsMatchz;FakeFilesystemVsRealTest.assertFileHandleOpenBehaviorsMatchs t&v&&                    HHHHHH  000                    HHHHHH &1   X$x..0SX]hm5S " 9MTTC8$x..HL7$x..HL IIcL(<7 8 8 8 8 8 9 9sf ,  ,$,$, A=A A-A! A-!A%%A-(A%)A-- B7A>>Bc@tj|tjSr)r rLR_OKrs rrz%FakeFilesystemVsRealTest._access_realsyrw'''rcL|j|tjSr)rrLr rrs rrz%FakeFilesystemVsRealTest._access_fakes|""4111rc||\}}|s!tj|rdSt tj||Sr)r!r rrHr~stat)r rprop support_dirr:unused_fake_paths rrz*FakeFilesystemVsRealTest._stat_result_realsU&*kk$&7&7# # rw}}Y77 4rwy))4000rc||\}}|s!|jj|rdSt |j||Sr)r!rrrHr~r)r rrr_r;s rrz*FakeFilesystemVsRealTest._stat_result_fakes^{{4(( 9 t|066yAA 4t|((33T:::rcx||\}}tj|rdStj|j}tj|rHtj|tj r|t|j z}|Sr) r!r rrHlstatrrrr5r rmr)r rr:rsizes rrz)FakeFilesystemVsRealTest._lstat_size_reals&*kk$&7&7# # 7== # # 4x ""* 7>>) $ $ ,{9%%0088 ,DN+++ rc||\}}|jj|rdS|j|j}|jj|rN|j|tj r|t|j z}|Sr) r!rrrHrrrrr5r r rmr)r runused_real_pathr;rs rrz)FakeFilesystemVsRealTest._lstat_size_fakes&*kk$&7&7#) <  " "9 - - 4|!!),,4 <  # #I . . ,|$$Y//::26BB ,DN+++ rc|dd|dd|dd|dd|dddS)Nrr)rz/aarr s r test_isabsz#FakeFilesystemVsRealTest.test_isabssv ..w=== ..w;;; ..w<<< ..w=== ..w<<<<>> &&w/////rct|dd|ddd|ddS)Nr$rr, link_to_emptyrr?rrs rtest_sym_link_to_empty_filez4FakeFilesystemVsRealTest.test_sym_link_to_empty_file;V%''' sG,,, sOW=== &&77777rct|dd|ddd|ddS)Nr$rr-rrrs rtest_hard_link_to_empty_filez5FakeFilesystemVsRealTest.test_hard_link_to_empty_fileArrct|ddd|ddd|ddS)Nr$rrr, link_to_filerrs rtest_sym_link_to_real_filez3FakeFilesystemVsRealTest.test_sym_link_to_real_fileGX%''' sG_=== sNG<<< &&~66666rct|ddd|ddd|ddS)Nr$rrr-rrrs rtest_hard_link_to_real_filez4FakeFilesystemVsRealTest.test_hard_link_to_real_fileMr rczt|ddd|ddS)Nr, broken_linkbrokenrrs rtest_broken_sym_linkz-FakeFilesystemVsRealTest.test_broken_sym_linkSs@%''' sM8<<< &&}55555rc|dd|dd|ddd|ddS)Nr#ra/br$a/b/filer9rrs rtest_file_in_a_folderz.FakeFilesystemVsRealTest.test_file_in_a_folderXs` sC((( sE*** sJ ;;; &&z22222rct|dd|dd|ddd|ddd |d dS) Nr#rrr$rr9r,a/link/a/b a/link/filerrs r test_absolute_sym_link_to_folderz9FakeFilesystemVsRealTest.test_absolute_sym_link_to_folder^s%''' sC((( sE*** sJ ;;; sHf555 &&}55555rct|dd|dd|ddd|ddd |d\}}tj||j||d dS) Nr#rrr$rr9r,rrfile)rr?r!r rGrr)r real_dirfake_dirs rtest_link_to_folder_after_chdirz8FakeFilesystemVsRealTest.test_link_to_folder_after_chdirfs%''' sC((( sE*** sJ ;;; sHf555![[//(  8$$$ &&v.....rct|dd|dd|ddd|ddd |d dS) Nr#rrr$rr9r,rr*rrrs r test_relative_sym_link_to_folderz9FakeFilesystemVsRealTest.test_relative_sym_link_to_folderrs%''' sC((( sE*** sJ ;;; sHc222 &&}55555rcttjdkrZ|dd|dd|ddd|ddSdS)Ndarwinr#rrr,a/b/c..)rsysplatformr?rrs rtest_sym_link_to_parentz0FakeFilesystemVsRealTest.test_sym_link_to_parentzs%''' <8 # #  " "3 , , ,  " "3 . . .  " "3 6 6 6  * *7 3 3 3 3 3 $ #rct|dd|ddd|dd|ddd |d dS) Nr#rr$a/targetr9rr,r"r# a/b/c/targetrrs r$test_path_through_sym_link_to_parentz=FakeFilesystemVsRealTest.test_path_through_sym_link_to_parents%''' sC((( sJ ;;; sE*** sGT222 &&~66666rc,t|dd|dd|dd|ddd|dd d |d dS) Nr#rra/sibling_of_br$a/sibling_of_b/targetr9r,r"../sibling_of_br)rrs r"test_sym_link_to_sibling_directoryz;FakeFilesystemVsRealTest.test_sym_link_to_sibling_directorys%''' sC((( sE*** s$4555 s$;ZHHH sG->??? &&~66666rc,t|dd|dd|dd|ddd|dd d |d dS) Nr#rrr,r$r-r9r,r"r.za/b/c/file_does_not_existrrs r4test_sym_link_to_sibling_directory_non_existent_filezMFakeFilesystemVsRealTest.test_sym_link_to_sibling_directory_non_existent_files%''' sC((( sE*** s$4555 s$;ZHHH sG->??? &&'BCCCCCrc,t|dd|dd|dd|ddd|dd d |d dS) Nr#rrr,r$r-r9r,r"z../broken_sibling_of_br)rrs r)test_broken_sym_link_to_sibling_directoryzBFakeFilesystemVsRealTest.test_broken_sym_link_to_sibling_directorys%''' sC((( sE*** s$4555 s$;ZHHH sG-EFFF &&~66666rc|dd|dd|dd|ddd|ddS) Nr#rrr,r$r-r9za/b/../sibling_of_b/targetrrs rtest_relative_pathz+FakeFilesystemVsRealTest.test_relative_pathsy sC((( sE*** s$4555 s$;ZHHH &&'CDDDDDrc|dd|dd|dd|ddd|ddS) Nr#rrr,r$r-r9za/b/../broken/targetrrs rtest_broken_relative_pathz2FakeFilesystemVsRealTest.test_broken_relative_pathsy sC((( sE*** s$4555 s$;ZHHH &&'=>>>>>rc|dd|ddd|dd|dd|ddd|d dS) Nr#rr$r(r9rr,r-za/b/../broken/../targetrrs rtest_bad_relative_pathz/FakeFilesystemVsRealTest.test_bad_relative_paths sC((( sJ ;;; sE*** s$4555 s$;ZHHH &&'@AAAAArc2|dddS)Ngetmtimez no/such/pathrrs rtest_getmtime_nonexistent_pathz7FakeFilesystemVsRealTest.test_getmtime_nonexistent_paths ..z>JJJJJrc|ddd|ddd|ddd|ddd|ddd|dd d|dd d|dd d|d d d|d d d|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|dd ddS)Nr$rrr3r0rzother contentsr%rreadplus writepluszr+zw+r* binaryreadr binarywrite binaryappendrbsother contentsr+abencr')rr(zutf-8)r?rrrs rtest_builtin_open_modesz0FakeFilesystemVsRealTest.test_builtin_open_modess sFO<<< sG_=== sHo>>> ++FC9IJJJ ++GS:JKKK ++Hc;KLLL sJ@@@ sKAAA ++J>NOOO ++K?OPPP sL2BCCC sM3CDDD sN4DEEE ++L$@QRRR ++M4ARSSS ++NDBSTTT ++FD:JKKK ++GT;KLLL ++Hd55555rrc,tjdSr)unittestmain)rs rrSrSs MOOOOOr__main__)__doc__r rIr$rErArRpyfakefsrrrpyfakefs.tests.test_utilsrr rTestCaserrSrfrirrrYsLK  8888888888CCCCCCE 5E 5E 5E 5E 5x0E 5E 5E 5P zHMOOOOOr