Skip to content

Commit

Permalink
Update Node test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Dec 14, 2024
1 parent d1e22d3 commit 6dee1dc
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 112 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@ jobs:
with:
no-lockfile: true
npm-test-script: 'test-ci'
node-versions: '20'
# We currently have some issues on Windows that will have to wait to be fixed
# os: 'ubuntu-latest,windows-latest'
os: 'ubuntu-latest'
node-versions: '20,22'
os: 'ubuntu-latest,windows-latest'
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
"lint:fix": "npm run lint -- --fix && npm run lint:fix:fast",
"lint:fix:fast": "prettier --cache --log-level warn --write .",
"prepare": "husky && custompatch",
"test": "run-s check build:* test:*",
"test:c8": "c8 --reporter=none node --test 'test/socket-npm.test.cjs'",
"test": "run-s check build:* test:* test:coverage:*",
"test-ci": "run-s build:* test:*",
"test:unit": "tap-run",
"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"
"test:coverage:c8": "c8 --reporter=none node --test 'test/socket-npm.test.cjs'",
"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"
},
"dependencies": {
"@apideck/better-ajv-errors": "^0.3.6",
Expand Down
193 changes: 96 additions & 97 deletions test/path-resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { afterEach, beforeEach, describe, it } from 'node:test'
import mockFs from 'mock-fs'
import nock from 'nock'

import { normalizePath } from '@socketsecurity/registry/lib/path'

import { getPackageFiles } from './dist/path-resolve'

const testPath = __dirname
const mockPath = path.join(testPath, 'mock')
const mockPath = normalizePath(path.join(testPath, 'mock'))

const globPatterns = {
general: {
Expand Down Expand Up @@ -88,10 +90,13 @@ describe('Path Resolve', () => {
[`${mockPath}/package.json`]: '{}'
})

assert.deepEqual(
await sortedGetPackageFiles(mockPath, ['.'], undefined, globPatterns),
[`${mockPath}/package.json`]
const actual = await sortedGetPackageFiles(
mockPath,
['.'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [`${mockPath}/package.json`])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
{
version: 2,
projectIgnorePaths: ['bar/*', '!bar/package.json'],
issueRules: {},
githubApp: {}
},
globPatterns
),
[
`${mockPath}/bar/package.json`,
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`
]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
{
version: 2,
projectIgnorePaths: ['bar/*', '!bar/package.json'],
issueRules: {},
githubApp: {}
},
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [
`${mockPath}/bar/package.json`,
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`
])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[
`${mockPath}/bar/package.json`,
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`
]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [
`${mockPath}/bar/package.json`,
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`
])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[`${mockPath}/foo/package-lock.json`, `${mockPath}/foo/package.json`]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`
])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[`${mockPath}/foo/package-lock.json`, `${mockPath}/foo/package.json`]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`
])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[`${mockPath}/package-lock.json`, `${mockPath}/package.json`]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [
`${mockPath}/package-lock.json`,
`${mockPath}/package.json`
])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[`${mockPath}/package.json`]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [`${mockPath}/package.json`])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[`${mockPath}/package.json`, `${mockPath}/yarn.lock`]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [
`${mockPath}/package.json`,
`${mockPath}/yarn.lock`
])
})

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

assert.deepEqual(
await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
),
[
`${mockPath}/abc/package.json`,
`${mockPath}/bar/package.json`,
`${mockPath}/bar/yarn.lock`,
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`,
`${mockPath}/package-lock.json`,
`${mockPath}/package.json`
]
const actual = await sortedGetPackageFiles(
mockPath,
['**/*'],
undefined,
globPatterns
)
assert.deepEqual(actual.map(normalizePath), [
`${mockPath}/abc/package.json`,
`${mockPath}/bar/package.json`,
`${mockPath}/bar/yarn.lock`,
`${mockPath}/foo/package-lock.json`,
`${mockPath}/foo/package.json`,
`${mockPath}/package-lock.json`,
`${mockPath}/package.json`
])
})
})
})
24 changes: 19 additions & 5 deletions test/socket-cdxgen.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict'
import path from 'node:path'
import { describe, it } from 'node:test'

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

const { distPath } = constants
const { distPath, execPath } = constants

const entryPath = path.join(distPath, 'cli.js')
const testPath = __dirname
const npmFixturesPath = path.join(testPath, 'socket-npm-fixtures')

const spawnOpts: PromiseSpawnOptions = {
cwd: distPath,
cwd: npmFixturesPath,
encoding: 'utf8'
}

describe('Socket cdxgen command', async () => {
it('should forwards known commands to cdxgen', async () => {
for (const command of ['-h', '--help']) {
// eslint-disable-next-line no-await-in-loop
const ret = await spawn('./cli.js', ['cdxgen', command], spawnOpts)
const ret = await spawn(
execPath,
[entryPath, 'cdxgen', command],
spawnOpts
)
assert.ok(ret.stdout.startsWith('cdxgen'), 'forwards commands to cdxgen')
}
})
it('should not forward unknown commands to cdxgen', async () => {
for (const command of ['-u', '--unknown']) {
// eslint-disable-next-line no-await-in-loop
await assert.rejects(
() => spawn('./cli.js', ['cdxgen', command], spawnOpts),
() => spawn(execPath, [entryPath, 'cdxgen', command], spawnOpts),
e => e?.['stderr']?.startsWith(`Unknown argument: ${command}`),
'singular'
)
}
await assert.rejects(
() => spawn('./cli.js', ['cdxgen', '-u', '-h', '--unknown'], spawnOpts),
() =>
spawn(
execPath,
[entryPath, 'cdxgen', '-u', '-h', '--unknown'],
spawnOpts
),
e => e?.['stderr']?.startsWith('Unknown arguments: -u, --unknown'),
'plural'
)
Expand Down
6 changes: 3 additions & 3 deletions test/socket-npm.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const { describe, it } = require('node:test')
const spawn = require('@npmcli/promise-spawn')

const constants = require('../scripts/constants')
const { distPath } = constants
const { distPath, execPath } = constants

const testPath = __dirname
const entryPath = path.join(distPath, 'cli.js')
const testPath = __dirname
const npmFixturesPath = path.join(testPath, 'socket-npm-fixtures')

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

0 comments on commit 6dee1dc

Please sign in to comment.