Test for "stg edit"
[stgit] / t / t3300-edit.sh
CommitLineData
2e37b61d
KH
1#!/bin/sh
2test_description='Test "stg edit"'
3
4. ./test-lib.sh
5
6test_expect_success 'Setup' '
7 printf "000\n111\n222\n" >> foo &&
8 git add foo &&
9 git commit -m "Initial commit" &&
10 sed -i "s/000/000xx/" foo &&
11 git commit -a -m "First change" &&
12 sed -i "s/111/111yy/" foo &&
13 git commit -a -m "Second change" &&
14 sed -i "s/222/222zz/" foo &&
15 git commit -a -m "Third change" &&
16 stg init &&
17 stg uncommit -n 3 p &&
18 stg pop
19'
20
21# Commit parse functions.
22msg () { git cat-file -p $1 | sed '1,/^$/d' | tr '\n' / | sed 's,/*$,,' ; }
23auth () { git log -n 1 --pretty=format:"%an, %ae" $1 ; }
24date () { git log -n 1 --pretty=format:%ai $1 ; }
25
26test_expect_success 'Edit message of top patch' '
27 test "$(msg HEAD)" = "Second change" &&
28 stg edit p2 -m "Second change 2" &&
29 test "$(msg HEAD)" = "Second change 2"
30'
31
32test_expect_success 'Edit message of non-top patch' '
33 test "$(msg HEAD^)" = "First change" &&
34 stg edit p1 -m "First change 2" &&
35 test "$(msg HEAD^)" = "First change 2"
36'
37
38test_expect_success 'Edit message of unapplied patch' '
39 test "$(msg $(stg id p3))" = "Third change" &&
40 stg edit p3 -m "Third change 2" &&
41 test "$(msg $(stg id p3))" = "Third change 2"
42'
43
44test_expect_success 'Set patch message with --file <file>' '
45 test "$(msg HEAD)" = "Second change 2" &&
46 echo "Pride or Prejudice" > commitmsg &&
47 stg edit p2 -f commitmsg &&
48 test "$(msg HEAD)" = "Pride or Prejudice"
49'
50
51test_expect_success 'Set patch message with --file -' '
52 echo "Pride and Prejudice" | stg edit p2 -f - &&
53 test "$(msg HEAD)" = "Pride and Prejudice"
54'
55
56( printf 'From: A U Thor <author@example.com>\nDate: <omitted>'
57 printf '\n\nPride and Prejudice' ) > expected-tmpl
58omit_date () { sed "s/^Date:.*$/Date: <omitted>/" ; }
59
60test_expect_success 'Save template to file' '
61 stg edit --save-template saved-tmpl p2 &&
62 omit_date < saved-tmpl > saved-tmpl-d &&
63 test_cmp expected-tmpl saved-tmpl-d
64'
65
66test_expect_success 'Save template to stdout' '
67 stg edit --save-template - p2 > saved-tmpl2 &&
68 omit_date < saved-tmpl2 > saved-tmpl2-d &&
69 test_cmp expected-tmpl saved-tmpl2-d
70'
71
72# Test the various ways of invoking the interactive editor. The
73# preference order should be
74#
75# 1. GIT_EDITOR
76# 2. stgit.editor (legacy)
77# 3. core.editor
78# 4. VISUAL
79# 5. EDITOR
80# 6. vi
81
82mkeditor ()
83{
84 cat > "$1" <<EOF
85#!/bin/sh
86printf "\n$1\n" >> "\$1"
87EOF
88 chmod a+x "$1"
89}
90
91mkeditor vi
92test_expect_failure 'Edit commit message interactively (vi)' '
93 m=$(msg HEAD) &&
94 PATH=.:$PATH stg edit p2 &&
95 test "$(msg HEAD)" = "$m/vi"
96'
97
98mkeditor e1
99test_expect_success 'Edit commit message interactively (EDITOR)' '
100 m=$(msg HEAD) &&
101 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
102 echo $m && echo $(msg HEAD) &&
103 test "$(msg HEAD)" = "$m/e1"
104'
105
106mkeditor e2
107test_expect_failure 'Edit commit message interactively (VISUAL)' '
108 m=$(msg HEAD) &&
109 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
110 test "$(msg HEAD)" = "$m/e2"
111'
112
113mkeditor e3
114test_expect_failure 'Edit commit message interactively (core.editor)' '
115 m=$(msg HEAD) &&
116 git config core.editor e3 &&
117 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
118 test "$(msg HEAD)" = "$m/e3"
119'
120
121mkeditor e4
122test_expect_success 'Edit commit message interactively (stgit.editor)' '
123 m=$(msg HEAD) &&
124 git config stgit.editor e4 &&
125 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
126 test "$(msg HEAD)" = "$m/e4"
127'
128
129mkeditor e5
130test_expect_failure 'Edit commit message interactively (GIT_EDITOR)' '
131 m=$(msg HEAD) &&
132 GIT_EDITOR=./e5 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
133 test "$(msg HEAD)" = "$m/e5"
134'
135
136rm -f vi e1 e2 e3 e4 e5
137git config --unset core.editor
138git config --unset stgit.editor
139
140mkeditor twoliner
141test_expect_failure 'Both noninterative and interactive editing' '
142 EDITOR=./twoliner stg edit -e -m "oneliner" p2 &&
143 test "$(msg HEAD)" = "oneliner/twoliner"
144'
145rm -f twoliner
146
147cat > diffedit <<EOF
148#!/bin/sh
149sed -i 's/111yy/111YY/' "\$1"
150EOF
151chmod a+x diffedit
152test_expect_success 'Edit patch diff' '
153 EDITOR=./diffedit stg edit -d p2 &&
154 test "$(grep 111 foo)" = "111YY"
155'
156rm -f diffedit
157
158test_expect_success 'Sign a patch' '
159 m=$(msg HEAD) &&
160 stg edit --sign p2 &&
161 test "$(msg HEAD)" = "$m//Signed-off-by: C O Mitter <committer@example.com>"
162'
163
164test_expect_success 'Acknowledge a patch' '
165 m=$(msg HEAD^) &&
166 stg edit --ack p1 &&
167 test "$(msg HEAD^)" = "$m//Acked-by: C O Mitter <committer@example.com>"
168'
169
170test_expect_success 'Set author' '
171 stg edit p2 --author "Jane Austin <jaustin@example.com>" &&
172 test "$(auth HEAD)" = "Jane Austin, jaustin@example.com"
173'
174
175test_expect_success 'Fail to set broken author' '
176 command_error stg edit p2 --author "No Mail Address" &&
177 test "$(auth HEAD)" = "Jane Austin, jaustin@example.com"
178'
179
180test_expect_success 'Set author name' '
181 stg edit p2 --authname "Jane Austen" &&
182 test "$(auth HEAD)" = "Jane Austen, jaustin@example.com"
183'
184
185test_expect_success 'Set author email' '
186 stg edit p2 --authemail "jausten@example.com" &&
187 test "$(auth HEAD)" = "Jane Austen, jausten@example.com"
188'
189
190test_expect_failure 'Set author date (RFC2822 format)' '
191 stg edit p2 --authdate "Wed, 10 Jul 2013 23:39:00 pm -0300" &&
192 test "$(date HEAD)" = "2013-07-10 23:39:00 -0300"
193'
194
195test_expect_failure 'Set author date (ISO 8601 format)' '
196 stg edit p2 --authdate "2013-01-28 22:30:00 -0300" &&
197 test "$(date HEAD)" = "2013-01-28 22:30:00 -0300"
198'
199
200test_expect_failure 'Fail to set invalid author date' '
201 command_error stg edit p2 --authdate "28 Jan 1813" &&
202 test "$(date HEAD)" = "2013-01-28 22:30:00 -0300"
203'
204
205test_done