forked from hangxingliu/wslgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
executable file
·125 lines (104 loc) · 3.75 KB
/
main.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
#=============================
#region Utils function
throw() {
echo -e "$RED[-] failed: $1$RESET";
exit 1;
}
clean-tmp-files() {
rm -f tmp*.*;
[[ -d "tmp-test-repo/.git" ]] && rm -rf tmp-test-repo;
}
setup-color-variables() {
if [[ -t 1 ]] && [[ "$(tput colors)" -ge 8 ]]; then
BOLD="\x1b[1m"; RED="\x1b[31m"; GREEN="\x1b[32m"; RESET="\x1b[0m";
fi
}
print-test-header() {
[[ -z "$test_counter" ]] && test_counter=1;
printf "$BOLD[.] test 0x%02x: %s$RESET\n" "$test_counter" "$1";
test_counter=$(($test_counter+1));
}
assert-expected-actual() {
diff -u tmp-expected.log tmp-actual.log ||
throw "actual log is not same as expected log!";
}
UNIT_TEST_SIGN="need-to-be-replaced-in-unit-test";
generate-wslgit-with-mock-mount-info() {
gawk -v replaceto="$1" '
/^\s+#\s*region\s+'$UNIT_TEST_SIGN'/ { print replaceto; ignore=1; next; }
/^\s+#\s*endregion\s+'$UNIT_TEST_SIGN'/ { ignore=0; next; }
ignore { next; }
{ print $0; }
' ../wslgit.dev.sh > tmp.sh || throw "generate tmp.sh for ci test failed!"
chmod +x tmp.sh || throw;
}
setup-color-variables;
#endregion
#=============================
_CWD="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)";
_PROJECT_DIR="$(dirname "$_CWD")";
# ==================_=======================
# _ __ ___ __ _(_)_ __
# | '_ ` _ \ / _` | | '_ \
# | | | | | | (_| | | | | |
# |_| |_| |_|\__,_|_|_| |_|
# ==========================================
main() {
pushd "$_CWD" || throw;
clean-tmp-files;
echo -e "$BOLD[.] check dependencies $RESET";
echo " gawk: $(ls -al "$(which gawk)")";
echo " mawk: $(ls -al "$(which mawk)")";
[[ -n "$(which gawk)" ]] || throw "gawk is not installed!";
[[ -n "$(which mawk)" ]] || throw "mawk is not installed!";
echo -e "$BOLD[.] test bash snippets $RESET";
bash ./bash-snippets/is-contains.sh || throw;
bash ./bash-snippets/drvfs-list-with-mawk.sh || throw;
echo -e "$BOLD[.] generate tmp.sh (gawk, C: => ${_PROJECT_DIR})$RESET";
generate-wslgit-with-mock-mount-info " echo 'C:\n$_PROJECT_DIR'";
start-test;
echo -e "$BOLD[.] generate tmp.sh (mawk, C: => ${_PROJECT_DIR})$RESET";
gawk '/gawk/ { gsub("gawk", "mawk"); } { print $0; }' ./tmp.sh > ./tmp2.sh || throw;
mv ./tmp2.sh ./tmp.sh && chmod +x tmp.sh || throw;
start-test;
echo -e "$BOLD$GREEN[+] all tests success! $RESET";
clean-tmp-files;
}
start-test() {
print-test-header "without argument";
git > tmp-expected.log
./tmp.sh > tmp-actual.log;
assert-expected-actual;
print-test-header "git --version";
git --version > tmp-expected.log
./tmp.sh --version > tmp-actual.log;
assert-expected-actual;
print-test-header "git rev-parse --show-toplevel";
echo "C:" > tmp-expected.log
./tmp.sh rev-parse --show-toplevel > tmp-actual.log;
assert-expected-actual;
print-test-header "git init";
if [[ -d tmp-test-repo ]]; then rm -rf tmp-test-repo || throw; fi
mkdir -p tmp-test-repo || throw;
pushd "tmp-test-repo" || throw;
echo "Initialized empty Git repository in C:\\test-ci\\tmp-test-repo\\.git\\" > tmp-expected.log;
../tmp.sh init | tee tmp-actual.log || throw;
assert-expected-actual;
print-test-header "git add file";
echo "# Just test" > 'this file.txt';
../tmp.sh add -v "C:\\test-ci\\tmp-test-repo\\this file.txt" || throw;
print-test-header "git commit";
../tmp.sh commit -m "init commit" || throw;
print-test-header "git git rev-parse --show-toplevel";
echo "C:\\test-ci\\tmp-test-repo" > tmp-expected.log
../tmp.sh rev-parse --show-toplevel > tmp-actual.log;
assert-expected-actual;
ARCHIVE_FILE='C:\\test-ci\\tmp-HEAD.zip';
print-test-header "git archive -v --format=zip --output=$ARCHIVE_FILE HEAD";
../tmp.sh archive -v --format=zip --output="$ARCHIVE_FILE" HEAD || throw;
[[ -f ../tmp-HEAD.zip ]] || throw;
popd; # exit directory tmp-test-repo
}
# launch from main function
main