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/>.
18
from snapcraft.tests import TestCase
21
class TestBasePlugin(TestCase):
24
from snapcraft import tests
27
class FakeTarballHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
30
data = 'Test fake tarball file'
31
self.send_response(200)
32
self.send_header('Content-Length', len(data))
33
self.send_header('Content-type', 'text/html')
35
self.wfile.write(data.encode())
37
def log_message(self, *args):
38
# Overwritten so the test does not write to stderr.
42
class TestBasePlugin(tests.TestCase):
23
44
def test_isurl(self):
24
45
plugin = snapcraft.BasePlugin('mock', {})
27
48
self.assertFalse(plugin.isurl('./'))
28
49
self.assertFalse(plugin.isurl('/foo'))
29
50
self.assertFalse(plugin.isurl('/fo:o'))
52
def test_pull_git_with_tag_and_branch_must_raise_error(self):
53
fake_logger = fixtures.FakeLogger(level=logging.ERROR)
54
self.useFixture(fake_logger)
56
plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
57
with self.assertRaises(SystemExit) as raised:
59
'dummy_source', source_tag='test_tag',
60
source_branch='test_branch')
62
self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
64
"You can't specify both source-tag and source-branch for a git "
65
"source (part 'test_plugin').\n")
66
self.assertEqual(expected, fake_logger.output)
68
def test_pull_tarball_must_download_to_sourcedir(self):
69
server = http.server.HTTPServer(('', 0), FakeTarballHTTPRequestHandler)
70
server_thread = threading.Thread(target=server.serve_forever)
71
self.addCleanup(server_thread.join)
72
self.addCleanup(server.server_close)
73
self.addCleanup(server.shutdown)
76
plugin_name = 'test_plugin'
77
dest_dir = os.path.join('parts', plugin_name, 'src')
79
tar_file_name = 'test.tar'
80
source = 'http://{}:{}/{file_name}'.format(
81
*server.server_address, file_name=tar_file_name)
82
plugin = snapcraft.BasePlugin(plugin_name, 'dummy_options')
83
plugin.pull_tarball(source)
85
with open(os.path.join(dest_dir, tar_file_name), 'r') as tar_file:
86
self.assertEqual('Test fake tarball file', tar_file.read())
88
def test_get_source_with_unrecognized_source_must_raise_error(self):
89
fake_logger = fixtures.FakeLogger(level=logging.ERROR)
90
self.useFixture(fake_logger)
92
plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
93
with self.assertRaises(SystemExit) as raised:
94
plugin.get_source('unrecognized://test_source')
96
self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
98
"Unrecognized source 'unrecognized://test_source' for part "
100
self.assertEqual(expected, fake_logger.output)
102
def test_makedirs_with_existing_dir(self):
103
plugin = snapcraft.BasePlugin('dummy_plugin', 'dummy_options')
104
plugin.makedirs(self.path)
105
self.assertTrue(os.path.exists(self.path))
107
def test_makedirs_with_unexisting_dir(self):
108
path = os.path.join(self.path, 'unexisting')
109
plugin = snapcraft.BasePlugin('dummy_plugin', 'dummy_options')
110
plugin.makedirs(path)
111
self.assertTrue(os.path.exists(path))
114
class GetSourceTestCase(tests.TestCase):
117
('bzr with source branch', {
118
'source_type': 'bzr',
119
'source_branch': 'test_branch',
121
'error': 'source-branch'}),
122
('tar with source branch', {
123
'source_type': 'tar',
124
'source_branch': 'test_branch',
126
'error': 'source-branch'}),
127
('tar with source tag', {
128
'source_type': 'tar',
129
'source_branch': None,
130
'source_tag': 'test_tag',
131
'error': 'source-tag'})
134
def test_get_source_with_branch_must_raise_error(self):
135
fake_logger = fixtures.FakeLogger(level=logging.ERROR)
136
self.useFixture(fake_logger)
138
plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
139
with self.assertRaises(SystemExit) as raised:
141
'dummy_source', source_type=self.source_type,
142
source_branch=self.source_branch, source_tag=self.source_tag)
144
self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
146
"You can't specify {} for a {} source "
147
"(part 'test_plugin').\n".format(self.error, self.source_type))
148
self.assertEqual(expected, fake_logger.output)