Codebase list ros-vcstools / upstream/0.1.42 test / test_hg.py
upstream/0.1.42

Tree @upstream/0.1.42 (Download .tar.gz)

test_hg.py @upstream/0.1.42raw · history · blame

  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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import absolute_import, print_function, unicode_literals

import os
import io
import unittest
import subprocess
import tempfile
import shutil

from vcstools.hg import HgClient


os.environ['EMAIL'] = 'Your Name <name@example.com>'


class HGClientTestSetups(unittest.TestCase):

    @classmethod
    def setUpClass(self):

        self.root_directory = tempfile.mkdtemp()
        self.directories = dict(setUp=self.root_directory)
        self.remote_path = os.path.join(self.root_directory, "remote")
        os.makedirs(self.remote_path)

        # create a "remote" repo
        for cmd in [
                "hg init",
                "touch fixed.txt",
                "hg add fixed.txt",
                "hg commit -m initial"]:
            subprocess.check_call(cmd, shell=True, cwd=self.remote_path)

        po = subprocess.Popen("hg log --template '{node|short}' -l1", shell=True, cwd=self.remote_path, stdout=subprocess.PIPE)
        self.local_version_init = po.stdout.read().decode('UTF-8').rstrip("'").lstrip("'")
        # in hg, tagging creates an own changeset, so we need to fetch version before tagging
        subprocess.check_call("hg tag test_tag", shell=True, cwd=self.remote_path)

        # files to be modified in "local" repo
        for cmd in [
                "touch modified.txt",
                "touch modified-fs.txt",
                "hg add modified.txt modified-fs.txt",
                "hg commit -m initial"]:
            subprocess.check_call(cmd, shell=True, cwd=self.remote_path)

        po = subprocess.Popen("hg log --template '{node|short}' -l1", shell=True, cwd=self.remote_path, stdout=subprocess.PIPE)
        self.local_version_second = po.stdout.read().decode('UTF-8').rstrip("'").lstrip("'")

        for cmd in [
                "touch deleted.txt",
                "touch deleted-fs.txt",
                "hg add deleted.txt deleted-fs.txt",
                "hg commit -m modified"]:
            subprocess.check_call(cmd, shell=True, cwd=self.remote_path)

        po = subprocess.Popen("hg log --template '{node|short}' -l1", shell=True, cwd=self.remote_path, stdout=subprocess.PIPE)
        self.local_version = po.stdout.read().decode('UTF-8').rstrip("'").lstrip("'")

        self.local_path = os.path.join(self.root_directory, "local")
        self.local_url = self.remote_path

        # create a hg branch
        for cmd in [
                "hg branch test_branch",
                "touch test.txt",
                "hg add test.txt",
                "hg commit -m test"]:
            subprocess.check_call(cmd, shell=True, cwd=self.remote_path)

        po = subprocess.Popen("hg log --template '{node|short}' -l1", shell=True, cwd=self.remote_path, stdout=subprocess.PIPE)
        self.branch_version = po.stdout.read().decode('UTF-8').rstrip("'").lstrip("'")


    @classmethod
    def tearDownClass(self):
        for d in self.directories:
            shutil.rmtree(self.directories[d])

    def tearDown(self):
        if os.path.exists(self.local_path):
            shutil.rmtree(self.local_path)


