Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2024 Omar Polo <op@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 . ../cmdline/common.sh
18 . ./common.sh
20 test_file_changed() {
21 local testroot=`test_init file_changed 1`
23 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
24 ret=$?
25 if [ $ret -ne 0 ]; then
26 echo "got clone failed unexpectedly" >&2
27 test_done "$testroot" 1
28 return 1
29 fi
31 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
32 ret=$?
33 if [ $ret -ne 0 ]; then
34 echo "got checkout failed unexpectedly" >&2
35 test_done "$testroot" 1
36 return 1
37 fi
39 echo "change alpha" > $testroot/wt/alpha
40 (cd $testroot/wt && got commit -m 'make changes' > /dev/null)
41 local commit_id=`git_show_head $testroot/repo-clone`
42 local author_time=`git_show_author_time $testroot/repo-clone`
44 timeout 5 ./http-server -p $GOTD_TEST_HTTP_PORT \
45 > $testroot/stdout &
47 got send -b main -q -r $testroot/repo-clone
48 ret=$?
49 if [ $ret -ne 0 ]; then
50 echo "got send failed unexpectedly" >&2
51 test_done "$testroot" "1"
52 return 1
53 fi
55 wait %1 # wait for the http "server"
57 d=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
59 cat <<-EOF > $testroot/stdout.expected
60 POST / HTTP/1.1
61 Host: localhost:${GOTD_TEST_HTTP_PORT}
62 Content-Type: application/json
63 Content-Length: 224
64 User-Agent: got-notify-http/${GOT_VERSION_STR}
65 Connection: close
67 {"notifications":[{"short":false,"id":"$commit_id","author":"$GOT_AUTHOR","date":"$d","message":"make changes\n","diffstat":{},"changes":{}}]}
68 EOF
70 cmp -s $testroot/stdout.expected $testroot/stdout
71 ret=$?
72 if [ $ret -ne 0 ]; then
73 diff -u $testroot/stdout.expected $testroot/stdout
74 test_done "$testroot" "$ret"
75 return 1
76 fi
78 test_done "$testroot" "$ret"
79 }
81 test_many_commits_not_summarized() {
82 local testroot=`test_init many_commits_not_summarized 1`
84 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
85 ret=$?
86 if [ $ret -ne 0 ]; then
87 echo "got clone failed unexpectedly" >&2
88 test_done "$testroot" 1
89 return 1
90 fi
92 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
93 ret=$?
94 if [ $ret -ne 0 ]; then
95 echo "got checkout failed unexpectedly" >&2
96 test_done "$testroot" 1
97 return 1
98 fi
100 for i in `seq 1 24`; do
101 echo "alpha $i" > $testroot/wt/alpha
102 (cd $testroot/wt && got commit -m 'make changes' > /dev/null)
103 local commit_id=`git_show_head $testroot/repo-clone`
104 local author_time=`git_show_author_time $testroot/repo-clone`
105 d=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
106 set -- "$@" "$commit_id $d"
107 done
109 timeout 5 ./http-server -p "$GOTD_TEST_HTTP_PORT" \
110 > $testroot/stdout &
112 got send -b main -q -r $testroot/repo-clone
113 ret=$?
114 if [ $ret -ne 0 ]; then
115 echo "got send failed unexpectedly" >&2
116 test_done "$testroot" "1"
117 return 1
118 fi
120 wait %1 # wait for the http "server"
122 cat <<-EOF > $testroot/stdout.expected
123 POST / HTTP/1.1
124 Host: localhost:${GOTD_TEST_HTTP_PORT}
125 Content-Type: application/json
126 Content-Length: 4939
127 User-Agent: got-notify-http/${GOT_VERSION_STR}
128 Connection: close
130 EOF
132 printf '{"notifications":[' >> $testroot/stdout.expected
133 comma=""
134 for i in `seq 1 24`; do
135 s=`pop_idx $i "$@"`
136 commit_id=$(echo $s | cut -d' ' -f1)
137 commit_time=$(echo $s | sed -e "s/^$commit_id //g")
138 printf '%s{"short":false,"id":"%s","author":"%s","date":"%s","message":"%s","diffstat":{},"changes":{}}' \
139 "$comma" "$commit_id" "$GOT_AUTHOR" "$commit_time" "make changes\n"
140 comma=","
141 done >> $testroot/stdout.expected
142 echo "]}" >> $testroot/stdout.expected
144 cmp -s $testroot/stdout.expected $testroot/stdout
145 ret=$?
146 if [ $ret -ne 0 ]; then
147 diff -u $testroot/stdout.expected $testroot/stdout
148 test_done "$testroot" "$ret"
149 return 1
150 fi
152 test_done "$testroot" "$ret"
155 test_many_commits_summarized() {
156 local testroot=`test_init many_commits_summarized 1`
158 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
159 ret=$?
160 if [ $ret -ne 0 ]; then
161 echo "got clone failed unexpectedly" >&2
162 test_done "$testroot" 1
163 return 1
164 fi
166 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
167 ret=$?
168 if [ $ret -ne 0 ]; then
169 echo "got checkout failed unexpectedly" >&2
170 test_done "$testroot" 1
171 return 1
172 fi
174 for i in `seq 1 51`; do
175 echo "alpha $i" > $testroot/wt/alpha
176 (cd $testroot/wt && got commit -m 'make changes' > /dev/null)
177 local commit_id=`git_show_head $testroot/repo-clone`
178 local short_commit_id=`trim_obj_id 33 $commit_id`
179 local author_time=`git_show_author_time $testroot/repo-clone`
180 d=`date -u -r $author_time +"%G-%m-%d"`
181 set -- "$@" "$short_commit_id $d"
182 done
184 timeout 5 ./http-server -p "$GOTD_TEST_HTTP_PORT" \
185 > $testroot/stdout &
187 got send -b main -q -r $testroot/repo-clone
188 ret=$?
189 if [ $ret -ne 0 ]; then
190 echo "got send failed unexpectedly" >&2
191 test_done "$testroot" "1"
192 return 1
193 fi
195 wait %1 # wait for the http "server"
197 cat <<-EOF > $testroot/stdout.expected
198 POST / HTTP/1.1
199 Host: localhost:${GOTD_TEST_HTTP_PORT}
200 Content-Type: application/json
201 Content-Length: 4864
202 User-Agent: got-notify-http/${GOT_VERSION_STR}
203 Connection: close
205 EOF
207 printf '{"notifications":[' >> $testroot/stdout.expected
208 comma=""
209 for i in `seq 1 51`; do
210 s=`pop_idx $i "$@"`
211 commit_id=$(echo $s | cut -d' ' -f1)
212 commit_time=$(echo $s | sed -e "s/^$commit_id //g")
213 printf '%s{"short":true,"id":"%s","author":"%s","date":"%s","message":"%s"}' \
214 "$comma" "$commit_id" "$GOT_AUTHOR_8" \
215 "$commit_time" "make changes"
216 comma=","
217 done >> $testroot/stdout.expected
218 echo "]}" >> $testroot/stdout.expected
220 cmp -s $testroot/stdout.expected $testroot/stdout
221 ret=$?
222 if [ $ret -ne 0 ]; then
223 diff -u $testroot/stdout.expected $testroot/stdout
224 test_done "$testroot" "$ret"
225 return 1
226 fi
228 test_done "$testroot" "$ret"
231 test_parseargs "$@"
232 run_test test_file_changed
233 run_test test_many_commits_not_summarized
234 run_test test_many_commits_summarized