2009-05-17 09:12:02 -03:00
|
|
|
"""Tests for distutils.archive_util."""
|
2009-05-17 12:03:23 -03:00
|
|
|
__revision__ = "$Id$"
|
2009-05-17 09:12:02 -03:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
2009-05-18 09:29:06 -03:00
|
|
|
from os.path import splitdrive
|
2009-05-17 09:12:02 -03:00
|
|
|
|
|
|
|
from distutils.archive_util import (check_archive_formats, make_tarball,
|
|
|
|
make_zipfile, make_archive)
|
|
|
|
from distutils.spawn import find_executable
|
|
|
|
from distutils.tests import support
|
|
|
|
|
|
|
|
try:
|
|
|
|
import zipfile
|
|
|
|
ZIP_SUPPORT = True
|
|
|
|
except ImportError:
|
|
|
|
ZIP_SUPPORT = find_executable('zip')
|
|
|
|
|
|
|
|
class ArchiveUtilTestCase(support.TempdirManager,
|
|
|
|
unittest.TestCase):
|
|
|
|
|
|
|
|
@unittest.skipUnless(find_executable('tar'), 'Need the tar command to run')
|
|
|
|
def test_make_tarball(self):
|
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
self.write_file([tmpdir, 'file1'], 'xxx')
|
|
|
|
self.write_file([tmpdir, 'file2'], 'xxx')
|
|
|
|
|
|
|
|
tmpdir2 = self.mkdtemp()
|
2009-05-18 09:29:06 -03:00
|
|
|
unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
|
|
|
|
"Source and target should be on same drive")
|
|
|
|
|
2009-05-17 09:12:02 -03:00
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
2009-05-18 05:22:38 -03:00
|
|
|
|
|
|
|
# working with relative paths to avoid tar warnings
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2009-05-18 09:29:06 -03:00
|
|
|
make_tarball(splitdrive(base_name)[1], '.')
|
2009-05-18 05:22:38 -03:00
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
2009-05-17 09:12:02 -03:00
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.tar.gz'
|
|
|
|
self.assert_(os.path.exists(tarball))
|
|
|
|
|
|
|
|
# trying an uncompressed one
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
2009-05-18 05:22:38 -03:00
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2009-05-18 09:29:06 -03:00
|
|
|
make_tarball(splitdrive(base_name)[1], '.', compress=None)
|
2009-05-18 05:22:38 -03:00
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
2009-05-17 09:12:02 -03:00
|
|
|
tarball = base_name + '.tar'
|
|
|
|
self.assert_(os.path.exists(tarball))
|
|
|
|
|
|
|
|
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
|
2009-05-17 12:03:23 -03:00
|
|
|
def test_make_zipfile(self):
|
2009-05-17 09:12:02 -03:00
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
self.write_file([tmpdir, 'file1'], 'xxx')
|
|
|
|
self.write_file([tmpdir, 'file2'], 'xxx')
|
|
|
|
|
|
|
|
tmpdir2 = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
make_zipfile(base_name, tmpdir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.zip'
|
|
|
|
|
|
|
|
def test_check_archive_formats(self):
|
|
|
|
self.assertEquals(check_archive_formats(['gztar', 'xxx', 'zip']),
|
|
|
|
'xxx')
|
|
|
|
self.assertEquals(check_archive_formats(['gztar', 'zip']), None)
|
|
|
|
|
|
|
|
def test_make_archive(self):
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir, 'archive')
|
|
|
|
self.assertRaises(ValueError, make_archive, base_name, 'xxx')
|
|
|
|
|
|
|
|
def test_suite():
|
|
|
|
return unittest.makeSuite(ArchiveUtilTestCase)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main(defaultTest="test_suite")
|