Skip to content

Commit 00226af

Browse files
authored
UpgradeDependencyVersion: warn on install failure instead of throwing (#7479)
1 parent e156df4 commit 00226af

2 files changed

Lines changed: 44 additions & 25 deletions

File tree

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

Lines changed: 2 additions & 2 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 {replaceMarkerByKind} from "../../markers";
34+
import {markupWarn, replaceMarkerByKind} from "../../markers";
3535
import {TreePrinters} from "../../print";
3636
import {
3737
createDependencyRecipeAccumulator,
@@ -301,7 +301,7 @@ export class UpgradeDependencyVersion extends ScanningRecipe<Accumulator> {
301301
);
302302
if (failureMessage) {
303303
const names = updateInfo.matchedDependencies.map(d => d.packageName).join(', ');
304-
throw new Error(`Failed to upgrade ${names} to ${recipe.newVersion}: ${failureMessage}`);
304+
return markupWarn(doc, `Failed to upgrade ${names} to ${recipe.newVersion}`, failureMessage);
305305
}
306306

307307
let modifiedDoc = doc;

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

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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";
2728
import * as fs from "fs";
2829
import * as path from "path";
2930
import * as semver from "semver";
@@ -294,33 +295,42 @@ describe("UpgradeDependencyVersion", () => {
294295
}, {unsafeCleanup: true});
295296
});
296297

297-
test("throws when install fails for non-existent version", async () => {
298+
test("adds warning marker when install fails for non-existent version", async () => {
298299
const spec = new RecipeSpec();
299300
spec.recipe = new UpgradeDependencyVersion({
300301
packageName: "uuid",
301302
newVersion: "^999.0.0" // Non-existent version
302303
});
303304

304305
await withDir(async (repo) => {
305-
await expect(spec.rewriteRun(
306+
await spec.rewriteRun(
306307
npm(
307308
repo.path,
308309
typescript(`const x = 1;`),
309-
packageJson(`
310-
{
311-
"name": "test-project",
312-
"version": "1.0.0",
313-
"dependencies": {
314-
"uuid": "^9.0.0"
310+
{
311+
...packageJson(`
312+
{
313+
"name": "test-project",
314+
"version": "1.0.0",
315+
"dependencies": {
316+
"uuid": "^9.0.0"
317+
}
315318
}
319+
`, (actual: string) => {
320+
expect(actual).toContain('/*~~(Failed to upgrade uuid to ^999.0.0');
321+
return actual;
322+
}), afterRecipe: async (doc: Json.Document) => {
323+
const warnMarker = findMarker(doc, MarkersKind.MarkupWarn);
324+
expect(warnMarker).toBeDefined();
325+
expect((warnMarker as any).message).toContain("Failed to upgrade uuid to ^999.0.0");
316326
}
317-
`)
327+
}
318328
)
319-
)).rejects.toThrow("Failed to upgrade uuid to ^999.0.0");
329+
);
320330
}, {unsafeCleanup: true});
321331
});
322332

323-
test("throws when install fails due to engine version mismatch", async () => {
333+
test("adds warning marker when install fails due to engine version mismatch", async () => {
324334
const spec = new RecipeSpec();
325335
spec.recipe = new UpgradeDependencyVersion({
326336
packageName: "uuid",
@@ -332,24 +342,33 @@ describe("UpgradeDependencyVersion", () => {
332342
fs.writeFileSync(path.join(repo.path, '.npmrc'), 'engine-strict=true');
333343

334344
// when / then
335-
await expect(spec.rewriteRun(
345+
await spec.rewriteRun(
336346
npm(
337347
repo.path,
338348
typescript(`const x = 1;`),
339-
packageJson(`
340-
{
341-
"name": "test-project",
342-
"version": "1.0.0",
343-
"engines": {
344-
"node": "504.436"
345-
},
346-
"dependencies": {
347-
"uuid": "^9.0.0"
349+
{
350+
...packageJson(`
351+
{
352+
"name": "test-project",
353+
"version": "1.0.0",
354+
"engines": {
355+
"node": "504.436"
356+
},
357+
"dependencies": {
358+
"uuid": "^9.0.0"
359+
}
348360
}
361+
`, (actual: string) => {
362+
expect(actual).toContain('/*~~(Failed to upgrade uuid to ^10.0.0');
363+
return actual;
364+
}), afterRecipe: async (doc: Json.Document) => {
365+
const warnMarker = findMarker(doc, MarkersKind.MarkupWarn);
366+
expect(warnMarker).toBeDefined();
367+
expect((warnMarker as any).message).toContain("Failed to upgrade uuid to ^10.0.0");
349368
}
350-
`)
369+
}
351370
)
352-
)).rejects.toThrow(/^Error: Failed to upgrade uuid to \^10\.0\.0:/);
371+
);
353372
}, {unsafeCleanup: true});
354373
});
355374

0 commit comments

Comments
 (0)