summaryrefslogtreecommitdiffstats
path: root/testing/mozharness/test/test_base_vcs_mercurial.py
blob: f00e0b586c2f50a4afd5d99d2bd578f94d5f6db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os
import platform
import shutil
import tempfile
import unittest

import mozharness.base.vcs.mercurial as mercurial

test_string = """foo
bar
baz"""

HG = ["hg"] + mercurial.HG_OPTIONS

# Known default .hgrc
os.environ["HGRCPATH"] = os.path.abspath(
    os.path.join(os.path.dirname(__file__), "helper_files", ".hgrc")
)


def cleanup():
    if os.path.exists("test_logs"):
        shutil.rmtree("test_logs")
    if os.path.exists("test_dir"):
        if os.path.isdir("test_dir"):
            shutil.rmtree("test_dir")
        else:
            os.remove("test_dir")
    for filename in ("localconfig.json", "localconfig.json.bak"):
        if os.path.exists(filename):
            os.remove(filename)


def get_mercurial_vcs_obj():
    m = mercurial.MercurialVCS()
    m.config = {}
    return m


def get_revisions(dest):
    m = get_mercurial_vcs_obj()
    retval = []
    command = HG + ["log", "-R", dest, "--template", "{node}\n"]
    for rev in m.get_output_from_command(command).split("\n"):
        rev = rev.strip()
        if not rev:
            continue
        retval.append(rev)
    return retval


class TestMakeAbsolute(unittest.TestCase):
    # _make_absolute() doesn't play nicely with windows/msys paths.
    # TODO: fix _make_absolute, write it out of the picture, or determine
    # that it's not needed on windows.
    if platform.system() not in ("Windows",):

        def test_absolute_path(self):
            m = get_mercurial_vcs_obj()
            self.assertEqual(m._make_absolute("/foo/bar"), "/foo/bar")

        def test_relative_path(self):
            m = get_mercurial_vcs_obj()
            self.assertEqual(m._make_absolute("foo/bar"), os.path.abspath("foo/bar"))

        def test_HTTP_paths(self):
            m = get_mercurial_vcs_obj()
            self.assertEqual(m._make_absolute("http://foo/bar"), "http://foo/bar")

        def test_absolute_file_path(self):
            m = get_mercurial_vcs_obj()
            self.assertEqual(m._make_absolute("file:///foo/bar"), "file:///foo/bar")

        def test_relative_file_path(self):
            m = get_mercurial_vcs_obj()
            self.assertEqual(
                m._make_absolute("file://foo/bar"), "file://%s/foo/bar" % os.getcwd()
            )


