Skip to content

Commit 5b212d9

Browse files
Fail recipe when npm install fails (#7227)
1 parent f9234ea commit 5b212d9

2 files changed

Lines changed: 41 additions & 22 deletions

File tree

rewrite-javascript/rewrite/src/javascript/recipes/upgrade-dependency-version.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
import * as path from "path";
3232
import * as semver from "semver";
3333
import * as picomatch from "picomatch";
34-
import {markupWarn, replaceMarkerByKind} from "../../markers";
34+
import {replaceMarkerByKind} from "../../markers";
3535
import {TreePrinters} from "../../print";
3636
import {
3737
createDependencyRecipeAccumulator,
@@ -301,11 +301,7 @@ export class UpgradeDependencyVersion extends ScanningRecipe<Accumulator> {
301301
);
302302
if (failureMessage) {
303303
const names = updateInfo.matchedDependencies.map(d => d.packageName).join(', ');
304-
return markupWarn(
305-
doc,
306-
`Failed to upgrade ${names} to ${recipe.newVersion}`,
307-
failureMessage
308-
);
304+
throw new Error(`Failed to upgrade ${names} to ${recipe.newVersion}: ${failureMessage}`);
309305
}
310306

311307
let modifiedDoc = doc;

rewrite-javascript/rewrite/test/javascript/recipes/upgrade-dependency-version.test.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
import {Json} from "../../../src/json";
2525
import {RecipeSpec} from "../../../src/test";
2626
import {withDir} from "tmp-promise";
27-
import {findMarker, MarkersKind} from "../../../src";
27+
import * as fs from "fs";
28+
import * as path from "path";
2829

2930
describe("UpgradeDependencyVersion", () => {
3031

@@ -292,40 +293,62 @@ describe("UpgradeDependencyVersion", () => {
292293
}, {unsafeCleanup: true});
293294
});
294295

295-
test("adds warning marker when version does not exist", async () => {
296+
test("throws when install fails for non-existent version", async () => {
296297
const spec = new RecipeSpec();
297298
spec.recipe = new UpgradeDependencyVersion({
298299
packageName: "uuid",
299300
newVersion: "^999.0.0" // Non-existent version
300301
});
301302

302303
await withDir(async (repo) => {
303-
await spec.rewriteRun(
304+
await expect(spec.rewriteRun(
304305
npm(
305306
repo.path,
306307
typescript(`const x = 1;`),
307-
{
308-
// Version doesn't change, but warning marker is added
309-
...packageJson(`
308+
packageJson(`
310309
{
311310
"name": "test-project",
312311
"version": "1.0.0",
313312
"dependencies": {
314313
"uuid": "^9.0.0"
315314
}
316315
}
317-
`, (actual: string) => {
318-
expect(actual).toContain('/*~~(Failed to upgrade uuid to ^999.0.0');
319-
return actual;
320-
}), afterRecipe: async (doc: Json.Document) => {
321-
// Should have a warning marker
322-
const warnMarker = findMarker(doc, MarkersKind.MarkupWarn);
323-
expect(warnMarker).toBeDefined();
324-
expect((warnMarker as any).message).toContain("Failed to upgrade uuid");
316+
`)
317+
)
318+
)).rejects.toThrow("Failed to upgrade uuid to ^999.0.0");
319+
}, {unsafeCleanup: true});
320+
});
321+
322+
test("throws when install fails due to engine version mismatch", async () => {
323+
const spec = new RecipeSpec();
324+
spec.recipe = new UpgradeDependencyVersion({
325+
packageName: "uuid",
326+
newVersion: "^10.0.0"
327+
});
328+
329+
await withDir(async (repo) => {
330+
// given
331+
fs.writeFileSync(path.join(repo.path, '.npmrc'), 'engine-strict=true');
332+
333+
// when / then
334+
await expect(spec.rewriteRun(
335+
npm(
336+
repo.path,
337+
typescript(`const x = 1;`),
338+
packageJson(`
339+
{
340+
"name": "test-project",
341+
"version": "1.0.0",
342+
"engines": {
343+
"node": "504.436"
344+
},
345+
"dependencies": {
346+
"uuid": "^9.0.0"
347+
}
325348
}
326-
}
349+
`)
327350
)
328-
);
351+
)).rejects.toThrow(/^Error: Failed to upgrade uuid to \^10\.0\.0:/);
329352
}, {unsafeCleanup: true});
330353
});
331354

0 commit comments

Comments
 (0)