Skip to content

Commit 11db302

Browse files
committed
Tests and repairs for stg log
Add t0009-log.sh to test `stg log`. Use `tformat` instead of `format` in default output format. There is no trailing newline when regular `format`, while `tformat` ensures a terminating newline. Check for invalid option and argument combination before performing any actions. New checks ensure that --clear is used by itself. Signed-off-by: Peter Grayson <[email protected]>
1 parent f7a5c51 commit 11db302

File tree

2 files changed

+148
-8
lines changed

2 files changed

+148
-8
lines changed

stgit/commands/log.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,30 @@ def show_log(stacklog, pathlim, num, full, show_diff):
9595
if show_diff:
9696
cmd.append('-p')
9797
elif not full:
98-
cmd.append('--pretty=format:%h %aD %s')
98+
cmd.append('--pretty=tformat:%h %aD %s')
9999
cmd.extend([stacklog.sha1, '--'])
100100
cmd.extend(pathlim)
101101
Run(*cmd).run()
102102

103103

104104
def func(parser, options, args):
105-
if options.branch:
106-
stack = directory.repository.get_stack(options.branch)
107-
else:
108-
stack = directory.repository.current_stack
105+
if options.clear:
106+
if (
107+
options.diff
108+
or options.number
109+
or options.full
110+
or options.graphical
111+
):
112+
parser.error('cannot combine --clear with other options')
113+
elif args:
114+
parser.error('cannot combine --clear with patch arguments')
115+
116+
if options.graphical:
117+
for o in ['diff', 'number', 'full']:
118+
if getattr(options, o):
119+
parser.error('cannot combine --graphical and --%s' % o)
120+
121+
stack = directory.repository.get_stack(options.branch)
109122
patches = common.parse_patches(args, list(stack.patchorder.all))
110123
logref = log.log_ref(stack.name)
111124
try:
@@ -122,9 +135,6 @@ def func(parser, options, args):
122135
pathlim = [os.path.join('patches', pn) for pn in patches]
123136

124137
if options.graphical:
125-
for o in ['diff', 'number', 'full']:
126-
if getattr(options, o):
127-
parser.error('cannot combine --graphical and --%s' % o)
128138
cmd = ['gitk', stacklog.simplified.sha1, '--'] + pathlim
129139
# Discard the exit codes generated by SIGINT, SIGKILL, and SIGTERM.
130140
Run(*cmd).returns([0, -2, -9, -15]).run()

