77import unittest
88import tarfile
99
10- from test import support
10+ from test import support , script_helper
1111
1212# Check for our compression modules.
1313try :
@@ -27,11 +27,13 @@ def md5sum(data):
2727 return md5 (data ).hexdigest ()
2828
2929TEMPDIR = os .path .abspath (support .TESTFN ) + "-tardir"
30+ tarextdir = TEMPDIR + '-extract-test'
3031tarname = support .findfile ("testtar.tar" )
3132gzipname = os .path .join (TEMPDIR , "testtar.tar.gz" )
3233bz2name = os .path .join (TEMPDIR , "testtar.tar.bz2" )
3334xzname = os .path .join (TEMPDIR , "testtar.tar.xz" )
3435tmpname = os .path .join (TEMPDIR , "tmp.tar" )
36+ dotlessname = os .path .join (TEMPDIR , "testtar" )
3537
3638md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
3739md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
@@ -1724,6 +1726,168 @@ def test_number_field_limits(self):
17241726 tarfile .itn (0x10000000000 , 6 , tarfile .GNU_FORMAT )
17251727
17261728
1729+ class CommandLineTest (unittest .TestCase ):
1730+
1731+ def tarfilecmd (self , * args ):
1732+ rc , out , err = script_helper .assert_python_ok ('-m' , 'tarfile' , * args )
1733+ return out
1734+
1735+ def tarfilecmd_failure (self , * args ):
1736+ return script_helper .assert_python_failure ('-m' , 'tarfile' , * args )
1737+
1738+ def make_simple_tarfile (self , tar_name ):
1739+ files = [support .findfile ('tokenize_tests.txt' ),
1740+ support .findfile ('tokenize_tests-no-coding-cookie-'
1741+ 'and-utf8-bom-sig-only.txt' )]
1742+ self .addCleanup (support .unlink , tar_name )
1743+ with tarfile .open (tar_name , 'w' ) as tf :
1744+ for tardata in files :
1745+ tf .add (tardata , arcname = os .path .basename (tardata ))
1746+
1747+ def test_test_command (self ):
1748+ for tar_name in (tarname , gzipname , bz2name , xzname ):
1749+ for opt in '-t' , '--test' :
1750+ out = self .tarfilecmd (opt , tar_name )
1751+ self .assertEqual (out , b'' )
1752+
1753+ def test_test_command_verbose (self ):
1754+ for tar_name in (tarname , gzipname , bz2name , xzname ):
1755+ for opt in '-v' , '--verbose' :
1756+ out = self .tarfilecmd (opt , '-t' , tar_name )
1757+ self .assertIn (b'is a tar archive.\n ' , out )
1758+
1759+ def test_test_command_invalid_file (self ):
1760+ zipname = support .findfile ('zipdir.zip' )
1761+ rc , out , err = self .tarfilecmd_failure ('-t' , zipname )
1762+ self .assertIn (b' is not a tar archive.' , err )
1763+ self .assertEqual (out , b'' )
1764+ self .assertEqual (rc , 1 )
1765+
1766+ for tar_name in (tarname , gzipname , bz2name , xzname ):
1767+ with self .subTest (tar_name = tar_name ):
1768+ with open (tar_name , 'rb' ) as f :
1769+ data = f .read ()
1770+ try :
1771+ with open (tmpname , 'wb' ) as f :
1772+ f .write (data [:511 ])
1773+ rc , out , err = self .tarfilecmd_failure ('-t' , tmpname )
1774+ self .assertEqual (out , b'' )
1775+ self .assertEqual (rc , 1 )
1776+ finally :
1777+ support .unlink (tmpname )
1778+
1779+ def test_list_command (self ):
1780+ self .make_simple_tarfile (tmpname )
1781+ with support .captured_stdout () as t :
1782+ with tarfile .open (tmpname , 'r' ) as tf :
1783+ tf .list (verbose = False )
1784+ expected = t .getvalue ().encode (sys .getfilesystemencoding ())
1785+ for opt in '-l' , '--list' :
1786+ out = self .tarfilecmd (opt , tmpname )
1787+ self .assertEqual (out , expected )
1788+
1789+ def test_list_command_verbose (self ):
1790+ self .make_simple_tarfile (tmpname )
1791+ with support .captured_stdout () as t :
1792+ with tarfile .open (tmpname , 'r' ) as tf :
1793+ tf .list (verbose = True )
1794+ expected = t .getvalue ().encode (sys .getfilesystemencoding ())
1795+ for opt in '-v' , '--verbose' :
1796+ out = self .tarfilecmd (opt , '-l' , tmpname )
1797+ self .assertEqual (out , expected )
1798+
1799+ def test_list_command_invalid_file (self ):
1800+ zipname = support .findfile ('zipdir.zip' )
1801+ rc , out , err = self .tarfilecmd_failure ('-l' , zipname )
1802+ self .assertIn (b' is not a tar archive.' , err )
1803+ self .assertEqual (out , b'' )
1804+ self .assertEqual (rc , 1 )
1805+
1806+ def test_create_command (self ):
1807+ files = [support .findfile ('tokenize_tests.txt' ),
1808+ support .findfile ('tokenize_tests-no-coding-cookie-'
1809+ 'and-utf8-bom-sig-only.txt' )]
1810+ for opt in '-c' , '--create' :
1811+ try :
1812+ out = self .tarfilecmd (opt , tmpname , * files )
1813+ self .assertEqual (out , b'' )
1814+ with tarfile .open (tmpname ) as tar :
1815+ tar .getmembers ()
1816+ finally :
1817+ support .unlink (tmpname )
1818+
1819+ def test_create_command_verbose (self ):
1820+ files = [support .findfile ('tokenize_tests.txt' ),
1821+ support .findfile ('tokenize_tests-no-coding-cookie-'
1822+ 'and-utf8-bom-sig-only.txt' )]
1823+ for opt in '-v' , '--verbose' :
1824+ try :
1825+ out = self .tarfilecmd (opt , '-c' , tmpname , * files )
1826+ self .assertIn (b' file created.' , out )
1827+ with tarfile .open (tmpname ) as tar :
1828+ tar .getmembers ()
1829+ finally :
1830+ support .unlink (tmpname )
1831+
1832+ def test_create_command_dotless_filename (self ):
1833+ files = [support .findfile ('tokenize_tests.txt' )]
1834+ try :
1835+ out = self .tarfilecmd ('-c' , dotlessname , * files )
1836+ self .assertEqual (out , b'' )
1837+ with tarfile .open (dotlessname ) as tar :
1838+ tar .getmembers ()
1839+ finally :
1840+ support .unlink (dotlessname )
1841+
1842+ def test_create_command_dot_started_filename (self ):
1843+ tar_name = os .path .join (TEMPDIR , ".testtar" )
1844+ files = [support .findfile ('tokenize_tests.txt' )]
1845+ try :
1846+ out = self .tarfilecmd ('-c' , tar_name , * files )
1847+ self .assertEqual (out , b'' )
1848+ with tarfile .open (tar_name ) as tar :
1849+ tar .getmembers ()
1850+ finally :
1851+ support .unlink (tar_name )
1852+
1853+ def test_extract_command (self ):
1854+ self .make_simple_tarfile (tmpname )
1855+ for opt in '-e' , '--extract' :
1856+ try :
1857+ with support .temp_cwd (tarextdir ):
1858+ out = self .tarfilecmd (opt , tmpname )
1859+ self .assertEqual (out , b'' )
1860+ finally :
1861+ support .rmtree (tarextdir )
1862+
1863+ def test_extract_command_verbose (self ):
1864+ self .make_simple_tarfile (tmpname )
1865+ for opt in '-v' , '--verbose' :
1866+ try :
1867+ with support .temp_cwd (tarextdir ):
1868+ out = self .tarfilecmd (opt , '-e' , tmpname )
1869+ self .assertIn (b' file is extracted.' , out )
1870+ finally :
1871+ support .rmtree (tarextdir )
1872+
1873+ def test_extract_command_different_directory (self ):
1874+ self .make_simple_tarfile (tmpname )
1875+ try :
1876+ with support .temp_cwd (tarextdir ):
1877+ out = self .tarfilecmd ('-e' , tmpname , 'spamdir' )
1878+ self .assertEqual (out , b'' )
1879+ finally :
1880+ support .rmtree (tarextdir )
1881+
1882+ def test_extract_command_invalid_file (self ):
1883+ zipname = support .findfile ('zipdir.zip' )
1884+ with support .temp_cwd (tarextdir ):
1885+ rc , out , err = self .tarfilecmd_failure ('-e' , zipname )
1886+ self .assertIn (b' is not a tar archive.' , err )
1887+ self .assertEqual (out , b'' )
1888+ self .assertEqual (rc , 1 )
1889+
1890+
17271891class ContextManagerTest (unittest .TestCase ):
17281892
17291893 def test_basic (self ):
0 commit comments