Hi,

On Wed, Jun 04, 2025 at 11:30:32AM +1000, Xan Phung wrote:
> ---
>  .gitignore               |   2 +
>  Makefile                 |  11 ++--
>  tests/README             | 109 +++++++++++++++++++++++++++++++++++++++
>  tests/echo-n.sh          |  10 ++++
>  tests/echo-n.sh.expected |   1 +
>  5 files changed, 130 insertions(+), 3 deletions(-)
>  create mode 100644 tests/README
>  create mode 100755 tests/echo-n.sh
>  create mode 100644 tests/echo-n.sh.expected

Good point, this is something that we discussed many times but we
didn't do before just because we were very lazy. I think it is a
good moment to begin with the tests.

Saying that, I think the test no-framework should be much more
simple, and encapsulated in the test directory. Every shell script
test should be independent, not sharing magic environment variables
and being responsible of how to test, instead of relaying in having
an expected file. Having the expected file works in some cases, but
in our case where in many cases we have to deal with signals it
does not behave very well.

Also, having just a set of shell scripts and using their exit status
remove the need of any documentation. Just try to get a successful
shell script execution.

My proposal is this:

8< -----

>From a75639f1799b03f164592c6f0d632a96f7000da7 Mon Sep 17 00:00:00 2001
From: "Roberto E. Vargas Caballero" <[email protected]>
Date: Wed, 5 Nov 2025 18:41:15 +0100
Subject: [PATCH] tests: Add initial support for tests

---
 .gitignore         |  2 ++
 Makefile           |  9 +++++++--
 tests/0001-echo.sh | 23 +++++++++++++++++++++++
 tests/Makefile     |  6 ++++++
 tests/runtests.sh  | 22 ++++++++++++++++++++++
 5 files changed, 60 insertions(+), 2 deletions(-)
 create mode 100755 tests/0001-echo.sh
 create mode 100644 tests/Makefile
 create mode 100755 tests/runtests.sh

diff --git a/.gitignore b/.gitignore
index f338199..0060cdd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -104,3 +104,5 @@
 /xargs
 /xinstall
 /yes
+/tests/test.log
+/tests/test.lst
diff --git a/Makefile b/Makefile
index 6596063..c478a2f 100644
--- a/Makefile
+++ b/Makefile
@@ -202,7 +202,7 @@ MAKEOBJ =\
 OBJ = $(LIBUTFOBJ) $(LIBUTILOBJ) $(MAKEOBJ)
 
 all: scripts/make
-       $(SMAKE) $(BIN)
+       +@$(SMAKE) $(BIN)
 
 scripts/make:
        $(CC) -o $@ make/*.c
@@ -252,9 +252,13 @@ sbase-box-uninstall: sbase-box proto
        $(DESTDIR)$(PREFIX)/bin/sbase-box -d $(DESTDIR)$(PREFIX)/bin/
        scripts/uninstall proto
 
+tests: all
+       @cd $@ && $(MAKE)
+
 dist: clean
        mkdir -p sbase
-       cp -R LICENSE Makefile README TODO config.mk *.c *.1 *.h libutf libutil 
make scripts sbase
+       cp LICENSE Makefile README TODO config.mk *.c *.1 *.h  sbase
+       cp -R libutf libutil make scripts tests sbase
        mv sbase sbase-$(VERSION)
        tar -cf sbase-$(VERSION).tar sbase-$(VERSION)
        gzip sbase-$(VERSION).tar
@@ -265,6 +269,7 @@ sbase-box: $(BIN)
        $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ build/*.c $(LIB)
 
 clean:
+       @cd tests && $(MAKE) clean
        rm -f $(BIN) $(OBJ) $(LIB) sbase-box sbase-$(VERSION).tar.gz
        rm -f scripts/make
        rm -f getconf.h
diff --git a/tests/0001-echo.sh b/tests/0001-echo.sh
new file mode 100755
index 0000000..7eb961f
--- /dev/null
+++ b/tests/0001-echo.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+set -e
+
+tmp1=tmp1.$$
+tmp2=tmp2.$$
+
+cleanup()
+{
+       st=$?
+       rm -f $tmp1 $tmp2
+       exit $st
+}
+
+trap cleanup EXIT HUP INT TERM
+
+cat <<'EOF' | tr -d '\n' > $tmp1
+--hello-- --world--!
+EOF
+
+../echo -n --hello-- --world--! > $tmp2
+
+diff -u $tmp1 $tmp2
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 0000000..1311918
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,6 @@
+all:
+       @./runtests.sh
+
+clean:
+       rm -f test.log
+       rm -f tmp*
diff --git a/tests/runtests.sh b/tests/runtests.sh
new file mode 100755
index 0000000..9433ae2
--- /dev/null
+++ b/tests/runtests.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+export TZ=UTC
+
+cleanup()
+{
+       st=$?
+       rm -f test.res
+       exit $st
+}
+
+trap cleanup EXIT HUP INT TERM
+
+for i in *-*.sh
+do
+       printf "Test: %s\n\n" $i >> test.log
+       (./$i >> test.log 2>&1 && printf '[PASS]\t' || printf '[FAIL]\t'
+       echo "$i") | tee -a test.log
+done |
+tee test.res
+
+! grep FAIL test.res >/dev/null
-- 
2.46.1


Reply via email to