»Core Development>Code coverage>Lib/distutils/tests/test_archive_util.py

Python code coverage for Lib/distutils/tests/test_archive_util.py

#countcontent
1n/a# -*- coding: utf-8 -*-
2n/a"""Tests for distutils.archive_util."""
3n/aimport unittest
4n/aimport os
5n/aimport sys
6n/aimport tarfile
7n/afrom os.path import splitdrive
8n/aimport warnings
9n/a
10n/afrom distutils import archive_util
11n/afrom distutils.archive_util import (check_archive_formats, make_tarball,
12n/a make_zipfile, make_archive,
13n/a ARCHIVE_FORMATS)
14n/afrom distutils.spawn import find_executable, spawn
15n/afrom distutils.tests import support
16n/afrom test.support import check_warnings, run_unittest, patch, change_cwd
17n/a
18n/atry:
19n/a import grp
20n/a import pwd
21n/a UID_GID_SUPPORT = True
22n/aexcept ImportError:
23n/a UID_GID_SUPPORT = False
24n/a
25n/atry:
26n/a import zipfile
27n/a ZIP_SUPPORT = True
28n/aexcept ImportError:
29n/a ZIP_SUPPORT = find_executable('zip')
30n/a
31n/atry:
32n/a import zlib
33n/a ZLIB_SUPPORT = True
34n/aexcept ImportError:
35n/a ZLIB_SUPPORT = False
36n/a
37n/atry:
38n/a import bz2
39n/aexcept ImportError:
40n/a bz2 = None
41n/a
42n/atry:
43n/a import lzma
44n/aexcept ImportError:
45n/a lzma = None
46n/a
47n/adef can_fs_encode(filename):
48n/a """
49n/a Return True if the filename can be saved in the file system.
50n/a """
51n/a if os.path.supports_unicode_filenames:
52n/a return True
53n/a try:
54n/a filename.encode(sys.getfilesystemencoding())
55n/a except UnicodeEncodeError:
56n/a return False
57n/a return True
58n/a
59n/a
60n/aclass ArchiveUtilTestCase(support.TempdirManager,
61n/a support.LoggingSilencer,
62n/a unittest.TestCase):
63n/a
64n/a @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
65n/a def test_make_tarball(self, name='archive'):
66n/a # creating something to tar
67n/a tmpdir = self._create_files()
68n/a self._make_tarball(tmpdir, name, '.tar.gz')
69n/a # trying an uncompressed one
70n/a self._make_tarball(tmpdir, name, '.tar', compress=None)
71n/a
72n/a @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
73n/a def test_make_tarball_gzip(self):
74n/a tmpdir = self._create_files()
75n/a self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip')
76n/a
77n/a @unittest.skipUnless(bz2, 'Need bz2 support to run')
78n/a def test_make_tarball_bzip2(self):
79n/a tmpdir = self._create_files()
80n/a self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2')
81n/a
82n/a @unittest.skipUnless(lzma, 'Need lzma support to run')
83n/a def test_make_tarball_xz(self):
84n/a tmpdir = self._create_files()
85n/a self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz')
86n/a
87n/a @unittest.skipUnless(can_fs_encode('Ã¥rchiv'),
88n/a 'File system cannot handle this filename')
89n/a def test_make_tarball_latin1(self):
90n/a """
91n/a Mirror test_make_tarball, except filename contains latin characters.
92n/a """
93n/a self.test_make_tarball('Ã¥rchiv') # note this isn't a real word
94n/a
95n/a @unittest.skipUnless(can_fs_encode('のアーカイブ'),
96n/a 'File system cannot handle this filename')
97n/a def test_make_tarball_extended(self):
98n/a """
99n/a Mirror test_make_tarball, except filename contains extended
100n/a characters outside the latin charset.
101n/a """
102n/a self.test_make_tarball('のアーカイブ') # japanese for archive
103n/a
104n/a def _make_tarball(self, tmpdir, target_name, suffix, **kwargs):
105n/a tmpdir2 = self.mkdtemp()
106n/a unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
107n/a "source and target should be on same drive")
108n/a
109n/a base_name = os.path.join(tmpdir2, target_name)
110n/a
111n/a # working with relative paths to avoid tar warnings
112n/a with change_cwd(tmpdir):
113n/a make_tarball(splitdrive(base_name)[1], 'dist', **kwargs)
114n/a
115n/a # check if the compressed tarball was created
116n/a tarball = base_name + suffix
117n/a self.assertTrue(os.path.exists(tarball))
118n/a self.assertEqual(self._tarinfo(tarball), self._created_files)
119n/a
120n/a def _tarinfo(self, path):
121n/a tar = tarfile.open(path)
122n/a try:
123n/a names = tar.getnames()
124n/a names.sort()
125n/a return tuple(names)
126n/a finally:
127n/a tar.close()
128n/a
129n/a _created_files = ('dist', 'dist/file1', 'dist/file2',
130n/a 'dist/sub', 'dist/sub/file3', 'dist/sub2')
131n/a
132n/a def _create_files(self):
133n/a # creating something to tar
134n/a tmpdir = self.mkdtemp()
135n/a dist = os.path.join(tmpdir, 'dist')
136n/a os.mkdir(dist)
137n/a self.write_file([dist, 'file1'], 'xxx')
138n/a self.write_file([dist, 'file2'], 'xxx')
139n/a os.mkdir(os.path.join(dist, 'sub'))
140n/a self.write_file([dist, 'sub', 'file3'], 'xxx')
141n/a os.mkdir(os.path.join(dist, 'sub2'))
142n/a return tmpdir
143n/a
144n/a @unittest.skipUnless(find_executable('tar') and find_executable('gzip')
145n/a and ZLIB_SUPPORT,
146n/a 'Need the tar, gzip and zlib command to run')
147n/a def test_tarfile_vs_tar(self):
148n/a tmpdir = self._create_files()
149n/a tmpdir2 = self.mkdtemp()
150n/a base_name = os.path.join(tmpdir2, 'archive')
151n/a old_dir = os.getcwd()
152n/a os.chdir(tmpdir)
153n/a try:
154n/a make_tarball(base_name, 'dist')
155n/a finally:
156n/a os.chdir(old_dir)
157n/a
158n/a # check if the compressed tarball was created
159n/a tarball = base_name + '.tar.gz'
160n/a self.assertTrue(os.path.exists(tarball))
161n/a
162n/a # now create another tarball using `tar`
163n/a tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
164n/a tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
165n/a gzip_cmd = ['gzip', '-f9', 'archive2.tar']
166n/a old_dir = os.getcwd()
167n/a os.chdir(tmpdir)
168n/a try:
169n/a spawn(tar_cmd)
170n/a spawn(gzip_cmd)
171n/a finally:
172n/a os.chdir(old_dir)
173n/a
174n/a self.assertTrue(os.path.exists(tarball2))
175n/a # let's compare both tarballs
176n/a self.assertEqual(self._tarinfo(tarball), self._created_files)
177n/a self.assertEqual(self._tarinfo(tarball2), self._created_files)
178n/a
179n/a # trying an uncompressed one
180n/a base_name = os.path.join(tmpdir2, 'archive')
181n/a old_dir = os.getcwd()
182n/a os.chdir(tmpdir)
183n/a try:
184n/a make_tarball(base_name, 'dist', compress=None)
185n/a finally:
186n/a os.chdir(old_dir)
187n/a tarball = base_name + '.tar'
188n/a self.assertTrue(os.path.exists(tarball))
189n/a
190n/a # now for a dry_run
191n/a base_name = os.path.join(tmpdir2, 'archive')
192n/a old_dir = os.getcwd()
193n/a os.chdir(tmpdir)
194n/a try:
195n/a make_tarball(base_name, 'dist', compress=None, dry_run=True)
196n/a finally:
197n/a os.chdir(old_dir)
198n/a tarball = base_name + '.tar'
199n/a self.assertTrue(os.path.exists(tarball))
200n/a
201n/a @unittest.skipUnless(find_executable('compress'),
202n/a 'The compress program is required')
203n/a def test_compress_deprecated(self):
204n/a tmpdir = self._create_files()
205n/a base_name = os.path.join(self.mkdtemp(), 'archive')
206n/a
207n/a # using compress and testing the PendingDeprecationWarning
208n/a old_dir = os.getcwd()
209n/a os.chdir(tmpdir)
210n/a try:
211n/a with check_warnings() as w:
212n/a warnings.simplefilter("always")
213n/a make_tarball(base_name, 'dist', compress='compress')
214n/a finally:
215n/a os.chdir(old_dir)
216n/a tarball = base_name + '.tar.Z'
217n/a self.assertTrue(os.path.exists(tarball))
218n/a self.assertEqual(len(w.warnings), 1)
219n/a
220n/a # same test with dry_run
221n/a os.remove(tarball)
222n/a old_dir = os.getcwd()
223n/a os.chdir(tmpdir)
224n/a try:
225n/a with check_warnings() as w:
226n/a warnings.simplefilter("always")
227n/a make_tarball(base_name, 'dist', compress='compress',
228n/a dry_run=True)
229n/a finally:
230n/a os.chdir(old_dir)
231n/a self.assertFalse(os.path.exists(tarball))
232n/a self.assertEqual(len(w.warnings), 1)
233n/a
234n/a @unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT,
235n/a 'Need zip and zlib support to run')
236n/a def test_make_zipfile(self):
237n/a # creating something to tar
238n/a tmpdir = self._create_files()
239n/a base_name = os.path.join(self.mkdtemp(), 'archive')
240n/a with change_cwd(tmpdir):
241n/a make_zipfile(base_name, 'dist')
242n/a
243n/a # check if the compressed tarball was created
244n/a tarball = base_name + '.zip'
245n/a self.assertTrue(os.path.exists(tarball))
246n/a with zipfile.ZipFile(tarball) as zf:
247n/a self.assertEqual(sorted(zf.namelist()),
248n/a ['dist/file1', 'dist/file2', 'dist/sub/file3'])
249n/a
250n/a @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
251n/a def test_make_zipfile_no_zlib(self):
252n/a patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError
253n/a
254n/a called = []
255n/a zipfile_class = zipfile.ZipFile
256n/a def fake_zipfile(*a, **kw):
257n/a if kw.get('compression', None) == zipfile.ZIP_STORED:
258n/a called.append((a, kw))
259n/a return zipfile_class(*a, **kw)
260n/a
261n/a patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile)
262n/a
263n/a # create something to tar and compress
264n/a tmpdir = self._create_files()
265n/a base_name = os.path.join(self.mkdtemp(), 'archive')
266n/a with change_cwd(tmpdir):
267n/a make_zipfile(base_name, 'dist')
268n/a
269n/a tarball = base_name + '.zip'
270n/a self.assertEqual(called,
271n/a [((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
272n/a self.assertTrue(os.path.exists(tarball))
273n/a with zipfile.ZipFile(tarball) as zf:
274n/a self.assertEqual(sorted(zf.namelist()),
275n/a ['dist/file1', 'dist/file2', 'dist/sub/file3'])
276n/a
277n/a def test_check_archive_formats(self):
278n/a self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
279n/a 'xxx')
280n/a self.assertIsNone(check_archive_formats(['gztar', 'bztar', 'xztar',
281n/a 'ztar', 'tar', 'zip']))
282n/a
283n/a def test_make_archive(self):
284n/a tmpdir = self.mkdtemp()
285n/a base_name = os.path.join(tmpdir, 'archive')
286n/a self.assertRaises(ValueError, make_archive, base_name, 'xxx')
287n/a
288n/a def test_make_archive_cwd(self):
289n/a current_dir = os.getcwd()
290n/a def _breaks(*args, **kw):
291n/a raise RuntimeError()
292n/a ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')
293n/a try:
294n/a try:
295n/a make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
296n/a except:
297n/a pass
298n/a self.assertEqual(os.getcwd(), current_dir)
299n/a finally:
300n/a del ARCHIVE_FORMATS['xxx']
301n/a
302n/a def test_make_archive_tar(self):
303n/a base_dir = self._create_files()
304n/a base_name = os.path.join(self.mkdtemp() , 'archive')
305n/a res = make_archive(base_name, 'tar', base_dir, 'dist')
306n/a self.assertTrue(os.path.exists(res))
307n/a self.assertEqual(os.path.basename(res), 'archive.tar')
308n/a self.assertEqual(self._tarinfo(res), self._created_files)
309n/a
310n/a @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
311n/a def test_make_archive_gztar(self):
312n/a base_dir = self._create_files()
313n/a base_name = os.path.join(self.mkdtemp() , 'archive')
314n/a res = make_archive(base_name, 'gztar', base_dir, 'dist')
315n/a self.assertTrue(os.path.exists(res))
316n/a self.assertEqual(os.path.basename(res), 'archive.tar.gz')
317n/a self.assertEqual(self._tarinfo(res), self._created_files)
318n/a
319n/a @unittest.skipUnless(bz2, 'Need bz2 support to run')
320n/a def test_make_archive_bztar(self):
321n/a base_dir = self._create_files()
322n/a base_name = os.path.join(self.mkdtemp() , 'archive')
323n/a res = make_archive(base_name, 'bztar', base_dir, 'dist')
324n/a self.assertTrue(os.path.exists(res))
325n/a self.assertEqual(os.path.basename(res), 'archive.tar.bz2')
326n/a self.assertEqual(self._tarinfo(res), self._created_files)
327n/a
328n/a @unittest.skipUnless(lzma, 'Need xz support to run')
329n/a def test_make_archive_xztar(self):
330n/a base_dir = self._create_files()
331n/a base_name = os.path.join(self.mkdtemp() , 'archive')
332n/a res = make_archive(base_name, 'xztar', base_dir, 'dist')
333n/a self.assertTrue(os.path.exists(res))
334n/a self.assertEqual(os.path.basename(res), 'archive.tar.xz')
335n/a self.assertEqual(self._tarinfo(res), self._created_files)
336n/a
337n/a def test_make_archive_owner_group(self):
338n/a # testing make_archive with owner and group, with various combinations
339n/a # this works even if there's not gid/uid support
340n/a if UID_GID_SUPPORT:
341n/a group = grp.getgrgid(0)[0]
342n/a owner = pwd.getpwuid(0)[0]
343n/a else:
344n/a group = owner = 'root'
345n/a
346n/a base_dir = self._create_files()
347n/a root_dir = self.mkdtemp()
348n/a base_name = os.path.join(self.mkdtemp() , 'archive')
349n/a res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
350n/a group=group)
351n/a self.assertTrue(os.path.exists(res))
352n/a
353n/a res = make_archive(base_name, 'zip', root_dir, base_dir)
354n/a self.assertTrue(os.path.exists(res))
355n/a
356n/a res = make_archive(base_name, 'tar', root_dir, base_dir,
357n/a owner=owner, group=group)
358n/a self.assertTrue(os.path.exists(res))
359n/a
360n/a res = make_archive(base_name, 'tar', root_dir, base_dir,
361n/a owner='kjhkjhkjg', group='oihohoh')
362n/a self.assertTrue(os.path.exists(res))
363n/a
364n/a @unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib")
365n/a @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
366n/a def test_tarfile_root_owner(self):
367n/a tmpdir = self._create_files()
368n/a base_name = os.path.join(self.mkdtemp(), 'archive')
369n/a old_dir = os.getcwd()
370n/a os.chdir(tmpdir)
371n/a group = grp.getgrgid(0)[0]
372n/a owner = pwd.getpwuid(0)[0]
373n/a try:
374n/a archive_name = make_tarball(base_name, 'dist', compress=None,
375n/a owner=owner, group=group)
376n/a finally:
377n/a os.chdir(old_dir)
378n/a
379n/a # check if the compressed tarball was created
380n/a self.assertTrue(os.path.exists(archive_name))
381n/a
382n/a # now checks the rights
383n/a archive = tarfile.open(archive_name)
384n/a try:
385n/a for member in archive.getmembers():
386n/a self.assertEqual(member.uid, 0)
387n/a self.assertEqual(member.gid, 0)
388n/a finally:
389n/a archive.close()
390n/a
391n/adef test_suite():
392n/a return unittest.makeSuite(ArchiveUtilTestCase)
393n/a
394n/aif __name__ == "__main__":
395n/a run_unittest(test_suite())