Skip to content

Commit f3f4d0f

Browse files
committed
add test for get tags step and speed up tests overall
tests would previously take ~38s on my machine. Now they take ~30s. Speed up is from removing a sleep that was used when creating tags to ensure each tag had a different creation time. We set GIT_COMMITTER_DATE to workaround that now, which sped up our tests. Signed-off-by: Taylor Silva <dev@taydev.net>
1 parent 6357f11 commit f3f4d0f

4 files changed

Lines changed: 49 additions & 18 deletions

File tree

test/all.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ set -e
55
$(dirname $0)/image.sh
66
$(dirname $0)/check.sh
77
$(dirname $0)/check_branches.sh
8+
$(dirname $0)/check_tags.sh
89
$(dirname $0)/get_branches.sh
10+
$(dirname $0)/get_tags.sh
911
$(dirname $0)/common.sh
1012
$(dirname $0)/get.sh
1113
$(dirname $0)/put.sh

test/get.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,12 +1075,11 @@ it_returns_list_of_all_tags_in_metadata() {
10751075
local ref1=$(make_commit_to_branch $repo branch-a)
10761076
local ref2=$(make_annotated_tag $repo "v1.1-pre" "tag 1")
10771077
local ref3=$(make_annotated_tag $repo "v1.1-final" "tag 2")
1078-
local ref4=$(make_commit_to_branch $repo branch-b)
1079-
local ref5=$(make_annotated_tag $repo "v1.1-branch-b" "tag 3")
1078+
local ref4=$(make_annotated_tag $repo "v1.1-branch-b" "tag 3")
10801079

10811080
local dest=$TMPDIR/destination
10821081
get_uri_at_branch_with_fetch_tags $repo branch-a $dest | jq -e "
1083-
.version == {ref: $(echo $ref4 | jq -R .)}
1082+
.version == {ref: $(echo $ref1 | jq -R .)}
10841083
and
10851084
(.metadata | .[] | select(.name == \"tags\") | .value == \"v1.1-branch-b,v1.1-final,v1.1-pre\")
10861085
"

test/get_tags.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source $(dirname $0)/helpers.sh

test/helpers.sh

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,22 @@ init_repo() {
4040
git config maintenance.auto false
4141

4242
# start with an initial commit
43-
git \
43+
local commit_date=$(_test_seq_date $(_test_next_seq .))
44+
GIT_COMMITTER_DATE="$commit_date" git \
4445
-c user.name='test' \
4546
-c user.email='test@example.com' \
46-
commit -q --allow-empty -m "init"
47+
commit -q --allow-empty -m "init" \
48+
--date "$commit_date"
4749

4850
# create some bogus branch
4951
git checkout -q -b bogus
5052

51-
git \
53+
commit_date=$(_test_seq_date $(_test_next_seq .))
54+
GIT_COMMITTER_DATE="$commit_date" git \
5255
-c user.name='test' \
5356
-c user.email='test@example.com' \
54-
commit -q --allow-empty -m "commit on other branch"
57+
commit -q --allow-empty -m "commit on other branch" \
58+
--date "$commit_date"
5559

5660
# back to master
5761
git checkout -q master
@@ -127,6 +131,23 @@ fetch_head_ref() {
127131
git -C $repo rev-parse HEAD
128132
}
129133

134+
# Per-repo monotonic counter used to give commits and waited tags distinct,
135+
# deterministic timestamps. git's committer-date and tag creator-date have
136+
# 1-second resolution; tests previously got distinct timestamps with `sleep 1`,
137+
# which made the suite slow. The counter is persisted to a file so it survives
138+
# the command-substitution subshells callers use.
139+
_test_next_seq() {
140+
local repo=$1
141+
local seq_file="$repo/.git/.test_seq"
142+
local seq=$(( $(cat "$seq_file" 2>/dev/null || echo 0) + 1 ))
143+
echo "$seq" > "$seq_file"
144+
echo "$seq"
145+
}
146+
147+
_test_seq_date() {
148+
date -u -d "@$(( 946684800 + $1 ))" "+%Y-%m-%d %H:%M:%S +0000"
149+
}
150+
130151
make_commit_to_file_on_branch() {
131152
local repo=$1
132153
local file=$2
@@ -154,10 +175,12 @@ make_commit_to_file_on_branch() {
154175
commit -q -m "commit $(wc -l $repo/$file) $msg" \
155176
--date "$(date -R -d '1 year')"
156177
else
157-
git -C $repo \
178+
local commit_date=$(_test_seq_date $(_test_next_seq $repo))
179+
GIT_COMMITTER_DATE="$commit_date" git -C $repo \
158180
-c user.name='test' \
159181
-c user.email='test@example.com' \
160-
commit -q -m "commit $(wc -l $repo/$file) $msg"
182+
commit -q -m "commit $(wc -l $repo/$file) $msg" \
183+
--date "$commit_date"
161184
fi
162185

163186
# output resulting sha
@@ -183,10 +206,12 @@ make_commit_to_file_on_branch_with_path() {
183206
mkdir -p $repo/$path
184207
echo x >> $repo/$path/$file
185208
git -C $repo add $path/$file
186-
git -C $repo \
209+
local commit_date=$(_test_seq_date $(_test_next_seq $repo))
210+
GIT_COMMITTER_DATE="$commit_date" git -C $repo \
187211
-c user.name='test' \
188212
-c user.email='test@example.com' \
189-
commit -q -m "commit $(wc -l $repo/$path/$file) $msg"
213+
commit -q -m "commit $(wc -l $repo/$path/$file) $msg" \
214+
--date "$commit_date"
190215

191216
# output resulting sha
192217
git -C $repo rev-parse HEAD
@@ -254,10 +279,12 @@ make_empty_commit() {
254279
local repo=$1
255280
local msg=${2-}
256281

257-
git -C $repo \
282+
local commit_date=$(_test_seq_date $(_test_next_seq $repo))
283+
GIT_COMMITTER_DATE="$commit_date" git -C $repo \
258284
-c user.name='test' \
259285
-c user.email='test@example.com' \
260-
commit -q --allow-empty -m "commit $msg"
286+
commit -q --allow-empty -m "commit $msg" \
287+
--date "$commit_date"
261288

262289
# output resulting sha
263290
git -C $repo rev-parse HEAD
@@ -271,11 +298,9 @@ make_annotated_tag() {
271298

272299
if [ "$wait" == true ]; then
273300
# Give each successive waited tag a distinct, increasing creation date so tags
274-
# sort deterministically. git's creatordate has only 1-second resolution, so
275-
# rather than sleeping 1s per tag we inject a monotonically increasing committer
276-
# date (the annotated tag's tagger/creator date).
277-
_annotated_tag_seq=$(( ${_annotated_tag_seq:-0} + 1 ))
278-
GIT_COMMITTER_DATE="$(date -u -d "@$(( 946684800 + _annotated_tag_seq ))" "+%Y-%m-%d %H:%M:%S +0000")" \
301+
# sort deterministically. The shared per-repo counter (see _test_next_seq) is
302+
# also bumped by commits, so commit and tag ordering interleaves correctly.
303+
GIT_COMMITTER_DATE="$(_test_seq_date $(_test_next_seq $repo))" \
279304
git -C $repo tag -f -a "$tag" -m "$msg"
280305
else
281306
git -C $repo tag -f -a "$tag" -m "$msg"

0 commit comments

Comments
 (0)