~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_git_gate.py

  • Committer: Curtis Hovey
  • Date: 2014-08-01 12:44:38 UTC
  • Revision ID: curtis@canonical.com-20140801124438-l48516pldkzh7g5n
Do not show all the files in the tarball because it distracts from the test output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import argparse
2
 
import mock
3
 
import subprocess
4
 
 
5
 
import git_gate
6
 
import tests
7
 
 
8
 
 
9
 
class TestParseArgs(tests.TestCase):
10
 
 
11
 
    def test_project_and_url(self):
12
 
        args = git_gate.parse_args(
13
 
            ["--project", "git.testing/project",
14
 
             "--project-url", "https://git.testing/project"])
15
 
        self.assertEqual(args.project, "git.testing/project")
16
 
        self.assertEqual(args.project_url, "https://git.testing/project")
17
 
        self.assertEqual(args.keep, False)
18
 
 
19
 
    def test_keep(self):
20
 
        args = git_gate.parse_args(
21
 
            ["--project", "git.testing/project",
22
 
             "--project-url", "https://git.testing/project", "--keep"])
23
 
        self.assertEqual(args.project, "git.testing/project")
24
 
        self.assertEqual(args.keep, True)
25
 
 
26
 
    def test_project_and_ref(self):
27
 
        args = git_gate.parse_args(
28
 
            ["--project", "git.testing/project", "--go-get-all",
29
 
             "--project-ref", "v1"])
30
 
        self.assertEqual(args.project, "git.testing/project")
31
 
        self.assertEqual(args.project_ref, "v1")
32
 
 
33
 
    def test_merging_other(self):
34
 
        args = git_gate.parse_args(
35
 
            ["--project", "git.testing/project", "--go-get-all",
36
 
             "--merge-url", "https://git.testing/proposed"])
37
 
        self.assertEqual(args.project, "git.testing/project")
38
 
        self.assertEqual(args.merge_url, "https://git.testing/proposed")
39
 
        self.assertEqual(args.merge_ref, "HEAD")
40
 
 
41
 
    def test_merging_other_ref(self):
42
 
        args = git_gate.parse_args(
43
 
            ["--project", "git.testing/project", "--go-get-all",
44
 
             "--merge-url", "https://git.testing/proposed",
45
 
             "--merge-ref", "feature"])
46
 
        self.assertEqual(args.project, "git.testing/project")
47
 
        self.assertEqual(args.merge_url, "https://git.testing/proposed")
48
 
        self.assertEqual(args.merge_ref, "feature")
49
 
 
50
 
    def test_project_with_deps(self):
51
 
        args = git_gate.parse_args(
52
 
            ["--project", "git.testing/project",
53
 
             "--project-url", "https://git.testing/project",
54
 
             "--dependencies", "git.testing/a", "git.testing/b"])
55
 
        self.assertEqual(args.project, "git.testing/project")
56
 
        self.assertEqual(args.project_url, "https://git.testing/project")
57
 
        self.assertEqual(args.dependencies, ["git.testing/a", "git.testing/b"])
58
 
        self.assertEqual(args.go_get_all, False)
59
 
        self.assertEqual(args.tsv_path, None)
60
 
 
61
 
    def test_project_with_go_deps(self):
62
 
        args = git_gate.parse_args(
63
 
            ["--project", "git.testing/project", "--go-get-all"])
64
 
        self.assertEqual(args.project, "git.testing/project")
65
 
        self.assertEqual(args.dependencies, None)
66
 
        self.assertEqual(args.go_get_all, True)
67
 
        self.assertEqual(args.tsv_path, None)
68
 
 
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")
79
 
 
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"])
83
 
        self.assertIn(
84
 
            "Must supply either --project-url or --go-get-all",
85
 
            stderr.getvalue())
86
 
 
87
 
    def test_error_go_get_feature_branch(self):
88
 
        with tests.parse_error(self) as stderr:
89
 
            git_gate.parse_args(
90
 
                ["--project", "git.testing/project",
91
 
                 "--project-url", "https://git.testing/project",
92
 
                 "--go-get-all", "--feature-branch"])
93
 
        self.assertIn(
94
 
            "Cannot use --feature-branch and --go-get-all together",
95
 
            stderr.getvalue())
96
 
 
97
 
 
98
 
class TestSubcommandError(tests.TestCase):
99
 
 
100
 
    def test_subcommand_error(self):
101
 
        proc_error = subprocess.CalledProcessError(1, ["git"])
102
 
        err = git_gate.SubcommandError("git", "clone", proc_error)
103
 
        self.assertEqual(str(err), "Subprocess git clone failed with code 1")
104
 
 
105
 
 
106
 
class TestGoTest(tests.TestCase):
107
 
    """
108
 
    Tests for go_test function.
109
 
 
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.
114
 
    """
115
 
 
116
 
    maxDiff = None
117
 
 
118
 
    def setUp(self):
119
 
        super(TestGoTest, self).setUp()
120
 
        # Patch out and record actions run as part of go_test()
121
 
        self.actions = []
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))
126
 
 
127
 
        # Verify go commands run with GOPATH overridden
128
 
        real_runner = git_gate.SubcommandRunner
129
 
 
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)
135
 
 
136
 
        patcher = mock.patch("git_gate.SubcommandRunner", side_effect=_check)
137
 
        patcher.start()
138
 
        self.addCleanup(patcher.stop)
139
 
 
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)
145
 
        patcher.start()
146
 
        self.addCleanup(patcher.stop)
147
 
 
148
 
    args = frozenset("project project_url project_ref feature_branch merge_url"
149
 
                     " merge_ref go_get_all dependencies tsv_path".split())
150
 
 
151
 
    @classmethod
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))
158
 
 
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/...')
168
 
        ])
169
 
 
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",
173
 
                              merge_ref="HEAD")
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/...')
186
 
        ])
187
 
 
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",
191
 
                              project_ref="v1",
192
 
                              merge_url="https://git.testing/proposed",
193
 
                              merge_ref="feature")
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/...')
210
 
        ])
211
 
 
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/...')
229
 
        ])
230
 
 
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', 'launchpad.net/godeps/...'),
239
 
            ('go', 'install', 'launchpad.net/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/...')
251
 
        ])
252
 
 
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/...')
269
 
        ])
270
 
 
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/...')
286
 
        ])
287
 
 
288
 
 
289
 
class TestFromFeatureDir(tests.TestCase):
290
 
    """Tests for from_feature_dir function."""
291
 
 
292
 
    def test_boring(self):
293
 
        directory = git_gate.from_feature_dir("github.com/juju/juju")
294
 
        self.assertEqual(directory, "github.com/juju/juju")
295
 
 
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")
299
 
 
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")