|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +{ |
| 4 | + # Silence shellcheck for global bats variables. |
| 5 | + # https://github.com/tiny-pilot/tinypilot/issues/1718 |
| 6 | + # shellcheck disable=SC2154 |
| 7 | + echo "${output}" "${status}" "${lines}" >/dev/null |
| 8 | +} |
| 9 | + |
| 10 | +# Wrapper for invoking the script under test as command. |
| 11 | +strip-marker-sections() { |
| 12 | + bash "${BATS_TEST_DIRNAME}/strip-marker-sections" "$@" |
| 13 | +} |
| 14 | + |
| 15 | +prints-help() { #@test |
| 16 | + run strip-marker-sections --help |
| 17 | + expected_output="$(cat << EOF |
| 18 | +Usage: strip-marker-sections [--help] TARGET_FILE |
| 19 | +Strips TinyPilot marker sections from a file. |
| 20 | + TARGET_FILE Path to file with marker sections. |
| 21 | + --help Display this help and exit. |
| 22 | +EOF |
| 23 | + )" |
| 24 | + |
| 25 | + [[ "${status}" == 0 ]] |
| 26 | + [[ "${output}" == "${expected_output}" ]] |
| 27 | +} |
| 28 | + |
| 29 | +rejects-missing-input-arg() { #@test |
| 30 | + run strip-marker-sections |
| 31 | + |
| 32 | + [[ "${status}" == 1 ]] |
| 33 | + [[ "${output}" == 'Input parameter missing: TARGET_FILE' ]] |
| 34 | +} |
| 35 | + |
| 36 | +rejects-illegal-flag() { #@test |
| 37 | + run strip-marker-sections --foo |
| 38 | + |
| 39 | + [[ "${status}" == 1 ]] |
| 40 | + [[ "${output}" == 'Illegal option: --foo' ]] |
| 41 | +} |
| 42 | + |
| 43 | +rejects-non-existing-file() { #@test |
| 44 | + run strip-marker-sections foo-file.txt |
| 45 | + |
| 46 | + [[ "${status}" == 1 ]] |
| 47 | + [[ "${output}" == 'Not a file: foo-file.txt' ]] |
| 48 | +} |
| 49 | + |
| 50 | +rejects-non-file() { #@test |
| 51 | + tmp_dir="$(mktemp --directory)" |
| 52 | + run strip-marker-sections "${tmp_dir}" |
| 53 | + |
| 54 | + [[ "${status}" == 1 ]] |
| 55 | + [[ "${output}" == "Not a file: ${tmp_dir}" ]] |
| 56 | +} |
| 57 | + |
| 58 | +noop-if-file-has-no-markers() { #@test |
| 59 | + target_file="$(mktemp)" |
| 60 | + cat << EOF > "${target_file}" |
| 61 | +line 1 |
| 62 | +line 2 |
| 63 | +line 3 |
| 64 | +EOF |
| 65 | + run strip-marker-sections "${target_file}" |
| 66 | + actual_contents="$(<"${target_file}")" |
| 67 | + expected_contents="$(cat << EOF |
| 68 | +line 1 |
| 69 | +line 2 |
| 70 | +line 3 |
| 71 | +EOF |
| 72 | + )" |
| 73 | + |
| 74 | + [[ "${status}" == 0 ]] |
| 75 | + [[ "${output}" == "" ]] |
| 76 | + [[ "${actual_contents}" == "${expected_contents}" ]] |
| 77 | +} |
| 78 | + |
| 79 | +preserves-whitespace() { #@test |
| 80 | + target_file="$(mktemp)" |
| 81 | + cat << EOF > "${target_file}" |
| 82 | + x y |
| 83 | +
|
| 84 | + 1 |
| 85 | +EOF |
| 86 | + run strip-marker-sections "${target_file}" |
| 87 | + actual_contents="$(<"${target_file}")" |
| 88 | + expected_contents="$(cat << EOF |
| 89 | + x y |
| 90 | +
|
| 91 | + 1 |
| 92 | +EOF |
| 93 | + )" |
| 94 | + |
| 95 | + [[ "${status}" == 0 ]] |
| 96 | + [[ "${output}" == "" ]] |
| 97 | + [[ "${actual_contents}" == "${expected_contents}" ]] |
| 98 | +} |
| 99 | + |
| 100 | +strips-marker-section() { #@test |
| 101 | + target_file="$(mktemp)" |
| 102 | + cat << EOF > "${target_file}" |
| 103 | +some line |
| 104 | +some other line |
| 105 | +# --- AUTOGENERATED BY TINYPILOT - START --- |
| 106 | +to be stripped |
| 107 | +# --- AUTOGENERATED BY TINYPILOT - END --- |
| 108 | +final line |
| 109 | +EOF |
| 110 | + run strip-marker-sections "${target_file}" |
| 111 | + actual_contents="$(<"${target_file}")" |
| 112 | + expected_contents="$(cat << EOF |
| 113 | +some line |
| 114 | +some other line |
| 115 | +final line |
| 116 | +EOF |
| 117 | + )" |
| 118 | + |
| 119 | + [[ "${status}" == 0 ]] |
| 120 | + [[ "${output}" == "" ]] |
| 121 | + [[ "${actual_contents}" == "${expected_contents}" ]] |
| 122 | +} |
| 123 | + |
| 124 | +strips-multiple-marker-sections() { #@test |
| 125 | + target_file="$(mktemp)" |
| 126 | + cat << EOF > "${target_file}" |
| 127 | +some line |
| 128 | +some other line |
| 129 | +# --- AUTOGENERATED BY TINYPILOT - START --- |
| 130 | +to be stripped |
| 131 | +# --- AUTOGENERATED BY TINYPILOT - END --- |
| 132 | +intermediate line |
| 133 | +# --- AUTOGENERATED BY TINYPILOT - START --- |
| 134 | +to be stripped too |
| 135 | +# --- AUTOGENERATED BY TINYPILOT - END --- |
| 136 | +final line |
| 137 | +EOF |
| 138 | + run strip-marker-sections "${target_file}" |
| 139 | + actual_contents="$(<"${target_file}")" |
| 140 | + expected_contents="$(cat << EOF |
| 141 | +some line |
| 142 | +some other line |
| 143 | +intermediate line |
| 144 | +final line |
| 145 | +EOF |
| 146 | + )" |
| 147 | + |
| 148 | + [[ "${status}" == 0 ]] |
| 149 | + [[ "${output}" == "" ]] |
| 150 | + [[ "${actual_contents}" == "${expected_contents}" ]] |
| 151 | +} |
| 152 | + |
| 153 | +fails-for-unmatched-start-marker() { #@test |
| 154 | + target_file="$(mktemp)" |
| 155 | + cat << EOF > "${target_file}" |
| 156 | +some line |
| 157 | +# --- AUTOGENERATED BY TINYPILOT - START --- |
| 158 | +to be stripped |
| 159 | +EOF |
| 160 | + run strip-marker-sections "${target_file}" |
| 161 | + actual_contents="$(<"${target_file}")" |
| 162 | + expected_contents="$(cat << EOF |
| 163 | +some line |
| 164 | +# --- AUTOGENERATED BY TINYPILOT - START --- |
| 165 | +to be stripped |
| 166 | +EOF |
| 167 | + )" |
| 168 | + |
| 169 | + [[ "${status}" == 1 ]] |
| 170 | + [[ "${output}" == "Unmatched start marker" ]] |
| 171 | + [[ "${actual_contents}" == "${expected_contents}" ]] |
| 172 | +} |
| 173 | + |
| 174 | +fails-for-unmatched-end-marker() { #@test |
| 175 | + target_file="$(mktemp)" |
| 176 | + cat << EOF > "${target_file}" |
| 177 | +some line |
| 178 | +# --- AUTOGENERATED BY TINYPILOT - END --- |
| 179 | +final line |
| 180 | +EOF |
| 181 | + run strip-marker-sections "${target_file}" |
| 182 | + actual_contents="$(<"${target_file}")" |
| 183 | + expected_contents="$(cat << EOF |
| 184 | +some line |
| 185 | +# --- AUTOGENERATED BY TINYPILOT - END --- |
| 186 | +final line |
| 187 | +EOF |
| 188 | + )" |
| 189 | + |
| 190 | + [[ "${status}" == 1 ]] |
| 191 | + [[ "${output}" == 'Unmatched end marker' ]] |
| 192 | + [[ "${actual_contents}" == "${expected_contents}" ]] |
| 193 | +} |
0 commit comments