-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappend.bats
85 lines (73 loc) · 2.27 KB
/
append.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
setup() {
V=1
load 'test_helper/bats-assert/load'
load 'test_helper/bats-support/load'
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
PATH="${DIR}/..:${PATH}"
# config bootstrap
RPI_PLUGINDIR="${DIR}/../bootstrap-plugins"
RPI_TESTING="true"
# init bootstrap
source bootstrap.sh
plugin_load append
# create fake image
mkdir -p "${RPI_BOOT}"
printf "[all]\n#dtoverlay=vc4-fkms-v3d\ndtparam=audio=off\ndisable_auto_turbo=1\n" > "${RPI_BOOT}/config.txt"
mkdir -p "${RPI_ROOT}/etc"
printf "testpi\n" > "${RPI_ROOT}/etc/hostname"
printf "Hello!\n" > "${RPI_ROOT}/etc/issue"
}
@test "plugin_prerun append" {
RPI_APPEND_FILE=""
RPI_APPEND_APPENDIX=""
run plugin_prerun append
assert_failure
RPI_APPEND_FILE="foo"
RPI_APPEND_APPENDIX=""
run plugin_prerun append
assert_failure
RPI_APPEND_FILE=""
RPI_APPEND_APPENDIX="bar"
run plugin_prerun append
assert_failure
RPI_APPEND_FILE="foo"
RPI_APPEND_APPENDIX="bar"
run plugin_prerun append
assert_success
}
@test "plugin_run append: string" {
local file="${RPI_ROOT}/etc/hostname"
# check if double string append is prevented
RPI_APPEND_FILE="${file}"
RPI_APPEND_APPENDIX="testpi"
run plugin_run append
assert_success
[ "$(wc -l "${file}" | cut -f1 -d' ')" == "1" ]
grep --quiet "testpi" "${file}"
# check if string append succeeds
RPI_APPEND_FILE="${file}"
RPI_APPEND_APPENDIX="# foo"
run plugin_run append
assert_success
[ "$(wc -l "${file}" | cut -f1 -d' ')" == "2" ]
grep -Pz --quiet "(?s)testpi\n# foo" "${file}"
}
@test "plugin_run append: file" {
local file="${RPI_ROOT}/etc/issue"
local appendix="${BATS_TMPDIR}/foo.${RANDOM}"
printf "foo\n" > "${appendix}"
# check if file append succeeds
RPI_APPEND_FILE="${file}"
RPI_APPEND_APPENDIX="${appendix}"
run plugin_run append
assert_success
[ "$(wc -l "${file}" | cut -f1 -d' ')" == "2" ]
grep -Pz --quiet "(?s)Hello!\nfoo" "${file}"
# check if double file append is prevented
run plugin_run append
assert_success
[ "$(wc -l "${file}" | cut -f1 -d' ')" == "2" ]
grep -Pz --quiet "(?s)Hello!\nfoo" "${file}"
# cleanup
rm "${appendix}"
}