Skip to content

Commit 0088496

Browse files
dschogitster
authored andcommitted
test-lib.sh: introduce test_commit() and test_merge() helpers
Often we just need to add a commit with a given (short) name, that will be tagged with the same name. Now, relatively complicated graphs can be constructed easily and in a clear fashion: test_commit A && test_commit B && git checkout A && test_commit C && test_merge D B will construct this graph: A - B \ \ C - D For simplicity, files whose name is the lower case version of the commit message (to avoid a warning about ambiguous names) will be committed, with the corresponding commit messages as contents. If you need to provide a different file/different contents, you can use the more explicit form test_commit $MESSAGE $FILENAME $CONTENTS Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 03af087 commit 0088496

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

t/README

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ library for your script to use.
212212
is to summarize successes and failures in the test script and
213213
exit with an appropriate error code.
214214

215+
- test_tick
216+
217+
Make commit and tag names consistent by setting the author and
218+
committer times to defined stated. Subsequent calls will
219+
advance the times by a fixed amount.
220+
221+
- test_commit <message> [<filename> [<contents>]]
222+
223+
Creates a commit with the given message, committing the given
224+
file with the given contents (default for both is to reuse the
225+
message string), and adds a tag (again reusing the message
226+
string as name). Calls test_tick to make the SHA-1s
227+
reproducible.
228+
229+
- test_merge <message> <commit-or-tag>
230+
231+
Merges the given rev using the given message. Like test_commit,
232+
creates a tag and calls test_tick before committing.
215233

216234
Tips for Writing Tests
217235
----------------------

t/test-lib.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,31 @@ test_tick () {
193193
export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
194194
}
195195

196+
# Call test_commit with the arguments "<message> [<file> [<contents>]]"
197+
#
198+
# This will commit a file with the given contents and the given commit
199+
# message. It will also add a tag with <message> as name.
200+
#
201+
# Both <file> and <contents> default to <message>.
202+
203+
test_commit () {
204+
file=${2:-$(echo "$1" | tr 'A-Z' 'a-z')}
205+
echo "${3-$1}" > "$file" &&
206+
git add "$file" &&
207+
test_tick &&
208+
git commit -m "$1" &&
209+
git tag "$1"
210+
}
211+
212+
# Call test_merge with the arguments "<message> <commit>", where <commit>
213+
# can be a tag pointing to the commit-to-merge.
214+
215+
test_merge () {
216+
test_tick &&
217+
git merge -m "$1" "$2" &&
218+
git tag "$1"
219+
}
220+
196221
# You are not expected to call test_ok_ and test_failure_ directly, use
197222
# the text_expect_* functions instead.
198223

0 commit comments

Comments
 (0)