a 5źdg°?ć@s”dZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z Gdd„dej ƒZGdd„dej ƒZed kre ”dS) z#Test harness for the zipapp module.éN)Ś requires_zlib)Śpatchc@s eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z e ƒdd„ƒZ dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Ze ej d2kd3”d4d5„ƒZ!e ej d2kd3”d6d7„ƒZ"d8S)9Ś ZipAppTestz!Test zipapp module functionality.cCs&t ”}| |j”t |j”|_dS©N©ŚtempfileŚTemporaryDirectoryZ addCleanupŚcleanupŚpathlibŚPathŚnameŚtmpdir©Śselfr ©rś,/usr/local/lib/python3.9/test/test_zipapp.pyŚsetUps zZipAppTest.setUpcCsN|jd}| ”|d ”|jd}t t|ƒt|ƒ”| | ””dS©NŚsourceś __main__.pyś source.pyz©r ŚmkdirŚtouchŚzipappŚcreate_archiveŚstrŚ assertTrueŚis_file©rrŚtargetrrrŚtest_create_archives    zZipAppTest.test_create_archivecCsF|jd}| ”|d ”|jd}t ||”| | ””dSr)r rrrrrrrrrrŚ test_create_archive_with_pathlib!s     z+ZipAppTest.test_create_archive_with_pathlibcCsø|jd}| ”|d ”|d ”|d ”|dd ”t ”}t t|ƒ|”| d”t   |d”0}|  d|  ””|  d |  ””Wdƒn1sŖ0YdS) NrrŚfooZbarz __init__.pyrŚrzfoo/zbar/) r rrŚioŚBytesIOrrrŚseekŚzipfileŚZipFileŚassertInŚnamelist©rrr ŚzrrrŚ test_create_archive_with_subdirs*s     z+ZipAppTest.test_create_archive_with_subdirscCsødd„}|jd}| ”|d ”|d ”|d ”|jd}tj|||dt |d ”@}| d| ””| d| ””|  d| ””Wdƒn1sŖ0YdS) NcSs |jdkS)Nz.pyc)Śsuffix©ŚpathrrrŚskip_pyc_files<szBZipAppTest.test_create_archive_with_filter..skip_pyc_filesrrśtest.pyztest.pycr©Śfilterr$) r rrrrr(r)r*r+Z assertNotIn)rr2rr r-rrrŚtest_create_archive_with_filter9s     z*ZipAppTest.test_create_archive_with_filtercCsĢdd„}|jd}| ”|d ”|d ”|d ”|dd ”|jd}tj|||d t |d ”D}| t|  ”ƒd ”|  d|  ””|  d|  ””Wdƒn1s¾0YdS) NcSs|jddkS)NrŚdummy)Śpartsr0rrrŚskip_dummy_dirOszIZipAppTest.test_create_archive_filter_exclude_dir..skip_dummy_dirrrr3r7ztest2.pyrr4r$é) r rrrrr(r)Ś assertEqualŚlenr+r*)rr9rr r-rrrŚ&test_create_archive_filter_exclude_dirKs     z1ZipAppTest.test_create_archive_filter_exclude_dircCsH|jd}| ”|d ”t t|ƒ”|jd}| | ””dSrr)rrZexpected_targetrrrŚ"test_create_archive_default_target_s    z-ZipAppTest.test_create_archive_default_targetcCs”|jd}| ”|d ”|d ”|jd}tj||ddt |d”0}dD]}| | |”j tj ”qVWdƒn1s†0YdS) Nrrr3rT)Ś compressedr$)rr3) r rrrrr(r)r;ZgetinfoZ compress_typeŚ ZIP_DEFLATED)rrr r-r rrrŚ$test_create_archive_with_compressionhs    ’z/ZipAppTest.test_create_archive_with_compressioncCsl|jd}| ”|d ”|jd}| tj”$t t|ƒt|ƒ”Wdƒn1s^0YdS)Nrśfoo.pyr©r rrŚ assertRaisesrŚ ZipAppErrorrrrrrrŚ test_no_mainws    zZipAppTest.test_no_maincCsp|jd}| ”|d ”|jd}| tj”(tjt|ƒt|ƒddWdƒn1sb0YdS)Nrrrś pkg.mod:fn©ŚmainrCrrrrŚtest_main_and_main_py€s    z ZipAppTest.test_main_and_main_pycCs–|jd}| ”|d ”|jd}tjt|ƒt|ƒddt t|ƒd”2}| d|  ””| d|  d””Wdƒn1sˆ0YdS) NrrBrrGrHr$rs pkg.mod.fn()) r rrrrrr(r)r*r+Śreadr,rrrŚtest_main_written‰s   zZipAppTest.test_main_writtencCs–|jd}| ”|d ”|d ”|jd}tjt|ƒt|ƒddt t|ƒd”&}| d|  ”  d ””Wdƒn1sˆ0YdS) NrrBzbar.pyrrGrHr$ér) r rrrrrr(r)r;r+Ścountr,rrrŚtest_main_only_written_once”s    z&ZipAppTest.test_main_only_written_oncec Cs¢|jd}| ”|jd}gd¢}|D]t}|j|dT| tj”(tjt|ƒt|ƒ|dWdƒn1st0YWdƒq(1s’0Yq(dS)Nrr) Śr#zfoo:z:barz12:barza.b.c.:dz.a:bza:b.za:.bz a:silly namerH)r rZsubTestrDrrErr)rrr ZproblemsrIrrrŚtest_main_validation„s  zZipAppTest.test_main_validationcCs||jd}| ”|d ”|jd}t t|ƒt|ƒ”| d”"}| | d”d”Wdƒn1sn0YdS)NrrrŚrbr:ó#!) r rrrrrŚopenZassertNotEqualrK©rrr ŚfrrrŚtest_default_no_shebang³s    z"ZipAppTest.test_default_no_shebangcCs|jd}| ”|d ”|jd}tjt|ƒt|ƒdd| d”2}| | d”d”| d |  ””Wdƒn1s‚0YdS) NrrrŚpython©Z interpreterrRr:rSspython ) r rrrrrrTr;rKŚreadlinerUrrrŚtest_custom_interpreter½s    z"ZipAppTest.test_custom_interpretercCsR|jd}| ”|d ”t ”}tjt|ƒ|dd| |  ”  d””dS)NrrrXrYs #!python ) r rrr%r&rrrrŚgetvalueŚ startswithrrrrŚtest_pack_to_fileobjÉs   zZipAppTest.test_pack_to_fileobjcCsZ|jd}| ”|d ”|jd}tjt|ƒt|ƒdd| t t|ƒ”d”dS©NrrrrXrY©r rrrrrr;Śget_interpreterrrrrŚtest_read_shebangŅs    zZipAppTest.test_read_shebangcCsV|jd}| ”|d ”|jd}t t|ƒt|ƒ”| t t|ƒ”d”dSrr`rrrrŚtest_read_missing_shebangŪs    z$ZipAppTest.test_read_missing_shebangcCs||jd}| ”|d ”|jd}tjt|ƒt|ƒdd|jd}tjt|ƒt|ƒdd| t t|ƒ”d”dS)NrrrrXrYś changed.pyzś python2.7r`©rrr Ś new_targetrrrŚtest_modify_shebangäs    zZipAppTest.test_modify_shebangcCst|jd}| ”|d ”|jd}tjt|ƒt|ƒddt ”}tjt|ƒ|dd| |  ”  d””dS)NrrrrXrYreó #!python2.7 ) r rrrrrr%r&rr\r]rfrrrŚtest_write_shebang_to_fileobjļs   z(ZipAppTest.test_write_shebang_to_fileobjcCsh|jd}| ”|d ”|jd}|jd}tj||ddtj||dd| t |”d”dS)Nrrz target1.pyzz target2.pyzrXrYre)r rrrrr;ra)rrZtarget1Ztarget2rrrŚtest_read_from_pathobjūs    z!ZipAppTest.test_read_from_pathobjcCs~|jd}| ”|d ”|jd}t ”}tjt|ƒ|ddt ”}| d”tj||dd|  |  ”  d””dS) NrrrrXrYrreri) r rrr%r&rrrr'rr\r])rrr Z temp_archivergrrrŚtest_read_from_fileobjs    z!ZipAppTest.test_read_from_fileobjcCs||jd}| ”|d ”|jd}tjt|ƒt|ƒdd|jd}tjt|ƒt|ƒdd| t t|ƒ”d”dS)NrrrrXrYrdr`rfrrrŚtest_remove_shebangs    zZipAppTest.test_remove_shebangcCs¬|jd}| ”|d ”t ”}tjt|ƒ|ddt ”}| d”tj||dd| d”t   |d”&}|  t |  ”ƒdh”Wdƒn1sž0YdS)NrrrXrYrr$)r rrr%r&rrrr'r(r)r;Śsetr+)rrr rgr-rrrŚtest_content_of_copied_archives    z)ZipAppTest.test_content_of_copied_archiveŚwin32z*Windows does not support an executable bitcCsZ|jd}| ”|d ”|jd}tjt|ƒt|ƒdd| | ”jtj @”dSr_) r rrrrrrŚstatŚst_modeŚS_IEXECrrrrŚtest_shebang_is_executable.s    z%ZipAppTest.test_shebang_is_executablecCsZ|jd}| ”|d ”|jd}tjt|ƒt|ƒdd| | ”jtj @”dS)NrrrrY) r rrrrrZ assertFalserqrrrsrrrrŚ!test_no_shebang_is_not_executable9s    z,ZipAppTest.test_no_shebang_is_not_executableN)#Ś__name__Ś __module__Ś __qualname__Ś__doc__rr!r"r.r6r=r>rrArFrJrLrOrQrWr[r^rbrcrhrjrkrlrmroŚunittestZskipIfŚsysŚplatformrtrurrrrrsD                  ’ ’rc@s^eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z e de j ddd„ƒZ dd„ZdS)ŚZipAppCmdlineTestz$Test zipapp module command line API.cCs&t ”}| |j”t |j”|_dSrrrrrrrIs zZipAppCmdlineTest.setUpcCs8|jd}| ”|d ”|jd}t ||”|Sr)r rrrrrrrrŚ make_archiveNs     zZipAppCmdlineTest.make_archivecCsN|jd}| ”|d ”t|ƒg}t |”| d”}| | ””dS)Nrrz.pyz) r rrrrrIŚ with_suffixrr)rrŚargsr rrrŚtest_cmdline_createWs     z%ZipAppCmdlineTest.test_cmdline_createcCs@| ”}|jd}t|ƒdt|ƒg}t |”| | ””dS©Nś target.pyzś-o)r~r rrrIrr)rŚoriginalr r€rrrŚtest_cmdline_copyas   z#ZipAppCmdlineTest.test_cmdline_copycCsj| ”}|jd}t|ƒdt|ƒg}| t”}t |”Wdƒn1sN0Y| |jj ”dSr‚© r~r rrDŚ SystemExitrrIrŚ exceptionŚcode©rr…r r€ŚcmrrrŚtest_cmdline_copy_inplaceis   (z+ZipAppCmdlineTest.test_cmdline_copy_inplacecCsn| ”}|jd}t|ƒdt|ƒddg}| t”}t |”Wdƒn1sR0Y| |jj ”dS)Nrƒr„z-mzfoo:barr‡r‹rrrŚtest_cmdline_copy_change_mainss   (z/ZipAppCmdlineTest.test_cmdline_copy_change_mainz sys.stdout)Z new_callablecCsl| ”}t|ƒdg}| t”}t |”Wdƒn1s>0Y| |jjd”| |  ”d”dS)Nś--inforzInterpreter: ) r~rrDrˆrrIr;r‰rŠr\)rZ mock_stdoutr r€rŒrrrŚtest_info_command}s   (z#ZipAppCmdlineTest.test_info_commandcCs\|jd}t|ƒdg}| t”}t |”Wdƒn1s@0Y| |jj”dS)Nz dummy.pyzr) r rrDrˆrrIrr‰rŠ)rr r€rŒrrrŚtest_info_errorˆs    (z!ZipAppCmdlineTest.test_info_errorN)rvrwrxryrr~rr†rrŽrr%ŚStringIOrr‘rrrrr}Es     r}Ś__main__)ryr%r rqr{rrzrr(Z test.supportrZ unittest.mockrZTestCaserr}rvrIrrrrŚs   8M