]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/commitdiff
Add simple test suite.
authorSimon Ruderich <simon@ruderich.org>
Sat, 1 Jun 2013 19:51:28 +0000 (21:51 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sat, 1 Jun 2013 19:51:28 +0000 (21:51 +0200)
14 files changed:
.gitignore
Makefile.am
configure.ac
tests/Makefile.am [new file with mode: 0644]
tests/example-noforce.sh [new symlink]
tests/example-noforce.sh.expected [new file with mode: 0644]
tests/example-redirects.sh [new file with mode: 0755]
tests/example-redirects.sh.expected [new file with mode: 0644]
tests/example-simple.sh [new file with mode: 0755]
tests/example-simple.sh.expected [new file with mode: 0644]
tests/example.c [new file with mode: 0644]
tests/example.expected [new file with mode: 0644]
tests/lib.sh [new file with mode: 0644]
tests/run.sh [new file with mode: 0755]

index e52dbb880e7498951c68f007445eda2a20fba78b..0148b421e2edd83d9a409a08506e067bfea85416 100644 (file)
 /src/coloredstderr.lo
 /src/libcoloredstderr.la
 /stamp-h1
+/tests/.deps/
+/tests/Makefile
+/tests/Makefile.in
+/tests/example
+/tests/example.o
+/tests/run.sh.log
+/tests/run.sh.trs
+/tests/test-suite.log
index 26226b0a29dc34a6ddb2c618ba127587bbfa1567..840d178d320c0d8c1f546164c0a06e1f172ce740 100644 (file)
@@ -1,3 +1,3 @@
-SUBDIRS = src
+SUBDIRS = src tests
 
 ACLOCAL_AMFLAGS = -I m4
index 6cd9d8fe187fc7104736e8ca216bb5fc475fa4a8..bfcb85e72e37db95ddfdb65f741b2e337ca9d40d 100644 (file)
@@ -28,6 +28,10 @@ LT_INIT([disable-static])
 
 AC_PROG_CC
 
+dnl Used by test suite.
+AC_PROG_SED
+AC_PROG_EGREP
+
 AC_C_INLINE
 
 AC_CHECK_HEADERS([fcntl.h])
@@ -47,5 +51,5 @@ AC_ARG_ENABLE([debug],
                    AC_DEFINE([DEBUG], 1, [Define to enable debug output.])
                fi])
 
