Skip to content

Commit

Permalink
Merge pull request #345 from mvargeson/mvargeson/issue-314/error-on-warn
Browse files Browse the repository at this point in the history
Add --error-on-warn flag to exit 1 on warnings
  • Loading branch information
orta authored Oct 20, 2022
2 parents fe6138b + 4eb3198 commit 86ed9f6
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test error-on-warn: at dev time patch-package warns but returns 0 1`] = `
"SNAPSHOT: at dev time patch-package warns but returns 0
Warning: patch-package detected a patch file version mismatch
Don't worry! This is probably fine. The patch was still applied
successfully. Here's the deets:
Patch file created for
left-pad@1.1.2
applied to
left-pad@1.1.3
At path
node_modules/left-pad
This warning is just to give you a heads-up. There is a small chance of
breakage even though the patch was applied successfully. Make sure the package
still behaves like you expect (you wrote tests, right?) and then run
patch-package left-pad
to update the version in the patch file name and make this warning go away.
---
patch-package finished with 1 warning(s).
END SNAPSHOT"
`;
22 changes: 22 additions & 0 deletions integration-tests/error-on-warn/error-on-warn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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=""

(>&2 echo "SNAPSHOT: at dev time patch-package warns but returns 0")
if ! patch-package;
then
exit 1
fi
(>&2 echo "END SNAPSHOT")

echo "adding --error-on-warn forces patch-package to return 1 at dev time"
if patch-package --error-on-warn;
then
exit 1
fi
5 changes: 5 additions & 0 deletions integration-tests/error-on-warn/error-on-warn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { runIntegrationTest } from "../runIntegrationTest"
runIntegrationTest({
projectName: "error-on-warn",
shouldProduceSnapshots: true,
})
11 changes: 11 additions & 0 deletions integration-tests/error-on-warn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "error-on-warn",
"version": "1.0.0",
"description": "integration test for patch-package",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"left-pad": "1.1.3"
}
}
13 changes: 13 additions & 0 deletions integration-tests/error-on-warn/patches/left-pad+1.1.2.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
@@ -7,7 +7,7 @@
module.exports = leftPad;

var cache = [
- '',
+ "",
' ',
' ',
' ',
8 changes: 8 additions & 0 deletions integration-tests/error-on-warn/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.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a"
integrity sha1-YS9hwDPzqeCOk58crr7qQbbzGZo=
12 changes: 10 additions & 2 deletions src/applyPatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ export function applyPatchesForApp({
reverse,
patchDir,
shouldExitWithError,
shouldExitWithWarning,
}: {
appPath: string
reverse: boolean
patchDir: string
shouldExitWithError: boolean
shouldExitWithWarning: boolean
}): void {
const patchesDirectory = join(appPath, patchDir)
const files = findPatchFiles(patchesDirectory)
Expand Down Expand Up @@ -221,9 +223,15 @@ export function applyPatchesForApp({
)
}

if (errors.length) {
process.exit(shouldExitWithError ? 1 : 0)
if (errors.length && shouldExitWithError) {
process.exit(1)
}

if (warnings.length && shouldExitWithWarning) {
process.exit(1)
}

process.exit(0)
}

export function applyPatch({
Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const argv = minimist(process.argv.slice(2), {
"help",
"version",
"error-on-fail",
"error-on-warn",
"create-issue",
],
string: ["patch-dir"],
Expand Down Expand Up @@ -78,7 +79,16 @@ if (argv.version || argv.v) {
// see https://github.com/ds300/patch-package/issues/86
const shouldExitWithError =
!!argv["error-on-fail"] || isCi || process.env.NODE_ENV === "test"
applyPatchesForApp({ appPath, reverse, patchDir, shouldExitWithError })

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

applyPatchesForApp({
appPath,
reverse,
patchDir,
shouldExitWithError,
shouldExitWithWarning,
})
}
}

Expand Down Expand Up @@ -116,6 +126,12 @@ Usage:
--error-on-fail is ${chalk.bold("switched on")} by default on CI.
See https://github.com/ds300/patch-package/issues/86 for background.
${chalk.bold("--error-on-warn")}
Forces patch-package to exit with code 1 after warning.
See https://github.com/ds300/patch-package/issues/314 for background.
${chalk.bold("--reverse")}
Expand Down

0 comments on commit 86ed9f6

Please sign in to comment.