-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.sh
executable file
·96 lines (71 loc) · 1.95 KB
/
tests.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
#!/usr/bin/env nix-shell
#!nix-shell -i bash_unit -p bash bash_unit coreutils openssl
##shellcheck shell=bash
set -euo pipefail
test_env_var() {
export SECRET_KEY="dotsecrets-key"
bash .secrets "VAR_ONE" "VAL ONE" >> .secrets
assert_equals 0 $?
grep "VAL ONE" .secrets
assert_equals 1 $? "unencrypted value found in .secrets"
assert_equals 'VAR_ONE=VAL ONE' "$(bash .secrets)"
}
test_empty_var_value() {
export SECRET_KEY="secret for empty value"
bash .secrets "VAR_NAME" >> .secrets
assert_equals 1 $? "should return error for empty var value"
}
test_file() {
export SECRET_KEY="dotsecrets-file"
echo -n "SECRET FILE" > secretfile
bash .secrets secretfile >> .secrets
assert_equals 0 $?
grep "SECRET FILE" .secrets
assert_equals 1 $? "unencrypted value found in .secrets"
rm secretfile
assert_fail "test -e secretfile" "secret file should not exists"
bash .secrets
assert_equals 0 $?
assert_equals 'SECRET FILE' "$(cat secretfile)"
}
test_wrong_secret_key_for_var() {
export SECRET_KEY="secret1"
bash .secrets "x" "y" >> .secrets
assert_equals 0 $?
export SECRET_KEY="secret2"
assert_fail "bash .secrets"
}
test_wrong_secret_key_for_file() {
export SECRET_KEY="secret1"
echo -n "zzz" > sfile
bash .secrets sfile >> .secrets
assert_equals 0 $?
rm sfile
export SECRET_KEY="secret2"
assert_fail "bash .secrets"
}
test_secret_key_not_set() {
unset SECRET_KEY
bash .secrets "VAR_ONE" "VAL_ONE" >> .secrets
assert_equals 1 $? "should return error if SECRET_KEY is not set"
}
PROJECT_ROOT="$(pwd)"
export PROJECT_ROOT
CODE_LEN=$(wc -l < dotsecrets)
export CODE_LEN
setup() {
cd "$(mktemp -d)"
cp "$PROJECT_ROOT/dotsecrets" .secrets
}
verify_code_unchanged() {
if [ "$(head -n "$CODE_LEN" .secrets)" != "$(cat "$PROJECT_ROOT/dotsecrets")" ]
then
echo ".dotsecrets code changed"
diff "$PROJECT_ROOT/dotsecrets" .secrets
exit 1
fi
}
teardown() {
verify_code_unchanged
cd "$PROJECT_ROOT"
}