2
3
from bzrlib.diff import _patch_header_date
5
from bzrlib.plugins.bzrtools.hunk_selector import (
9
from bzrlib.plugins.bzrtools.errors import NoColor
10
from bzrlib.plugins.bzrtools import cmd_shelf
13
7
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
14
8
ORIGINAL = '\n\nhello test world\n\n'
35
29
def _check_diff(self, diff=DIFF_1, filename='test_file'):
36
30
old_tree = self.tree.basis_tree()
40
old_date = _patch_header_date(old_tree,
41
old_tree.inventory.path2id(filename),
43
new_date = _patch_header_date(self.tree,
44
self.tree.inventory.path2id(filename), filename)
31
old_date = _patch_header_date(old_tree,
32
old_tree.inventory.path2id(filename),
34
new_date = _patch_header_date(self.tree,
35
self.tree.inventory.path2id(filename),
48
37
keys = { 'filename' : filename , 'old_date': old_date,
49
38
'new_date': new_date}
50
39
hdr = self.DIFF_HEADER % keys
54
43
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
56
45
old_tree = self.tree.basis_tree()
59
old_date = _patch_header_date(old_tree,
60
old_tree.inventory.path2id(filename),
46
old_date = _patch_header_date(old_tree,
47
old_tree.inventory.path2id(filename),
64
49
diff = diff % { 'filename' : filename, 'old_date': old_date,
65
50
'new_date': new_date}
66
51
shelf = open(os.path.join(self.tree.basedir,
77
62
def __test_loop(self, count):
78
63
self.tree = self.make_branch_and_tree('.')
79
64
self.__create_and_add_test_file()
80
for counter in range(count):
81
69
# Modify the test file
82
# write in binary mode because on win32 line-endings should be
70
# write in binary mode because on win32 line-endings should be LF
84
71
f = file('test_file', 'wb')
85
72
f.write(self.MODIFIED)
90
self.tree.lock_write()
92
new_date = _patch_header_date(self.tree,
93
self.tree.inventory.path2id('test_file'), 'test_file')
77
new_date = _patch_header_date(self.tree,
78
self.tree.inventory.path2id('test_file'), 'test_file')
97
80
# Shelve the changes
98
81
self.run_bzr('shelve', '--all', retcode=0)
319
302
def test_shelf_delete(self):
320
303
self.tree = self.make_branch_and_tree('.')
321
self.tree.lock_write()
323
self.__create_and_add_test_file()
324
self.__create_and_add_test_file(filename='test_file2')
326
# Modify the test files
327
# write in binary mode because on win32 line-endings should be LF
328
f = file('test_file', 'wb')
329
f.write(self.MODIFIED)
331
f = file('test_file2', 'wb')
332
f.write(self.MODIFIED)
334
new_date = _patch_header_date(self.tree,
335
self.tree.inventory.path2id('test_file'), 'test_file')
305
self.__create_and_add_test_file()
306
self.__create_and_add_test_file(filename='test_file2')
308
# Modify the test files
309
# write in binary mode because on win32 line-endings should be LF
310
f = file('test_file', 'wb')
311
f.write(self.MODIFIED)
313
f = file('test_file2', 'wb')
314
f.write(self.MODIFIED)
316
new_date = _patch_header_date(self.tree,
317
self.tree.inventory.path2id('test_file'), 'test_file')
339
319
# Shelve the changes
340
320
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
429
409
self.run_bzr('shelf', 'ls', retcode=0)
431
411
old_tree = self.tree.basis_tree()
433
self.tree.lock_read()
435
old_date = _patch_header_date(old_tree,
436
old_tree.inventory.path2id('test_file'),
438
new_date = _patch_header_date(self.tree,
439
self.tree.inventory.path2id('test_file'), 'test_file')
412
old_date = _patch_header_date(old_tree,
413
old_tree.inventory.path2id('test_file'),
415
new_date = _patch_header_date(self.tree,
416
self.tree.inventory.path2id('test_file'),
443
418
# Fake a -p0 shelved patch
444
419
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
445
420
'new_date' : new_date}
577
552
# Hex and is cracky, so it shouldn't work
578
553
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
579
554
self.assertTrue("Invalid patch name '0x00'" in error)
581
def test_color_hunk_selector(self):
582
"""Make sure NoColor is raised iff terminal.has_ansi_colors is False"""
583
hs = ShelveHunkSelector([])
584
hs = UnshelveHunkSelector([])
586
from bzrlib.plugins.bzrtools import terminal
588
from bzrtools import terminal
589
old_has_ansi_colors = terminal.has_ansi_colors
590
terminal.has_ansi_colors = lambda: False
592
self.assertRaises(NoColor, ShelveHunkSelector, [], True)
593
self.assertRaises(NoColor, UnshelveHunkSelector, [], True)
594
terminal.has_ansi_colors = lambda: True
595
hs = ShelveHunkSelector([], color=True)
596
hs = UnshelveHunkSelector([], color=True)
598
terminal.has_ansi_colors = old_has_ansi_colors
600
def test_no_color(self):
601
"""Ensure --no-color switch can be passed"""
602
self.tree = self.make_branch_and_tree('.')
606
self.tree.add(subdir)
609
self.__create_and_add_test_file()
611
# Modify the test file
612
# write in binary mode because on win32 line-endings should be LF
613
f = file('test_file', 'wb')
614
f.write(self.MODIFIED)
616
stdout, error = self.run_bzr_captured(['shelve', '--all',
618
stdout, error = self.run_bzr_captured(['unshelve', '--all',
621
def test_shelf_help(self):
622
self.assertContainsRe(cmd_shelf().help(),
623
'list\n.*List the patches on the current shelf')