]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: check script options work as expected
authorSami Kerola <kerolasa@iki.fi>
Sun, 24 May 2015 17:14:58 +0000 (18:14 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sat, 6 Jun 2015 13:36:38 +0000 (14:36 +0100)
Options --flush and --timing remain unchecked, as I do not have an idea
how to check them reliably.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
term-utils/Makemodule.am
term-utils/script.c
tests/commands.sh
tests/expected/script/options [new file with mode: 0644]
tests/expected/script/options-append [new file with mode: 0644]
tests/expected/script/options-force [new file with mode: 0644]
tests/expected/script/options-quiet [new file with mode: 0644]
tests/expected/script/options-return [new file with mode: 0644]
tests/ts/script/options [new file with mode: 0755]

index e7ac707fe45a1cf8fe5bfcd4d69ed6c7d6065507..de62098aa688b720eb2f73e4d940237bc5c46fd8 100644 (file)
@@ -9,6 +9,11 @@ endif
 if HAVE_UTEMPTER
 script_LDADD += -lutempter
 endif
+
+check_PROGRAMS += test_script
+test_script_SOURCES = $(script_SOURCES)
+test_script_LDADD = $(script_LDADD)
+test_script_CFLAGS = -DTEST_SCRIPT $(logger_CFLAGS)
 endif # BUILD_SCRIPT
 
 if BUILD_SCRIPTREPLAY
index e0083f246ed42f947cb8d7f33aa6031cd45d56b9..dd3b4b592f0260eb35e564ec7c789ea11526b3b2 100644 (file)
@@ -123,6 +123,23 @@ sigset_t block_mask, unblock_mask;
 int die;
 int resized;
 
+/*
+ * For tests we want to be able to control time output
+ */
+#ifdef TEST_SCRIPT
+static inline time_t script_time(time_t *t)
+{
+       const char *str = getenv("SCRIPT_TEST_SECOND_SINCE_EPOCH");
+       time_t sec;
+
+       if (str && sscanf(str, "%ld", &sec) == 1)
+               return sec;
+       return time(t);
+}
+#else  /* !TEST_SCRIPT */
+# define script_time(x) time(x)
+#endif
+
 static void
 die_if_link(char *fn) {
        struct stat s;
@@ -446,7 +463,7 @@ dooutput(void) {
                timingfd = fdopen(STDERR_FILENO, "w");
 
        if (!qflg) {
-               time_t tvec = time((time_t *)NULL);
+               time_t tvec = script_time((time_t *)NULL);
                my_strftime(obuf, sizeof obuf, "%c\n", localtime(&tvec));
                fprintf(fscript, _("Script started on %s"), obuf);
        }
@@ -588,7 +605,7 @@ done(void) {
                if (fscript) {
                        if (!qflg) {
                                char buf[BUFSIZ];
-                               tvec = time((time_t *)NULL);
+                               tvec = script_time((time_t *)NULL);
                                my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec));
                                fprintf(fscript, _("\nScript done on %s"), buf);
                        }
index dfb19e3151426381fb309f19b1b82bb2f6a40794..f865c0773731f93afa3e0e5fcb4ab6562197bc6d 100644 (file)
@@ -25,6 +25,7 @@ TS_HELPER_MD5="$top_builddir/test_md5"
 TS_HELPER_MORE=${TS_HELPER_MORE-"$top_builddir/test_more"}
 TS_HELPER_PARTITIONS="$top_builddir/sample-partitions"
 TS_HELPER_PATHS="$top_builddir/test_pathnames"
+TS_HELPER_SCRIPT="$top_builddir/test_script"
 TS_HELPER_SIGRECEIVE="$top_builddir/test_sigreceive"
 TS_HELPER_STRUTILS="$top_builddir/test_strutils"
 TS_HELPER_SYSINFO="$top_builddir/test_sysinfo"
diff --git a/tests/expected/script/options b/tests/expected/script/options
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/expected/script/options-append b/tests/expected/script/options-append
new file mode 100644 (file)
index 0000000..b2455f0
--- /dev/null
@@ -0,0 +1,8 @@
+Script started on Sun May 24 17:43:18 2015
+append1\r
+
+Script done on Sun May 24 17:43:18 2015
+Script started on Sun May 24 17:43:18 2015
+append2\r
+
+Script done on Sun May 24 17:43:18 2015
diff --git a/tests/expected/script/options-force b/tests/expected/script/options-force
new file mode 100644 (file)
index 0000000..f462b98
--- /dev/null
@@ -0,0 +1,14 @@
+test_script: output file `typescript' is a link
+Use --force if you really want to use it.
+Program not started.
+1
+Script started on Sun May 24 17:43:18 2015
+with force\r
+
+Script done on Sun May 24 17:43:18 2015
+0
+Script started on Sun May 24 17:43:18 2015
+not typescript\r
+
+Script done on Sun May 24 17:43:18 2015
+0
diff --git a/tests/expected/script/options-quiet b/tests/expected/script/options-quiet
new file mode 100644 (file)
index 0000000..2f70287
--- /dev/null
@@ -0,0 +1,4 @@
+Script started on Sun May 24 17:43:18 2015
+quiet1\r
+Script started on Sun May 24 17:43:18 2015
+quiet2\r
diff --git a/tests/expected/script/options-return b/tests/expected/script/options-return
new file mode 100644 (file)
index 0000000..3b60589
--- /dev/null
@@ -0,0 +1,16 @@
+Script started on Sun May 24 17:43:18 2015
+
+Script done on Sun May 24 17:43:18 2015
+0
+Script started on Sun May 24 17:43:18 2015
+
+Script done on Sun May 24 17:43:18 2015
+0
+Script started on Sun May 24 17:43:18 2015
+
+Script done on Sun May 24 17:43:18 2015
+42
+Script started on Sun May 24 17:43:18 2015
+
+Script done on Sun May 24 17:43:18 2015
+127
diff --git a/tests/ts/script/options b/tests/ts/script/options
new file mode 100755 (executable)
index 0000000..2656c42
--- /dev/null
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+# This file is part of util-linux.
+#
+# This file 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 2 of the License, or
+# (at your option) any later version.
+#
+# This file 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.
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="options"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+export SCRIPT_TEST_SECOND_SINCE_EPOCH=1432489398
+export TZ="GMT"
+
+ts_check_test_command "$TS_HELPER_SCRIPT"
+
+ts_init_subtest "append"
+$TS_HELPER_SCRIPT --command "echo append1" $TS_OUTPUT </dev/null >/dev/null 2>&1
+$TS_HELPER_SCRIPT --append -c "echo append2" $TS_OUTPUT </dev/null >/dev/null 2>&1
+ts_finalize_subtest
+
+ts_init_subtest "force"
+ln -s $TS_OUTPUT typescript
+$TS_HELPER_SCRIPT --command "echo no force" </dev/null >/dev/null 2>> $TS_OUTPUT
+echo $? >> $TS_OUTPUT
+$TS_HELPER_SCRIPT -a --force --command "echo with force" </dev/null >/dev/null
+echo $? >> $TS_OUTPUT
+rm -f typescript
+ln -s $TS_OUTPUT tpircsepyt
+$TS_HELPER_SCRIPT -a --command "echo not typescript" tpircsepyt </dev/null >/dev/null
+echo $? >> $TS_OUTPUT
+rm -f tpircsepyt
+ts_finalize_subtest
+
+ts_init_subtest "quiet"
+$TS_HELPER_SCRIPT --quiet --command "echo quiet1" $TS_OUTPUT </dev/null >/dev/null 2>&1
+$TS_HELPER_SCRIPT -a -q --command "echo quiet2" $TS_OUTPUT </dev/null >/dev/null 2>&1
+ts_finalize_subtest
+
+ts_init_subtest "return"
+$TS_HELPER_SCRIPT --command "exit 1" $TS_OUTPUT </dev/null >/dev/null 2>&1
+echo $? >> $TS_OUTPUT
+$TS_HELPER_SCRIPT -a --command "exit 0" $TS_OUTPUT </dev/null >/dev/null 2>&1
+echo $? >> $TS_OUTPUT
+$TS_HELPER_SCRIPT -e --append -c "exit 42" $TS_OUTPUT </dev/null >/dev/null 2>&1
+echo $? >> $TS_OUTPUT
+$TS_HELPER_SCRIPT --return --append -c "exit 127" $TS_OUTPUT </dev/null >/dev/null 2>&1
+echo $? >> $TS_OUTPUT
+ts_finalize_subtest
+
+ts_finalize