From 84f932c810dae9ba0dd948e3a90c2f90d1f4e24a Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Mon, 23 Feb 2009 11:49:09 -0600 Subject: [PATCH] Add installed() function to check if a program exists. 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 | 6 ++++++ tests/lib.sh.test | 7 +++++++ tests/lib.sh.test.out | 1 + tests/run.sh | 21 +++++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 tests/lib.sh.test create mode 100644 tests/lib.sh.test.out create mode 100755 tests/run.sh diff --git a/lib.sh b/lib.sh index 582e1cf..31b56c0 100644 --- 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 index 0000000..d21ee2d --- /dev/null +++ b/tests/lib.sh.test @@ -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 index 0000000..79a69a0 --- /dev/null +++ b/tests/lib.sh.test.out @@ -0,0 +1 @@ +which installed diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..805b450 --- /dev/null +++ b/tests/run.sh @@ -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 -- 2.45.2