Skip to content

Commit c87136d

Browse files
author
test
committed
add combined tests
goal was to make tests faster result: 200 seconds -> 90 seconds -> still too slow -> need synthetic tests
1 parent f20c0cf commit c87136d

File tree

137 files changed

+2113
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+2113
-4
lines changed

β€Žintegration-tests/combined/gen.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#! /bin/sh
2+
3+
# TODO use synthetic test packages
4+
5+
# NOTE versions are wrong
6+
# versions were pinned manually in work/packages.json
7+
8+
# the idea here is that 'git checkout' is faster than 'yarn install'
9+
# and if we have one common package with all the dependencies,
10+
# we can use that as the 'main' branch
11+
# and create tests as fork branches, based on the main branch
12+
13+
set -e
14+
#set -x # debug
15+
16+
keys="dependencies devDependencies peerDependencies"
17+
18+
#false &&
19+
for pkg in ../*/package.json
20+
do
21+
[ "$pkg" = "../combined/package.json" ] && continue
22+
for key in $keys
23+
do
24+
jq -r ".$key | keys | .[]" <"$pkg" >>$key.txt 2>/dev/null || true
25+
done
26+
done
27+
28+
#false &&
29+
for key in $keys
30+
do
31+
cat $key.txt | sort | uniq >$key.txt.2
32+
mv $key.txt.2 $key.txt
33+
done
34+
35+
#../ignores-scripts-when-making-patch/package.json: "naughty-package": "file:./naughty-package",
36+
37+
work_dir=work
38+
mkdir "$work_dir"
39+
(
40+
cd "$work_dir"
41+
yarn init -y
42+
43+
# TODO only add to package.json but dont install yet
44+
cat ../dependencies.txt | grep -v -x naughty-package | xargs -r yarn add
45+
cat ../devDependencies.txt | xargs -r yarn add --dev
46+
cat ../peerDependencies.txt | xargs -r yarn add --peer
47+
48+
stat ../../../patch-package.tgz # make sure the file exists
49+
yarn add patch-package@file:../../../patch-package.tgz
50+
51+
# TODO install all packages in one go
52+
#yarn install
53+
54+
# add this to package.json
55+
# "scripts": {
56+
# "postinstall": "patch-package"
57+
# },
58+
# yes i know `sponge` but this is more portable
59+
cat package.json | jq '.scripts.postinstall = "patch-package"' >package.json.1
60+
mv package.json.1 package.json
61+
62+
export GIT_AUTHOR_NAME=test
63+
export GIT_AUTHOR_EMAIL=
64+
export GIT_COMMITTER_NAME=test
65+
export GIT_COMMITTER_EMAIL=
66+
[ -e .gitignore ] && rm -v .gitignore # just make sure. track all files with git
67+
git init
68+
git add .
69+
git commit -m init
70+
) # cd "$work_dir"
71+
72+
mkdir test
73+
#ls ../../*/*.sh | grep -v ^../../combined/ | xargs cp -v -t test/
74+
ls ../*/*.sh | grep -v ^../combined/ | xargs cp -v -t test/

β€Žintegration-tests/combined/run.sh

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
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+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
echo "add a file"
2+
echo "this is a new file" > node_modules/lodash/newFile.md
3+
4+
echo "remove a file"
5+
rm node_modules/lodash/fp/__.js
6+
7+
expect_ok "generate patch file" npx patch-package lodash
8+
9+
echo "remove node_modules"
10+
rm -rf node_modules
11+
12+
echo "remove postinstall script"
13+
pkg_jq 'del(.scripts.postinstall)'
14+
15+
echo "reinstall node_modules"
16+
yarn
17+
18+
expect_ok "apply patch after reinstall" npx patch-package
19+
20+
echo "check that the file was added"
21+
expect_ok "check that the file was added" cat node_modules/lodash/newFile.md
22+
23+
expect_error "check that the file was removed" cat node_modules/lodash/fp/__.js
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
patch-package 0.0.0
2+
β€’ Creating temporary folder
3+
β€’ Installing lodash@4.17.21 with yarn
4+
β€’ Diffing your files with clean files
5+
βœ” Created file patches/lodash+4.17.21.patch
6+
7+
πŸ’‘ lodash is on GitHub! To draft an issue based on your patch run
8+
9+
yarn patch-package lodash --create-issue
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
patch-package 0.0.0
2+
Applying patches...
3+
lodash@4.17.21 βœ”
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a new file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cat: node_modules/lodash/fp/__.js: No such file or directory
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cp -r "$1"/patches .
2+
3+
expect_error "patch-package fails when patch file is invalid" npx patch-package

0 commit comments

Comments
 (0)