| 1 |
#!/usr/bin/env python |
| 2 |
# Software License Agreement (BSD License) |
| 3 |
# |
| 4 |
# Copyright (c) 2009, Willow Garage, Inc. |
| 5 |
# All rights reserved. |
| 6 |
# |
| 7 |
# Redistribution and use in source and binary forms, with or without |
| 8 |
# modification, are permitted provided that the following conditions |
| 9 |
# are met: |
| 10 |
# |
| 11 |
# * Redistributions of source code must retain the above copyright |
| 12 |
# notice, this list of conditions and the following disclaimer. |
| 13 |
# * Redistributions in binary form must reproduce the above |
| 14 |
# copyright notice, this list of conditions and the following |
| 15 |
# disclaimer in the documentation and/or other materials provided |
| 16 |
# with the distribution. |
| 17 |
# * Neither the name of Willow Garage, Inc. nor the names of its |
| 18 |
# contributors may be used to endorse or promote products derived |
| 19 |
# from this software without specific prior written permission. |
| 20 |
# |
| 21 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 |
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 |
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 |
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 31 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 |
# POSSIBILITY OF SUCH DAMAGE. |
| 33 |
|
| 34 |
from __future__ import absolute_import, print_function, unicode_literals |
| 35 |
|
| 36 |
import platform |
| 37 |
import os |
| 38 |
import io |
| 39 |
import fnmatch |
| 40 |
import shutil |
| 41 |
import subprocess |
| 42 |
import tempfile |
| 43 |
import unittest |
| 44 |
from vcstools.bzr import BzrClient, _get_bzr_version |
| 45 |
|
| 46 |
|
| 47 |
os.environ['EMAIL'] = 'Your Name <[email protected]>' |
| 48 |
|
| 49 |
|
| 50 |
class BzrClientTestSetups(unittest.TestCase): |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def setUpClass(self): |
| 54 |
self.root_directory = tempfile.mkdtemp() |
| 55 |
self.directories = dict(setUp=self.root_directory) |
| 56 |
self.remote_path = os.path.join(self.root_directory, "remote") |
| 57 |
os.makedirs(self.remote_path) |
| 58 |
|
| 59 |
# create a "remote" repo |
| 60 |
subprocess.check_call(["bzr", "init"], cwd=self.remote_path) |
| 61 |
subprocess.check_call(["touch", "fixed.txt"], cwd=self.remote_path) |
| 62 |
subprocess.check_call(["bzr", "add", "fixed.txt"], cwd=self.remote_path) |
| 63 |
subprocess.check_call(["bzr", "commit", "-m", "initial"], cwd=self.remote_path) |
| 64 |
subprocess.check_call(["bzr", "tag", "test_tag"], cwd=self.remote_path) |
| 65 |
self.local_version_init = "1" |
| 66 |
|
| 67 |
# files to be modified in "local" repo |
| 68 |
subprocess.check_call(["touch", "modified.txt"], cwd=self.remote_path) |
| 69 |
subprocess.check_call(["touch", "modified-fs.txt"], cwd=self.remote_path) |
| 70 |
subprocess.check_call(["bzr", "add", "modified.txt", "modified-fs.txt"], cwd=self.remote_path) |
| 71 |
subprocess.check_call(["bzr", "commit", "-m", "initial"], cwd=self.remote_path) |
| 72 |
self.local_version_second = "2" |
| 73 |
|
| 74 |
subprocess.check_call(["touch", "deleted.txt"], cwd=self.remote_path) |
| 75 |
subprocess.check_call(["touch", "deleted-fs.txt"], cwd=self.remote_path) |
| 76 |
subprocess.check_call(["bzr", "add", "deleted.txt", "deleted-fs.txt"], cwd=self.remote_path) |
| 77 |
subprocess.check_call(["bzr", "commit", "-m", "modified"], cwd=self.remote_path) |
| 78 |
self.local_version = "3" |
| 79 |
|
| 80 |
self.local_path = os.path.join(self.root_directory, "local") |
| 81 |
|
| 82 |
@classmethod |
| 83 |
def tearDownClass(self): |
| 84 |
for d in self.directories: |
| 85 |
shutil.rmtree(self.directories[d]) |
| 86 |
|
| 87 |
def tearDown(self): |
| 88 |
if os.path.exists(self.local_path): |
| 89 |
shutil.rmtree(self.local_path) |
| 90 |
|
| 91 |
|
| 92 |
class BzrClientTest(BzrClientTestSetups): |
| 93 |
|
| 94 |
def test_url_matches_with_shortcut_strings(self): |
| 95 |
client = BzrClient(self.local_path) |
| 96 |
self.assertTrue(client.url_matches('test1234', 'test1234')) |
| 97 |
|
| 98 |
def test_url_matches_with_shortcut_strings_slashes(self): |
| 99 |
client = BzrClient(self.local_path) |
| 100 |
self.assertTrue(client.url_matches('test1234/', 'test1234')) |
| 101 |
self.assertTrue(client.url_matches('test1234', 'test1234/')) |
| 102 |
self.assertTrue(client.url_matches('test1234/', 'test1234/')) |
| 103 |
|
| 104 |
def get_launchpad_info(self, url): |
| 105 |
po = subprocess.Popen(["bzr", "info", url], stdout=subprocess.PIPE) |
| 106 |
output = po.stdout.read() |
| 107 |
# it is not great to use the same code for testing as in |
| 108 |
# production, but relying on fixed bzr info output is just as |
| 109 |
# bad. |
| 110 |
for line in output.splitlines(): |
| 111 |
sline = line.decode('UTF-8').strip() |
| 112 |
for prefix in ['shared repository: ', |
| 113 |
'repository branch: ', |
| 114 |
'branch root: ']: |
| 115 |
if sline.startswith(prefix): |
| 116 |
return sline[len(prefix):] |
| 117 |
return None |
| 118 |
|
| 119 |
# this test fails on travis with bzr 2.1.4 and python2.6, but |
| 120 |
# probably due to the messed up source install of bzr using python2.7 |
| 121 |
if not (platform.python_version().startswith('2.6') and |
| 122 |
'2.1' in _get_bzr_version()): |
| 123 |
def test_url_matches_with_shortcut(self): |
| 124 |
# bzr on launchpad should have shared repository |
| 125 |
client = BzrClient(self.local_path) |
| 126 |
url = 'lp:bzr' |
| 127 |
url2 = self.get_launchpad_info(url) |
| 128 |
self.assertFalse(url2 is None) |
| 129 |
self.assertTrue(client.url_matches(url2, url), "%s~=%s" % (url, url2)) |
| 130 |
|
| 131 |
# launchpad on launchpad should be a branch root |
| 132 |
url = 'lp:launchpad' |
| 133 |
url2 = self.get_launchpad_info(url) |
| 134 |
self.assertFalse(url2 is None) |
| 135 |
self.assertTrue(client.url_matches(url2, url), "%s~=%s" % (url, url2)) |
| 136 |
|
| 137 |
def test_get_url_by_reading(self): |
| 138 |
client = BzrClient(self.local_path) |
| 139 |
url = self.remote_path |
| 140 |
self.assertFalse(client.path_exists()) |
| 141 |
self.assertFalse(client.detect_presence()) |
| 142 |
self.assertTrue(client.checkout(url)) |
| 143 |
self.assertTrue(client.path_exists()) |
| 144 |
self.assertTrue(client.detect_presence()) |
| 145 |
self.assertEqual(client.get_url(), self.remote_path) |
| 146 |
self.assertEqual(client.get_version(), self.local_version) |
| 147 |
self.assertEqual(client.get_version(self.local_version_init[0:6]), self.local_version_init) |
| 148 |
self.assertEqual(client.get_version("test_tag"), self.local_version_init) |
| 149 |
|
| 150 |
def test_get_url_nonexistant(self): |
| 151 |
local_path = "/tmp/dummy" |
| 152 |
client = BzrClient(local_path) |
| 153 |
self.assertEqual(client.get_url(), None) |
| 154 |
|
| 155 |
def test_get_type_name(self): |
| 156 |
local_path = "/tmp/dummy" |
| 157 |
client = BzrClient(local_path) |
| 158 |
self.assertEqual(client.get_vcs_type_name(), 'bzr') |
| 159 |
|
| 160 |
def test_checkout_invalid(self): |
| 161 |
"makes sure failed checkout results in False, not Exception" |
| 162 |
url = self.remote_path + "foobar" |
| 163 |
client = BzrClient(self.local_path) |
| 164 |
self.assertFalse(client.path_exists()) |
| 165 |
self.assertFalse(client.detect_presence()) |
| 166 |
self.assertFalse(client.checkout(url)) |
| 167 |
|
| 168 |
def test_checkout_invalid_update(self): |
| 169 |
"makes sure no exception happens on invalid update" |
| 170 |
url = self.remote_path |
| 171 |
client = BzrClient(self.local_path) |
| 172 |
self.assertTrue(client.checkout(url)) |
| 173 |
new_version = 'foobar' |
| 174 |
self.assertFalse(client.update(new_version)) |
| 175 |
|
| 176 |
def test_checkout(self): |
| 177 |
url = self.remote_path |
| 178 |
client = BzrClient(self.local_path) |
| 179 |
self.assertFalse(client.path_exists()) |
| 180 |
self.assertFalse(client.detect_presence()) |
| 181 |
self.assertTrue(client.checkout(url)) |
| 182 |
self.assertTrue(client.path_exists()) |
| 183 |
self.assertTrue(client.detect_presence()) |
| 184 |
self.assertEqual(client.get_path(), self.local_path) |
| 185 |
self.assertEqual(client.get_url(), url) |
| 186 |
|
| 187 |
def test_checkout_dir_exists(self): |
| 188 |
url = self.remote_path |
| 189 |
client = BzrClient(self.local_path) |
| 190 |
self.assertFalse(client.path_exists()) |
| 191 |
os.makedirs(self.local_path) |
| 192 |
self.assertTrue(client.checkout(url)) |
| 193 |
# non-empty |
| 194 |
self.assertFalse(client.checkout(url)) |
| 195 |
|
| 196 |
def test_checkout_specific_version_and_update(self): |
| 197 |
url = self.remote_path |
| 198 |
version = "1" |
| 199 |
client = BzrClient(self.local_path) |
| 200 |
self.assertFalse(client.path_exists()) |
| 201 |
self.assertFalse(client.detect_presence()) |
| 202 |
self.assertTrue(client.checkout(url, version)) |
| 203 |
self.assertTrue(client.path_exists()) |
| 204 |
self.assertTrue(client.detect_presence()) |
| 205 |
self.assertEqual(client.get_path(), self.local_path) |
| 206 |
self.assertEqual(client.get_url(), url) |
| 207 |
self.assertEqual(client.get_version(), version) |
| 208 |
|
| 209 |
new_version = '2' |
| 210 |
self.assertTrue(client.update(new_version)) |
| 211 |
self.assertEqual(client.get_version(), new_version) |
| 212 |
|
| 213 |
def testDiffClean(self): |
| 214 |
client = BzrClient(self.remote_path) |
| 215 |
self.assertEquals('', client.get_diff()) |
| 216 |
|
| 217 |
def testStatusClean(self): |
| 218 |
client = BzrClient(self.remote_path) |
| 219 |
self.assertEquals('', client.get_status()) |
| 220 |
|
| 221 |
def test_get_environment_metadata(self): |
| 222 |
# Verify that metadata is generated |
| 223 |
directory = tempfile.mkdtemp() |
| 224 |
self.directories['local'] = directory |
| 225 |
local_path = os.path.join(directory, "local") |
| 226 |
client = BzrClient(local_path) |
| 227 |
self.assertTrue('version' in client.get_environment_metadata()) |
| 228 |
|
| 229 |
|
| 230 |
class BzrClientLogTest(BzrClientTestSetups): |
| 231 |
|
| 232 |
@classmethod |
| 233 |
def setUpClass(self): |
| 234 |
BzrClientTestSetups.setUpClass() |
| 235 |
client = BzrClient(self.local_path) |
| 236 |
client.checkout(self.remote_path) |
| 237 |
|
| 238 |
def test_get_log_defaults(self): |
| 239 |
client = BzrClient(self.local_path) |
| 240 |
client.checkout(self.remote_path) |
| 241 |
log = client.get_log() |
| 242 |
self.assertEquals(3, len(log)) |
| 243 |
self.assertEquals('modified', log[0]['message']) |
| 244 |
for key in ['id', 'author', 'email', 'date', 'message']: |
| 245 |
self.assertTrue(log[0][key] is not None, key) |
| 246 |
|
| 247 |
def test_get_log_limit(self): |
| 248 |
client = BzrClient(self.local_path) |
| 249 |
client.checkout(self.remote_path) |
| 250 |
log = client.get_log(limit=1) |
| 251 |
self.assertEquals(1, len(log)) |
| 252 |
self.assertEquals('modified', log[0]['message']) |
| 253 |
|
| 254 |
def test_get_log_path(self): |
| 255 |
client = BzrClient(self.local_path) |
| 256 |
client.checkout(self.remote_path) |
| 257 |
log = client.get_log(relpath='fixed.txt') |
| 258 |
self.assertEquals('initial', log[0]['message']) |
| 259 |
|
| 260 |
|
| 261 |
class BzrClientAffectedFilesTest(BzrClientTestSetups): |
| 262 |
|
| 263 |
@classmethod |
| 264 |
def setUpClass(self): |
| 265 |
BzrClientTestSetups.setUpClass() |
| 266 |
client = BzrClient(self.local_path) |
| 267 |
client.checkout(self.remote_path) |
| 268 |
|
| 269 |
def test_get_log_defaults(self): |
| 270 |
client = BzrClient(self.local_path) |
| 271 |
client.checkout(self.remote_path) |
| 272 |
log = client.get_log(limit=1)[0] |
| 273 |
affected = client.get_affected_files(log['id']) |
| 274 |
self.assertEqual(sorted(['deleted-fs.txt', 'deleted.txt']), |
| 275 |
sorted(affected)) |
| 276 |
|
| 277 |
|
| 278 |
class BzrDiffStatClientTest(BzrClientTestSetups): |
| 279 |
|
| 280 |
@classmethod |
| 281 |
def setUpClass(self): |
| 282 |
# setup a local repo once for all diff and status test |
| 283 |
BzrClientTestSetups.setUpClass() |
| 284 |
url = self.remote_path |
| 285 |
client = BzrClient(self.local_path) |
| 286 |
client.checkout(url) |
| 287 |
# after setting up "local" repo, change files and make some changes |
| 288 |
subprocess.check_call(["rm", "deleted-fs.txt"], cwd=self.local_path) |
| 289 |
subprocess.check_call(["bzr", "rm", "deleted.txt"], cwd=self.local_path) |
| 290 |
f = io.open(os.path.join(self.local_path, "modified.txt"), 'a') |
| 291 |
f.write('0123456789abcdef') |
| 292 |
f.close() |
| 293 |
f = io.open(os.path.join(self.local_path, "modified-fs.txt"), 'a') |
| 294 |
f.write('0123456789abcdef') |
| 295 |
f.close() |
| 296 |
f = io.open(os.path.join(self.local_path, "added-fs.txt"), 'w') |
| 297 |
f.write('0123456789abcdef') |
| 298 |
f.close() |
| 299 |
f = io.open(os.path.join(self.local_path, "added.txt"), 'w') |
| 300 |
f.write('0123456789abcdef') |
| 301 |
f.close() |
| 302 |
subprocess.check_call(["bzr", "add", "added.txt"], cwd=self.local_path) |
| 303 |
|
| 304 |
def tearDown(self): |
| 305 |
pass |
| 306 |
|
| 307 |
@classmethod |
| 308 |
def tearDownClass(self): |
| 309 |
BzrClientTestSetups.tearDownClass() |
| 310 |
|
| 311 |
def test_diff(self): |
| 312 |
client = BzrClient(self.local_path) |
| 313 |
self.assertTrue(client.path_exists()) |
| 314 |
self.assertTrue(client.detect_presence()) |
| 315 |
# using fnmatch because date and time change (remove when bzr reaches diff --format) |
| 316 |
diff = client.get_diff() |
| 317 |
self.assertTrue(diff is not None) |
| 318 |
self.assertTrue(fnmatch.fnmatch(diff, "=== added file 'added.txt'\n--- ./added.txt\t????-??-?? ??:??:?? +0000\n+++ ./added.txt\t????-??-?? ??:??:?? +0000\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\n\n=== removed file 'deleted-fs.txt'\n=== removed file 'deleted.txt'\n=== modified file 'modified-fs.txt'\n--- ./modified-fs.txt\t????-??-?? ??:??:?? +0000\n+++ ./modified-fs.txt\t????-??-?? ??:??:?? +0000\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\n\n=== modified file 'modified.txt'\n--- ./modified.txt\t????-??-?? ??:??:?? +0000\n+++ ./modified.txt\t????-??-?? ??:??:?? +0000\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file")) |
| 319 |
|
| 320 |
def test_diff_relpath(self): |
| 321 |
client = BzrClient(self.local_path) |
| 322 |
self.assertTrue(client.path_exists()) |
| 323 |
self.assertTrue(client.detect_presence()) |
| 324 |
# using fnmatch because date and time change (remove when bzr introduces diff --format) |
| 325 |
diff = client.get_diff(basepath=os.path.dirname(self.local_path)) |
| 326 |
self.assertTrue(diff is not None) |
| 327 |
self.assertTrue(fnmatch.fnmatch(diff, "=== added file 'added.txt'\n--- local/added.txt\t????-??-?? ??:??:?? +0000\n+++ local/added.txt\t????-??-?? ??:??:?? +0000\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\n\n=== removed file 'deleted-fs.txt'\n=== removed file 'deleted.txt'\n=== modified file 'modified-fs.txt'\n--- local/modified-fs.txt\t????-??-?? ??:??:?? +0000\n+++ local/modified-fs.txt\t????-??-?? ??:??:?? +0000\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file\n\n=== modified file 'modified.txt'\n--- local/modified.txt\t????-??-?? ??:??:?? +0000\n+++ local/modified.txt\t????-??-?? ??:??:?? +0000\n@@ -0,0 +1,1 @@\n+0123456789abcdef\n\\ No newline at end of file")) |
| 328 |
|
| 329 |
def test_status(self): |
| 330 |
client = BzrClient(self.local_path) |
| 331 |
self.assertTrue(client.path_exists()) |
| 332 |
self.assertTrue(client.detect_presence()) |
| 333 |
self.assertEquals('+N ./added.txt\n D ./deleted-fs.txt\n-D ./deleted.txt\n M ./modified-fs.txt\n M ./modified.txt\n', client.get_status()) |
| 334 |
|
| 335 |
def test_status_relpath(self): |
| 336 |
client = BzrClient(self.local_path) |
| 337 |
self.assertTrue(client.path_exists()) |
| 338 |
self.assertTrue(client.detect_presence()) |
| 339 |
self.assertEquals('+N local/added.txt\n D local/deleted-fs.txt\n-D local/deleted.txt\n M local/modified-fs.txt\n M local/modified.txt\n', client.get_status(basepath=os.path.dirname(self.local_path))) |
| 340 |
|
| 341 |
def test_status_untracked(self): |
| 342 |
client = BzrClient(self.local_path) |
| 343 |
self.assertTrue(client.path_exists()) |
| 344 |
self.assertTrue(client.detect_presence()) |
| 345 |
self.assertEquals('? ./added-fs.txt\n+N ./added.txt\n D ./deleted-fs.txt\n-D ./deleted.txt\n M ./modified-fs.txt\n M ./modified.txt\n', client.get_status(untracked=True)) |
| 346 |
|
| 347 |
|
| 348 |
class BzrDiffStatClientTest(BzrClientTestSetups): |
| 349 |
|
| 350 |
@classmethod |
| 351 |
def setUpClass(self): |
| 352 |
# setup a local repo once for all diff and status test |
| 353 |
BzrClientTestSetups.setUpClass() |
| 354 |
url = self.remote_path |
| 355 |
client = BzrClient(self.local_path) |
| 356 |
client.checkout(url) |
| 357 |
|
| 358 |
self.basepath_export = os.path.join(self.root_directory, 'export') |
| 359 |
|
| 360 |
def tearDown(self): |
| 361 |
pass |
| 362 |
|
| 363 |
@classmethod |
| 364 |
def tearDownClass(self): |
| 365 |
BzrClientTestSetups.tearDownClass() |
| 366 |
|
| 367 |
def test_export_repository(self): |
| 368 |
client = BzrClient(self.local_path) |
| 369 |
self.assertTrue( |
| 370 |
client.export_repository(self.local_version, self.basepath_export) |
| 371 |
) |
| 372 |
|
| 373 |
self.assertTrue(os.path.exists(self.basepath_export + '.tar.gz')) |
| 374 |
self.assertFalse(os.path.exists(self.basepath_export + '.tar')) |
| 375 |
self.assertFalse(os.path.exists(self.basepath_export)) |