"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Mark Jamsek <mark@jamsek.com>
Subject:
Re: tog: basic diff regress
To:
Game of Trees <gameoftrees@openbsd.org>
Date:
Sat, 15 Apr 2023 18:59:49 +1000

Download raw body.

Thread
On 23-04-15 06:42PM, Mark Jamsek wrote:
> On 23-04-15 09:52AM, Stefan Sperling wrote:
> > On Sat, Apr 15, 2023 at 02:35:59PM +1000, Mark Jamsek wrote:
> > > The below diff applies on top, and makes $testname global so we can use
> > > that for each test script path. The objective is to minimise repeated
> > > copypasta errors. With this, if a new test is written that copypastas an
> > > existing similar one as a template, we don't have to worry about
> > > changing the script path each time. What do you think?
> > 
> > I think it would make more sense to export the GOT_TOG_TEST variable
> > to all tests, in order to match the name used by code in tog.c.
> > 
> > And could this variable be renamed to something that describes the
> > prupose more clearly, like TOG_TEST_SCRIPT?
> > (tog does not use a GOT_ prefix for its other environment variables)
> > 
> > Then the tests would read like this:
> > 
> > 	cat <<EOF >$TOG_TEST_SCRIPT
> >   SCREENDUMP
> >   EOF
> 
> Yes, I like that idea much better! I'll do that now.
> 
> Here's a basic tog blame test. We need some delay in order to capture
> the blame view once it has finished annotating the file else we capture
> the '........' instead of the hash prefix.

This is a rebased diff on top of main with the basic diff and blame
regress, and your TOG_TEST_SCRIPT suggestion.

diff refs/remotes/origin/main refs/heads/main
commit - 3ab12c25da83457de7f24433e48198c1206e56d5
commit + 12a60afa798a175502eca27729f4fcaa07bc14aa
blob - 01409b433f1bc9d3bbbc19b1428c29ddf72802e1
blob + eca41169193af38a1320fdb788385993cd078872
--- regress/tog/Makefile
+++ regress/tog/Makefile
@@ -1,4 +1,4 @@
-REGRESS_TARGETS=log
+REGRESS_TARGETS=log diff blame
 NOOBJ=Yes
 
 GOT_TEST_ROOT=/tmp
@@ -6,4 +6,10 @@ log:
 log:
 	./log.sh -q -r "$(GOT_TEST_ROOT)"
 
+diff:
+	./diff.sh -q -r "$(GOT_TEST_ROOT)"
+
+blame:
+	./blame.sh -q -r "$(GOT_TEST_ROOT)"
+
 .include <bsd.regress.mk>
blob - cba908caf919a31069c38c9bd18d8b7ea8952ba1
blob + e03be4c0dcc342c5c4bee7432c3f25219682d085
--- regress/tog/common.sh
+++ regress/tog/common.sh
@@ -62,7 +62,7 @@ set_test_env()
 
 set_test_env()
 {
-	export GOT_TOG_TEST=$1
+	export TOG_TEST_SCRIPT=$1
 	export TOG_SCR_DUMP=$2
 
 	if [ -n "${3}" ]; then
@@ -88,7 +88,7 @@ test_init()
 
 	testroot=`mktemp -d "$GOT_TEST_ROOT/tog-test-$testname-XXXXXXXX"`
 
-	set_test_env $testroot/log_test $testroot/view $columns $lines
+	set_test_env $testroot/$testname $testroot/view $columns $lines
 
 	mkdir $testroot/repo
 	git_init $testroot/repo
blob - /dev/null
blob + 6111e72d88fd895142b80755b0d07a795e739aa5 (mode 755)
--- /dev/null
+++ regress/tog/blame.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+#
+# Copyright (c) 2023 Mark Jamsek <mark@jamsek.dev>
+#
+# 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.
+
+. ./common.sh
+
+test_blame_basic()
+{
+	test_init blame_basic 80 8
+
+	local commit_id1=`git_show_head $testroot/repo`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo aaaa >> $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m "a change" > /dev/null)
+	local commit_id2=`git_show_head $testroot/repo`
+
+	echo bbbb >> $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m "b change" > /dev/null)
+	local commit_id3=`git_show_head $testroot/repo`
+
+	echo cccc >> $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m "c change" > /dev/null)
+	local commit_id4=`git_show_head $testroot/repo`
+	local author_time=`git_show_author_time $testroot/repo`
+	local ymd=`date -u -r $author_time +"%G-%m-%d"`
+
+	cat <<EOF >$TOG_TEST_SCRIPT
+SLEEP 1		wait for blame to finish
+SCREENDUMP
+EOF
+
+	local commit_id1_short=`trim_obj_id 32 $commit_id1`
+	local commit_id2_short=`trim_obj_id 32 $commit_id2`
+	local commit_id3_short=`trim_obj_id 32 $commit_id3`
+	local commit_id4_short=`trim_obj_id 32 $commit_id4`
+
+	cat <<EOF >$testroot/view.expected
+commit $commit_id4
+[1/4] /alpha
+$commit_id1_short alpha
+$commit_id2_short aaaa
+$commit_id3_short bbbb
+$commit_id4_short cccc
+
+
+EOF
+
+	cd $testroot/wt && tog blame alpha
+	cmp -s $testroot/view.expected $testroot/view
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/view.expected $testroot/view
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	test_done "$testroot" "$ret"
+}
+
+test_parseargs "$@"
+run_test test_blame_basic
blob - 17a02a06bcc9a0d2dfc44329aa56339f21148c9d
blob + f48397af6cde738244b53c3c26263b6fa6031107
--- regress/tog/log.sh
+++ regress/tog/log.sh
@@ -25,7 +25,7 @@ test_log_hsplit_diff()
 	local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
 	local ymd=`date -u -r $author_time +"%G-%m-%d"`
 
