14_test_gbp_import_dscs.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # vim: set fileencoding=utf-8 :
  2. # (C) 2012 Guido Günther <agx@sigxcpu.org>
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, please see
  15. # <http://www.gnu.org/licenses/>
  16. """Test L{gbp.pq}"""
  17. from . import context
  18. from . import testutils
  19. import gbp.log
  20. import gbp.scripts.import_dscs as import_dscs
  21. from gbp.errors import GbpError
  22. class StubGitImportDsc(object):
  23. """
  24. A Stub for GitImportDsc.
  25. """
  26. def __init__(self, args):
  27. self.failfile = None
  28. for arg in args:
  29. if arg.startswith('--failfile'):
  30. self.failfile = "%s.dsc" % arg.split('=')[1]
  31. def importdsc(self, dsc):
  32. """
  33. Stub the dsc import and fail if we were told to do
  34. so by the --failfile option.
  35. """
  36. return 1 if dsc.filename == self.failfile else 0
  37. class DscStub(object):
  38. def __init__(self, filename, version):
  39. self.filename = filename
  40. self.version = version
  41. self.dscfile = filename
  42. @classmethod
  43. def parse(cls, filename):
  44. # filename is like file1.dsc, file2.dsc, use
  45. # the digit as version number
  46. version = filename[4]
  47. return cls(filename, version)
  48. # hook up stubs
  49. import_dscs.GitImportDsc = StubGitImportDsc
  50. import_dscs.DscFile = DscStub
  51. class TestImportDscs(testutils.DebianGitTestRepo):
  52. """Test L{gbp.scripts.import_dscs}'s """
  53. def setUp(self):
  54. testutils.DebianGitTestRepo.setUp(self)
  55. context.chdir(self.repo.path)
  56. self.orig_err = gbp.log.err
  57. gbp.log.err = self._check_err_msg
  58. def _check_err_msg(self, err):
  59. self.assertIsInstance(err, GbpError)
  60. self.assertIn("Failed to import", err.message)
  61. def test_import_success(self):
  62. """Test importing success with stub"""
  63. ret = import_dscs.main(['argv0', 'file1.dsc', 'file2.dsc'])
  64. self.assertEqual(ret, 0)
  65. def test_import_fail_first(self):
  66. ret = import_dscs.main(['argv0',
  67. '--failfile=file1',
  68. 'file1.dsc'])
  69. self.assertEqual(ret, 1)
  70. def test_import_fail_second(self):
  71. ret = import_dscs.main(['argv0',
  72. '--failfile=file1',
  73. '--failfile=file2',
  74. 'file1.dsc',
  75. 'file2.dsc'])
  76. self.assertEqual(ret, 1)
  77. def tearDown(self):
  78. gbp.log.err = self.orig_err
  79. testutils.DebianGitTestRepo.tearDown(self)
  80. context.teardown()