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