class TestHg(unittest.TestCase):
    def _init_hg_repo(self, hg_obj, repodir):
        hg_obj.run_command(
            [
                "bash",
                os.path.join(
                    os.path.dirname(__file__), "helper_files", "init_hgrepo.sh"
                ),
                repodir,
            ]
        )

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()
        self.repodir = os.path.join(self.tmpdir, "repo")
        m = get_mercurial_vcs_obj()
        self._init_hg_repo(m, self.repodir)
        self.revisions = get_revisions(self.repodir)
        self.wc = os.path.join(self.tmpdir, "wc")
        self.pwd = os.getcwd()

    def tearDown(self):
        shutil.rmtree(self.tmpdir)
        os.chdir(self.pwd)

    def test_get_branch(self):
        m = get_mercurial_vcs_obj()
        m.clone(self.repodir, self.wc)
        b = m.get_branch_from_path(self.wc)
        self.assertEqual(b, "default")

    def test_get_branches(self):
        m = get_mercurial_vcs_obj()
        m.clone(self.repodir, self.wc)
        branches = m.get_branches_from_path(self.wc)
        self.assertEqual(sorted(branches), sorted(["branch2", "default"]))

    def test_clone(self):
        m = get_mercurial_vcs_obj()
        rev = m.clone(self.repodir, self.wc, update_dest=False)
        self.assertEqual(rev, None)
        self.assertEqual(self.revisions, get_revisions(self.wc))
        self.assertEqual(sorted(os.listdir(self.wc)), [".hg"])

    def test_clone_into_non_empty_dir(self):
        m = get_mercurial_vcs_obj()
        m.mkdir_p(self.wc)
        open(os.path.join(self.wc, "test.txt"), "w").write("hello")
        m.clone(self.repodir, self.wc, update_dest=False)
        self.assertTrue(not os.path.exists(os.path.join(self.wc, "test.txt")))

    def test_clone_update(self):
        m = get_mercurial_vcs_obj()
        rev = m.clone(self.repodir, self.wc, update_dest=True)
        self.assertEqual(rev, self.revisions[0])

    def test_clone_branch(self):
        m = get_mercurial_vcs_obj()
        m.clone(self.repodir, self.wc, branch="branch2", update_dest=False)
        # On hg 1.6, we should only have a subset of the revisions
        if m.hg_ver() >= (1, 6, 0):
            self.assertEqual(self.revisions[1:], get_revisions(self.wc))
        else:
            self.assertEqual(self.revisions, get_revisions(self.wc))

    def test_clone_update_branch(self):
        m = get_mercurial_vcs_obj()
        rev = m.clone(
            self.repodir,
            os.path.join(self.tmpdir, "wc"),
            branch="branch2",
            update_dest=True,
        )
        self.assertEqual(rev, self.revisions[1], self.revisions)

    def test_clone_revision(self):
        m = get_mercurial_vcs_obj()
        m.clone(self.repodir, self.wc, revision=self.revisions[0], update_dest=False)
        # We'll only get a subset of the revisions
        self.assertEqual(
            self.revisions[:1] + self.revisions[2:], get_revisions(self.wc)
        )

    def test_update_revision(self):
        m = get_mercurial_vcs_obj()
        rev = m.clone(self.repodir, self.wc, update_dest=False)
        self.assertEqual(rev, None)

        rev = m.update(self.wc, revision=self.revisions[1])
        self.assertEqual(rev, self.revisions[1])

    def test_pull(self):
        m = get_mercurial_vcs_obj()
        # Clone just the first rev
        m.clone(self.repodir, self.wc, revision=self.revisions[-1], update_dest=False)
        self.assertEqual(get_revisions(self.wc), self.revisions[-1:])

        # Now pull in new changes
        rev = m.pull(self.repodir, self.wc, update_dest=False)
        self.assertEqual(rev, None)
        self.assertEqual(get_revisions(self.wc), self.revisions)

    def test_pull_revision(self):
        m = get_mercurial_vcs_obj()
        # Clone just the first rev
        m.clone(self.repodir, self.wc, revision=self.revisions[-1], update_dest=False)
        self.assertEqual(get_revisions(self.wc), self.revisions[-1:])

        # Now pull in just the last revision
        rev = m.pull(
            self.repodir, self.wc, revision=self.revisions[0], update_dest=False
        )
        self.assertEqual(rev, None)

        # We'll be missing the middle revision (on another branch)
        self.assertEqual(
            get_revisions(self.wc), self.revisions[:1] + self.revisions[2:]
        )

    def test_pull_branch(self):
        m = get_mercurial_vcs_obj()
        # Clone just the first rev
        m.clone(self.repodir, self.wc, revision=self.revisions[-1], update_dest=False)
        self.assertEqual(get_revisions(self.wc), self.revisions[-1:])

        # Now pull in the other branch
        rev = m.pull(self.repodir, self.wc, branch="branch2", update_dest=False)
        self.assertEqual(rev, None)

        # On hg 1.6, we'll be missing the last revision (on another branch)
        if m.hg_ver() >= (1, 6, 0):
            self.assertEqual(get_revisions(self.wc), self.revisions[1:])
        else:
            self.assertEqual(get_revisions(self.wc), self.revisions)

    def test_pull_unrelated(self):
        m = get_mercurial_vcs_obj()
        # Create a new repo
        repo2 = os.path.join(self.tmpdir, "repo2")
        self._init_hg_repo(m, repo2)

        self.assertNotEqual(self.revisions, get_revisions(repo2))

        # Clone the original repo
        m.clone(self.repodir, self.wc, update_dest=False)
        # Hide the wanted error
        m.config = {"log_to_console": False}
        # Try and pull in changes from the new repo
        self.assertRaises(
            mercurial.VCSException, m.pull, repo2, self.wc, update_dest=False
        )

    def test_push(self):
        m = get_mercurial_vcs_obj()
        m.clone(self.repodir, self.wc, revision=self.revisions[-2])
        m.push(src=self.repodir, remote=self.wc)
        self.assertEqual(get_revisions(self.wc), self.revisions)

    def test_push_with_branch(self):
        m = get_mercurial_vcs_obj()
        if m.hg_ver() >= (1, 6, 0):
            m.clone(self.repodir, self.wc, revision=self.revisions[-1])
            m.push(src=self.repodir, remote=self.wc, branch="branch2")
            m.push(src=self.repodir, remote=self.wc, branch="default")
            self.assertEqual(get_revisions(self.wc), self.revisions)

    def test_push_with_revision(self):
        m = get_mercurial_vcs_obj()
        m.clone(self.repodir, self.wc, revision=self.revisions[-2])
        m.push(src=self.repodir, remote=self.wc, revision=self.revisions[-1])
        self.assertEqual(get_revisions(self.wc), self.revisions[-2:])

    def test_mercurial(self):
        m = get_mercurial_vcs_obj()
        m.vcs_config = {
            "repo": self.repodir,
            "dest": self.wc,
            "vcs_share_base": os.path.join(self.tmpdir, "share"),
        }
        m.ensure_repo_and_revision()
        rev = m.ensure_repo_and_revision()
        self.assertEqual(rev, self.revisions[0])

    def test_push_new_branches_not_allowed(self):
        m = get_mercurial_vcs_obj()
        m.clone(self.repodir, self.wc, revision=self.revisions[0])
        # Hide the wanted error
        m.config = {"log_to_console": False}
        self.assertRaises(
            Exception, m.push, self.repodir, self.wc, push_new_branches=False
        )

    def test_mercurial_relative_dir(self):
        m = get_mercurial_vcs_obj()
        repo = os.path.basename(self.repodir)
        wc = os.path.basename(self.wc)
        m.vcs_config = {
            "repo": repo,
            "dest": wc,
            "revision": self.revisions[-1],
            "vcs_share_base": os.path.join(self.tmpdir, "share"),
        }
        m.chdir(os.path.dirname(self.repodir))
        try:
            rev = m.ensure_repo_and_revision()
            self.assertEqual(rev, self.revisions[-1])
            m.info("Creating test.txt")
            open(os.path.join(self.wc, "test.txt"), "w").write("hello!")

            m = get_mercurial_vcs_obj()
            m.vcs_config = {
                "repo": repo,
                "dest": wc,
                "revision": self.revisions[0],
                "vcs_share_base": os.path.join(self.tmpdir, "share"),
            }
            rev = m.ensure_repo_and_revision()
            self.assertEqual(rev, self.revisions[0])
            # Make sure our local file didn't go away
            self.assertTrue(os.path.exists(os.path.join(self.wc, "test.txt")))
        finally:
            m.chdir(self.pwd)

    def test_mercurial_update_tip(self):
        m = get_mercurial_vcs_obj()
        m.vcs_config = {
            "repo": self.repodir,
            "dest": self.wc,
            "revision": self.revisions[-1],
            "vcs_share_base": os.path.join(self.tmpdir, "share"),
        }
        rev = m.ensure_repo_and_revision()
        self.assertEqual(rev, self.revisions[-1])
        open(os.path.join(self.wc, "test.txt"), "w").write("hello!")

        m = get_mercurial_vcs_obj()
        m.vcs_config = {
            "repo": self.repodir,
            "dest": self.wc,
            "vcs_share_base": os.path.join(self.tmpdir, "share"),
        }
        rev = m.ensure_repo_and_revision()
        self.assertEqual(rev, self.revisions[0])
        # Make sure our local file didn't go away
        self.assertTrue(os.path.exists(os.path.join(self.wc, "test.txt")))

    def test_mercurial_update_rev(self):
        m = get_mercurial_vcs_obj()
        m.vcs_config = {
            "repo": self.repodir,
            "dest": self.wc,
            "revision": self.revisions[-1],
            "vcs_share_base": os.path.join(self.tmpdir, "share"),
        }
        rev = m.ensure_repo_and_revision()
        self.assertEqual(rev, self.revisions[-1])
        open(os.path.join(self.wc, "test.txt"), "w").write("hello!")

        m = get_mercurial_vcs_obj()
        m.vcs_config = {
            "repo": self.repodir,
            "dest": self.wc,
            "revision": self.revisions[0],
            "vcs_share_base": os.path.join(self.tmpdir, "share"),
        }
        rev = m.ensure_repo_and_revision()
        self.assertEqual(rev, self.revisions[0])
        # Make sure our local file didn't go away
        self.assertTrue(os.path.exists(os.path.join(self.wc, "test.txt")))

    def test_make_hg_url(self):
        # construct an hg url specific to revision, branch and filename and try to pull it down
        file_url = mercurial.make_hg_url(
            "hg.mozilla.org",
            "//build/tools/",
            revision="FIREFOX_3_6_12_RELEASE",
            filename="/lib/python/util/hg.py",
            protocol="https",
        )
        expected_url = (
            "https://hg.mozilla.org/build/tools/raw-file/"
            "FIREFOX_3_6_12_RELEASE/lib/python/util/hg.py"
        )
        self.assertEqual(file_url, expected_url)

    def test_make_hg_url_no_filename(self):
        file_url = mercurial.make_hg_url(
            "hg.mozilla.org",
            "/build/tools",
            revision="default",
            protocol="https",
        )
        expected_url = "https://hg.mozilla.org/build/tools/rev/default"
        self.assertEqual(file_url, expected_url)

    def test_make_hg_url_no_revision_no_filename(self):
        repo_url = mercurial.make_hg_url(
            "hg.mozilla.org",
            "/build/tools",
            protocol="https",
        )
        expected_url = "https://hg.mozilla.org/build/tools"
        self.assertEqual(repo_url, expected_url)

    def test_make_hg_url_different_protocol(self):
        repo_url = mercurial.make_hg_url(
            "hg.mozilla.org",
            "/build/tools",
            protocol="ssh",
        )
        expected_url = "ssh://hg.mozilla.org/build/tools"
        self.assertEqual(repo_url, expected_url)


if __name__ == "__main__":
    unittest.main()