-AC_CONFIG_FILES([Makefile src/Makefile])
+AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile])
 AC_OUTPUT
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644 (file)
index 0000000..9928cdc
--- /dev/null
@@ -0,0 +1,15 @@
+TESTS = run.sh
+check_PROGRAMS = example
+example_SOURCES = example.c
+
+dist_check_SCRIPTS = run.sh lib.sh
+dist_check_DATA = example.expected \
+                  example-noforce.sh \
+                  example-noforce.sh.expected \
+                  example-redirects.sh \
+                  example-redirects.sh.expected \
+                  example-simple.sh \
+                  example-simple.sh.expected
+
+# Used by run.sh.
+export EGREP
diff --git a/tests/example-noforce.sh b/tests/example-noforce.sh
new file mode 120000 (symlink)
index 0000000..9f2d093
--- /dev/null
@@ -0,0 +1 @@
+example-simple.sh
\ No newline at end of file
diff --git a/tests/example-noforce.sh.expected b/tests/example-noforce.sh.expected
new file mode 100644 (file)
index 0000000..9285a60
--- /dev/null
@@ -0,0 +1,3 @@
+write to stdout
+write to stderr
+write to stdout without newlinewrite to stderr without newline
\ No newline at end of file
diff --git a/tests/example-redirects.sh b/tests/example-redirects.sh
new file mode 100755 (executable)
index 0000000..8ac150d
--- /dev/null
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+# Copyright (C) 2013  Simon Ruderich
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+echo write to stderr 1 >&2
+echo write to stdout 1
+
+(
+    echo write to stdout which gets redirected to stderr
+) >&2
+
+echo write to stdout 2
+
+(
+    echo write to stderr which gets redirected to stdout >&2
+) 2>&1
+
+echo write to stdout 3
+
+(
+    (
+        echo another redirect to stderr
+    ) >&3
+) 3>&2
diff --git a/tests/example-redirects.sh.expected b/tests/example-redirects.sh.expected
new file mode 100644 (file)
index 0000000..b702b14
--- /dev/null
@@ -0,0 +1,8 @@
+>stderr>write to stderr 1
+<stderr<write to stdout 1
+>stderr>write to stdout which gets redirected to stderr
+<stderr<write to stdout 2
+write to stderr which gets redirected to stdout
+write to stdout 3
+>stderr>another redirect to stderr
+<stderr<
\ No newline at end of file
diff --git a/tests/example-simple.sh b/tests/example-simple.sh
new file mode 100755 (executable)
index 0000000..523fe6d
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+# Copyright (C) 2013  Simon Ruderich
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+echo write to stdout
+echo write to stderr >&2
+
+printf '%s' 'write to stdout without newline'
+printf '%s' 'write to stderr without newline' >&2
diff --git a/tests/example-simple.sh.expected b/tests/example-simple.sh.expected
new file mode 100644 (file)
index 0000000..4173673
--- /dev/null
@@ -0,0 +1,3 @@
+write to stdout
+>stderr>write to stderr
+<stderr<write to stdout without newline>stderr>write to stderr without newline<stderr<
\ No newline at end of file
diff --git a/tests/example.c b/tests/example.c
new file mode 100644 (file)
index 0000000..20f7310
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Test basic features of coloredstderr.
+ *
+ * Copyright (C) 2013  Simon Ruderich
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+
+int main(int argc, char **argv) {
+    fprintf(stderr, "write to stderr: %d\n", argc);
+    printf("write to stdout\n");
+    fflush(stdout);
+
+    write(STDERR_FILENO, "write to stderr 2", 17);
+    write(STDOUT_FILENO, "write to stdout 2", 17);
+
+    fprintf(stderr, "\n");
+    fprintf(stdout, "\n");
+
+    return EXIT_SUCCESS;
+}
diff --git a/tests/example.expected b/tests/example.expected
new file mode 100644 (file)
index 0000000..b44ee0b
--- /dev/null
@@ -0,0 +1,4 @@
+>stderr>write to stderr: 1
+<stderr<write to stdout
+>stderr>write to stderr 2<stderr<write to stdout 2>stderr>
+<stderr<
diff --git a/tests/lib.sh b/tests/lib.sh
new file mode 100644 (file)
index 0000000..9480769
--- /dev/null
@@ -0,0 +1,91 @@
+# Library for the test suite.
+
+# Copyright (C) 2013  Simon Ruderich
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# Allow running the script directly without running `make check`.
+test "x$builddir" = x && builddir=.
+test "x$EGREP" = x && EGREP='grep -E'
+
+# In case we are called with LD_PRELOAD already set.
+unset LD_PRELOAD
+# Clean locale for reproducible tests.
+LC_ALL=C
+unset LANGUAGE
+
+
+die() {
+    echo "$@" >&2
+    exit 1
+}
+
+get_library_path() {
+    # Get name of the real library file from libtool's .la file.
+    dlname=`$EGREP "^dlname='" "$builddir/../src/libcoloredstderr.la"` \
+        || die 'dlname not found'
+    dlname=`echo "$dlname" | sed "s/^dlname='//; s/'$//"`
+
+    library="$builddir/../src/.libs/$dlname"
+    test -e "$library" || die "'$library' not found"
+
+    echo "`pwd`/$library"
+}
+
+library=`get_library_path`
+force_write=1
+
+
+run_test() {
+    printf '%s' "Running test '$*' .. "
+
+    testcase="$1"
+    expected="$2"
+    shift
+    shift
+
+    (
+        # Standard setup.
+        LD_PRELOAD="$library"
+        COLORED_STDERR_FDS=2,
+        export LD_PRELOAD
+        export COLORED_STDERR_FDS
+
+        # Change pre/post strings for simpler testing.
+        COLORED_STDERR_PRE='>stderr>'
+        COLORED_STDERR_POST='<stderr<'
+        export COLORED_STDERR_PRE
+        export COLORED_STDERR_POST
+        # And force writes to a file (unless we are testing the force).
+        if test "x$force_write" != x; then
+            COLORED_STDERR_FORCE_WRITE=1
+            export COLORED_STDERR_FORCE_WRITE
+        fi
+
+        "$testcase" > output 2>&1
+    )
+
+    diff -u "$expected" output \
+        || die 'failed!'
+    rm output
+    echo 'passed.'
+}
+
+test_script() {
+    run_test "$srcdir/$1" "$srcdir/$1.expected"
+}
+test_program() {
+    run_test "$builddir/$1" "$srcdir/$1.expected"
+}
diff --git a/tests/run.sh b/tests/run.sh
new file mode 100755 (executable)
index 0000000..1adadd5
--- /dev/null
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+# Test suite for coloredstderr.
+
+# Copyright (C) 2013  Simon Ruderich
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+set -e
+
+
+test "x$srcdir" = x && srcdir=.
+. "$srcdir/lib.sh"
+
+
+# Make sure we don't write to non-ttys by default.
+force_write=
+test_script example-noforce.sh
+force_write=1
+
+test_script example-simple.sh
+test_script example-redirects.sh
+test_program example