64
52
self.assertEqual(args.project, "git.testing/project")
65
53
self.assertEqual(args.dependencies, None)
66
54
self.assertEqual(args.go_get_all, True)
67
self.assertEqual(args.tsv_path, None)
69
def test_project_with_tsv_path(self):
70
args = git_gate.parse_args(
71
["--project", "git.testing/project",
72
"--project-url", "https://git.testing/project",
73
"--tsv-path", "/a/file.tsv"])
74
self.assertEqual(args.project, "git.testing/project")
75
self.assertEqual(args.project_url, "https://git.testing/project")
76
self.assertEqual(args.dependencies, None)
77
self.assertEqual(args.go_get_all, False)
78
self.assertEqual(args.tsv_path, "/a/file.tsv")
80
def test_error_on_project_url_missing(self):
81
with tests.parse_error(self) as stderr:
82
git_gate.parse_args(["--project", "git.testing/project"])
84
"Must supply either --project-url or --go-get-all",
87
def test_error_go_get_feature_branch(self):
88
with tests.parse_error(self) as stderr:
90
["--project", "git.testing/project",
91
"--project-url", "https://git.testing/project",
92
"--go-get-all", "--feature-branch"])
94
"Cannot use --feature-branch and --go-get-all together",
98
class TestSubcommandError(tests.TestCase):
57
class TestSubcommandError(unittest.TestCase):
100
59
def test_subcommand_error(self):
101
60
proc_error = subprocess.CalledProcessError(1, ["git"])
102
61
err = git_gate.SubcommandError("git", "clone", proc_error)
103
62
self.assertEqual(str(err), "Subprocess git clone failed with code 1")
106
class TestGoTest(tests.TestCase):
108
Tests for go_test function.
110
Class has setup that patches out each operation with relevent side effects,
111
running a command, printing output, or changing directory. Those are
112
recorded in order in the actions list, so each test can supply a set of
113
arguments then just match the actions.
119
super(TestGoTest, self).setUp()
120
# Patch out and record actions run as part of go_test()
122
self.patch_action("git_gate.print_now", lambda s: ("print", s))
123
self.patch_action("git_gate.SubcommandRunner.__call__",
124
lambda self, *args: (self.command,) + args)
125
self.patch_action("os.chdir", lambda d: ("chdir", d))
127
# Verify go commands run with GOPATH overridden
128
real_runner = git_gate.SubcommandRunner
130
def _check(command, environ=None):
131
if command in ("go", "godeps"):
132
self.assertIsInstance(environ, dict)
133
self.assertEquals(environ.get("GOPATH"), "/tmp/fake")
134
return real_runner(command, environ)
136
patcher = mock.patch("git_gate.SubcommandRunner", side_effect=_check)
138
self.addCleanup(patcher.stop)
140
def patch_action(self, target, func):
141
"""Patch target recording each call in actions as wrapped by func."""
142
def _record(*args, **kwargs):
143
self.actions.append(func(*args, **kwargs))
144
patcher = mock.patch(target, _record)
146
self.addCleanup(patcher.stop)
148
args = frozenset("project project_url project_ref feature_branch merge_url"
149
" merge_ref go_get_all dependencies tsv_path".split())
152
def make_args(cls, project, **kwargs):
153
"""Gives args like parse_args with all values defaulted to None."""
154
if not cls.args.issuperset(kwargs):
155
raise ValueError("Invalid arguments given: {!r}".format(kwargs))
156
kwargs["project"] = project
157
return argparse.Namespace(**dict((k, kwargs.get(k)) for k in cls.args))
159
def test_get_test(self):
160
args = self.make_args("git.testing/project", go_get_all=True)
161
git_gate.go_test(args, "/tmp/fake")
162
self.assertEqual(self.actions, [
163
('print', 'Getting git.testing/project and dependencies using go'),
164
('go', 'get', '-v', '-d', '-t', 'git.testing/project/...'),
165
('chdir', '/tmp/fake/src/git.testing/project'),
166
('go', 'build', 'git.testing/project/...'),
167
('go', 'test', 'git.testing/project/...')
170
def test_get_merge_test(self):
171
args = self.make_args("git.testing/project", go_get_all=True,
172
merge_url="https://git.testing/proposed",
174
git_gate.go_test(args, "/tmp/fake")
175
self.assertEqual(self.actions, [
176
('print', 'Getting git.testing/project and dependencies using go'),
177
('go', 'get', '-v', '-d', '-t', 'git.testing/project/...'),
178
('chdir', '/tmp/fake/src/git.testing/project'),
179
('print', 'Merging https://git.testing/proposed ref HEAD'),
180
('git', 'fetch', 'https://git.testing/proposed', 'HEAD'),
181
('git', 'merge', '--no-ff', '-m', 'Merged HEAD', 'FETCH_HEAD'),
182
('print', 'Updating git.testing/project dependencies using go'),
183
('go', 'get', '-v', '-d', '-t', 'git.testing/project/...'),
184
('go', 'build', 'git.testing/project/...'),
185
('go', 'test', 'git.testing/project/...')
188
def test_get_merge_other_test(self):
189
args = self.make_args("git.testing/project", go_get_all=True,
190
project_url="https://git.testing/project",
192
merge_url="https://git.testing/proposed",
194
git_gate.go_test(args, "/tmp/fake")
195
self.assertEqual(self.actions, [
196
('print', 'Cloning git.testing/project from'
197
' https://git.testing/project'),
198
('git', 'clone', 'https://git.testing/project',
199
'/tmp/fake/src/git.testing/project'),
200
('chdir', '/tmp/fake/src/git.testing/project'),
201
('print', 'Switching repository to v1'),
202
('git', 'checkout', 'v1'),
203
('print', 'Merging https://git.testing/proposed ref feature'),
204
('git', 'fetch', 'https://git.testing/proposed', 'feature'),
205
('git', 'merge', '--no-ff', '-m', 'Merged feature', 'FETCH_HEAD'),
206
('print', 'Updating git.testing/project dependencies using go'),
207
('go', 'get', '-v', '-d', '-t', 'git.testing/project/...'),
208
('go', 'build', 'git.testing/project/...'),
209
('go', 'test', 'git.testing/project/...')
212
def test_deps_test(self):
213
args = self.make_args("git.testing/project",
214
project_url="https://git.testing/project",
215
dependencies=["git.testing/a", "git.testing/b"])
216
git_gate.go_test(args, "/tmp/fake")
217
self.assertEqual(self.actions, [
218
('print', 'Cloning git.testing/project from'
219
' https://git.testing/project'),
220
('git', 'clone', 'https://git.testing/project',
221
'/tmp/fake/src/git.testing/project'),
222
('chdir', '/tmp/fake/src/git.testing/project'),
223
('print', 'Getting git.testing/a and dependencies using go'),
224
('go', 'get', '-v', '-d', 'git.testing/a'),
225
('print', 'Getting git.testing/b and dependencies using go'),
226
('go', 'get', '-v', '-d', 'git.testing/b'),
227
('go', 'build', 'git.testing/project/...'),
228
('go', 'test', 'git.testing/project/...')
231
def test_tsv_test(self):
232
args = self.make_args("git.testing/project",
233
project_url="https://git.testing/project",
234
tsv_path="dependencies.tsv")
235
git_gate.go_test(args, "/tmp/fake")
236
self.assertEqual(self.actions, [
237
('print', 'Getting and installing godeps'),
238
('go', 'get', '-v', '-d', 'github.com/rogpeppe/godeps/...'),
239
('go', 'install', 'github.com/rogpeppe/godeps/...'),
240
('print', 'Cloning git.testing/project from'
241
' https://git.testing/project'),
242
('git', 'clone', 'https://git.testing/project',
243
'/tmp/fake/src/git.testing/project'),
244
('chdir', '/tmp/fake/src/git.testing/project'),
245
('print', 'Getting dependencies using godeps from'
246
' /tmp/fake/src/git.testing/project/dependencies.tsv'),
247
('/tmp/fake/bin/godeps', '-u',
248
'/tmp/fake/src/git.testing/project/dependencies.tsv'),
249
('go', 'build', 'git.testing/project/...'),
250
('go', 'test', 'git.testing/project/...')
253
def test_feature_branch(self):
254
args = self.make_args("vgo.testing/project.v2.feature",
255
project_url="https://git.testing/project",
256
project_ref="v2.feature",
257
feature_branch=True, dependencies=[])
258
git_gate.go_test(args, "/tmp/fake")
259
self.assertEqual(self.actions, [
260
('print', 'Cloning vgo.testing/project.v2 from'
261
' https://git.testing/project'),
262
('git', 'clone', 'https://git.testing/project',
263
'/tmp/fake/src/vgo.testing/project.v2'),
264
('chdir', '/tmp/fake/src/vgo.testing/project.v2'),
265
('print', 'Switching repository to v2.feature'),
266
('git', 'checkout', 'v2.feature'),
267
('go', 'build', 'vgo.testing/project.v2/...'),
268
('go', 'test', 'vgo.testing/project.v2/...')
271
def test_no_magic_dots(self):
272
args = self.make_args("vgo.testing/project.dot.love",
273
project_url="https://git.testing/project",
274
project_ref="dot.love", dependencies=[])
275
git_gate.go_test(args, "/tmp/fake")
276
self.assertEqual(self.actions, [
277
('print', 'Cloning vgo.testing/project.dot.love from'
278
' https://git.testing/project'),
279
('git', 'clone', 'https://git.testing/project',
280
'/tmp/fake/src/vgo.testing/project.dot.love'),
281
('chdir', '/tmp/fake/src/vgo.testing/project.dot.love'),
282
('print', 'Switching repository to dot.love'),
283
('git', 'checkout', 'dot.love'),
284
('go', 'build', 'vgo.testing/project.dot.love/...'),
285
('go', 'test', 'vgo.testing/project.dot.love/...')
289
class TestFromFeatureDir(tests.TestCase):
290
"""Tests for from_feature_dir function."""
292
def test_boring(self):
293
directory = git_gate.from_feature_dir("github.com/juju/juju")
294
self.assertEqual(directory, "github.com/juju/juju")
296
def test_gopkg(self):
297
directory = git_gate.from_feature_dir("gopkg.in/juju/charm.v6")
298
self.assertEqual(directory, "gopkg.in/juju/charm.v6")
300
def test_gopkg_feature(self):
301
directory = git_gate.from_feature_dir("gopkg.in/juju/charm.v6.minver")
302
self.assertEqual(directory, "gopkg.in/juju/charm.v6")