aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xscripts/test/test.test63
-rw-r--r--toys/pending/test.c69
2 files changed, 132 insertions, 0 deletions
diff --git a/scripts/test/test.test b/scripts/test/test.test
new file mode 100755
index 00000000..a66dd3c6
--- /dev/null
+++ b/scripts/test/test.test
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+# TODO: Should also have device and socket files
+
+mkdir d
+touch f
+ln -s /dev/null L
+echo nonempty > s
+mkfifo p
+
+type_test()
+{
+ result=""
+ for i in d f L s p n
+ do
+ if test $* $i
+ then
+ result=$result$i
+ fi
+ done
+ printf "%s" $result
+}
+
+testing "test -b" "type_test -b" "" "" ""
+testing "test -c" "type_test -c" "L" "" ""
+testing "test -d" "type_test -d" "d" "" ""
+testing "test -f" "type_test -f" "fs" "" ""
+testing "test -h" "type_test -h" "L" "" ""
+testing "test -L" "type_test -L" "L" "" ""
+testing "test -s" "type_test -s" "ds" "" ""
+testing "test -S" "type_test -S" "" "" ""
+testing "test -p" "type_test -p" "p" "" ""
+testing "test -e" "type_test -e" "dfLsp" "" ""
+testing "test ! -e" "type_test ! -e" "n" "" ""
+
+rm f L s p
+rmdir d
+
+# TODO: Test rwx gu t
+
+testing "test" "test "" || test a && echo yes" "yes\n" "" ""
+testing "test -n" "test -n "" || test -n a && echo yes" "yes\n" "" ""
+testing "test -z" "test -n a || test -n "" && echo yes" "yes\n" "" ""
+testing "test a = b" "test a = b || test "" = "" && echo yes" "yes\n" "" ""
+testing "test a != b" "test "" != "" || test a = b && echo yes" "yes\n" "" ""
+
+arith_test()
+{
+ test -1 $1 1 && echo l
+ test 0 $1 0 && echo e
+ test -3 $1 -5 && echo g
+}
+
+testing "test -eq" "arith_test -eq" "e\n" "" ""
+testing "test -ne" "arith_test -ne" "l\ng\n" "" ""
+testing "test -gt" "arith_test -gt" "g\n" "" ""
+testing "test -ge" "arith_test -ge" "e\ng\n" "" ""
+testing "test -lt" "arith_test -lt" "l\n" "" ""
+testing "test -le" "arith_test -le" "l\ne\n" "" ""
diff --git a/toys/pending/test.c b/toys/pending/test.c
index 481eaec4..c03e4502 100644
--- a/toys/pending/test.c
+++ b/toys/pending/test.c
@@ -41,5 +41,74 @@ config TEST
void test_main(void)
{
+ int id, not;
+ char *s, *err_fmt = "Bad flag '%s'";
+
+ toys.exitval = 2;
+ if (!strcmp("[", toys.which->name))
+ if (!strcmp("]", toys.optargs[--toys.optc])) error_exit("Missing ']'");
+ if (!strcmp("!", toys.optargs[0])) {
+ not = 1;
+ toys.optargs++;
+ toys.optc--;
+ }
+ if (!toys.optc) toys.exitval = 0;
+ else if (toys.optargs[0][0] == '-') {
+ id = stridx("bcdefghLpSsurwxznt", toys.optargs[0][1]);
+ if (id == -1 || toys.optargs[0][2]) error_exit(err_fmt, toys.optargs[0]);
+ if (id < 12) {
+ struct stat st;
+ int nolink;
+
+ toys.exitval = 1;
+ if (lstat(toys.optargs[1], &st) == -1) return;
+ nolink = !S_ISLNK(st.st_mode);
+ if (!nolink && (stat(toys.optargs[1], &st) == -1)) return;
+
+ if (id == 0) toys.exitval = !S_ISBLK(st.st_mode); // b
+ else if (id == 1) toys.exitval = !S_ISCHR(st.st_mode); // c
+ else if (id == 2) toys.exitval = !S_ISDIR(st.st_mode); // d
+ else if (id == 3) toys.exitval = 0; // e
+ else if (id == 4) toys.exitval = !S_ISREG(st.st_mode); // f
+ else if (id == 5) toys.exitval = !(st.st_mode & S_ISGID); // g
+ else if ((id == 6) || (id == 7)) toys.exitval = nolink; // hL
+ else if (id == 8) toys.exitval = !S_ISFIFO(st.st_mode); // p
+ else if (id == 9) toys.exitval = !S_ISSOCK(st.st_mode); // S
+ else if (id == 10) toys.exitval = st.st_size == 0; // s
+ else toys.exitval = !(st.st_mode & S_ISUID); // u
+ }
+ else if (id < 15) // rwx
+ toys.exitval = access(toys.optargs[1], 1 << (id - 12)) == -1;
+ else if (id < 17) // zn
+ toys.exitval = toys.optargs[1] && !*toys.optargs[1] ^ (id - 15);
+ else { // t
+ struct termios termios;
+ toys.exitval = tcgetattr(atoi(toys.optargs[1]), &termios) == -1;
+ }
+ }
+ else if (toys.optc == 1) toys.exitval = *toys.optargs[0] == 0;
+ else if (toys.optc == 3) {
+ if (*toys.optargs[1] == '-') {
+ long a = atol(toys.optargs[0]), b = atol(toys.optargs[2]);
+
+ s = toys.optargs[1] + 1;
+ if (!strcmp("eq", s)) toys.exitval = a != b;
+ else if (!strcmp("ne", s)) toys.exitval = a == b;
+ else if (!strcmp("gt", s)) toys.exitval = a < b;
+ else if (!strcmp("ge", s)) toys.exitval = a <= b;
+ else if (!strcmp("lt", s)) toys.exitval = a > b;
+ else if (!strcmp("le", s)) toys.exitval = a >= b;
+ else error_exit(err_fmt, toys.optargs[1]);
+ }
+ else {
+ int result = strcmp(toys.optargs[0], toys.optargs[2]);
+
+ s = toys.optargs[1];
+ if (!strcmp("=", s)) toys.exitval = !!result;
+ else if (!strcmp("!=", s)) toys.exitval = !result;
+ else error_exit(err_fmt, toys.optargs[1]);
+ }
+ }
+ toys.exitval ^= not;
return;
}