class HGClientTest(HGClientTestSetups):

    def test_get_url_by_reading(self):
        url = self.local_url
        client = HgClient(self.local_path)
        client.checkout(url, self.local_version)
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(client.get_url(), self.local_url)
        self.assertEqual(client.get_version(), self.local_version)
        self.assertEqual(client.get_version(self.local_version_init[0:6]), self.local_version_init)
        self.assertEqual(client.get_version("test_tag"), self.local_version_init)

    def test_get_url_nonexistant(self):
        local_path = "/tmp/dummy"
        client = HgClient(local_path)
        self.assertEqual(client.get_url(), None)

    def test_get_type_name(self):
        local_path = "/tmp/dummy"
        client = HgClient(local_path)
        self.assertEqual(client.get_vcs_type_name(), 'hg')

    def test_checkout(self):
        url = self.local_url
        client = HgClient(self.local_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url))
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(client.get_path(), self.local_path)
        self.assertEqual(client.get_url(), url)
        self.assertEqual(client.get_version(), self.local_version)

    def test_checkout_dir_exists(self):
        url = self.remote_path
        client = HgClient(self.local_path)
        self.assertFalse(client.path_exists())
        os.makedirs(self.local_path)
        self.assertTrue(client.checkout(url))
        # non-empty
        self.assertFalse(client.checkout(url))

    def test_checkout_emptystringversion(self):
        # special test to check that version '' means the same as None
        url = self.local_url
        client = HgClient(self.local_path)
        self.assertTrue(client.checkout(url, ''))
        self.assertEqual(client.get_version(), self.local_version)

    # test for #3497
    def test_checkout_into_subdir_without_existing_parent(self):
        local_path = os.path.join(self.local_path, "nonexistant_subdir")
        url = self.local_url
        client = HgClient(local_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url))
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(client.get_path(), local_path)
        self.assertEqual(client.get_url(), url)

    def test_checkout_specific_version_and_update(self):
        url = self.local_url
        version = self.local_version
        client = HgClient(self.local_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url, version))
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(client.get_path(), self.local_path)
        self.assertEqual(client.get_url(), url)
        self.assertEqual(client.get_version(), version)

        new_version = self.local_version_second
        self.assertTrue(client.update(new_version))
        self.assertEqual(client.get_version(), new_version)

        self.assertTrue(client.update())
        self.assertEqual(client.get_version(), self.local_version)

        self.assertTrue(client.update(new_version))
        self.assertEqual(client.get_version(), new_version)

        self.assertTrue(client.update(''))
        self.assertEqual(client.get_version(), self.local_version)

    def test_get_current_version_label(self):
        url = self.local_url
        version = self.local_version
        client = HgClient(self.local_path)
        client.checkout(url, version='test_tag')
        self.assertEqual(client.get_current_version_label(), 'default')
        client.update(version='default')
        self.assertEqual(client.get_current_version_label(), 'default')
        client.update(version='test_branch')
        self.assertEqual(client.get_current_version_label(), 'test_branch')


    def test_get_remote_version(self):
        url = self.local_url
        version = self.local_version
        client = HgClient(self.local_path)
        client.checkout(url)
        self.assertEqual(client.get_remote_version(fetch=True), self.local_version)
        client.checkout(url, version='test_tag')
        self.assertEqual(client.get_remote_version(fetch=True), self.local_version)
        client.update(version='default')
        self.assertEqual(client.get_remote_version(fetch=True), self.local_version)
        client.update(version='test_branch')
        self.assertEqual(client.get_remote_version(fetch=True), self.branch_version)

    def testDiffClean(self):
        client = HgClient(self.remote_path)
        self.assertEquals('', client.get_diff())

    def testStatusClean(self):
        client = HgClient(self.remote_path)
        self.assertEquals('', client.get_status())

    def test_get_environment_metadata(self):
        # Verify that metadata is generated
        directory = tempfile.mkdtemp()
        self.directories['local'] = directory
        local_path = os.path.join(directory, "local")
        client = HgClient(local_path)
        self.assertTrue('version' in client.get_environment_metadata())


