|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# Path to the script under test; adjust as needed |
| 4 | +SCRIPT="${BATS_TEST_DIRNAME}/../src/markdown.sh" |
| 5 | +HELPERS="${BATS_TEST_DIRNAME}/../src/helpers.sh" |
| 6 | + |
| 7 | +load 'test_helper/bats-support/load' |
| 8 | +load 'test_helper/bats-assert/load' |
| 9 | +load "$HELPERS" |
| 10 | +load "$SCRIPT" |
| 11 | + |
| 12 | +# Stub dependencies and source function |
| 13 | +setup() { |
| 14 | + # Stub portable_mktemp and normalize_blank_lines |
| 15 | + portable_mktemp() { mktemp; } |
| 16 | + normalize_blank_lines() { cat "$1"; } |
| 17 | + |
| 18 | + # # Source the script containing manage_section |
| 19 | + # [ -f "$SCRIPT" ] || BATS_SKIP="Script under test not found: $SCRIPT" |
| 20 | + # source "$SCRIPT" |
| 21 | +} |
| 22 | + |
| 23 | +teardown() { |
| 24 | + # Clean up any temporary files |
| 25 | + rm -f "$tmp_orig" "$tmp_new" "$tmpfile" |
| 26 | +} |
| 27 | + |
| 28 | +# Helper to extract managed output file path |
| 29 | +get_tmpfile() { |
| 30 | + # $output from prior run: last line is tmpfile path |
| 31 | + echo "$output" | tail -n1 |
| 32 | +} |
| 33 | + |
| 34 | +@test "append mode: appends new section to existing file" { |
| 35 | + tmp_orig=$(mktemp) |
| 36 | + cat <<EOF >"$tmp_orig" |
| 37 | +Line1 |
| 38 | +Line2 |
| 39 | +EOF |
| 40 | + tmp_new=$(mktemp) |
| 41 | + echo "Appended content" >"$tmp_new" |
| 42 | + |
| 43 | + run manage_section "# Changelog" "$tmp_orig" "$tmp_new" append "NewSection" |
| 44 | + [ "$status" -eq 0 ] |
| 45 | + tmpfile=$(get_tmpfile) |
| 46 | + |
| 47 | + run cat "$tmpfile" |
| 48 | + [ "$status" -eq 0 ] |
| 49 | + # original content preserved |
| 50 | + [[ "$output" == *"Line1"* ]] |
| 51 | + [[ "$output" == *"Line2"* ]] |
| 52 | + # new header and content at end |
| 53 | + [[ "$output" == *"## NewSection"* ]] |
| 54 | + [[ "$output" == *"Appended content"* ]] |
| 55 | +} |
| 56 | + |
| 57 | +@test "append mode: missing original file behaves like empty" { |
| 58 | + tmp_new=$(mktemp) |
| 59 | + echo "Only content" >"$tmp_new" |
| 60 | + |
| 61 | + run manage_section "# Changelog" "/nonexistent/file" "$tmp_new" append "OnlySection" |
| 62 | + [ "$status" -eq 0 ] |
| 63 | + tmpfile=$(get_tmpfile) |
| 64 | + |
| 65 | + run cat "$tmpfile" |
| 66 | + [ "$status" -eq 0 ] |
| 67 | + # should only contain new section |
| 68 | + [[ "$output" == *"## OnlySection"* ]] |
| 69 | + [[ "$output" == *"Only content"* ]] |
| 70 | +} |
| 71 | + |
| 72 | +@test "prepend mode: empty original file inserts title and section" { |
| 73 | + tmp_new=$(mktemp) |
| 74 | + echo "NewLine1" >"$tmp_new" |
| 75 | + |
| 76 | + run manage_section "# Changelog" "/nonexistent" "$tmp_new" prepend "Intro" |
| 77 | + [ "$status" -eq 0 ] |
| 78 | + tmpfile=$(get_tmpfile) |
| 79 | + |
| 80 | + run cat "$tmpfile" |
| 81 | + # should start with title |
| 82 | + [[ "$output" =~ "# Changelog" ]] |
| 83 | + # then new section header |
| 84 | + [[ "$output" =~ "## Intro" ]] |
| 85 | + # then content |
| 86 | + [[ "$output" =~ "NewLine1" ]] |
| 87 | +} |
| 88 | + |
| 89 | +@test "prepend mode: existing title, inserts before first same-level header" { |
| 90 | + tmp_orig=$(mktemp) |
| 91 | + cat <<EOF >"$tmp_orig" |
| 92 | +# Changelog |
| 93 | +
|
| 94 | +## OldSec |
| 95 | +Old content |
| 96 | +EOF |
| 97 | + tmp_new=$(mktemp) |
| 98 | + echo "Fresh content" >"$tmp_new" |
| 99 | + |
| 100 | + run manage_section "# Changelog" "$tmp_orig" "$tmp_new" prepend "FreshSec" |
| 101 | + [ "$status" -eq 0 ] |
| 102 | + tmpfile=$(get_tmpfile) |
| 103 | + |
| 104 | + run cat "$tmpfile" |
| 105 | + # # Changelog remains at top |
| 106 | + assert_output --regexp "^# Changelog" |
| 107 | + # FreshSec comes before OldSec |
| 108 | + assert_output --regexp "## FreshSec" |
| 109 | + assert_output --regexp "Fresh content" |
| 110 | + # OldSec still present after |
| 111 | + assert_output --regexp "## OldSec" |
| 112 | +} |
| 113 | + |
| 114 | +@test "update mode: replaces existing section content" { |
| 115 | + tmp_orig=$(mktemp) |
| 116 | + cat <<EOF >"$tmp_orig" |
| 117 | +# Changelog |
| 118 | +
|
| 119 | +## SectionA |
| 120 | +Old line1 |
| 121 | +Old line2 |
| 122 | +
|
| 123 | +## SectionB |
| 124 | +B content |
| 125 | +EOF |
| 126 | + tmp_new=$(mktemp) |
| 127 | + echo "NewA1" >"$tmp_new" |
| 128 | + echo "NewA2" >>"$tmp_new" |
| 129 | + |
| 130 | + run manage_section "# Changelog" "$tmp_orig" "$tmp_new" update "SectionA" |
| 131 | + [ "$status" -eq 0 ] |
| 132 | + tmpfile=$(get_tmpfile) |
| 133 | + |
| 134 | + run cat "$tmpfile" |
| 135 | + # SectionA content should be replaced |
| 136 | + [[ "$output" == *"## SectionA"* ]] |
| 137 | + [[ "$output" == *"NewA1"* ]] |
| 138 | + [[ "$output" == *"NewA2"* ]] |
| 139 | + # SectionB remains |
| 140 | + [[ "$output" == *"## SectionB"* ]] |
| 141 | + [[ "$output" == *"B content"* ]] |
| 142 | +} |
| 143 | + |
| 144 | +@test "update mode: fallback to prepend when section missing" { |
| 145 | + tmp_orig=$(mktemp) |
| 146 | + echo "# Changelog" >"$tmp_orig" |
| 147 | + tmp_new=$(mktemp) |
| 148 | + echo "Fallback content" >"$tmp_new" |
| 149 | + |
| 150 | + run manage_section "# Changelog" "$tmp_orig" "$tmp_new" update "MissingSec" |
| 151 | + [ "$status" -eq 0 ] |
| 152 | + tmpfile=$(get_tmpfile) |
| 153 | + |
| 154 | + run cat "$tmpfile" |
| 155 | + # MissingSec should be prepended |
| 156 | + [[ "$output" =~ "## MissingSec" ]] |
| 157 | + [[ "$output" =~ "Fallback content" ]] |
| 158 | +} |
| 159 | + |
| 160 | +@test "invalid mode returns error" { |
| 161 | + tmp_orig=$(mktemp) |
| 162 | + tmp_new=$(mktemp) |
| 163 | + echo "X" >"$tmp_new" |
| 164 | + |
| 165 | + run manage_section "Title" "$tmp_orig" "$tmp_new" invalid "Sec" |
| 166 | + [ "$status" -ne 0 ] |
| 167 | + [[ "$output" =~ "Invalid mode provided" ]] |
| 168 | +} |
| 169 | + |
| 170 | +@test "custom header insertion" { |
| 171 | + tmp_orig=$(mktemp) |
| 172 | + echo "# Changelog" >"$tmp_orig" |
| 173 | + tmp_new=$(mktemp) |
| 174 | + echo "Custom content" >"$tmp_new" |
| 175 | + |
| 176 | + # Using single '#' header instead of default '##' |
| 177 | + run manage_section "# Changelog" "$tmp_orig" "$tmp_new" prepend "CustomHdr" "##" |
| 178 | + [ "$status" -eq 0 ] |
| 179 | + tmpfile=$(get_tmpfile) |
| 180 | + |
| 181 | + run cat "$tmpfile" |
| 182 | + assert_output --regexp "^# Changelog" |
| 183 | + # Should use '# CustomHdr' |
| 184 | + assert_output --regexp "## CustomHdr" |
| 185 | + assert_output --partial "Custom content" |
| 186 | +} |
| 187 | + |
| 188 | +@test "long new content handled correctly" { |
| 189 | + tmp_orig=$(mktemp) |
| 190 | + echo "# Changelog" >"$tmp_orig" |
| 191 | + tmp_new=$(mktemp) |
| 192 | + # generate 1000 lines |
| 193 | + # shellcheck disable=SC2034 |
| 194 | + for i in $(seq 1 1000); do printf "Line %s\n" "$i" >>"$tmp_new"; done |
| 195 | + |
| 196 | + run manage_section "# Changelog" "$tmp_orig" "$tmp_new" prepend "Bulk" |
| 197 | + [ "$status" -eq 0 ] |
| 198 | + tmpfile=$(get_tmpfile) |
| 199 | + |
| 200 | + run wc -l <"$tmpfile" |
| 201 | + cat "$tmpfile" | head |
| 202 | + # Should be original lines + 1 title + blanks + 1 header + blank + 1000 content lines |
| 203 | + expected=$((1 + 1 + 1 + 1 + 1000)) |
| 204 | + assert_output "$expected" |
| 205 | +} |
| 206 | + |
| 207 | +@test "unexpected formatting: missing blank lines around headers" { |
| 208 | + tmp_orig=$(mktemp) |
| 209 | + # Title and section without blank lines |
| 210 | + cat <<EOF >"$tmp_orig" |
| 211 | +# Changelog |
| 212 | +## OldSec |
| 213 | +Old |
| 214 | +EOF |
| 215 | + tmp_new=$(mktemp) |
| 216 | + echo "NewFmt" >"$tmp_new" |
| 217 | + |
| 218 | + run manage_section "# Changelog" "$tmp_orig" "$tmp_new" prepend "FmtTest" |
| 219 | + [ "$status" -eq 0 ] |
| 220 | + tmpfile=$(get_tmpfile) |
| 221 | + |
| 222 | + run sed -n '1,5p' "$tmpfile" |
| 223 | + # Check that new section inserted with blank lines |
| 224 | + [[ "$output" =~ "# Changelog" ]] |
| 225 | + [[ "$output" =~ "## FmtTest" ]] |
| 226 | + [[ "$output" =~ "NewFmt" ]] |
| 227 | +} |
0 commit comments