-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathprint-marker-sections.bats
162 lines (140 loc) · 3.43 KB
/
print-marker-sections.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
#!/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.
print-marker-sections() {
bash "${BATS_TEST_DIRNAME}/print-marker-sections" "$@"
}
prints-help() { #@test
run print-marker-sections --help
expected_output="$(cat << EOF
Usage: print-marker-sections [--help] TARGET_FILE
Prints the contents of 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 print-marker-sections
expected_output="$(cat << EOF
Input parameter missing: TARGET_FILE
Use the '--help' flag for more information
EOF
)"
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}
rejects-illegal-flag() { #@test
run print-marker-sections --foo
expected_output="$(cat << EOF
Unknown flag: --foo
Use the '--help' flag for more information
EOF
)"
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}
rejects-non-existing-file() { #@test
run print-marker-sections foo-file.txt
expected_output="$(cat << EOF
Not a file: foo-file.txt
Use the '--help' flag for more information
EOF
)"
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}
rejects-non-file() { #@test
tmp_dir="$(mktemp --directory)"
run print-marker-sections "${tmp_dir}"
expected_output="$(cat << EOF
Not a file: ${tmp_dir}
Use the '--help' flag for more information
EOF
)"
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}
empty-output-if-file-has-no-markers() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
line 1
line 2
line 3
EOF
run print-marker-sections "${target_file}"
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
}
prints-marker-section() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
some other line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be
printed
# --- AUTOGENERATED BY TINYPILOT - END ---
final line
EOF
run print-marker-sections "${target_file}"
expected_output="$(cat << EOF
to be
printed
EOF
)"
(( "${status}" == 0 ))
[[ "${output}" == "${expected_output}" ]]
}
prints-multiple-marker-sections() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
some other line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be
# --- AUTOGENERATED BY TINYPILOT - END ---
intermediate line
# --- AUTOGENERATED BY TINYPILOT - START ---
printed
# --- AUTOGENERATED BY TINYPILOT - END ---
final line
EOF
run print-marker-sections "${target_file}"
expected_output="$(cat << EOF
to be
printed
EOF
)"
(( "${status}" == 0 ))
[[ "${output}" == "${expected_output}" ]]
}
fails-for-unmatched-start-marker() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
# --- AUTOGENERATED BY TINYPILOT - START ---
to be printed
EOF
run print-marker-sections "${target_file}"
(( "${status}" == 1 ))
[[ "${output}" == "Unmatched start marker" ]]
}
fails-for-unmatched-end-marker() { #@test
target_file="$(mktemp)"
cat << EOF > "${target_file}"
some line
# --- AUTOGENERATED BY TINYPILOT - END ---
final line
EOF
run print-marker-sections "${target_file}"
(( "${status}" == 1 ))
[[ "${output}" == 'Unmatched end marker' ]]
}