14_test_gbp_import_dscs.py 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# vim: set fileencoding=utf-8 :
# (C) 2012 Guido Günther <agx@sigxcpu.org>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
14 15
#    along with this program; if not, please see
#    <http://www.gnu.org/licenses/>
16 17
"""Test L{gbp.pq}"""

18
from . import context
19
from . import testutils
20

21
import gbp.log
22 23
import gbp.scripts.import_dscs as import_dscs

24
from gbp.errors import GbpError
25

26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
class StubGitImportDsc(object):
    """
    A Stub for GitImportDsc.
    """
    def __init__(self, args):
        self.failfile = None
        for arg in args:
            if arg.startswith('--failfile'):
                self.failfile = "%s.dsc" % arg.split('=')[1]

    def importdsc(self, dsc):
        """
        Stub the dsc import and fail if we were told to do
        so by the --failfile option.
        """
        return 1 if dsc.filename == self.failfile else 0

44

45 46 47 48
class DscStub(object):
    def __init__(self, filename, version):
        self.filename = filename
        self.version = version
49
        self.dscfile = filename
50

51 52 53 54 55 56
    @classmethod
    def parse(cls, filename):
        # filename is like file1.dsc, file2.dsc, use
        # the digit as version number
        version = filename[4]
        return cls(filename, version)
57

Guido Günther's avatar
Guido Günther committed
58

59 60 61 62 63
class TestImportDscs(testutils.DebianGitTestRepo):
    """Test L{gbp.scripts.import_dscs}'s """

    def setUp(self):
        testutils.DebianGitTestRepo.setUp(self)
64
        context.chdir(self.repo.path)
65 66 67
        self.orig_err = gbp.log.err
        gbp.log.err = self._check_err_msg

68 69 70 71 72
        self.safed_GitImportDsc = import_dscs.GitImportDsc
        self.safed_DscFile = import_dscs.DscFile
        import_dscs.GitImportDsc = StubGitImportDsc
        import_dscs.DscFile = DscStub

73 74
    def _check_err_msg(self, err):
        self.assertIsInstance(err, GbpError)
75
        self.assertIn("Failed to import", str(err))
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    def test_import_success(self):
        """Test importing success with stub"""
        ret = import_dscs.main(['argv0', 'file1.dsc', 'file2.dsc'])
        self.assertEqual(ret, 0)

    def test_import_fail_first(self):
        ret = import_dscs.main(['argv0',
                                '--failfile=file1',
                                'file1.dsc'])
        self.assertEqual(ret, 1)

    def test_import_fail_second(self):
        ret = import_dscs.main(['argv0',
                                '--failfile=file1',
                                '--failfile=file2',
                                'file1.dsc',
                                'file2.dsc'])
        self.assertEqual(ret, 1)

    def tearDown(self):
97
        gbp.log.err = self.orig_err
98
        testutils.DebianGitTestRepo.tearDown(self)
99
        context.teardown()
100 101 102

        import_dscs.GitImportDsc = self.safed_GitImportDsc
        import_dscs.DscFile = self.safed_DscFile