Merge lp:~sergiusens/snapcraft/source-tests into lp:~snappy-dev/snapcraft/core

Proposed by Sergio Schvezov
Status: Merged
Approved by: Sergio Schvezov
Approved revision: 151
Merged at revision: 147
Proposed branch: lp:~sergiusens/snapcraft/source-tests
Merge into: lp:~snappy-dev/snapcraft/core
Prerequisite: lp:~sergiusens/snapcraft/less-source
Diff against target: 253 lines (+239/-0)
1 file modified
snapcraft/tests/test_sources.py (+239/-0)
To merge this branch: bzr merge lp:~sergiusens/snapcraft/source-tests
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve
Michael Vogt (community) Approve
Review via email: mp+269649@code.launchpad.net

Commit message

Unit tests for sources

Description of the change

For all but Tar.

To post a comment you must log in.
Revision history for this message
Michael Vogt (mvo) wrote :

This looks great, one whitespace remark because I wanted to comment on at least one line :) But seriously, good stuff and fine to merge.

review: Approve
Revision history for this message
Snappy Tarmac (snappydevtarmac) wrote :

There are additional revisions which have not been approved in review. Please seek review and approval of these new revisions.

Revision history for this message
Sergio Schvezov (sergiusens) wrote :

bzr pump from previous review and white space fix from review

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snapcraft/tests/test_sources.py'
2--- snapcraft/tests/test_sources.py 2015-09-01 10:10:21 +0000
3+++ snapcraft/tests/test_sources.py 2015-09-01 10:10:21 +0000
4@@ -17,6 +17,7 @@
5 import os
6 import http.server
7 import threading
8+import unittest.mock
9
10 from snapcraft import sources
11 from snapcraft import tests
12@@ -59,3 +60,241 @@
13
14 with open(os.path.join(dest_dir, tar_file_name), 'r') as tar_file:
15 self.assertEqual('Test fake tarball file', tar_file.read())
16+
17+
18+class SourceTestCase(tests.TestCase):
19+
20+ def setUp(self):
21+ super().setUp()
22+
23+ patcher = unittest.mock.patch('snapcraft.common.run')
24+ self.mock_run = patcher.start()
25+ self.mock_run.return_value = True
26+ self.addCleanup(patcher.stop)
27+
28+ patcher = unittest.mock.patch('os.rmdir')
29+ self.mock_rmdir = patcher.start()
30+ self.addCleanup(patcher.stop)
31+
32+ patcher = unittest.mock.patch('os.path.exists')
33+ self.mock_path_exists = patcher.start()
34+ self.mock_path_exists.return_value = False
35+ self.addCleanup(patcher.stop)
36+
37+
38+class TestBazaar(SourceTestCase):
39+
40+ def test_pull(self):
41+ bzr = sources.Bazaar('lp:my-source', 'source_dir')
42+
43+ bzr.pull()
44+
45+ self.mock_rmdir.assert_called_once_with('source_dir')
46+ self.mock_run.assert_called_once_with(
47+ ['bzr', 'branch', 'lp:my-source', 'source_dir'], cwd=os.getcwd())
48+
49+ def test_pull_tag(self):
50+ bzr = sources.Bazaar('lp:my-source', 'source_dir', source_tag='tag')
51+ bzr.pull()
52+
53+ self.mock_run.assert_called_once_with(
54+ ['bzr', 'branch', '-r', 'tag:tag', 'lp:my-source', 'source_dir'], cwd=os.getcwd())
55+
56+ def test_pull_existing_with_tag(self):
57+ self.mock_path_exists.return_value = True
58+
59+ bzr = sources.Bazaar('lp:my-source', 'source_dir', source_tag='tag')
60+ bzr.pull()
61+
62+ self.mock_run.assert_called_once_with(
63+ ['bzr', 'pull', '-r', 'tag:tag', 'lp:my-source', '-d', 'source_dir'], cwd=os.getcwd())
64+
65+ def test_provision(self):
66+ bzr = sources.Bazaar('lp:my-source', 'source_dir')
67+ bzr.provision('dst')
68+
69+ self.mock_run.assert_called_once_with(
70+ ['cp', '-Trfa', 'source_dir', 'dst'], cwd=os.getcwd())
71+
72+ def test_init_with_source_branch_raises_exception(self):
73+ with self.assertRaises(sources.IncompatibleOptionsError) as raised:
74+ sources.Bazaar('lp:mysource', 'source_dir', source_branch='branch')
75+
76+ expected_message = 'can\'t specify a source-branch for a bzr source'
77+ self.assertEqual(raised.exception.message, expected_message)
78+
79+
80+class TestGit(SourceTestCase):
81+
82+ def test_pull(self):
83+ git = sources.Git('git://my-source', 'source_dir')
84+
85+ git.pull()
86+
87+ self.mock_run.assert_called_once_with(
88+ ['git', 'clone', 'git://my-source', 'source_dir'], cwd=os.getcwd())
89+
90+ def test_pull_branch(self):
91+ git = sources.Git('git://my-source', 'source_dir', source_branch='my-branch')
92+ git.pull()
93+
94+ self.mock_run.assert_called_once_with(
95+ ['git', 'clone', '--branch', 'my-branch', 'git://my-source', 'source_dir'], cwd=os.getcwd())
96+
97+ def test_pull_tag(self):
98+ git = sources.Git('git://my-source', 'source_dir', source_tag='tag')
99+ git.pull()
100+
101+ self.mock_run.assert_called_once_with(
102+ ['git', 'clone', '--branch', 'tag', 'git://my-source', 'source_dir'], cwd=os.getcwd())
103+
104+ def test_pull_existing(self):
105+ self.mock_path_exists.return_value = True
106+
107+ git = sources.Git('git://my-source', 'source_dir')
108+ git.pull()
109+
110+ self.mock_run.assert_called_once_with(
111+ ['git', '-C', 'source_dir', 'pull', 'git://my-source', 'HEAD'], cwd=os.getcwd())
112+
113+ def test_pull_existing_with_tag(self):
114+ self.mock_path_exists.return_value = True
115+
116+ git = sources.Git('git://my-source', 'source_dir', source_tag='tag')
117+ git.pull()
118+
119+ self.mock_run.assert_called_once_with(
120+ ['git', '-C', 'source_dir', 'pull', 'git://my-source', 'refs/tags/tag'], cwd=os.getcwd())
121+
122+ def test_pull_existing_with_branch(self):
123+ self.mock_path_exists.return_value = True
124+
125+ git = sources.Git('git://my-source', 'source_dir', source_branch='my-branch')
126+ git.pull()
127+
128+ self.mock_run.assert_called_once_with(
129+ ['git', '-C', 'source_dir', 'pull', 'git://my-source', 'refs/heads/my-branch'], cwd=os.getcwd())
130+
131+ def test_provision(self):
132+ bzr = sources.Git('git://my-source', 'source_dir')
133+ bzr.provision('dst')
134+
135+ self.mock_run.assert_called_once_with(
136+ ['cp', '-Trfa', 'source_dir', 'dst'], cwd=os.getcwd())
137+
138+ def test_init_with_source_branch_and_tag_raises_exception(self):
139+ with self.assertRaises(sources.IncompatibleOptionsError) as raised:
140+ sources.Git('git://mysource', 'source_dir', source_tag='tag', source_branch='branch')
141+
142+ expected_message = 'can\'t specify both source-tag and source-branch for a git source'
143+ self.assertEqual(raised.exception.message, expected_message)
144+
145+
146+class TestMercurial(SourceTestCase):
147+
148+ def test_pull(self):
149+ hg = sources.Mercurial('hg://my-source', 'source_dir')
150+ hg.pull()
151+
152+ self.mock_run.assert_called_once_with(
153+ ['hg', 'clone', 'hg://my-source', 'source_dir'], cwd=os.getcwd())
154+
155+ def test_pull_branch(self):
156+ hg = sources.Mercurial('hg://my-source', 'source_dir', source_branch='my-branch')
157+ hg.pull()
158+
159+ self.mock_run.assert_called_once_with(
160+ ['hg', 'clone', '-u', 'my-branch', 'hg://my-source', 'source_dir'], cwd=os.getcwd())
161+
162+ def test_pull_tag(self):
163+ hg = sources.Mercurial('hg://my-source', 'source_dir', source_tag='tag')
164+ hg.pull()
165+
166+ self.mock_run.assert_called_once_with(
167+ ['hg', 'clone', '-u', 'tag', 'hg://my-source', 'source_dir'], cwd=os.getcwd())
168+
169+ def test_pull_existing(self):
170+ self.mock_path_exists.return_value = True
171+
172+ hg = sources.Mercurial('hg://my-source', 'source_dir')
173+ hg.pull()
174+
175+ self.mock_run.assert_called_once_with(
176+ ['hg', 'pull', 'hg://my-source'], cwd=os.getcwd())
177+
178+ def test_pull_existing_with_tag(self):
179+ self.mock_path_exists.return_value = True
180+
181+ hg = sources.Mercurial('hg://my-source', 'source_dir', source_tag='tag')
182+ hg.pull()
183+
184+ self.mock_run.assert_called_once_with(
185+ ['hg', 'pull', '-r', 'tag', 'hg://my-source'], cwd=os.getcwd())
186+
187+ def test_pull_existing_with_branch(self):
188+ self.mock_path_exists.return_value = True
189+
190+ hg = sources.Mercurial('hg://my-source', 'source_dir', source_branch='my-branch')
191+ hg.pull()
192+
193+ self.mock_run.assert_called_once_with(
194+ ['hg', 'pull', '-b', 'my-branch', 'hg://my-source'], cwd=os.getcwd())
195+
196+ def test_provision(self):
197+ bzr = sources.Mercurial('hg://my-source', 'source_dir')
198+ bzr.provision('dst')
199+
200+ self.mock_run.assert_called_once_with(
201+ ['cp', '-Trfa', 'source_dir', 'dst'], cwd=os.getcwd())
202+
203+ def test_init_with_source_branch_and_tag_raises_exception(self):
204+ with self.assertRaises(sources.IncompatibleOptionsError) as raised:
205+ sources.Mercurial('hg://mysource', 'source_dir', source_tag='tag', source_branch='branch')
206+
207+ expected_message = 'can\'t specify both source-tag and source-branch for a mercurial source'
208+ self.assertEqual(raised.exception.message, expected_message)
209+
210+
211+class TestLocal(SourceTestCase):
212+
213+ def setUp(self):
214+ super().setUp()
215+
216+ patcher = unittest.mock.patch('os.path.isdir')
217+ self.mock_isdir = patcher.start()
218+ self.mock_isdir.return_value = True
219+ self.addCleanup(patcher.stop)
220+
221+ patcher = unittest.mock.patch('os.symlink')
222+ self.mock_symlink = patcher.start()
223+ self.addCleanup(patcher.stop)
224+
225+ patcher = unittest.mock.patch('os.remove')
226+ self.mock_remove = patcher.start()
227+ self.addCleanup(patcher.stop)
228+ self.mock_symlink.return_value = True
229+
230+ patcher = unittest.mock.patch('os.path.abspath')
231+ self.mock_abspath = patcher.start()
232+ self.mock_abspath.return_value = '/home/ubuntu/sources/snap/source'
233+ self.addCleanup(patcher.stop)
234+
235+ def test_pull(self):
236+ local = sources.Local('.', 'source_dir')
237+ self.assertTrue(local.pull())
238+
239+ def test_provision(self):
240+ local = sources.Local('.', 'source_dir')
241+ local.provision('dst')
242+
243+ self.mock_rmdir.assert_called_once_with('dst')
244+ self.mock_symlink.assert_called_once_with('/home/ubuntu/sources/snap/source', 'dst')
245+
246+ def test_provision_when_target_is_file(self):
247+ self.mock_isdir.return_value = False
248+
249+ local = sources.Local('.', 'source_dir')
250+ local.provision('dst')
251+
252+ self.mock_remove.assert_called_once_with('dst')
253+ self.mock_symlink.assert_called_once_with('/home/ubuntu/sources/snap/source', 'dst')

Subscribers

People subscribed via source and target branches