#!/bin/sh # # Copyright (c) 2024 Omar Polo # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. . ../cmdline/common.sh . ./common.sh test_file_changed() { local testroot=`test_init file_changed 1` got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone ret=$? if [ $ret -ne 0 ]; then echo "got clone failed unexpectedly" >&2 test_done "$testroot" 1 return 1 fi got checkout -q $testroot/repo-clone $testroot/wt >/dev/null ret=$? if [ $ret -ne 0 ]; then echo "got checkout failed unexpectedly" >&2 test_done "$testroot" 1 return 1 fi echo "change alpha" > $testroot/wt/alpha (cd $testroot/wt && got commit -m 'make changes' > /dev/null) local commit_id=`git_show_head $testroot/repo-clone` local author_time=`git_show_author_time $testroot/repo-clone` timeout 5 ./http-server -p $GOTD_TEST_HTTP_PORT \ > $testroot/stdout & got send -b main -q -r $testroot/repo-clone ret=$? if [ $ret -ne 0 ]; then echo "got send failed unexpectedly" >&2 test_done "$testroot" "1" return 1 fi wait %1 # wait for the http "server" d=`date -u -r $author_time +"%a %b %e %X %Y UTC"` cat <<-EOF > $testroot/stdout.expected POST / HTTP/1.1 Host: localhost:${GOTD_TEST_HTTP_PORT} Content-Type: application/json Content-Length: 224 User-Agent: got-notify-http/${GOT_VERSION_STR} Connection: close {"notifications":[{"short":false,"id":"$commit_id","author":"$GOT_AUTHOR","date":"$d","message":"make changes\n","diffstat":{},"changes":{}}]} EOF cmp -s $testroot/stdout.expected $testroot/stdout ret=$? if [ $ret -ne 0 ]; then diff -u $testroot/stdout.expected $testroot/stdout test_done "$testroot" "$ret" return 1 fi test_done "$testroot" "$ret" } test_many_commits_not_summarized() { local testroot=`test_init many_commits_not_summarized 1` got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone ret=$? if [ $ret -ne 0 ]; then echo "got clone failed unexpectedly" >&2 test_done "$testroot" 1 return 1 fi got checkout -q $testroot/repo-clone $testroot/wt >/dev/null ret=$? if [ $ret -ne 0 ]; then echo "got checkout failed unexpectedly" >&2 test_done "$testroot" 1 return 1 fi for i in `seq 1 24`; do echo "alpha $i" > $testroot/wt/alpha (cd $testroot/wt && got commit -m 'make changes' > /dev/null) local commit_id=`git_show_head $testroot/repo-clone` local author_time=`git_show_author_time $testroot/repo-clone` d=`date -u -r $author_time +"%a %b %e %X %Y UTC"` set -- "$@" "$commit_id $d" done timeout 5 ./http-server -p "$GOTD_TEST_HTTP_PORT" \ > $testroot/stdout & got send -b main -q -r $testroot/repo-clone ret=$? if [ $ret -ne 0 ]; then echo "got send failed unexpectedly" >&2 test_done "$testroot" "1" return 1 fi wait %1 # wait for the http "server" cat <<-EOF > $testroot/stdout.expected POST / HTTP/1.1 Host: localhost:${GOTD_TEST_HTTP_PORT} Content-Type: application/json Content-Length: 4939 User-Agent: got-notify-http/${GOT_VERSION_STR} Connection: close EOF printf '{"notifications":[' >> $testroot/stdout.expected comma="" for i in `seq 1 24`; do s=`pop_idx $i "$@"` commit_id=$(echo $s | cut -d' ' -f1) commit_time=$(echo $s | sed -e "s/^$commit_id //g") printf '%s{"short":false,"id":"%s","author":"%s","date":"%s","message":"%s","diffstat":{},"changes":{}}' \ "$comma" "$commit_id" "$GOT_AUTHOR" "$commit_time" "make changes\n" comma="," done >> $testroot/stdout.expected echo "]}" >> $testroot/stdout.expected cmp -s $testroot/stdout.expected $testroot/stdout ret=$? if [ $ret -ne 0 ]; then diff -u $testroot/stdout.expected $testroot/stdout test_done "$testroot" "$ret" return 1 fi test_done "$testroot" "$ret" } test_many_commits_summarized() { local testroot=`test_init many_commits_summarized 1` got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone ret=$? if [ $ret -ne 0 ]; then echo "got clone failed unexpectedly" >&2 test_done "$testroot" 1 return 1 fi got checkout -q $testroot/repo-clone $testroot/wt >/dev/null ret=$? if [ $ret -ne 0 ]; then echo "got checkout failed unexpectedly" >&2 test_done "$testroot" 1 return 1 fi for i in `seq 1 51`; do echo "alpha $i" > $testroot/wt/alpha (cd $testroot/wt && got commit -m 'make changes' > /dev/null) local commit_id=`git_show_head $testroot/repo-clone` local short_commit_id=`trim_obj_id 33 $commit_id` local author_time=`git_show_author_time $testroot/repo-clone` d=`date -u -r $author_time +"%G-%m-%d"` set -- "$@" "$short_commit_id $d" done timeout 5 ./http-server -p "$GOTD_TEST_HTTP_PORT" \ > $testroot/stdout & got send -b main -q -r $testroot/repo-clone ret=$? if [ $ret -ne 0 ]; then echo "got send failed unexpectedly" >&2 test_done "$testroot" "1" return 1 fi wait %1 # wait for the http "server" cat <<-EOF > $testroot/stdout.expected POST / HTTP/1.1 Host: localhost:${GOTD_TEST_HTTP_PORT} Content-Type: application/json Content-Length: 4864 User-Agent: got-notify-http/${GOT_VERSION_STR} Connection: close EOF printf '{"notifications":[' >> $testroot/stdout.expected comma="" for i in `seq 1 51`; do s=`pop_idx $i "$@"` commit_id=$(echo $s | cut -d' ' -f1) commit_time=$(echo $s | sed -e "s/^$commit_id //g") printf '%s{"short":true,"id":"%s","author":"%s","date":"%s","message":"%s"}' \ "$comma" "$commit_id" "$GOT_AUTHOR_8" \ "$commit_time" "make changes" comma="," done >> $testroot/stdout.expected echo "]}" >> $testroot/stdout.expected cmp -s $testroot/stdout.expected $testroot/stdout ret=$? if [ $ret -ne 0 ]; then diff -u $testroot/stdout.expected $testroot/stdout test_done "$testroot" "$ret" return 1 fi test_done "$testroot" "$ret" } test_parseargs "$@" run_test test_file_changed run_test test_many_commits_not_summarized run_test test_many_commits_summarized