Skip to content

Commit efb8fe3

Browse files
committed
Copy package files with absolute paths
1 parent 409616d commit efb8fe3

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

node-src/lib/turbosnap/findChangedDependencies.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import snykGraph from '@snyk/dep-graph';
2-
import { mkdtempSync as unMockedMkdtempSync, statSync as unMockedStatSync } from 'fs';
2+
import {
3+
copyFileSync as unMockedCopyFileSync,
4+
mkdtempSync as unMockedMkdtempSync,
5+
statSync as unMockedStatSync,
6+
} from 'fs';
37
import { buildDepTreeFromFiles } from 'snyk-nodejs-lockfile-parser';
48
import snyk from 'snyk-nodejs-plugin';
59
import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
@@ -21,6 +25,9 @@ const tmpdir = '/tmpdir';
2125
const statSync = unMockedStatSync as Mock;
2226
statSync.mockReturnValue({ size: 1 });
2327

28+
const copyFileSync = unMockedCopyFileSync as Mock;
29+
copyFileSync.mockReturnValue(undefined);
30+
2431
const mkdtempSync = unMockedMkdtempSync as Mock;
2532
mkdtempSync.mockReturnValue(tmpdir);
2633

@@ -348,4 +355,46 @@ describe('findChangedDependencies', () => {
348355
}
349356
);
350357
});
358+
359+
it('handles relative paths correctly when copying files to temp directory', async () => {
360+
// Mock the repository root to be different from current working directory
361+
getRepositoryRoot.mockResolvedValue('/root/subdir');
362+
363+
// Mock findFilesFromRepositoryRoot to return relative paths
364+
findFilesFromRepositoryRoot.mockImplementation((_, ...patterns) => {
365+
const results: string[] = [];
366+
for (const pattern of patterns) {
367+
if (pattern === 'package.json') {
368+
results.push('package.json');
369+
} else if (
370+
pattern === 'yarn.lock' ||
371+
pattern === 'pnpm-lock.yaml' ||
372+
pattern === 'package-lock.json'
373+
) {
374+
results.push('package-lock.json');
375+
}
376+
}
377+
return Promise.resolve(results);
378+
});
379+
380+
mockInspect(/* HEAD */ ['[email protected]'], /* Baseline A */ ['[email protected]']);
381+
mockChangedPackagesGraph(['[email protected]']);
382+
383+
const context = getContext({
384+
git: {
385+
packageMetadataChanges: [{ changedFiles: ['package.json'], commit: 'A' }],
386+
},
387+
});
388+
389+
await expect(findChangedDependencies(context)).resolves.toEqual(['react']);
390+
391+
expect(copyFileSync).toHaveBeenCalledWith(
392+
'/root/subdir/package.json',
393+
`${tmpdir}/package.json`
394+
);
395+
expect(copyFileSync).toHaveBeenCalledWith(
396+
'/root/subdir/package-lock.json',
397+
`${tmpdir}/package-lock.json`
398+
);
399+
});
351400
});

node-src/lib/turbosnap/findChangedDependencies.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ export const findChangedDependencies = async (ctx: Context) => {
112112
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'chromatic'));
113113
tmpdirsCreated.add(tmpdir);
114114

115+
const absoluteManifestPath = path.join(rootPath, manifestPath);
116+
const absoluteLockfilePath = path.join(rootPath, lockfilePath);
115117
const temporaryManifestPath = path.join(tmpdir, path.basename(manifestPath));
116118
const temporaryLockfilePath = path.join(tmpdir, path.basename(lockfilePath));
117119

118-
fs.copyFileSync(manifestPath, temporaryManifestPath);
119-
fs.copyFileSync(lockfilePath, temporaryLockfilePath);
120+
fs.copyFileSync(absoluteManifestPath, temporaryManifestPath);
121+
fs.copyFileSync(absoluteLockfilePath, temporaryLockfilePath);
120122

121123
const headDependencies = await getDependencies(ctx, {
122124
rootPath: tmpdir,

0 commit comments

Comments
 (0)