-
Notifications
You must be signed in to change notification settings - Fork 1
/
installer.bats
165 lines (130 loc) · 3.99 KB
/
installer.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bats
#
#+Bats tests for the blib installer.
#+
#+Copyright (C) 2018 David Hobach LGPLv3
#+0.3
#load user data if available
USER_DATA_FILE="$BATS_TEST_DIRNAME/tests/user_test_data.bash"
USER_DATA_AVAILABLE=1
load "$USER_DATA_FILE" &> /dev/null && USER_DATA_AVAILABLE=0
#path to the installer
INSTALLER="$BATS_TEST_DIRNAME/installer"
#default installation directory
DEFAULT_INSTALL_DIR="/usr/lib"
#to become root
SUDO_PREFIX="su root -c"
command -v sudo &> /dev/null && SUDO_PREFIX="sudo $SUDO_PREFIX"
function skipIfRootUnavailable {
[[ "$UTD_PW_FREE_USER" != "root" ]] && skip "UTD_PW_FREE_USER would have to be specified as root in the user test data file $USER_DATA_FILE for this test to work." || return 0
}
function skipIfInstalled {
[ -e "/usr/bin/blib" ] && skip "It appears that blib is installed on your system. Since this test may cause changes to your current installation, it is skipped." || return 0
}
#ensureInstalled [install dir]
function ensureInstalled {
local installDir="${1:-$DEFAULT_INSTALL_DIR}"
[ -f "/usr/bin/blib" ]
[ -d "$installDir/blib" ]
[ -d "$installDir" ]
[ -d "/usr/bin/" ]
if command -v pandoc &> /dev/null ; then
[ -f "/usr/share/man/man3/blib.3" ]
fi
#attempt to read a few files
cat "/usr/bin/blib" > /dev/null
cat "$installDir/blib/lib/cdoc" > /dev/null
#make sure the test marker is there (otherwise we shouldn't be operating on this folder)
cat "$installDir/blib/doc/BATS_TEST_MARKER" > /dev/null
#attempt write
local tFile="$installDir/blib/doc/TEST_WRITE_FILE"
touch "$tFile"
rm "$tFile"
}
#ensureRemoved [install dir]
function ensureRemoved {
local installDir="${1:-$DEFAULT_INSTALL_DIR}"
[ ! -e "/usr/bin/blib" ]
[ ! -e "$installDir/blib" ]
[ ! -e "/usr/share/man/man3/blib.3" ]
[ -d "$installDir" ]
[ -d "/usr/bin/" ]
[ -d "/usr/share/man/man3" ]
}
#testInstall [expected status] [install dir] [test reinstall]
function testInstall {
local eStatus=$1
local installDir="${2:-$DEFAULT_INSTALL_DIR}"
local testRe="${3:-1}"
echo "Installation: $installDir"
run $SUDO_PREFIX "$INSTALLER install \"$installDir\""
echo "$output"
[ $status -eq $eStatus ]
[ -n "$output" ]
if [ $eStatus -eq 0 ] ; then
ensureInstalled "$installDir" || exit 1
if [ $testRe -eq 0 ] ; then
run $SUDO_PREFIX "$INSTALLER reinstall \"$installDir\""
echo "Reinstall:"
echo "$output"
[ $status -eq $eStatus ]
[ -n "$output" ]
ensureInstalled "$installDir" || exit 2
fi
#set a marker indicating that this installation was completed from this test
touch "$installDir/blib/doc/BATS_TEST_MARKER"
fi
}
#testUninstall [expected status] [install dir]
function testUninstall {
local eStatus=$1
local installDir="${2:-$DEFAULT_INSTALL_DIR}"
echo "Removal: $installDir"
run $SUDO_PREFIX "$INSTALLER uninstall \"$installDir\""
echo "$output"
[ $status -eq $eStatus ]
[ -n "$output" ]
if [ $eStatus -eq 0 ] ; then
ensureRemoved "$installDir" || exit 1
fi
}
@test "install & reinstall" {
skipIfRootUnavailable
skipIfInstalled
local tmpInst="$(mktemp -d)"
[ -n "$tmpInst" ]
ensureRemoved
ensureRemoved "$tmpInst"
testInstall 0 "$tmpInst" 0
testUninstall 0 "$tmpInst"
testInstall 0 "$tmpInst/" 0
testUninstall 0 "$tmpInst/"
#default install test
testInstall 0
testUninstall 0
testInstall 0
#cleanup
rm -rf "$tmpInst"
}
@test "blib tests" {
ensureInstalled
#NOTE: it is recommended to run this test as the user who runs blib
#it was installed from the previous test
#the whole point of this test is to run the blib tests with the installed version as that one might have access issues, the installation partially failed or whatever
cd ~
run blib test
echo "$output"
[ $status -eq 0 ]
[ -n "$output" ]
}
@test "uninstall" {
skipIfRootUnavailable
#Note: the following line also checks whether the installation was created by the test above (test marker)
ensureInstalled
#failing
testUninstall 1 "/tmp/nonexisting"
testUninstall 1 "/tmp/nonexisting/"
ensureInstalled
#hopefully working, was installed above
testUninstall 0
}