Skip to content

Commit 3cec019

Browse files
committed
refactor: Use symlinks to "rename" package-lock.json files
1 parent f287031 commit 3cec019

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

internal/shrinkwrap-extractor/test/lib/convertToShrinkwrap.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "node:path";
2-
import {readFile, mkdir, writeFile, unlink} from "node:fs/promises";
2+
import {readFile, mkdir, writeFile, unlink, symlink} from "node:fs/promises";
33
import convertPackageLockToShrinkwrap from "../../lib/convertPackageLockToShrinkwrap.js";
44
import {test} from "node:test";
55
import assert from "node:assert";
@@ -18,10 +18,27 @@ function setupPacoteMock() {
1818
});
1919
}
2020

21+
/**
22+
* Create a temporary symlink from package-lock.fixture.json to package-lock.json
23+
* This is needed because @npmcli/arborist.loadVirtual() expects package-lock.json
24+
*
25+
* @param {string} fixtureDir - Directory containing the fixture file
26+
* @returns {Promise<string>} Path to the created symlink
27+
*/
28+
async function setupFixtureSymlink(fixtureDir) {
29+
const symlinkPath = path.join(fixtureDir, "package-lock.json");
30+
const targetPath = "package-lock.fixture.json";
31+
await symlink(targetPath, symlinkPath);
32+
return symlinkPath;
33+
}
34+
2135
test("Convert package-lock.json to shrinkwrap", async (t) => {
2236
const __dirname = import.meta.dirname;
2337

2438
const cwd = path.join(__dirname, "..", "fixture", "project.a");
39+
const symlinkPath = await setupFixtureSymlink(cwd);
40+
t.after(async () => await unlink(symlinkPath).catch(() => {}));
41+
2542
const targetPackageName = "@ui5/cli";
2643
const shrinkwrapJson = await convertPackageLockToShrinkwrap(cwd, targetPackageName);
2744

@@ -80,6 +97,9 @@ test("Compare generated shrinkwrap with expected result", async (t) => {
8097

8198
// Generate shrinkwrap from fixture
8299
const cwd = path.join(__dirname, "..", "fixture", "project.a");
100+
const symlinkPath = await setupFixtureSymlink(cwd);
101+
t.after(async () => await unlink(symlinkPath).catch(() => {}));
102+
83103
const targetPackageName = "@ui5/cli";
84104
const generatedShrinkwrap = await convertPackageLockToShrinkwrap(cwd, targetPackageName);
85105

@@ -131,6 +151,9 @@ test("Compare generated shrinkwrap with expected result", async (t) => {
131151

132152
// Generate shrinkwrap from fixture
133153
const cwd = path.join(__dirname, "..", "fixture", "project.b");
154+
const symlinkPath = await setupFixtureSymlink(cwd);
155+
t.after(async () => await unlink(symlinkPath).catch(() => {}));
156+
134157
const targetPackageName = "@ui5/cli";
135158

136159
const generatedShrinkwrap = await convertPackageLockToShrinkwrap(cwd, targetPackageName);
@@ -150,6 +173,8 @@ test("Compare generated shrinkwrap with expected result", async (t) => {
150173
test("Error handling - invalid target package name", async (t) => {
151174
const __dirname = import.meta.dirname;
152175
const validCwd = path.join(__dirname, "..", "fixture", "project.a");
176+
const symlinkPath = await setupFixtureSymlink(validCwd);
177+
t.after(async () => await unlink(symlinkPath).catch(() => {}));
153178

154179
await assert.rejects(
155180
convertPackageLockToShrinkwrap(validCwd, null),
@@ -170,6 +195,8 @@ test("Error handling - invalid target package name", async (t) => {
170195
test("Error handling - target package not found", async (t) => {
171196
const __dirname = import.meta.dirname;
172197
const validCwd = path.join(__dirname, "..", "fixture", "project.a");
198+
const symlinkPath = await setupFixtureSymlink(validCwd);
199+
t.after(async () => await unlink(symlinkPath).catch(() => {}));
173200

174201
await assert.rejects(
175202
convertPackageLockToShrinkwrap(validCwd, "non-existent-package"),
@@ -188,27 +215,42 @@ test("Error handling - invalid package-lock.json files", async (t) => {
188215
const __dirname = import.meta.dirname;
189216

190217
// Test malformed JSON
218+
const malformedDir = path.join(__dirname, "..", "fixture", "invalid", "malformed");
219+
const malformedSymlink = await setupFixtureSymlink(malformedDir);
220+
t.after(async () => await unlink(malformedSymlink).catch(() => {}));
221+
191222
await assert.rejects(
192-
convertPackageLockToShrinkwrap(path.join(__dirname, "..", "fixture", "invalid", "malformed"), "@ui5/cli"),
223+
convertPackageLockToShrinkwrap(malformedDir, "@ui5/cli"),
193224
/Unexpected token/
194225
);
195226

196227
// Test missing packages field
228+
const noPackagesDir = path.join(__dirname, "..", "fixture", "invalid", "no-packages");
229+
const noPackagesSymlink = await setupFixtureSymlink(noPackagesDir);
230+
t.after(async () => await unlink(noPackagesSymlink).catch(() => {}));
231+
197232
await assert.rejects(
198-
convertPackageLockToShrinkwrap(path.join(__dirname, "..", "fixture", "invalid", "no-packages"), "@ui5/cli"),
233+
convertPackageLockToShrinkwrap(noPackagesDir, "@ui5/cli"),
199234
/Invalid package-lock\.json: missing packages field/
200235
);
201236

202237
// Test invalid packages field
238+
const invalidPackagesDir = path.join(__dirname, "..", "fixture", "invalid", "invalid-packages");
239+
const invalidPackagesSymlink = await setupFixtureSymlink(invalidPackagesDir);
240+
t.after(async () => await unlink(invalidPackagesSymlink).catch(() => {}));
241+
203242
await assert.rejects(
204-
convertPackageLockToShrinkwrap(
205-
path.join(__dirname, "..", "fixture", "invalid", "invalid-packages"), "@ui5/cli"),
243+
convertPackageLockToShrinkwrap(invalidPackagesDir, "@ui5/cli"),
206244
/Invalid package-lock\.json: packages field must be an object/
207245
);
208246

209247
// Test unsupported lockfile version
248+
const v2Dir = path.join(__dirname, "..", "fixture", "invalid", "v2");
249+
const v2Symlink = await setupFixtureSymlink(v2Dir);
250+
t.after(async () => await unlink(v2Symlink).catch(() => {}));
251+
210252
await assert.rejects(
211-
convertPackageLockToShrinkwrap(path.join(__dirname, "..", "fixture", "invalid", "v2"), "@ui5/cli"),
253+
convertPackageLockToShrinkwrap(v2Dir, "@ui5/cli"),
212254
/Unsupported lockfile version: 2\. Only lockfile version 3 is supported/
213255
);
214256
});

0 commit comments

Comments
 (0)