summaryrefslogtreecommitdiff
path: root/tests/19_test_gbp_scripts_config.py
blob: 8bb2105373a7851311967f43f2f33ac5afa3e97e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# vim: set fileencoding=utf-8 :
# (C) 2014 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
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""Test L{gbp} config"""

import os
import sys
# Try unittest2 for CentOS
try:
    import unittest2 as unittest
except ImportError:
    import unittest
import gbp.scripts.config

class TestGbpConfigCommand(unittest.TestCase):
    class SingleValuePrintStub(object):
        def __init__(self):
            self.result = None

        def __call__(self, arg):
            self.result = arg

    class AllValuesPrintStub(object):
        def __init__(self, cmd):
            self.cmd = cmd
            self.result = {}

        def __call__(self, arg):
            k, v = arg.split('=', 1)
            self.result[k] = v

    def setUp(self):
        self.conffiles_save = os.environ.get('GBP_CONF_FILES')
        self.confname = 'tests/data/gbp_config.conf'
        self.assertTrue(os.stat(self.confname))
        os.environ['GBP_CONF_FILES'] = self.confname

    def test_invocation_single_value(self):
        """Can invoke it for a sngle value  without error"""
        ret = gbp.scripts.config.main(['doesnotmatter', 'coolcommand.branchname'])
        self.assertEqual(ret, 0)

    def test_invocation_missing_value(self):
        """Can we detect a missing value"""
        ret = gbp.scripts.config.main(['doesnotmatter', 'coolcommand.doesnotexist'])
        self.assertEqual(ret, 1)

    def test_print_cmd_single_value(self):
        """Can we fetch a single configuration value"""
        printstub = self.SingleValuePrintStub()
        query = 'coolcommand.branchname'
        ret = gbp.scripts.config.print_cmd_single_value(query, printstub)
        self.assertEqual(printstub.result, '%s=abranch' % query)
        self.assertEqual(ret, 0)

    def test_print_cmd_all_values(self):
        """Can we fetch the configuration for all commands"""
        for cmd in [ 'buildpackage',
                     'clone',
                     'config',
                     'create_remote_repo',
                     'dch',
                     'import_dsc',
                     'import_orig',
                     'pq',
                     'pull' ]:
            printstub = self.AllValuesPrintStub(cmd)
            ret = gbp.scripts.config.print_cmd_all_values(cmd, printstub)
            self.assertTrue('%s.color' % cmd in list(printstub.result.keys()))
            self.assertEqual(ret, 0)

    def test_invalid_cms(self):
        """Invalid commands or those not using the config should rerturn an error code"""
        for cmd in [ "import_dscs", "supercommand" ]:
            printstub = self.AllValuesPrintStub(cmd)
            ret = gbp.scripts.config.print_cmd_all_values(cmd, printstub)
            self.assertEqual(ret, 2)