-	cat <<EOF >$testroot/log_test
+	cat <<EOF >$TOG_TEST_SCRIPT
 KEY_ENTER	open diff view of selected commit
 S		toggle horizontal split
 SCREENDUMP
@@ -82,7 +82,7 @@ test_log_vsplit_diff()
 	local blobid_alpha=`get_blob_id $testroot/repo "" alpha`
 	local blobid_beta=`get_blob_id $testroot/repo "" beta`
 
-	cat <<EOF >$testroot/log_test
+	cat <<EOF >$TOG_TEST_SCRIPT
 KEY_ENTER	open diff view of selected commit in vertical split
 SCREENDUMP
 EOF
@@ -146,7 +146,7 @@ test_log_show_author()
 	local commit1=`git_show_head $testroot/repo`
 	local id1_len8=`trim_obj_id 32 $commit1`
 
-	cat <<EOF >$testroot/log_test
+	cat <<EOF >$TOG_TEST_SCRIPT
 @		toggle show author
 SCREENDUMP
 EOF
@@ -186,7 +186,7 @@ test_log_scroll_right()
 
 	local commit1=`git_show_head $testroot/repo`
 
-	cat <<EOF >$testroot/log_test
+	cat <<EOF >$TOG_TEST_SCRIPT
 l		scroll right
 l		scroll right
 SCREENDUMP
@@ -219,7 +219,7 @@ test_log_hsplit_ref()
 	local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
 	local ymd=`date -u -r $author_time +"%G-%m-%d"`
 
-	cat <<EOF >$testroot/log_test
+	cat <<EOF >$TOG_TEST_SCRIPT
 R		open ref view
 S		toggle horizontal split
 -		reduce size of ref view split
@@ -260,7 +260,7 @@ test_log_hsplit_tree()
 	local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
 	local ymd=`date -u -r $author_time +"%G-%m-%d"`
 
-	cat <<EOF >$testroot/log_test
+	cat <<EOF >$TOG_TEST_SCRIPT
 T		open tree view
 S		toggle horizontal split
 j		move selection cursor down one entry to "beta"
@@ -306,7 +306,7 @@ test_log_logmsg_widechar()
 	local commit1=`git_show_parent_commit $testroot/repo`
 	local blobid=`get_blob_id $testroot/repo "" $(widechar_filename)`
 
-	cat <<EOF >$testroot/log_test
+	cat <<EOF >$TOG_TEST_SCRIPT
 KEY_ENTER	open selected commit in diff view
 F		toggle fullscreen
 SCREENDUMP
