Merge branch 'main' of feistymeow.org:feisty_meow
[feisty_meow.git] / testkit / prepare_tools.sh
1 #!/bin/bash
2
3 # Author: Chris Koeritz
4 #
5 # Note:
6 # We do not want to "exit" from this file at all (nor from any file that it
7 # invokes either), since this script is intended for use by the bash 'source'
8 # command.  If we exit, that will exit from the calling shell as well, which
9 # torpedoes whatever one was doing in that shell.
10 # There is a variable below called BADNESS that indicates when errors
11 # occurred during processing, and if it's not empty at the end of the script
12 # then we will consider this a failed run, and we will not set the test's
13 # sentinel variable which other scripts check to see if the environment
14 # was loaded properly.
15
16 # make sure whether they have defined the top-level location for us.
17 if [ ! -z "$1" ]; then
18   # first attempt is to use the first parameter, if one is provided.  this should
19   # be an absolute path reference to this very file, from which we can deduce the
20   # starting directory.
21   GRITTY_TESTING_TOP_LEVEL="$( cd "$( dirname "$1" )" && \pwd )"
22   # for this case, they also don't need to be stranded in a new shell, because we
23   # assume they have sourced this file instead of bashing it.
24   NO_SUBSHELL=true
25 fi
26 if [ -z "$GRITTY_TESTING_TOP_LEVEL" ]; then
27   # otherwise, if they didn't explicitly set the top-level directory, we will
28   # do it using some unix trickery.
29   if [[ "$0" =~ .*bash ]]; then
30     echo "----"
31     echo "This script was not launched properly with 'source'.  The script should"
32     echo "be started like this: source prepare_tools.sh prepare_tools.sh"
33     echo "The double entry is required for bash's source command to find the path."
34     BADNESS=true
35   fi
36   GRITTY_TESTING_TOP_LEVEL="$( cd "$( dirname "$0" 2>/dev/null )" && \pwd )"
37 else
38   # we assume they are managing this script more closely and do not need (or want) a bash sub-shell.
39   NO_SUBSHELL=true
40 fi
41 GRITTY_TESTING_TOP_LEVEL="$(echo "$GRITTY_TESTING_TOP_LEVEL" | sed -e 's/\/cygdrive\/\(.\)/\1:/')"
42
43 # the top-level directory for tests, i.e. the root of testing hierarchy.
44 export TESTKIT_ROOT="$GRITTY_TESTING_TOP_LEVEL"
45
46 # location needed for shunit temporary files.
47 export TMPDIR="$HOME/.shunit-temp"
48 if [ ! -d "$TMPDIR" ]; then
49   mkdir -p "$TMPDIR"
50   if [ $? -ne 0 ]; then
51     echo "Failure during creation of TMPDIR for shunit!"
52     exit 1
53   fi
54 fi
55
56 # a bit of a dance to not pull in code too early...
57 export TESTKIT_BOOTSTRAPPING=true
58 source "$TESTKIT_ROOT/library/establish_environment.sh"
59 unset TESTKIT_BOOTSTRAPPING
60 # done with dancing, ready to pull in anything else from testkit.
61
62 #source "$TESTKIT_ROOT/library/helper_methods.sh"
63
64 # where the shunit library resides.
65 export SHUNIT_DIR="$TESTKIT_ROOT/shunit"
66
67 # establish the TMP variable if it's not already set.
68 export TMP
69 if [ -z "$TMP" ]; then
70   TMP="$HOME/tmp"
71   if [ ! -d "$TMP" ]; then mkdir "$TMP"; fi
72 fi
73 TMP="$(echo "$TMP" | sed -e 's/\/cygdrive\/\(.\)/\1:/')"
74 if [ ! -d "$TMP" ]; then 
75   echo "The TMP directory was set as $TMP but cannot be created or found."
76   echo "If there is a file at that location, please move or delete it."
77   exit 1
78 fi
79
80 ##############
81
82 # commonly used environment variables...
83
84 # TEST_TEMP is a folder where we can generate a collection of junk files.
85 export TEST_TEMP="$TMP/testkit_logs_${USER}"
86 if [ ! -d "$TEST_TEMP" ]; then
87   mkdir -p "$TEST_TEMP"
88 fi
89
90 # this variable points to the last output from a grid command.
91 export TESTKIT_OUTPUT_FILE="$TEST_TEMP/testkit_output.log"
92 export TESTKIT_TIMING_FILE="$TEST_TEMP/testkit_times.log"
93 export CONGLOMERATED_TESTKIT_OUTPUT="$TEST_TEMP/full_testkit_output.log"
94
95 ##############
96
97 # uncomment this to enable extra output.
98 export DEBUGGING=true
99
100 ##############
101
102 # turn this printout off in non-debugging mode or if the terminal setting
103 # seems to indicate that we're running in a login environment (where any
104 # echoing to standard out can screw up scp and sftp for that account).
105 if [ ! -z "$DEBUGGING" -a -z "$SHOWED_SETTINGS_ALREADY" \
106     -a -z "$BADNESS" -a -z "$SILENT_RUNNING" -a "${TERM}" != "dumb" \
107     -a -z "$PBS_ENVIRONMENT" ]; then
108   echo "==========================================================="
109   echo "Testkit environment loaded."
110   var TESTKIT_ROOT TESTKIT_CFG_FILE TMP TEST_TEMP
111   echo "==========================================================="
112 fi
113
114 if [ ! -z "$(uname -a | grep -i darwin)" -a -z "$BADNESS" ]; then
115   # add in the mac binaries if this is darwin.
116   export PATH="$TESTKIT_ROOT/bin/macosx:$PATH"
117 else
118   # no change, but we want to make sure sub-shells inherit the path.
119   export PATH="$PATH"
120 fi
121
122 if [ -z "$NO_SUBSHELL" -a -z "$BADNESS" ]; then
123   # at this point we go into a new interactive shell, so as to ensure the
124   # environment parameters stay right.
125   # the self-location code at the top doesn't work properly if this file is
126   # sourced into a current environment.
127   bash
128 fi
129
130 if [ ! -z "$BADNESS" ]; then
131   echo
132   echo "----"
133   echo "There were errors in setting up the xsede tests--see above messages."
134   unset TESTKIT_SENTINEL TESTKIT_ROOT GRITTY_TESTING_TOP_LEVEL SHUNIT_DIR BADNESS
135 else
136   # if things were successful, we can finally set our indicator for the scripts to check.
137   export TESTKIT_SENTINEL=initialized
138 fi
139