Skip to content

Commit 6dee1dc

Browse files
committed
Update Node test workflow
1 parent d1e22d3 commit 6dee1dc

File tree

5 files changed

+123
-112
lines changed

5 files changed

+123
-112
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@ jobs:
2424
with:
2525
no-lockfile: true
2626
npm-test-script: 'test-ci'
27-
node-versions: '20'
28-
# We currently have some issues on Windows that will have to wait to be fixed
29-
# os: 'ubuntu-latest,windows-latest'
30-
os: 'ubuntu-latest'
27+
node-versions: '20,22'
28+
os: 'ubuntu-latest,windows-latest'

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
"lint:fix": "npm run lint -- --fix && npm run lint:fix:fast",
5252
"lint:fix:fast": "prettier --cache --log-level warn --write .",
5353
"prepare": "husky && custompatch",
54-
"test": "run-s check build:* test:*",
55-
"test:c8": "c8 --reporter=none node --test 'test/socket-npm.test.cjs'",
54+
"test": "run-s check build:* test:* test:coverage:*",
5655
"test-ci": "run-s build:* test:*",
5756
"test:unit": "tap-run",
58-
"test:coverage": "cp -r .tap/coverage/*.json coverage/tmp && c8 --reporter=lcov --reporter=text --include 'dist/{module-sync,require}/*.js' --exclude 'dist/require/vendor.js' report"
57+
"test:coverage:c8": "c8 --reporter=none node --test 'test/socket-npm.test.cjs'",
58+
"test:coverage:merge": "cp -r .tap/coverage/*.json coverage/tmp && c8 --reporter=lcov --reporter=text --include 'dist/{module-sync,require}/*.js' --exclude 'dist/require/vendor.js' report"
5959
},
6060
"dependencies": {
6161
"@apideck/better-ajv-errors": "^0.3.6",

test/path-resolve.test.ts

Lines changed: 96 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { afterEach, beforeEach, describe, it } from 'node:test'
55
import mockFs from 'mock-fs'
66
import nock from 'nock'
77

8+
import { normalizePath } from '@socketsecurity/registry/lib/path'
9+
810
import { getPackageFiles } from './dist/path-resolve'
911

1012
const testPath = __dirname
11-
const mockPath = path.join(testPath, 'mock')
13+
const mockPath = normalizePath(path.join(testPath, 'mock'))
1214

1315
const globPatterns = {
1416
general: {
@@ -88,10 +90,13 @@ describe('Path Resolve', () => {
8890
[`${mockPath}/package.json`]: '{}'
8991
})
9092

91-
assert.deepEqual(
92-
await sortedGetPackageFiles(mockPath, ['.'], undefined, globPatterns),
93-
[`${mockPath}/package.json`]
93+
const actual = await sortedGetPackageFiles(
94+
mockPath,
95+
['.'],
96+
undefined,
97+
globPatterns
9498
)
99+
assert.deepEqual(actual.map(normalizePath), [`${mockPath}/package.json`])
95100
})
96101

97102
it('should respect ignores from socket config', async () => {
@@ -102,24 +107,22 @@ describe('Path Resolve', () => {
102107
[`${mockPath}/foo/package.json`]: '{}'
103108
})
104109

105-
assert.deepEqual(
106-
await sortedGetPackageFiles(
107-
mockPath,
108-
['**/*'],
109-
{
110-
version: 2,
111-
projectIgnorePaths: ['bar/*', '!bar/package.json'],
112-
issueRules: {},
113-
githubApp: {}
114-
},
115-
globPatterns
116-
),
117-
[
118-
`${mockPath}/bar/package.json`,
119-
`${mockPath}/foo/package-lock.json`,
120-
`${mockPath}/foo/package.json`
121-
]
110+
const actual = await sortedGetPackageFiles(
111+
mockPath,
112+
['**/*'],
113+
{
114+
version: 2,
115+
projectIgnorePaths: ['bar/*', '!bar/package.json'],
116+
issueRules: {},
117+
githubApp: {}
118+
},
119+
globPatterns
122120
)
121+
assert.deepEqual(actual.map(normalizePath), [
122+
`${mockPath}/bar/package.json`,
123+
`${mockPath}/foo/package-lock.json`,
124+
`${mockPath}/foo/package.json`
125+
])
123126
})
124127

125128
it('should respect .gitignore', async () => {
@@ -131,19 +134,17 @@ describe('Path Resolve', () => {
131134
[`${mockPath}/foo/package.json`]: '{}'
132135
})
133136

134-
assert.deepEqual(
135-
await sortedGetPackageFiles(
136-
mockPath,
137-
['**/*'],
138-
undefined,
139-
globPatterns
140-
),
141-
[
142-
`${mockPath}/bar/package.json`,
143-
`${mockPath}/foo/package-lock.json`,
144-
`${mockPath}/foo/package.json`
145-
]
137+
const actual = await sortedGetPackageFiles(
138+
mockPath,
139+
['**/*'],
140+
undefined,
141+
globPatterns
146142
)
143+
assert.deepEqual(actual.map(normalizePath), [
144+
`${mockPath}/bar/package.json`,
145+
`${mockPath}/foo/package-lock.json`,
146+
`${mockPath}/foo/package.json`
147+
])
147148
})
148149

149150
it('should always ignore some paths', async () => {
@@ -162,15 +163,16 @@ describe('Path Resolve', () => {
162163
[`${mockPath}/foo/package.json`]: '{}'
163164
})
164165

165-
assert.deepEqual(
166-
await sortedGetPackageFiles(
167-
mockPath,
168-
['**/*'],
169-
undefined,
170-
globPatterns
171-
),
172-
[`${mockPath}/foo/package-lock.json`, `${mockPath}/foo/package.json`]
166+
const actual = await sortedGetPackageFiles(
167+
mockPath,
168+
['**/*'],
169+
undefined,
170+
globPatterns
173171
)
172+
assert.deepEqual(actual.map(normalizePath), [
173+
`${mockPath}/foo/package-lock.json`,
174+
`${mockPath}/foo/package.json`
175+
])
174176
})
175177

176178
it('should ignore irrelevant matches', async () => {
@@ -181,15 +183,16 @@ describe('Path Resolve', () => {
181183
[`${mockPath}/foo/random.json`]: '{}'
182184
})
183185

184-
assert.deepEqual(
185-
await sortedGetPackageFiles(
186-
mockPath,
187-
['**/*'],
188-
undefined,
189-
globPatterns
190-
),
191-
[`${mockPath}/foo/package-lock.json`, `${mockPath}/foo/package.json`]
186+
const actual = await sortedGetPackageFiles(
187+
mockPath,
188+
['**/*'],
189+
undefined,
190+
globPatterns
192191
)
192+
assert.deepEqual(actual.map(normalizePath), [
193+
`${mockPath}/foo/package-lock.json`,
194+
`${mockPath}/foo/package.json`
195+
])
193196
})
194197

195198
it('should be lenient on oddities', async () => {
@@ -199,15 +202,13 @@ describe('Path Resolve', () => {
199202
}
200203
})
201204

202-
assert.deepEqual(
203-
await sortedGetPackageFiles(
204-
mockPath,
205-
['**/*'],
206-
undefined,
207-
globPatterns
208-
),
209-
[]
205+
const actual = await sortedGetPackageFiles(
206+
mockPath,
207+
['**/*'],
208+
undefined,
209+
globPatterns
210210
)
211+
assert.deepEqual(actual.map(normalizePath), [])
211212
})
212213

213214
it('should resolve package and lock file', async () => {
@@ -216,31 +217,30 @@ describe('Path Resolve', () => {
216217
[`${mockPath}/package.json`]: '{}'
217218
})
218219

219-
assert.deepEqual(
220-
await sortedGetPackageFiles(
221-
mockPath,
222-
['**/*'],
223-
undefined,
224-
globPatterns
225-
),
226-
[`${mockPath}/package-lock.json`, `${mockPath}/package.json`]
220+
const actual = await sortedGetPackageFiles(
221+
mockPath,
222+
['**/*'],
223+
undefined,
224+
globPatterns
227225
)
226+
assert.deepEqual(actual.map(normalizePath), [
227+
`${mockPath}/package-lock.json`,
228+
`${mockPath}/package.json`
229+
])
228230
})
229231

230232
it('should resolve package without lock file', async () => {
231233
mockFs({
232234
[`${mockPath}/package.json`]: '{}'
233235
})
234236

235-
assert.deepEqual(
236-
await sortedGetPackageFiles(
237-
mockPath,
238-
['**/*'],
239-
undefined,
240-
globPatterns
241-
),
242-
[`${mockPath}/package.json`]
237+
const actual = await sortedGetPackageFiles(
238+
mockPath,
239+
['**/*'],
240+
undefined,
241+
globPatterns
243242
)
243+
assert.deepEqual(actual.map(normalizePath), [`${mockPath}/package.json`])
244244
})
245245

246246
it('should support alternative lock files', async () => {
@@ -249,15 +249,16 @@ describe('Path Resolve', () => {
249249
[`${mockPath}/package.json`]: '{}'
250250
})
251251

252-
assert.deepEqual(
253-
await sortedGetPackageFiles(
254-
mockPath,
255-
['**/*'],
256-
undefined,
257-
globPatterns
258-
),
259-
[`${mockPath}/package.json`, `${mockPath}/yarn.lock`]
252+
const actual = await sortedGetPackageFiles(
253+
mockPath,
254+
['**/*'],
255+
undefined,
256+
globPatterns
260257
)
258+
assert.deepEqual(actual.map(normalizePath), [
259+
`${mockPath}/package.json`,
260+
`${mockPath}/yarn.lock`
261+
])
261262
})
262263

263264
it('should handle all variations', async () => {
@@ -271,23 +272,21 @@ describe('Path Resolve', () => {
271272
[`${mockPath}/abc/package.json`]: '{}'
272273
})
273274

274-
assert.deepEqual(
275-
await sortedGetPackageFiles(
276-
mockPath,
277-
['**/*'],
278-
undefined,
279-
globPatterns
280-
),
281-
[
282-
`${mockPath}/abc/package.json`,
283-
`${mockPath}/bar/package.json`,
284-
`${mockPath}/bar/yarn.lock`,
285-
`${mockPath}/foo/package-lock.json`,
286-
`${mockPath}/foo/package.json`,
287-
`${mockPath}/package-lock.json`,
288-
`${mockPath}/package.json`
289-
]
275+
const actual = await sortedGetPackageFiles(
276+
mockPath,
277+
['**/*'],
278+
undefined,
279+
globPatterns
290280
)
281+
assert.deepEqual(actual.map(normalizePath), [
282+
`${mockPath}/abc/package.json`,
283+
`${mockPath}/bar/package.json`,
284+
`${mockPath}/bar/yarn.lock`,
285+
`${mockPath}/foo/package-lock.json`,
286+
`${mockPath}/foo/package.json`,
287+
`${mockPath}/package-lock.json`,
288+
`${mockPath}/package.json`
289+
])
291290
})
292291
})
293292
})

test/socket-cdxgen.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from 'node:assert/strict'
2+
import path from 'node:path'
23
import { describe, it } from 'node:test'
34

45
import spawn from '@npmcli/promise-spawn'
@@ -9,32 +10,45 @@ type PromiseSpawnOptions = Exclude<Parameters<typeof spawn>[2], undefined> & {
910
encoding?: BufferEncoding | undefined
1011
}
1112

12-
const { distPath } = constants
13+
const { distPath, execPath } = constants
14+
15+
const entryPath = path.join(distPath, 'cli.js')
16+
const testPath = __dirname
17+
const npmFixturesPath = path.join(testPath, 'socket-npm-fixtures')
1318

1419
const spawnOpts: PromiseSpawnOptions = {
15-
cwd: distPath,
20+
cwd: npmFixturesPath,
1621
encoding: 'utf8'
1722
}
1823

1924
describe('Socket cdxgen command', async () => {
2025
it('should forwards known commands to cdxgen', async () => {
2126
for (const command of ['-h', '--help']) {
2227
// eslint-disable-next-line no-await-in-loop
23-
const ret = await spawn('./cli.js', ['cdxgen', command], spawnOpts)
28+
const ret = await spawn(
29+
execPath,
30+
[entryPath, 'cdxgen', command],
31+
spawnOpts
32+
)
2433
assert.ok(ret.stdout.startsWith('cdxgen'), 'forwards commands to cdxgen')
2534
}
2635
})
2736
it('should not forward unknown commands to cdxgen', async () => {
2837
for (const command of ['-u', '--unknown']) {
2938
// eslint-disable-next-line no-await-in-loop
3039
await assert.rejects(
31-
() => spawn('./cli.js', ['cdxgen', command], spawnOpts),
40+
() => spawn(execPath, [entryPath, 'cdxgen', command], spawnOpts),
3241
e => e?.['stderr']?.startsWith(`Unknown argument: ${command}`),
3342
'singular'
3443
)
3544
}
3645
await assert.rejects(
37-
() => spawn('./cli.js', ['cdxgen', '-u', '-h', '--unknown'], spawnOpts),
46+
() =>
47+
spawn(
48+
execPath,
49+
[entryPath, 'cdxgen', '-u', '-h', '--unknown'],
50+
spawnOpts
51+
),
3852
e => e?.['stderr']?.startsWith('Unknown arguments: -u, --unknown'),
3953
'plural'
4054
)

test/socket-npm.test.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const { describe, it } = require('node:test')
88
const spawn = require('@npmcli/promise-spawn')
99

1010
const constants = require('../scripts/constants')
11-
const { distPath } = constants
11+
const { distPath, execPath } = constants
1212

13-
const testPath = __dirname
1413
const entryPath = path.join(distPath, 'cli.js')
14+
const testPath = __dirname
1515
const npmFixturesPath = path.join(testPath, 'socket-npm-fixtures')
1616

1717
// These aliases are defined in package.json.
@@ -28,7 +28,7 @@ for (const npm of ['npm8', 'npm10']) {
2828
it('should bail on new typosquat', async () => {
2929
await assert.rejects(
3030
() =>
31-
spawn(process.execPath, [entryPath, 'npm', 'install', 'bowserify'], {
31+
spawn(execPath, [entryPath, 'npm', 'install', 'bowserify'], {
3232
cwd: path.join(npmFixturesPath, 'lacking-typosquat'),
3333
encoding: 'utf8',
3434
env: {

0 commit comments

Comments
 (0)