]> ruderich.org/simon Gitweb - config/dotfiles.git/commitdiff
Add installed() function to check if a program exists.
authorSimon Ruderich <simon@ruderich.org>
Mon, 23 Feb 2009 17:49:09 +0000 (11:49 -0600)
committerSimon Ruderich <simon@ruderich.org>
Mon, 23 Feb 2009 17:49:09 +0000 (11:49 -0600)
This can be used in setup.sh functions so links are only installed for
applications which exist on the machine.

Also add tests for the function.

lib.sh
tests/lib.sh.test [new file with mode: 0644]
tests/lib.sh.test.out [new file with mode: 0644]
tests/run.sh [new file with mode: 0755]

diff --git a/lib.sh b/lib.sh
index 582e1cffe66b1f440dcec0744c08f336fffeb8ea..31b56c066db9596df61488efd4e7a48a6c8cbeae 100644 (file)
--- a/lib.sh
+++ b/lib.sh
@@ -11,6 +11,12 @@ unset LS_COLORS
 m4=`which m4`
 
 
+# Check if the given program is installed. Returns 0 if it exists, 1
+# otherwise; so it can be used in if.
+installed() {
+    which $1 > /dev/null
+}
+
 # Creates a symbolic link for file $1 in dirname of $2 with name of basenmae
 # $2.
 #
diff --git a/tests/lib.sh.test b/tests/lib.sh.test
new file mode 100644 (file)
index 0000000..d21ee2d
--- /dev/null
@@ -0,0 +1,7 @@
+
+
+. ../lib.sh
+
+# Tests for installed().
+installed which && echo which installed
+installed doesnt-exist && echo doesnt-exist installed
diff --git a/tests/lib.sh.test.out b/tests/lib.sh.test.out
new file mode 100644 (file)
index 0000000..79a69a0
--- /dev/null
@@ -0,0 +1 @@
+which installed
diff --git a/tests/run.sh b/tests/run.sh
new file mode 100755 (executable)
index 0000000..805b450
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+# Runs all tests in this directory.
+
+
+# Get all test files.
+for file in *.test; do
+    # Run the test file in sh and zsh and get its output.
+    sh $file  > $file.out.sh  2>&1
+    zsh $file > $file.out.zsh 2>&1
+
+    # Check if the output matches the expected one. If not abort with exit
+    # code 1.
+    diff -u -N $file.out $file.out.sh  || exit 1
+    diff -u -N $file.out $file.out.zsh || exit 1
+done
+
+# Remove all temporary files.
+rm -rf tmp
+rm *.out.sh
+rm *.out.zsh