t/t0009-log.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/sh
2+
3+
test_description='Test the log command.'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'Attempt log on uninitialized branch' '
8+
command_error stg log 2>&1 >/dev/null | grep -e "branch not initialized"
9+
'
10+
11+
test_expect_success 'Initialize the StGIT repository' '
12+
stg init
13+
'
14+
15+
test_expect_success 'Empty log' '
16+
stg log 2>&1 >/dev/null | grep -e "Log is empty"
17+
'
18+
19+
test_expect_success 'Add some patches' '
20+
test_commit p0 file0.txt foo0 &&
21+
test_commit p1 file1.txt foo1 &&
22+
test_commit p2 file2.txt foo2 &&
23+
test_commit p3 file3.txt foo3 &&
24+
stg uncommit -n 4
25+
'
26+
27+
test_expect_success 'Test log of all patches' '
28+
stg log | head -n 1 | grep -e "uncommit"
29+
'
30+
31+
test_expect_success 'Test invalid opts with clear' '
32+
command_error stg log --diff --clear 2>&1 >/dev/null |
33+
grep -e "cannot combine --clear with other options" &&
34+
stg log | head -n 1 | grep -e "uncommit"
35+
'
36+
37+
test_expect_success 'Test invalid args with clear' '
38+
command_error stg log --clear p0 p1 2>&1 >/dev/null |
39+
grep -e "cannot combine --clear with patch arguments" &&
40+
stg log | head -n 1 | grep -e "uncommit"
41+
'
42+
43+
test_expect_success 'Test invalid opts with graphical' '
44+
command_error stg log --graphical -n 5 p0 p1 2>&1 >/dev/null |
45+
grep -e "cannot combine --graphical and --number"
46+
'
47+
48+
test_expect_success 'Test log full' '
49+
stg log --full > log.txt &&
50+
test_line_count = 5 log.txt &&
51+
head -n 1 log.txt | tail -n 1 | grep -e "commit"
52+
head -n 2 log.txt | tail -n 1 | grep -e "Author: A Ú Thor"
53+
head -n 3 log.txt | tail -n 1 | grep -e "Date: "
54+
head -n 4 log.txt | tail -n 1 | grep -E "^$"
55+
head -n 5 log.txt | tail -n 1 | grep -E "^ uncommit"
56+
'
57+
58+
test_expect_success 'Make changes to patches' '
59+
stg goto p1 &&
60+
echo "bar1" > file1.txt &&
61+
stg refresh &&
62+
echo "baz1" > file1.txt &&
63+
stg refresh &&
64+
stg goto p2 &&
65+
echo "bar2" > file2.txt &&
66+
stg refresh &&
67+
stg goto p3 &&
68+
stg edit --sign
69+
'
70+
71+
test_expect_success 'Verify log for p0' '
72+
stg log p0 > log.txt &&
73+
test_line_count = 1 log.txt &&
74+
grep -e "uncommit" log.txt
75+
'
76+
77+
test_expect_success 'Log with diff' '
78+
stg log --diff p0 > log.txt &&
79+
grep -e "diff --git a/patches/p0 b/patches/p0" log.txt
80+
'
81+
82+
test_expect_success 'Verify log for p1' '
83+
stg log p1 > log.txt &&
84+
test_line_count = 3 log.txt &&
85+
head -n 1 log.txt | tail -n 1 | grep -e "refresh" &&
86+
head -n 2 log.txt | tail -n 1 | grep -e "refresh" &&
87+
head -n 3 log.txt | tail -n 1 | grep -e "uncommit"
88+
'
89+
90+
test_expect_success 'Verify log for p2' '
91+
stg log p2 > log.txt &&
92+
test_line_count = 3 log.txt &&
93+
head -n 1 log.txt | tail -n 1 | grep -e "refresh" &&
94+
head -n 2 log.txt | tail -n 1 | grep -e "goto" &&
95+
head -n 3 log.txt | tail -n 1 | grep -e "uncommit"
96+
'
97+
98+
test_expect_success 'Verify log for p3' '
99+
stg log p3 > log.txt &&
100+
test_line_count = 3 log.txt &&
101+
head -n 1 log.txt | tail -n 1 | grep -e "edit" &&
102+
head -n 2 log.txt | tail -n 1 | grep -e "goto" &&
103+
head -n 3 log.txt | tail -n 1 | grep -e "uncommit"
104+
'
105+
106+
test_expect_success 'Verify log for p2 and p3' '
107+
stg log p2 p3 > log.txt &&
108+
test_line_count = 5 log.txt &&
109+
head -n 1 log.txt | tail -n 1 | grep -e "edit" &&
110+
head -n 2 log.txt | tail -n 1 | grep -e "goto" &&
111+
head -n 3 log.txt | tail -n 1 | grep -e "refresh" &&
112+
head -n 4 log.txt | tail -n 1 | grep -e "goto" &&
113+
head -n 5 log.txt | tail -n 1 | grep -e "uncommit"
114+
'
115+
116+
test_expect_success 'Log with number' '
117+
stg log -n3 p2 p3 > log.txt &&
118+
test_line_count = 3 log.txt &&
119+
head -n 1 log.txt | tail -n 1 | grep -e "edit" &&
120+
head -n 2 log.txt | tail -n 1 | grep -e "goto" &&
121+
head -n 3 log.txt | tail -n 1 | grep -e "refresh"
122+
'
123+
124+
test_expect_success 'Clear the log' '
125+
stg log --clear &&
126+
test "$(echo $(stg series --noprefix))" = "p0 p1 p2 p3" &&
127+
stg log 2>&1 >/dev/null | grep -e "Log is empty"
128+
'
129+
130+
test_done

0 commit comments

Comments
 (0)