blob - /dev/null
blob + 04242def3c3bd1f7ec0b24922210fbedced28abb (mode 755)
--- /dev/null
+++ regress/tog/diff.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+#
+# Copyright (c) 2023 Mark Jamsek <mark@jamsek.dev>
+#
+# 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.
+
+. ./common.sh
+
+test_diff_contiguous_commits()
+{
+	test_init diff_contiguous_commits
+
+	local commit_id1=`git_show_head $testroot/repo`
+	local alpha_id_old=`get_blob_id $testroot/repo "" alpha`
+
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "changed alpha"
+	local author_time=`git_show_author_time $testroot/repo`
+	local date=`date -u -r $author_time +"%a %b %e %X %Y UTC"`
+	local head_id=`git_show_head $testroot/repo`
+	local head_id_truncated=`trim_obj_id 13 $head_id`
+	local alpha_id=`get_blob_id $testroot/repo "" alpha`
+
+	cat <<EOF >$TOG_TEST_SCRIPT
+SCREENDUMP
+EOF
+
+	cat <<EOF >$testroot/view.expected
+[1/20] diff $commit_id1 $head_id_truncated
+commit $head_id (master)
+from: Flan Hacker <flan_hacker@openbsd.org>
+date: $date
+
+changed alpha
+
+M  alpha  |  1+  1-
+
+1 file changed, 1 insertion(+), 1 deletion(-)
+
+commit - $commit_id1
+commit + $head_id
+blob - $alpha_id_old
+blob + $alpha_id
+--- alpha
++++ alpha
+@@ -1 +1 @@
+-alpha
++modified alpha
+
+
+
+(END)
+EOF
+
+	cd $testroot/repo && tog diff $commit_id1 $head_id
+	cmp -s $testroot/view.expected $testroot/view
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/view.expected $testroot/view
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	test_done "$testroot" "$ret"
+}
+
+test_diff_arbitrary_commits()
+{
+	test_init diff_arbitrary_commits 80 18
+
+	local commit_id1=`git_show_head $testroot/repo`
+	local alpha_id_old=`get_blob_id $testroot/repo "" alpha`
+
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "changed alpha"
+	local commit_id2=`git_show_head $testroot/repo`
+
+	echo "modified alpha again" > $testroot/repo/alpha
+	echo "new file" > $testroot/repo/new
+	(cd $testroot/repo && git add new)
+	git_commit $testroot/repo -m "new file"
+	local head_id=`git_show_head $testroot/repo`
+	local head_id_truncated=`trim_obj_id 13 $head_id`
+	local alpha_id=`get_blob_id $testroot/repo "" alpha`
+	local new_id=`get_blob_id $testroot/repo "" new`
+
+	cat <<EOF >$TOG_TEST_SCRIPT
+SCREENDUMP
+EOF
+
+	cat <<EOF >$testroot/view.expected
+[1/16] diff $commit_id1 $head_id_truncated
+commit - $commit_id1
+commit + $head_id
+blob - $alpha_id_old
+blob + $alpha_id
+--- alpha
++++ alpha
+@@ -1 +1 @@
+-alpha
++modified alpha again
+blob - /dev/null
+blob + $new_id (mode 644)
+--- /dev/null
++++ new
+@@ -0,0 +1 @@
++new file
+
+(END)
+EOF
+
+	cd $testroot/repo && tog diff $commit_id1 $head_id
+	cmp -s $testroot/view.expected $testroot/view
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/view.expected $testroot/view
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	test_done "$testroot" "$ret"
+}
+
+test_parseargs "$@"
+run_test test_diff_contiguous_commits
+run_test test_diff_arbitrary_commits
blob - e94969c6a9902f9b0f221e3e3dc2e5e2b10ff627
blob + 58b6de2aeabae779595bb12e87b863f66f0600a9
--- tog/tog.c
+++ tog/tog.c
@@ -1637,6 +1637,11 @@ tog_read_script_key(FILE *script, int *ch, int *done)
 			err = got_ferror(script, GOT_ERR_IO);
 			goto done;
 		}
+	}
+	if (strncasecmp(line, "SLEEP ", 6) == 0 &&
+	    isdigit((unsigned char)line[6])) {
+		sleep(line[6] - '0');
+		*ch = -1;
 	} else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
 		*ch = KEY_ENTER;
 	else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
@@ -4168,7 +4173,7 @@ init_mock_term(const char *test_script_path)
 	const struct got_error	*err = NULL;
 
 	if (test_script_path == NULL || *test_script_path == '\0')
-		return got_error_msg(GOT_ERR_IO, "GOT_TOG_TEST not defined");
+		return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
 
 	tog_io.f = fopen(test_script_path, "re");
 	if (tog_io.f == NULL) {
@@ -9738,7 +9743,7 @@ main(int argc, char *argv[])
 	 * Test mode init must happen before pledge() because "tty" will
 	 * not allow TTY-related ioctls to occur via regular files.
 	 */
-	test_script_path = getenv("GOT_TOG_TEST");
+	test_script_path = getenv("TOG_TEST_SCRIPT");
 	if (test_script_path != NULL) {
 		error = init_mock_term(test_script_path);
 		if (error) {

-- 
Mark Jamsek <fnc.bsdbox.org|got.bsdbox.org>
GPG: F2FF 13DE 6A06 C471 CA80  E6E2 2930 DC66 86EE CF68