class HGClientLogTest(HGClientTestSetups):

    @classmethod
    def setUpClass(self):
        HGClientTestSetups.setUpClass()
        client = HgClient(self.local_path)
        client.checkout(self.local_url)

    def test_get_log_defaults(self):
        client = HgClient(self.local_path)
        client.checkout(self.local_url)
        log = client.get_log()
        self.assertEquals(4, len(log))
        self.assertEquals('modified', log[0]['message'])
        for key in ['id', 'author', 'email', 'date', 'message']:
            self.assertTrue(log[0][key] is not None, key)

    def test_get_log_limit(self):
        client = HgClient(self.local_path)
        client.checkout(self.local_url)
        log = client.get_log(limit=1)
        self.assertEquals(1, len(log))
        self.assertEquals('modified', log[0]['message'])

    def test_get_log_path(self):
        client = HgClient(self.local_path)
        client.checkout(self.local_url)
        log = client.get_log(relpath='fixed.txt')
        self.assertEquals('initial', log[0]['message'])


class HGAffectedFilesTest(HGClientTestSetups):

    @classmethod
    def setUpClass(self):
        HGClientTestSetups.setUpClass()
        client = HgClient(self.local_path)
        client.checkout(self.local_url)

    def test_get_log_defaults(self):
        client = HgClient(self.local_path)
        client.checkout(self.local_url)
        log = client.get_log(limit=1)[0]
        affected = client.get_affected_files(log['id'])

        self.assertEqual(sorted(['deleted-fs.txt', 'deleted.txt']),
                         sorted(affected))



class HGDiffStatClientTest(HGClientTestSetups):

    @classmethod
    def setUpClass(self):
        HGClientTestSetups.setUpClass()
        url = self.local_url
        client = HgClient(self.local_path)
        client.checkout(url)
        # after setting up "local" repo, change files and make some changes
        subprocess.check_call("rm deleted-fs.txt", shell=True, cwd=self.local_path)
        subprocess.check_call("hg rm deleted.txt", shell=True, cwd=self.local_path)
        f = io.open(os.path.join(self.local_path, "modified.txt"), 'a')
        f.write('0123456789abcdef')
        f.close()
        f = io.open(os.path.join(self.local_path, "modified-fs.txt"), 'a')
        f.write('0123456789abcdef')
        f.close()
        f = io.open(os.path.join(self.local_path, "added-fs.txt"), 'w')
        f.write('0123456789abcdef')
        f.close()
        f = io.open(os.path.join(self.local_path, "added.txt"), 'w')
        f.write('0123456789abcdef')
        f.close()
        subprocess.check_call("hg add added.txt", shell=True, cwd=self.local_path)

    def tearDown(self):
        pass

    def test_diff(self):

        client = HgClient(self.local_path)
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEquals('diff --git ./added.txt ./added.txt\nnew file mode 100644\n--- /dev/null\n+++ ./added.txt\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\ndiff --git ./deleted.txt ./deleted.txt\ndeleted file mode 100644\ndiff --git ./modified-fs.txt ./modified-fs.txt\n--- ./modified-fs.txt\n+++ ./modified-fs.txt\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\ndiff --git ./modified.txt ./modified.txt\n--- ./modified.txt\n+++ ./modified.txt\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file', client.get_diff())

    def test_diff_relpath(self):

        client = HgClient(self.local_path)
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())

        self.assertEquals('diff --git local/added.txt local/added.txt\nnew file mode 100644\n--- /dev/null\n+++ local/added.txt\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\ndiff --git local/deleted.txt local/deleted.txt\ndeleted file mode 100644\ndiff --git local/modified-fs.txt local/modified-fs.txt\n--- local/modified-fs.txt\n+++ local/modified-fs.txt\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\ndiff --git local/modified.txt local/modified.txt\n--- local/modified.txt\n+++ local/modified.txt\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file', client.get_diff(basepath=os.path.dirname(self.local_path)))

    def test_get_version_modified(self):
        client = HgClient(self.local_path)
        self.assertFalse(client.get_version().endswith('+'))

    def test_status(self):
        client = HgClient(self.local_path)
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEquals('M modified-fs.txt\nM modified.txt\nA added.txt\nR deleted.txt\n! deleted-fs.txt\n', client.get_status())

    def test_status_relpath(self):
        client = HgClient(self.local_path)
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEquals('M local/modified-fs.txt\nM local/modified.txt\nA local/added.txt\nR local/deleted.txt\n! local/deleted-fs.txt\n', client.get_status(basepath=os.path.dirname(self.local_path)))

    def testStatusUntracked(self):
        client = HgClient(self.local_path)
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEquals('M modified-fs.txt\nM modified.txt\nA added.txt\nR deleted.txt\n! deleted-fs.txt\n? added-fs.txt\n', client.get_status(untracked=True))

    def test_hg_diff_path_change_None(self):
        from vcstools.hg import _hg_diff_path_change
        self.assertEqual(_hg_diff_path_change(None, '/tmp/dummy'), None)


