-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.sh
executable file
·149 lines (125 loc) · 3.24 KB
/
test.sh
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
#!/usr/bin/env bash
# COLORS
NC='\033[0m' # Text Reset
RED='\033[0;31m' # Red
GREEN='\033[0;32m' # Green
# all functions in this file that start with 'test' are executed
tests="$(grep -E '^function test(.*)' test.sh | awk '{print $2}' | grep -E -o '\w+')"
PATH=$PWD:$PATH
# this is not exactly a test of rbu but ensures that the README.md has
# the usage updated and correct
function testShouldHaveUsageInREADME() {
readme="$(dirname "$(which rbu)")/README.md"
if [[ "$(cat "$readme")" == *"$(rbu -h)"* ]]; then
pass
else
fail "README.md does not contain all of 'rbu -h'"
fi
}
function testShouldAlwaysCreateFirstBackup() {
uuidgen > infile.txt
rbu infile.txt
exp=infile.txt.1
[ -f $exp ] && pass && return
fail "expected $exp"
}
function testShouldCreateAllBackups() {
uuidgen > infile.txt
for _ in $(seq 5); do rbu -a infile.txt ; done
for f in infile.txt.{1,2,3,4,5}; do
[ ! -f "$f" ] && \
fail "expected $f to exist" && \
return
done
pass
}
function testShouldNotBackupIdenticalFiles() {
uuidgen > infile.txt
cp infile.txt infile.txt.1
rbu infile.txt > /dev/null
[ -f infile.txt.2 ] && \
fail "expected infile.txt.2 to not exist" && \
return
pass
}
function testShouldBackupChangedFiles() {
uuidgen > infile.txt
uuidgen > infile.txt.1
rbu infile.txt > /dev/null
exp=infile.txt.2
[ -f "$exp" ] && pass && return
fail "expected $exp to not exist"
}
function testShouldBackupIdenticalFilesWithFlag() {
uuidgen > infile.txt
cp infile.txt infile.txt.1
rbu -a infile.txt
exp=infile.txt.2
[ -f "$exp" ] && pass && return
fail "expected $exp to exist"
}
function testShouldNotExceedMax() {
uuidgen > infile.txt
for _ in $(seq 3); do
rbu -m 2 -a infile.txt
done
[ -f infile.txt.3 ] && \
fail "expected infile.txt.3 to NOT exist" && \
return
pass
}
function testShouldUseSpecifiedDir() {
uuidgen > infile.txt
mkdir baks
rbu -d baks -a infile.txt
[ -f infile.txt.1 ] && \
fail "expected infile.txt.1 to NOT exist" && \
return
[ -f baks/infile.txt.1 ] && pass && return
fail "expected baks/infile.txt.1 to NOT exist"
}
function testShouldCreateSpecifiedDirIfItDoesNotExist() {
uuidgen > infile.txt
rbu -d baks -a infile.txt
[ -d "baks" ] && pass && return
fail "expected $exp to be created"
}
function testShouldErrorIfInfileIsDirectory() {
mkdir baks
if log="$(rbu baks &> /dev/stdout)"; then
fail "expected exit code 1"
fi
[[ "$log" == *is\ a\ directory* ]] && pass && return
fail "expected '$log' to say infile is directory"
}
function testShouldErrorWhenNoFileSpecified() {
if log="$(rbu -m 10 &> /dev/stdout)"; then
fail "expected exit code 1"
fi
[[ "$log" == *Usage* ]] && pass && return
fail "expected '$log' to say infile is directory"
}
function testShouldMoveDotOneToDotTwo() {
uuidgen > infile.txt
uuidone="$(uuidgen | tee infile.txt.1)"
rbu infile.txt
[ ! -f "infile.txt.2" ] && \
fail "expected infile.txt.2 to exist" && \
return
[ "$(cat infile.txt.2)" == "$uuidone" ] && \
pass && \
return
fail "expected infile.txt.1 to have been moved to infile.txt.2"
}
function pass() {
echo -e "${GREEN}OK${NC}"
}
function fail() {
echo -e "${RED}FAIL${NC}: $1"
}
for t in $tests; do
pushd "$(mktemp -d -t tmp.XXXXXXXXX)" > /dev/null
echo -n "$t: "
eval "$t"
popd > /dev/null
done