Skip to content

Commit 54045ab

Browse files
gowridurgadgowridurgad
andauthored
Scope test lockfiles by package manager and update cache tests (#1495)
* fix security alerts * fix security alerts * address Copilot suggestions --------- Co-authored-by: gowridurgad <gowridurgad@gmail.com>
1 parent c882bff commit 54045ab

File tree

11 files changed

+329
-3350
lines changed

11 files changed

+329
-3350
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,18 @@ typings/
9393

9494
# DynamoDB Local files
9595
.dynamodb/
96+
97+
# Ignore everything inside fixture package-manager folders
98+
__tests__/data/npm/**
99+
__tests__/data/pnpm/**
100+
__tests__/data/yarn/**
101+
102+
# Keep only fixture manifests + lockfiles
103+
!__tests__/data/npm/package.json
104+
!__tests__/data/npm/package-lock.json
105+
106+
!__tests__/data/pnpm/package.json
107+
!__tests__/data/pnpm/pnpm-lock.yaml
108+
109+
!__tests__/data/yarn/package.json
110+
!__tests__/data/yarn/yarn.lock

__tests__/cache-restore.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import * as utils from '../src/cache-utils';
88
import {restoreCache} from '../src/cache-restore';
99

1010
describe('cache-restore', () => {
11-
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
11+
const packageManagers = ['yarn', 'npm', 'pnpm'] as const;
12+
type PackageManager = (typeof packageManagers)[number];
13+
14+
const setWorkspaceFor = (pm: PackageManager) => {
15+
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data', pm);
16+
};
17+
const originalGithubWorkspace = process.env['GITHUB_WORKSPACE'];
1218
if (!process.env.RUNNER_OS) {
1319
process.env.RUNNER_OS = 'Linux';
1420
}
@@ -25,7 +31,7 @@ describe('cache-restore', () => {
2531
'abf7c9b306a3149dcfba4673e2362755503bcceaab46f0e4e6fee0ade493e20c';
2632
const pnpmFileHash =
2733
'26309058093e84713f38869c50cf1cee9b08155ede874ec1b44ce3fca8c68c70';
28-
const cachesObject = {
34+
const cachesObject: Record<string, string> = {
2935
[npmCachePath]: npmFileHash,
3036
[pnpmCachePath]: pnpmFileHash,
3137
[yarn1CachePath]: yarnFileHash,
@@ -128,9 +134,11 @@ describe('cache-restore', () => {
128134
['yarn', '1.2.3', yarnFileHash],
129135
['npm', '', npmFileHash],
130136
['pnpm', '', pnpmFileHash]
131-
])(
137+
] as const)(
132138
'restored dependencies for %s',
133139
async (packageManager, toolVersion, fileHash) => {
140+
// Set workspace to the appropriate fixture folder
141+
setWorkspaceFor(packageManager);
134142
getCommandOutputSpy.mockImplementation((command: string) => {
135143
if (command.includes('version')) {
136144
return toolVersion;
@@ -158,9 +166,11 @@ describe('cache-restore', () => {
158166
['yarn', '1.2.3', yarnFileHash],
159167
['npm', '', npmFileHash],
160168
['pnpm', '', pnpmFileHash]
161-
])(
169+
] as const)(
162170
'dependencies are changed %s',
163171
async (packageManager, toolVersion, fileHash) => {
172+
// Set workspace to the appropriate fixture folder
173+
setWorkspaceFor(packageManager);
164174
getCommandOutputSpy.mockImplementation((command: string) => {
165175
if (command.includes('version')) {
166176
return toolVersion;
@@ -181,6 +191,11 @@ describe('cache-restore', () => {
181191
});
182192

183193
afterEach(() => {
194+
if (originalGithubWorkspace === undefined) {
195+
delete process.env['GITHUB_WORKSPACE'];
196+
} else {
197+
process.env['GITHUB_WORKSPACE'] = originalGithubWorkspace;
198+
}
184199
jest.resetAllMocks();
185200
jest.clearAllMocks();
186201
});

__tests__/data/npm/package-lock.json

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__tests__/data/npm/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "npm-fixture",
3+
"engines": {
4+
"node": "^24.0.0"
5+
},
6+
"dependencies": {
7+
"accepts": "^1.3.8",
8+
"array-flatten": "^3.0.0",
9+
"bytes": "^3.1.2",
10+
"content-disposition": "^0.5.4"
11+
}
12+
}

0 commit comments

Comments
 (0)