~longsleep/snapcraft/snapcraft-debs-plugin

« back to all changes in this revision

Viewing changes to snapcraft/tests/test_base_plugin.py

  • Committer: Snappy Tarmac
  • Author(s): Sergio Schvezov
  • Date: 2015-10-26 15:25:06 UTC
  • mfrom: (250.2.4 work)
  • Revision ID: snappy_tarmac-20151026152506-7hqsn1kftvqcuncl
Plugins raise exception instead of true/false by sergiusens approved by elopio

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
 
import fixtures
18
 
import logging
19
17
import unittest.mock
20
18
 
21
19
import snapcraft
 
20
from snapcraft import sources
22
21
from snapcraft import tests
23
22
 
24
23
 
34
33
 
35
34
class TestBasePlugin(tests.TestCase):
36
35
 
37
 
    def test_get_source_with_unrecognized_source_must_raise_error(self):
38
 
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
39
 
        self.useFixture(fake_logger)
40
 
 
 
36
    def test_get_source_with_unrecognized_source_must_raise_exception(self):
41
37
        options = MockOptions('unrecognized://test_source')
42
38
        plugin = snapcraft.BasePlugin('test_plugin', options)
43
 
        with self.assertRaises(SystemExit) as raised:
 
39
        with self.assertRaises(ValueError) as raised:
44
40
            plugin.pull()
45
41
 
46
 
        self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
47
 
        expected = (
48
 
            "Unrecognized source 'unrecognized://test_source' for part "
49
 
            "'test_plugin': No handler to manage source.\n")
50
 
        self.assertEqual(expected, fake_logger.output)
 
42
        self.assertEqual(raised.exception.__str__(),
 
43
                         'no handler to manage source')
51
44
 
52
45
    @unittest.mock.patch('os.path.isdir')
53
 
    def test_local_non_dir_source_path_must_raise_error(self, mock_isdir):
54
 
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
55
 
        self.useFixture(fake_logger)
56
 
 
 
46
    def test_local_non_dir_source_path_must_raise_exception(self, mock_isdir):
57
47
        options = MockOptions('file')
58
48
        mock_isdir.return_value = False
59
49
        plugin = snapcraft.BasePlugin('test_plugin', options)
60
 
        with self.assertRaises(SystemExit) as raised:
 
50
        with self.assertRaises(ValueError) as raised:
61
51
            plugin.pull()
62
52
 
63
53
        mock_isdir.assert_called_once_with('file')
64
 
        self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
65
 
        expected = (
66
 
            "Unrecognized source 'file' for part 'test_plugin': "
67
 
            "Local source is not a directory.\n")
68
 
        self.assertEqual(expected, fake_logger.output)
 
54
 
 
55
        self.assertEqual(raised.exception.__str__(),
 
56
                         'local source is not a directory')
69
57
 
70
58
 
71
59
class GetSourceWithBranches(tests.TestCase):
84
72
    ]
85
73
 
86
74
    def test_get_source_with_branch_and_tag_must_raise_error(self):
87
 
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
88
 
        self.useFixture(fake_logger)
89
 
 
90
75
        options = MockOptions('lp:source', self.source_type,
91
76
                              self.source_branch, self.source_tag)
92
77
        plugin = snapcraft.BasePlugin('test_plugin', options)
93
 
        with self.assertRaises(SystemExit) as raised:
 
78
        with self.assertRaises(sources.IncompatibleOptionsError) as raised:
94
79
            plugin.pull()
95
80
 
96
 
        self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
97
 
        expected = (
98
 
            'Issues while setting up sources for part \'test_plugin\': '
 
81
        self.assertEqual(
 
82
            raised.exception.__str__(),
99
83
            'can\'t specify both source-tag and source-branch for a {} '
100
 
            "source.\n".format(self.source_type))
101
 
        self.assertEqual(expected, fake_logger.output)
 
84
            'source'.format(self.source_type))
102
85
 
103
86
 
104
87
class GetSourceTestCase(tests.TestCase):
122
105
    ]
123
106
 
124
107
    def test_get_source_with_branch_must_raise_error(self):
125
 
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
126
 
        self.useFixture(fake_logger)
127
 
 
128
108
        options = MockOptions('lp:this', self.source_type, self.source_branch,
129
109
                              self.source_tag)
130
110
        plugin = snapcraft.BasePlugin('test_plugin', options)
131
111
 
132
 
        with self.assertRaises(SystemExit) as raised:
 
112
        with self.assertRaises(sources.IncompatibleOptionsError) as raised:
133
113
            plugin.pull()
134
114
 
135
 
        self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
136
 
        expected = (
137
 
            'Issues while setting up sources for part \'test_plugin\': can\'t '
138
 
            'specify a {} for a {} source.\n'
139
 
            .format(self.error, self.source_type))
140
 
        self.assertEqual(expected, fake_logger.output)
 
115
        self.assertEqual(
 
116
            raised.exception.__str__(),
 
117
            'can\'t specify a {} for a {} source'.format(
 
118
                self.error, self.source_type))