Skip to content

Commit 11e7bf7

Browse files
committed
feat: expect-patch flag
1 parent bd2e9a4 commit 11e7bf7

File tree

9 files changed

+123
-5
lines changed

9 files changed

+123
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ Run `patch-package` without arguments to apply all patches in your project.
229229

230230
Specify the name for the directory in which the patch files are located
231231

232+
- `--expect-patch`
233+
234+
Prints error when no patches happen. Best combined with `--error-on-fail` (enabled by default on CI).
235+
232236
#### Notes
233237

234238
To apply patches individually, you may use `git`:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test expect-patch: 00: expect-patch flag prints an error message and by default does not fail 1`] = `
4+
"SNAPSHOT: expect-patch flag prints an error message and by default does not fail
5+
patch-package 0.0.0
6+
Applying patches...
7+
No patch files found
8+
---
9+
patch-package finished with 1 error(s).
10+
END SNAPSHOT"
11+
`;
12+
13+
exports[`Test expect-patch: 01: expect-patch is silent when patches happen 1`] = `
14+
"SNAPSHOT: expect-patch is silent when patches happen
15+
patch-package 0.0.0
16+
Applying patches...
17+
18+
END SNAPSHOT"
19+
`;
20+
21+
exports[`Test expect-patch: 02: expect-patch flag produces error for error-on-fail flag 1`] = `
22+
"SNAPSHOT: expect-patch flag produces error for error-on-fail flag
23+
patch-package 0.0.0
24+
Applying patches...
25+
No patch files found
26+
---
27+
patch-package finished with 1 error(s).
28+
END SNAPSHOT"
29+
`;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# make sure errors stop the script
2+
set -e
3+
4+
echo "add patch-package"
5+
yarn add $1
6+
alias patch-package=./node_modules/.bin/patch-package
7+
8+
export NODE_ENV="development"
9+
export CI="true"
10+
11+
echo "SNAPSHOT: expect-patch flag prints an error message and by default does not fail"
12+
if ! patch-package --expect-patch;
13+
then
14+
exit 1
15+
fi
16+
echo "END SNAPSHOT"
17+
18+
echo "SNAPSHOT: expect-patch is silent when patches happen"
19+
if ! patch-package --expect-patch --patch-dir patches-custom;
20+
then
21+
exit 1
22+
fi
23+
echo "END SNAPSHOT"
24+
25+
echo "SNAPSHOT: expect-patch flag produces error for error-on-fail flag"
26+
if patch-package --expect-patch --error-on-fail;
27+
then
28+
exit 1
29+
fi
30+
echo "END SNAPSHOT"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { runIntegrationTest } from "../runIntegrationTest"
2+
runIntegrationTest({
3+
projectName: "expect-patch",
4+
shouldProduceSnapshots: true,
5+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "expect-patch",
3+
"version": "1.0.0",
4+
"description": "integration test for patch-package",
5+
"main": "index.js",
6+
"author": "",
7+
"license": "ISC",
8+
"dependencies": {
9+
"left-pad": "^1.3.0"
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/node_modules/left-pad/index.js b/node_modules/left-pad/index.js
2+
index 26f73ff..60f3f56 100644
3+
--- a/node_modules/left-pad/index.js
4+
+++ b/node_modules/left-pad/index.js
5+
@@ -4,7 +4,7 @@
6+
* To Public License, Version 2, as published by Sam Hocevar. See
7+
* http://www.wtfpl.net/ for more details. */
8+
'use strict';
9+
-module.exports = leftPad;
10+
+module.exports = patch-package;
11+
12+
var cache = [
13+
'',
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
left-pad@^1.3.0:
6+
version "1.3.0"
7+
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
8+
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==

src/applyPatches.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,34 @@ export function applyPatchesForApp({
9595
appPath,
9696
reverse,
9797
patchDir,
98+
expectPatch,
9899
shouldExitWithError,
99100
shouldExitWithWarning,
100101
bestEffort,
101102
}: {
102103
appPath: string
103104
reverse: boolean
104105
patchDir: string
106+
expectPatch: boolean
105107
shouldExitWithError: boolean
106108
shouldExitWithWarning: boolean
107109
bestEffort: boolean
108110
}): void {
109111
const patchesDirectory = join(appPath, patchDir)
110112
const groupedPatches = getGroupedPatches(patchesDirectory)
111113

112-
if (groupedPatches.numPatchFiles === 0) {
113-
console.log(chalk.blueBright("No patch files found"))
114-
return
115-
}
116-
117114
const errors: string[] = []
118115
const warnings: string[] = [...groupedPatches.warnings]
119116

117+
if (groupedPatches.numPatchFiles === 0) {
118+
if (expectPatch) {
119+
errors.push("No patch files found")
120+
} else {
121+
console.log(chalk.blueBright("No patch files found"))
122+
return
123+
}
124+
}
125+
120126
for (const patches of Object.values(
121127
groupedPatches.pathSpecifierToPatchFiles,
122128
)) {

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const argv = minimist(process.argv.slice(2), {
2525
"error-on-warn",
2626
"create-issue",
2727
"partial",
28+
"expect-patch",
2829
"",
2930
],
3031
string: ["patch-dir", "append", "rebase"],
@@ -114,11 +115,13 @@ if (argv.version || argv.v) {
114115
process.env.NODE_ENV === "test"
115116

116117
const shouldExitWithWarning = !!argv["error-on-warn"]
118+
const expectPatch = !!argv["expect-patch"]
117119

118120
applyPatchesForApp({
119121
appPath,
120122
reverse,
121123
patchDir,
124+
expectPatch,
122125
shouldExitWithError,
123126
shouldExitWithWarning,
124127
bestEffort: argv.partial,
@@ -177,6 +180,15 @@ Usage:
177180
This option was added to help people using CircleCI avoid an issue around caching
178181
and patch file updates (https://github.com/ds300/patch-package/issues/37),
179182
but might be useful in other contexts too.
183+
184+
${chalk.bold("--expect-patch")}
185+
186+
Prints an error if no patch files were found.
187+
188+
This option works in tandem with ${chalk.bold(
189+
"--error-on-fail",
190+
)} (enabled by default in CI) to prevent
191+
accidental skips due to patch folder was missed or ignored. For example during COPY in Dockerfile, or in .dockerignore.
180192
181193
182194
2. Creating patch files

0 commit comments

Comments
 (0)