class HGRemoteFetchTest(HGClientTestSetups):

    def test_get_remote_version(self):
        url = self.local_url
        version = self.local_version
        client = HgClient(self.local_path)
        client.checkout(url, version='default')
        self.assertEqual(client.get_remote_version(fetch=True), self.local_version)
        self.assertEqual(client.get_version(), self.local_version)

        for cmd in [
                "hg checkout default",
                "touch remote_new.txt",
                "hg add remote_new.txt",
                "hg commit -m remote_new"]:
            subprocess.check_call(cmd, shell=True, cwd=self.remote_path)
        po = subprocess.Popen("hg log --template '{node|short}' -l1", shell=True, cwd=self.remote_path, stdout=subprocess.PIPE)
        remote_new_version = po.stdout.read().decode('UTF-8').rstrip("'").lstrip("'")
        self.assertNotEqual(self.local_version, remote_new_version)

        self.assertEqual(client.get_remote_version(fetch=False), self.local_version)
        self.assertEqual(client.get_remote_version(fetch=True), remote_new_version)
        self.assertEqual(client.get_remote_version(fetch=False), remote_new_version)
        self.assertEqual(client.get_version(), self.local_version)


class HGExportRepositoryClientTest(HGClientTestSetups):

    @classmethod
    def setUpClass(self):
        HGClientTestSetups.setUpClass()
        url = self.local_url
        client = HgClient(self.local_path)
        client.checkout(url)

        self.basepath_export = os.path.join(self.root_directory, 'export')

    def tearDown(self):
        pass

    def test_export_repository(self):
        client = HgClient(self.local_path)
        self.assertTrue(
          client.export_repository(self.local_version, self.basepath_export)
        )

        self.assertTrue(os.path.exists(self.basepath_export + '.tar.gz'))
        self.assertFalse(os.path.exists(self.basepath_export + '.tar'))
        self.assertFalse(os.path.exists(self.basepath_export))


class HGGetBranchesClientTest(HGClientTestSetups):

    @classmethod
    def setUpClass(self):
        HGClientTestSetups.setUpClass()
        url = self.local_url
        client = HgClient(self.local_path)
        client.checkout(url)

    def tearDown(self):
        pass

    def test_get_branches(self):
        client = HgClient(self.local_path)
        # Make a local branch
        subprocess.check_call('hg branch test_branch2', shell=True,
                              cwd=self.local_path, stdout=subprocess.PIPE)
        subprocess.check_call('hg commit -m "Making test_branch2"', shell=True,
                              cwd=self.local_path, stdout=subprocess.PIPE)
        self.assertEqual(client.get_branches(), ['test_branch2', 'test_branch', 'default'])

        # Make a remote branch
        subprocess.check_call('hg branch remote_branch', shell=True,
                              cwd=self.remote_path, stdout=subprocess.PIPE)
        subprocess.check_call("touch fixed.txt", shell=True,
                              cwd=self.remote_path)
        subprocess.check_call("hg add fixed.txt", shell=True,
                              cwd=self.remote_path)
        subprocess.check_call("hg commit -m initial", shell=True,
                              cwd=self.remote_path)
        self.assertEqual(client.get_branches(local_only=True), ['test_branch2', 'test_branch', 'default'])
        self.assertEqual(client.get_branches(), ['remote_branch', 'test_branch2', 'test_branch', 'default'])