06_test_upstream_source.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # vim: set fileencoding=utf-8 :
  2. """Test the L{UpstreamSource} class"""
  3. from . import context
  4. import glob
  5. import os
  6. import tarfile
  7. import unittest
  8. import zipfile
  9. from gbp.pkg import UpstreamSource
  10. class TestDir(unittest.TestCase):
  11. def setUp(self):
  12. self.tmpdir = context.new_tmpdir(__name__)
  13. self.upstream_dir = self.tmpdir.join('test-1.0')
  14. os.mkdir(self.upstream_dir)
  15. def test_directory(self):
  16. """Upstream source is a directory"""
  17. source = UpstreamSource(self.upstream_dir)
  18. self.assertEqual(source.is_orig(), False)
  19. self.assertEqual(source.path, self.upstream_dir)
  20. self.assertEqual(source.unpacked, self.upstream_dir)
  21. self.assertEqual(source.guess_version(), ('test', '1.0'))
  22. def tearDown(self):
  23. context.teardown()
  24. class TestTar(unittest.TestCase):
  25. """Test if packing tar archives works"""
  26. def _check_tar(self, us, positive=[], negative=[]):
  27. t = tarfile.open(name=us.path, mode="r:bz2")
  28. for f in positive:
  29. i = t.getmember(f)
  30. self.assertEqual(type(i), tarfile.TarInfo)
  31. for f in negative:
  32. try:
  33. t.getmember(f)
  34. self.fail("Found %s in archive" % f)
  35. except KeyError:
  36. pass
  37. t.close()
  38. def setUp(self):
  39. self.tmpdir = context.new_tmpdir(__name__)
  40. self.source = UpstreamSource(os.path.join(context.projectdir, "gbp"))
  41. def tearDown(self):
  42. context.teardown()
  43. def test_pack_tar(self):
  44. """Check if packing tar archives works"""
  45. target = self.tmpdir.join("gbp_0.1.tar.bz2")
  46. repacked = self.source.pack(target)
  47. self.assertEqual(repacked.is_orig(), True)
  48. self.assertEqual(repacked.is_dir(), False)
  49. self.assertEqual(repacked.guess_version(), ('gbp', '0.1'))
  50. self._check_tar(repacked, ["gbp/errors.py", "gbp/__init__.py"])
  51. def test_pack_filtered(self):
  52. """Check if filtering out files works"""
  53. target = self.tmpdir.join("gbp_0.1.tar.bz2")
  54. repacked = self.source.pack(target, ["__init__.py"])
  55. self.assertEqual(repacked.is_orig(), True)
  56. self.assertEqual(repacked.is_dir(), False)
  57. self._check_tar(repacked, ["gbp/errors.py"],
  58. ["gbp/__init__.py"])
  59. class TestZip(unittest.TestCase):
  60. """Test if unpacking zip archives works"""
  61. def setUp(self):
  62. self.tmpdir = context.new_tmpdir(__name__)
  63. self.zipfile = self.tmpdir.join("gbp-0.1.zip")
  64. z = zipfile.ZipFile(self.zipfile, "w")
  65. for f in glob.glob(os.path.join(context.projectdir, "gbp/*.py")):
  66. z.write(f, f, zipfile.ZIP_DEFLATED)
  67. z.close()
  68. def tearDown(self):
  69. context.teardown()
  70. def test_unpack(self):
  71. source = UpstreamSource(self.zipfile)
  72. self.assertEqual(source.is_orig(), False)
  73. self.assertEqual(source.is_dir(), False)
  74. self.assertEqual(source.unpacked, None)
  75. self.assertEqual(source.guess_version(), ('gbp', '0.1'))
  76. source.unpack(str(self.tmpdir))
  77. self.assertNotEqual(source.unpacked, None)