Skip to content

feat: expect-patch flag #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ Run `patch-package` without arguments to apply all patches in your project.

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

- `--expect-patch`

Prints error when no patches happen. Best combined with `--error-on-fail` (enabled by default on CI).

#### Notes

To apply patches individually, you may use `git`:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test expect-patch: 00: expect-patch flag prints an error message and by default does not fail 1`] = `
"SNAPSHOT: expect-patch flag prints an error message and by default does not fail
patch-package 0.0.0
Applying patches...
No patch files found
---
patch-package finished with 1 error(s).
END SNAPSHOT"
`;

exports[`Test expect-patch: 01: expect-patch is silent when patches happen 1`] = `
"SNAPSHOT: expect-patch is silent when patches happen
patch-package 0.0.0
Applying patches...
[email protected]
END SNAPSHOT"
`;

exports[`Test expect-patch: 02: expect-patch flag produces error for error-on-fail flag 1`] = `
"SNAPSHOT: expect-patch flag produces error for error-on-fail flag
patch-package 0.0.0
Applying patches...
No patch files found
---
patch-package finished with 1 error(s).
END SNAPSHOT"
`;
30 changes: 30 additions & 0 deletions integration-tests/expect-patch/expect-patch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# make sure errors stop the script
set -e

echo "add patch-package"
yarn add $1
alias patch-package=./node_modules/.bin/patch-package

export NODE_ENV="development"
export CI="true"

echo "SNAPSHOT: expect-patch flag prints an error message and by default does not fail"
if ! patch-package --expect-patch;
then
exit 1
fi
echo "END SNAPSHOT"

echo "SNAPSHOT: expect-patch is silent when patches happen"
if ! patch-package --expect-patch --patch-dir patches-custom;
then
exit 1
fi
echo "END SNAPSHOT"

echo "SNAPSHOT: expect-patch flag produces error for error-on-fail flag"
if patch-package --expect-patch --error-on-fail;
then
exit 1
fi
echo "END SNAPSHOT"
5 changes: 5 additions & 0 deletions integration-tests/expect-patch/expect-patch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { runIntegrationTest } from "../runIntegrationTest"
runIntegrationTest({
projectName: "expect-patch",
shouldProduceSnapshots: true,
})
11 changes: 11 additions & 0 deletions integration-tests/expect-patch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "expect-patch",
"version": "1.0.0",
"description": "integration test for patch-package",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"left-pad": "^1.3.0"
}
}
13 changes: 13 additions & 0 deletions integration-tests/expect-patch/patches-custom/left-pad+1.3.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/left-pad/index.js b/node_modules/left-pad/index.js
index 26f73ff..60f3f56 100644
--- a/node_modules/left-pad/index.js
+++ b/node_modules/left-pad/index.js
@@ -4,7 +4,7 @@
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details. */
'use strict';
-module.exports = leftPad;
+module.exports = patch-package;

var cache = [
'',
8 changes: 8 additions & 0 deletions integration-tests/expect-patch/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


left-pad@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==
16 changes: 11 additions & 5 deletions src/applyPatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,34 @@ export function applyPatchesForApp({
appPath,
reverse,
patchDir,
expectPatch,
shouldExitWithError,
shouldExitWithWarning,
bestEffort,
}: {
appPath: string
reverse: boolean
patchDir: string
expectPatch: boolean
shouldExitWithError: boolean
shouldExitWithWarning: boolean
bestEffort: boolean
}): void {
const patchesDirectory = join(appPath, patchDir)
const groupedPatches = getGroupedPatches(patchesDirectory)

if (groupedPatches.numPatchFiles === 0) {
console.log(chalk.blueBright("No patch files found"))
return
}

const errors: string[] = []
const warnings: string[] = [...groupedPatches.warnings]

if (groupedPatches.numPatchFiles === 0) {
if (expectPatch) {
errors.push("No patch files found")
} else {
console.log(chalk.blueBright("No patch files found"))
return
}
}

for (const patches of Object.values(
groupedPatches.pathSpecifierToPatchFiles,
)) {
Expand Down
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const argv = minimist(process.argv.slice(2), {
"error-on-warn",
"create-issue",
"partial",
"expect-patch",
"",
],
string: ["patch-dir", "append", "rebase"],
Expand Down Expand Up @@ -114,11 +115,13 @@ if (argv.version || argv.v) {
process.env.NODE_ENV === "test"

const shouldExitWithWarning = !!argv["error-on-warn"]
const expectPatch = !!argv["expect-patch"]

applyPatchesForApp({
appPath,
reverse,
patchDir,
expectPatch,
shouldExitWithError,
shouldExitWithWarning,
bestEffort: argv.partial,
Expand Down Expand Up @@ -177,6 +180,15 @@ Usage:
This option was added to help people using CircleCI avoid an issue around caching
and patch file updates (https://github.com/ds300/patch-package/issues/37),
but might be useful in other contexts too.

${chalk.bold("--expect-patch")}

Prints an error if no patch files were found.

This option works in tandem with ${chalk.bold(
"--error-on-fail",
)} (enabled by default in CI) to prevent
accidental skips due to patch folder was missed or ignored. For example during COPY in Dockerfile, or in .dockerignore.


2. Creating patch files
Expand Down