|
| 1 | +#! /usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +#enable_debug=true; set -x |
| 6 | +enable_debug=false; set +x |
| 7 | + |
| 8 | +#test_script="$1" |
| 9 | +test_script_list="$@" |
| 10 | +# test/patch-parse-failure.sh |
| 11 | + |
| 12 | +# FIXME test/shrinkwrap.sh |
| 13 | +# Patch file: patches/left-pad+1.1.3.patch |
| 14 | +# Patch was made for version: 1.1.3 |
| 15 | +# Installed version: 1.1.1 |
| 16 | + |
| 17 | + |
| 18 | +# FIXME |
| 19 | +cat >/dev/null <<EOF |
| 20 | +EOF |
| 21 | + |
| 22 | +# test |
| 23 | +test_script_list=$(cat <<'EOF' |
| 24 | +test/ignore-whitespace.sh.FIXME |
| 25 | +test/shrinkwrap.sh.FIXME |
| 26 | +test/dev-only-patches.sh.FIXME |
| 27 | +test/patch-parse-failure.sh |
| 28 | +test/broken-patch-file.sh |
| 29 | +test/fails-when-no-package.sh |
| 30 | +test/custom-resolutions.sh |
| 31 | +test/collate-errors.sh |
| 32 | +test/no-symbolic-links.sh |
| 33 | +test/error-on-fail.sh |
| 34 | +test/custom-patch-dir.sh |
| 35 | +test/delete-scripts.sh |
| 36 | +test/ignores-scripts-when-making-patch.sh |
| 37 | +test/adding-and-deleting-files.sh |
| 38 | +test/create-issue.sh |
| 39 | +test/scoped-package.sh |
| 40 | +test/package-gets-updated.sh |
| 41 | +test/happy-path-npm.sh |
| 42 | +test/happy-path-yarn.sh |
| 43 | +test/unexpected-patch-creation-failure.sh |
| 44 | +test/delete-old-patch-files.sh |
| 45 | +test/lerna-canary.sh |
| 46 | +test/nested-packages.sh |
| 47 | +test/file-mode-changes.sh |
| 48 | +test/nested-scoped-packages.sh |
| 49 | +test/include-exclude-regex-relativity.sh |
| 50 | +test/yarn-workspaces.sh |
| 51 | +test/reverse-option.sh |
| 52 | +test/include-exclude-paths.sh |
| 53 | +EOF |
| 54 | +) |
| 55 | + |
| 56 | +function pkg_jq() { |
| 57 | + # yes i know sponge. this is portable |
| 58 | + cat package.json | jq "$@" >package.json.1 |
| 59 | + mv package.json.1 package.json |
| 60 | +} |
| 61 | + |
| 62 | +if $enable_debug; then |
| 63 | +function debug() { |
| 64 | + echo " $(tput setaf 8)$@$(tput sgr0)" |
| 65 | +} |
| 66 | +else |
| 67 | +function debug(){ :; } |
| 68 | +fi |
| 69 | + |
| 70 | +if $enable_debug; then |
| 71 | +function echo_on(){ :; } |
| 72 | +function echo_off(){ :; } |
| 73 | +else |
| 74 | +# https://stackoverflow.com/questions/17840322/how-to-undo-exec-dev-null-in-bash |
| 75 | +function echo_on() { |
| 76 | + exec 1>&5 |
| 77 | + exec 1>&6 |
| 78 | +} |
| 79 | + |
| 80 | +function echo_off() { |
| 81 | + # TODO write to logfile instead of /dev/null -> help to debug |
| 82 | + exec 5>&1 1>/dev/null |
| 83 | + exec 6>&2 2>/dev/null |
| 84 | +} |
| 85 | +fi |
| 86 | +# color demo: |
| 87 | +# for n in $(seq 0 16); do printf "$(tput setaf $n)$n$(tput sgr0) "; done; echo |
| 88 | +# 7 = light gray |
| 89 | +# 8 = dark gray |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +for test_script in $test_script_list |
| 94 | +do |
| 95 | + |
| 96 | +#echo "$(tput setaf 6)TEST$(tput sgr0) $test_script" # cyan |
| 97 | +echo "$(tput setaf 11)TEST$(tput sgr0) $test_script" # yellow |
| 98 | + |
| 99 | +if (echo "$test_script" | grep -q '\.FIXME$') |
| 100 | +then |
| 101 | + echo " skip" |
| 102 | + echo |
| 103 | + continue |
| 104 | +fi |
| 105 | + |
| 106 | +work_dir=work |
| 107 | + |
| 108 | +[ -e "$test_script" ] || { |
| 109 | + echo "no such file: $test_script" >&2 |
| 110 | + exit 1 |
| 111 | +} |
| 112 | + |
| 113 | +test_name="$(basename "$test_script" .sh)" |
| 114 | + |
| 115 | +test_script="$(readlink -f "$test_script")" # absolute path |
| 116 | +test_script_base="${test_script%.*}" |
| 117 | + |
| 118 | +patches_src="$(readlink -f "../$test_name/patches")" |
| 119 | + |
| 120 | +test_src="$(readlink -f "../$test_name")" |
| 121 | + |
| 122 | +[ -d snapshot ] || mkdir snapshot |
| 123 | +snapshot_dir="$(readlink -f snapshot)" |
| 124 | + |
| 125 | +snapshot_index=-1 |
| 126 | + |
| 127 | +function expect_error() { |
| 128 | + echo_on |
| 129 | + expect_return "!=0" "$@" |
| 130 | + echo_off |
| 131 | +} |
| 132 | + |
| 133 | +function expect_ok() { |
| 134 | + echo_on |
| 135 | + expect_return "==0" "$@" |
| 136 | + echo_off |
| 137 | +} |
| 138 | + |
| 139 | +function expect_return() { |
| 140 | + snapshot_index=$((snapshot_index + 1)) |
| 141 | + local rc_condition="$1" |
| 142 | + shift |
| 143 | + local name="$1" |
| 144 | + shift |
| 145 | + git add . >/dev/null |
| 146 | + git commit -m 'before expect_error' >/dev/null || true |
| 147 | + local rc=0 |
| 148 | + debug "exec: $*" |
| 149 | + out=$("$@" 2>&1) || rc=$? # https://stackoverflow.com/questions/18621990 |
| 150 | + debug "rc = $rc" |
| 151 | + if (( "$rc" $rc_condition )) |
| 152 | + then |
| 153 | + # passed the rc check |
| 154 | + echo " $(tput setaf 2)PASS$(tput sgr0) $name ($rc $rc_condition)" |
| 155 | + git add . >/dev/null |
| 156 | + git commit -m 'after expect_error' >/dev/null || true |
| 157 | + #shot="$snapshot_dir/$test_name/$snapshot_index.txt" |
| 158 | + #shot="$test_script_base.out-$snapshot_index.txt" |
| 159 | + |
| 160 | + # cleanup snapshot |
| 161 | + # # generated by patch-package 0.0.0 on 2022-04-13 20:43:55 |
| 162 | + # FIXME: patch-package should have a "timeless" option (both CLI and env) -> all times are unix zero = 1970-01-01 00:00:00 |
| 163 | + out="$(echo "$out" | sed -E 's/^# generated by patch-package 0.0.0 on [0-9 :-]+$/# generated by patch-package 0.0.0 on 1970-01-01 00:00:00/')" |
| 164 | + shot="$test_script.$snapshot_index.txt" |
| 165 | + debug "using snapshot file: $shot" |
| 166 | + # to update a snapshot, delete the old snapshot file |
| 167 | + if [ -e "$shot" ]; then |
| 168 | + debug comparing snapshot |
| 169 | + if diff -u --color "$shot" <(echo "$out") # returns 0 if equal, print diff if not equal |
| 170 | + then |
| 171 | + echo " $(tput setaf 2)PASS$(tput sgr0) $name (snapshot)" |
| 172 | + else |
| 173 | + echo " $(tput setaf 1)FAIL$(tput sgr0) $name (snapshot)" |
| 174 | + return 1 |
| 175 | + fi |
| 176 | + else |
| 177 | + debug writing snapshot |
| 178 | + [ -d "$snapshot_dir/$test_name" ] || mkdir -v -p "$snapshot_dir/$test_name" |
| 179 | + echo " writing snapshot $(basename "$shot")" |
| 180 | + echo "$out" >"$shot" |
| 181 | + fi |
| 182 | + return 0 |
| 183 | + else |
| 184 | + # fail |
| 185 | + git add . >/dev/null |
| 186 | + git commit -m 'after expect_error' >/dev/null || true |
| 187 | + echo " $(tput setaf 1)FAIL$(tput sgr0) $name. actual $rc vs expected $rc_condition" |
| 188 | + if [ "$rc" = 127 ]; then |
| 189 | + echo "internal error? rc=$rc can mean: command not found. maybe you forgot 'npx' before the command? command was: $*" |
| 190 | + fi |
| 191 | + echo "out: $out" |
| 192 | + # TODO expected $out |
| 193 | + return 1 |
| 194 | + fi |
| 195 | +} |
| 196 | + |
| 197 | +git_main_branch=main |
| 198 | + |
| 199 | +( |
| 200 | +cd "$work_dir" |
| 201 | +git checkout --force $git_main_branch >/dev/null 2>&1 || true |
| 202 | +git reset --hard >/dev/null |
| 203 | +git clean --force -d # remove empty untraced directories https://stackoverflow.com/questions/28565473/git-clean-removes-empty-directories |
| 204 | + |
| 205 | +# FIXME in rare cases, this fails to delete the branch |
| 206 | +#git branch -D $test_name >/dev/null 2>&1 || true |
| 207 | +debug "delete branch, try 1" |
| 208 | +git branch -D $test_name >/dev/null 2>&1 || true # debug |
| 209 | +debug "delete branch, try 2" |
| 210 | +git branch -D $test_name >/dev/null 2>&1 || true # debug |
| 211 | + |
| 212 | +# FIXME this fails in rare cases. bug in git? |
| 213 | +debug "create branch" |
| 214 | +git branch $test_name >/dev/null |
| 215 | +debug "switch branch" |
| 216 | +git switch $test_name >/dev/null 2>&1 |
| 217 | + |
| 218 | +if [ -d "$patches_src" ]; then |
| 219 | + debug "copying patches for this test:" |
| 220 | + ( |
| 221 | + # debug |
| 222 | + cd "$test_src" |
| 223 | + find patches/ -type f | while read f; do debug " $f"; done |
| 224 | + ) |
| 225 | + cp -r "$patches_src" . |
| 226 | +fi |
| 227 | + |
| 228 | +git add . >/dev/null |
| 229 | +git commit -m before >/dev/null || true |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +# prepare |
| 234 | + |
| 235 | +export CI=true # needed so patch-package returns 1 on error |
| 236 | +# see shouldExitWithError in applyPatches.ts |
| 237 | +# see run-tests.sh |
| 238 | +# FIXME? patch-package should always return 1 on error |
| 239 | +# tests: |
| 240 | +# test/fails-when-no-package.sh |
| 241 | +# test/broken-patch-file.sh |
| 242 | +# ... |
| 243 | + |
| 244 | +export NODE_ENV="" # test/error-on-fail.sh sets NODE_ENV="development" |
| 245 | + |
| 246 | + |
| 247 | + |
| 248 | +# run |
| 249 | + |
| 250 | +# set argv. $1 == $test_src |
| 251 | +set -- "$test_src" |
| 252 | + |
| 253 | +debug "writing logfile $test_script.log" |
| 254 | +debug "sourcing $test_script ..." |
| 255 | +echo_off |
| 256 | +source "$test_script" |
| 257 | +echo_on |
| 258 | +debug "sourcing $test_script done" |
| 259 | + |
| 260 | +git add . >/dev/null |
| 261 | +git commit -m after >/dev/null || true |
| 262 | + |
| 263 | +# TODO |
| 264 | +) |
| 265 | + |
| 266 | +echo # empty line after each test |
| 267 | + |
| 268 | +done |
| 269 | + |
| 270 | +echo "$(tput setaf 2)PASS ALL$(tput sgr0)" |
| 271 | + |
0 commit comments