Skip to content

Commit 4fb63a7

Browse files
committed
THX-1138: Add test
1 parent 13bf4c1 commit 4fb63a7

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

scripts/tests.sh

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/env bash
2+
# test.sh
3+
4+
TEST="test_$(date --utc +"%s")"
5+
trap 'rm -rf $TEST; \
6+
git reset --hard $STARTING_COMMIT;' EXIT
7+
#git reset HEAD~$TEST_COMMIT_COUNT;' EXIT
8+
9+
STARTING_COMMIT=$(git rev-parse HEAD)
10+
11+
TEST_COMMIT_COUNT=0
12+
13+
function printHeading () {
14+
txt="$@";
15+
printf "\n\e[01;39m${txt}\e[0m ";
16+
printf '\n%*s' "$((${COLUMNS}-$((${COLUMNS}-$(wc -c<<<$txt)+1))))" | tr ' ' -;
17+
printf '\n'
18+
}
19+
20+
function add_history() {
21+
if [[ $# -ne 3 ]] && [[ $# -ne 4 ]]; then echo "ERROR: Exactly 3 or 4 arguments required!"; return 1; fi
22+
DIR="$1"
23+
COUNT="$2"
24+
BRANCH_TYPE="$3"
25+
MAJOR="$4"
26+
cd "$DIR"
27+
for i in $(seq 1 $COUNT); do
28+
TEST_FILE="${i}_$(date +"%s%6N")"
29+
touch $TEST_FILE
30+
git add "$TEST_FILE" >/dev/null 2>&1
31+
[[ -n $MAJOR ]] && git commit -m "+semver major $TEST" >/dev/null 2>&1
32+
git commit -m "Merge pull request #9999 from AlexAtkinson/$BRANCH_TYPE/${TEST}_$i" >/dev/null 2>&1
33+
done
34+
cd - >/dev/null 2>&1
35+
}
36+
37+
function test_previous() {
38+
if [[ $# -lt 3 ]]; then echo "ERROR: At least 3 arguments required!"; return 1; fi
39+
TEST_TYPE="${1:-Repository}" # Repository, Directory
40+
DIRECTORY="${2:-./}"
41+
ASSERTION="$3"
42+
COMMENT="${@:4}"; [[ -z $COMMENT ]] && COMMENT="No Comment"
43+
if [[ "$TEST_TYPE" == "Repository" ]]; then
44+
echo -e "\e[01;39m$TEST_TYPE: Previous Version ($COMMENT)\e[0m"
45+
TEST_OUTPUT=$(scripts/detectPreviousVersion.sh)
46+
if grep -q "$ASSERTION" <<<$TEST_OUTPUT; then RESULT="\e[01;32mOK\e[0m"; else RESULT="\e[01;31mFAIL\e[0m"; FAILURE="TRUE"; fi
47+
echo -e " $RESULT - $TEST_OUTPUT"
48+
fi
49+
if [[ "$TEST_TYPE" == "Directory" ]]; then
50+
echo -e "\e[01;39m$TEST_TYPE - $DIRECTORY: Previous Version ($COMMENT)\e[0m"
51+
TEST_OUTPUT=$(scripts/detectPreviousVersion.sh -d "$DIRECTORY" -n "${DIRECTORY##*/}")
52+
if grep -q "$ASSERTION" <<<$TEST_OUTPUT; then RESULT="\e[01;32mOK\e[0m"; else RESULT="\e[01;31mFAIL\e[0m"; FAILURE="TRUE"; fi
53+
echo -e " $RESULT - $TEST_OUTPUT"
54+
fi
55+
}
56+
57+
function test_new() {
58+
if [[ $# -lt 3 ]]; then echo "ERROR: At least 3 arguments required!"; return 1; fi
59+
TEST_TYPE="${1:-Repository}" # Repository, Directory
60+
DIRECTORY="${2:-./}"
61+
ASSERTION="$3"
62+
COMMENT="${@:4}"; [[ -z $COMMENT ]] && COMMENT="No Comment"
63+
if [[ "$TEST_TYPE" == "Repository" ]]; then
64+
echo -e "\e[01;39m$TEST_TYPE: New Version ($COMMENT)\e[0m"
65+
TEST_OUTPUT=$(scripts/detectNewVersion.sh)
66+
if grep -q "$ASSERTION" <<<$TEST_OUTPUT; then RESULT="\e[01;32mOK\e[0m"; else RESULT="\e[01;31mFAIL\e[0m"; FAILURE="TRUE"; fi
67+
echo -e " $RESULT - $TEST_OUTPUT"
68+
fi
69+
if [[ "$TEST_TYPE" == "Directory" ]]; then
70+
echo -e "\e[01;39m$TEST_TYPE - $DIRECTORY: New Version ($COMMENT)\e[0m"
71+
TEST_OUTPUT=$(scripts/detectNewVersion.sh -d "$DIRECTORY" -n "${DIRECTORY##*/}")
72+
if grep -q "$ASSERTION" <<<$TEST_OUTPUT; then RESULT="\e[01;32mOK\e[0m"; else RESULT="\e[01;31mFAIL\e[0m"; FAILURE="TRUE"; fi
73+
echo -e " $RESULT - $TEST_OUTPUT"
74+
fi
75+
}
76+
77+
relative_path="$(dirname "${BASH_SOURCE[0]}")"
78+
dir="$(realpath "${relative_path}")"
79+
80+
ci_name=$("${dir}/detect-ci.sh")
81+
origin=$(git config --get remote.origin.url)
82+
83+
#origin=${ci_name:-origin}
84+
# Executes in ANY CI so long as repo origin is one of the following.
85+
# Uncomment origin override to restrict this.
86+
[[ "$origin" =~ "[email protected]"* || "$ci_name" == "github" ]] && origin_host=github
87+
[[ "$origin" =~ "[email protected]"* || "$ci_name" == "gitlab" ]] && origin_host=gitlab
88+
[[ "$origin" =~ "[email protected]"* || "$ci_name" == "bitbucket" ]] && origin_host=bitbucket
89+
90+
case "$origin_host" in
91+
github)
92+
merge_string="Merge pull request #"
93+
column=7
94+
field=2
95+
;;
96+
gitlab)
97+
merge_string="Merge branch"
98+
column=4
99+
field=1
100+
;;
101+
bitbucket)
102+
merge_string="Merged in"
103+
column=4
104+
field=1
105+
;;
106+
*)
107+
echo -e "\e[01;31mERROR\e[0m: 591 - Unsupported origin host."
108+
exit 1
109+
;;
110+
esac
111+
112+
113+
# TEST: Repo Versioning
114+
115+
## Activities
116+
117+
# Tests:
118+
# - Repo Versioning
119+
# - Diectory Versioning
120+
# - x Minor Increments
121+
# - x Patch Increments
122+
123+
printHeading Running Test: $TEST
124+
125+
echo NOTE: These tests are not committed.
126+
127+
mkdir -p $TEST/{A..C}
128+
129+
# Directory Test: A
130+
# ASSERTIONS:
131+
# - Previous version is: A_0.0.0
132+
# - New Version is: ERROR: 599
133+
test_previous "Repository" "./" "$(scripts/detectPreviousVersion.sh)"
134+
test_new "Repository" "./" " 599"
135+
136+
test_previous "Directory" "$TEST/A" "A_0.0.0"
137+
test_new "Directory" "$TEST/A" " 599"
138+
139+
test_previous "Directory" "$TEST/B" "B_0.0.0"
140+
add_history "$TEST/B" 3 ops
141+
test_new "Directory" "$TEST/B" "B_0.0.3" patches +3
142+
git tag -a "B_0.0.3" -m "TAG"
143+
test_previous "Directory" "$TEST/B" "B_0.0.3" previous version tagged
144+
add_history "$TEST/B" 17 ops
145+
test_new "Directory" "$TEST/B" "B_0.0.20" patches +17
146+
git tag -d "B_0.0.3" >/dev/null 2>&1
147+
148+
test_previous "Directory" "$TEST/C" "C_0.0.0"
149+
add_history "$TEST/C" 9 ops
150+
test_new "Directory" "$TEST/C" "C_0.0.9" patches +9
151+
git tag -a "C_0.0.9" -m "TAG"
152+
test_previous "Directory" "$TEST/C" "C_0.0.9" previous version tagged
153+
test_new "Directory" "$TEST/C" " 599"
154+
155+
add_history "$TEST/C" 5 feature
156+
test_new "Directory" "$TEST/C" "C_0.5.0" features +5
157+
git tag -a "C_0.5.0" -m "TAG"
158+
add_history "$TEST/C" 19 ops
159+
test_new "Directory" "$TEST/C" "C_0.5.19" patches +19
160+
add_history "$TEST/C" 1 features major
161+
test_new "Directory" "$TEST/C" "C_1.0.0" features +1 BREAKING
162+
git tag -d "C_0.0.9" >/dev/null 2>&1
163+
git tag -d "C_0.5.0" >/dev/null 2>&1
164+

0 commit comments

Comments
 (0)