1#!/bin/bash
2# Copyright 2013 the V8 project authors. All rights reserved.
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8#       notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10#       copyright notice, this list of conditions and the following
11#       disclaimer in the documentation and/or other materials provided
12#       with the distribution.
13#     * Neither the name of Google Inc. nor the names of its
14#       contributors may be used to endorse or promote products derived
15#       from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29# Tests the push-to-trunk.sh script. Needs to be run in V8 base dir:
30# ./tools/test-push-to-trunk.sh
31
32# TODO(machenbach): Check automatically if expectations match.
33# TODO(machenbach): Mock out version number retrieval.
34# TODO(machenbach): Allow multiple different test cases.
35# TODO(machenbach): Allow multi line mock output.
36# TODO(machenbach): Represent test expectations/mock output without an array
37# index increment.
38
39########## Stdin for push-to-trunk.sh
40
41# Confirm push to trunk commit ID
42INPUT[0]="Y"
43# Open editor
44INPUT[1]=""
45# Confirm increment version number
46INPUT[2]="Y"
47# Reviewer for V8 CL
48INPUT[3]="reviewer@chromium.org"
49# Enter LGTM for V8 CL
50INPUT[4]="LGTM"
51# Confirm checkout sanity
52INPUT[5]="Y"
53# Manually type in trunk revision
54INPUT[6]="12345"
55# Reviewer for Chromium CL
56INPUT[7]="reviewer@chromium.org"
57
58########## Expected commands and mock output
59
60EXP[0]="git status -s -uno"
61OUT[0]=""
62EXP[1]="git status -s -b -uno"
63OUT[1]="## some_branch"
64EXP[2]="git svn fetch"
65OUT[2]=""
66EXP[3]="git branch"
67OUT[3]="not the temp branch"
68EXP[4]="git checkout -b prepare-push-temporary-branch-created-by-script"
69OUT[4]=""
70EXP[5]="git branch"
71OUT[5]="not the branch"
72EXP[6]="git branch"
73OUT[6]="not the trunk branch"
74EXP[7]="git checkout -b prepare-push svn/bleeding_edge"
75OUT[7]=""
76EXP[8]="git log -1 --format=%H ChangeLog"
77OUT[8]="hash1"
78EXP[9]="git log -1 hash1"
79OUT[9]=""
80EXP[10]="git log hash1..HEAD --format=%H"
81OUT[10]="hash2"
82EXP[11]="git log -1 hash2 --format=\"%w(80,8,8)%s\""
83OUT[11]="Log line..."
84EXP[12]="git log -1 hash2 --format=\"%B\""
85OUT[12]="BUG=6789"
86EXP[13]="git log -1 hash2 --format=\"%w(80,8,8)(%an)\""
87OUT[13]="   (author@chromium.org)"
88EXP[14]="git commit -a -m \"Prepare push to trunk.  Now working on version 3.4.5.\""
89OUT[14]=""
90EXP[15]="git cl upload -r reviewer@chromium.org --send-mail"
91OUT[15]=""
92EXP[16]="git cl dcommit"
93OUT[16]=""
94EXP[17]="git svn fetch"
95OUT[17]=""
96EXP[18]="git checkout svn/bleeding_edge"
97OUT[18]=""
98EXP[19]="git log -1 --format=%H --grep=Prepare push to trunk.  Now working on version 3.4.5."
99OUT[19]="hash3"
100EXP[20]="git diff svn/trunk"
101OUT[20]="patch1"
102EXP[21]="git checkout -b trunk-push svn/trunk"
103OUT[21]=""
104EXP[22]="git apply --index --reject /tmp/v8-push-to-trunk-tempfile-patch"
105OUT[22]=""
106EXP[23]="git add src/version.cc"
107OUT[23]=""
108EXP[24]="git commit -F /tmp/v8-push-to-trunk-tempfile-commitmsg"
109OUT[24]=""
110EXP[25]="git svn dcommit"
111OUT[25]="r1234"
112EXP[26]="git svn tag 3.4.5 -m \"Tagging version 3.4.5\""
113OUT[26]=""
114EXP[27]="git status -s -uno"
115OUT[27]=""
116EXP[28]="git checkout master"
117OUT[28]=""
118EXP[29]="git pull"
119OUT[29]=""
120EXP[30]="git checkout -b v8-roll-12345"
121OUT[30]=""
122EXP[31]="git commit -am Update V8 to version 3.4.5."
123OUT[31]=""
124EXP[32]="git cl upload --send-mail"
125OUT[32]=""
126EXP[33]="git checkout -f some_branch"
127OUT[33]=""
128EXP[34]="git branch -D prepare-push-temporary-branch-created-by-script"
129OUT[34]=""
130EXP[35]="git branch -D prepare-push"
131OUT[35]=""
132EXP[36]="git branch -D trunk-push"
133OUT[36]=""
134
135########## Global temp files for test input/output
136
137export TEST_OUTPUT=$(mktemp)
138export INDEX=$(mktemp)
139export MOCK_OUTPUT=$(mktemp)
140export EXPECTED_COMMANDS=$(mktemp)
141
142########## Command index
143
144inc_index() {
145  local I="$(command cat $INDEX)"
146  let "I+=1"
147  echo "$I" > $INDEX
148  echo $I
149}
150
151echo "-1" > $INDEX
152export -f inc_index
153
154########## Mock output accessor
155
156get_mock_output() {
157  local I=$1
158  let "I+=1"
159  command sed "${I}q;d" $MOCK_OUTPUT
160}
161
162export -f get_mock_output
163
164for E in "${OUT[@]}"; do
165  echo $E
166done > $MOCK_OUTPUT
167
168########## Expected commands accessor
169
170get_expected_command() {
171  local I=$1
172  let "I+=1"
173  command sed "${I}q;d" $EXPECTED_COMMANDS
174}
175
176export -f get_expected_command
177
178for E in "${EXP[@]}"; do
179  echo $E
180done > $EXPECTED_COMMANDS
181
182########## Mock commands
183
184git() {
185  # All calls to git are mocked out. Expected calls and mock output are stored
186  # in the EXP/OUT arrays above.
187  local I=$(inc_index)
188  local OUT=$(get_mock_output $I)
189  local EXP=$(get_expected_command $I)
190  echo "#############################" >> $TEST_OUTPUT
191  echo "Com. Index:  $I" >> $TEST_OUTPUT
192  echo "Expected:    ${EXP}" >> $TEST_OUTPUT
193  echo "Actual:      git $@" >> $TEST_OUTPUT
194  echo "Mock Output: ${OUT}" >> $TEST_OUTPUT
195  echo "${OUT}"
196}
197
198mv() {
199  echo "#############################" >> $TEST_OUTPUT
200  echo "mv $@" >> $TEST_OUTPUT
201}
202
203sed() {
204  # Only calls to sed * -i * are mocked out.
205  echo "#############################" >> $TEST_OUTPUT
206  local arr=$@
207  if [[ "${arr[@]}" =~ "-i" || "${arr[${#arr[@]}-1]}" == "-i" ]]; then
208    echo "sed $@" >> $TEST_OUTPUT
209  else
210    echo "sed $@" >> $TEST_OUTPUT
211    command sed "$@"
212  fi
213}
214
215editor() {
216  echo "#############################" >> $TEST_OUTPUT
217  echo "editor $@" >> $TEST_OUTPUT
218}
219
220cd() {
221  echo "#############################" >> $TEST_OUTPUT
222  echo "cd $@" >> $TEST_OUTPUT
223}
224
225export -f git
226export -f mv
227export -f sed
228export -f cd
229export -f editor
230export EDITOR=editor
231
232########## Invoke script with test stdin
233
234for i in "${INPUT[@]}"; do 
235    echo $i
236done | tools/push-to-trunk.sh -c "path/to/chromium"
237
238echo "Collected output:"
239command cat $TEST_OUTPUT
240
241########## Clean up
242
243rm -rf $TEST_OUTPUT
244rm -rf $INDEX
245rm -rf $MOCK_OUTPUT
246rm -rf $EXPECTED_COMMANDS
247