-
-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathstrip-marker-sections.bats
More file actions
193 lines (167 loc) · 4.17 KB
/
strip-marker-sections.bats
File metadata and controls
193 lines (167 loc) · 4.17 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
{
# Silence shellcheck for global bats variables.
# https://github.com/tiny-pilot/tinypilot/issues/1718
# shellcheck disable=SC2154
echo "${output}" "${status}" "${lines}" >/dev/null
}
# Wrapper for invoking the script under test as command.
strip-marker-sections() {
bash "${BATS_TEST_DIRNAME}/strip-marker-sections" "$@"
}
prints-help() { #@test
run strip-marker-sections --help
expected_output="$(cat << EOF
Usage: strip-marker-sections [--help] TARGET_FILE
Strips TinyPilot marker sections from a file.
TARGET_FILE Path to file with marker sections.
--help Display this help and exit.
EOF
)"
(( "${status}" == 0 ))
[[ "${output}" == "${expected_output}" ]]
}
rejects-missing-input-arg() { #@test
run strip-marker-sections
(( "${status}" == 1 ))
[[ "${output}" == 'Input parameter missing: TARGET_FILE' ]]
}
rejects-illegal-flag() { #@test
run strip-marker-sections --foo
(( "${status}" == 1 ))
[[ "${output}" == 'Illegal option: --foo' ]]
}
rejects-non-existing-file() { #@test
run strip-marker-sections foo-file.txt
(( "${status}" == 1 ))
[[ "${output}" == 'Not a file: foo-file.txt' ]]
}
rejects-non-file() { #@test
tmp_dir="$(mktemp --directory)"
run strip-marker-sections "${tmp_dir}"
(( "${status}" == 1 ))
[[ "${output}" == "Not a file: ${tmp_dir}" ]]
}
noop-if-file-has-no-markers() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
line 1
line 2
line 3
EOF
run strip-marker-sections "${target_file}"
actual_contents="$(<"${target_file}")"
expected_contents="$(cat << EOF
line 1
line 2
line 3
EOF
)"
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
preserves-whitespace() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
x y
1
EOF
run strip-marker-sections "${target_file}"
actual_contents="$(<"${target_file}")"
expected_contents="$(cat << EOF
x y
1
EOF
)"
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
strips-marker-section() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
some other line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be stripped
# --- AUTOGENERATED BY TINYPILOT - END ---
final line
EOF
run strip-marker-sections "${target_file}"
actual_contents="$(<"${target_file}")"
expected_contents="$(cat << EOF
some line
some other line
final line
EOF
)"
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
strips-multiple-marker-sections() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
some other line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be stripped
# --- AUTOGENERATED BY TINYPILOT - END ---
intermediate line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be stripped too
# --- AUTOGENERATED BY TINYPILOT - END ---
final line
EOF
run strip-marker-sections "${target_file}"
actual_contents="$(<"${target_file}")"
expected_contents="$(cat << EOF
some line
some other line
intermediate line
final line
EOF
)"
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
fails-for-unmatched-start-marker() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be stripped
EOF
run strip-marker-sections "${target_file}"
actual_contents="$(<"${target_file}")"
expected_contents="$(cat << EOF
some line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be stripped
EOF
)"
(( "${status}" == 1 ))
[[ "${output}" == "Unmatched start marker" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
fails-for-unmatched-end-marker() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
# --- AUTOGENERATED BY TINYPILOT - END ---
final line
EOF
run strip-marker-sections "${target_file}"
actual_contents="$(<"${target_file}")"
expected_contents="$(cat << EOF
some line
# --- AUTOGENERATED BY TINYPILOT - END ---
final line
EOF
)"
(( "${status}" == 1 ))
[[ "${output}" == 'Unmatched end marker' ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}