Skip to content

Commit

Permalink
chore: collect demo data with npm-ls args (#1230)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck authored Oct 30, 2024
1 parent 8ed0ae7 commit a0882cc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 37 deletions.
37 changes: 27 additions & 10 deletions .github/workflows/npm-ls_demo-results.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ env:

jobs:
demo-results:
name: '${{ matrix.subject }} npm${{ matrix.npm-version }} node${{ matrix.node-version }} ${{ matrix.os }}'
name: '${{ matrix.subject }} ${{matrix.additional_npm-ls_args}} | npm${{ matrix.npm-version }} node${{ matrix.node-version }} ${{ matrix.os }}'
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false # gather all the results !
Expand All @@ -24,16 +24,17 @@ jobs:
# - bundled-dependencies
# - dev-dependencies
# - juice-shop
- local-dependencies
# - local-dependencies
- local-workspaces
# - package-integrity
# - package-with-build-id
additional_npm-ls_args: [ '' ]
npm-version:
## see https://www.npmjs.com/package/npm?activeTab=versions
## see also: https://github.com/npm/cli/releases
- '10' # https://github.com/npm/cli/releases/tag/v10.0.0-pre.0
- '9' # Current
- '8' # Latest
- '10' # Current
- '9' # Legacy
- '8' # Legacy
- '7' # Legacy
- '6' # Legacy
node-version:
Expand All @@ -48,6 +49,22 @@ jobs:
- ubuntu-latest
- windows-latest
- macos-latest
include:
- subject: local-workspaces
additional_npm-ls_args: '-w my-local-e'
npm-version: '10' # Current
node-version: '22' # Current
os: ubuntu-latest
- subject: local-workspaces
additional_npm-ls_args: '-w my-local -w my-local-e'
npm-version: '10' # Current
node-version: '22' # Current
os: ubuntu-latest
- subject: local-workspaces
additional_npm-ls_args: '--workspaces'
npm-version: '10' # Current
node-version: '22' # Current
os: ubuntu-latest
exclude:
- # macos-latest no longer supports node14
os: macos-latest
Expand Down Expand Up @@ -81,7 +98,7 @@ jobs:
- name: prepare output
run: |
mkdir -p '${{ env.RESULTS_DIR }}'
echo 'OUT_FILE_PREFIX=${{ env.RESULTS_DIR }}/npm-ls_npm${{ matrix.npm-version }}_node${{ matrix.node-version }}_${{ matrix.os }}' >> $GITHUB_ENV
echo 'OUT_FILE_PREFIX=${{ env.RESULTS_DIR }}/npm-ls${{ matrix.additional_npm-ls_args }}_npm${{ matrix.npm-version }}_node${{ matrix.node-version }}_${{ matrix.os }}' >> $GITHUB_ENV
working-directory: '${{ env.DEMO_ROOT_DIR}}/${{ matrix.subject }}'
shell: bash # don't want to write tht code twice, windows and unix-like
- name: Setup Node.js ${{ matrix.node-version }}
Expand All @@ -103,15 +120,15 @@ jobs:
# important: the command in use is the same used by the internal builders of this project
run: >
npm --prefix project
ls --json --all --long
2> ${{ env.OUT_FILE_PREFIX }}.err.log
1> ${{ env.OUT_FILE_PREFIX }}.json
ls --json --all --long ${{ matrix.additional_npm-ls_args }}
2> '${{ env.OUT_FILE_PREFIX }}.err.log'
1> '${{ env.OUT_FILE_PREFIX }}.json'
working-directory: '${{ env.DEMO_ROOT_DIR}}/${{ matrix.subject }}'
- name: Artifact RESULTS
# see https://github.com/actions/upload-artifact
uses: actions/upload-artifact@v4
with:
name: '${{ env.RESULTS_ARTIFACT }}_${{ matrix.subject }}_npm${{ matrix.npm-version }}_node${{ matrix.node-version }}_${{ matrix.os }}'
name: '${{ env.RESULTS_ARTIFACT }}_${{ matrix.subject }}${{ matrix.additional_npm-ls_args }}_npm${{ matrix.npm-version }}_node${{ matrix.node-version }}_${{ matrix.os }}'
path: '${{ env.DEMO_ROOT_DIR}}/*/${{ env.RESULTS_DIR }}'
if-no-files-found: error
retention-days: 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const dirDemoRes = dirname(__dirname)

const fnamePattern = /^npm-ls_npm(?<npm>\d+)_node(?<node>\d+)_(?<os>.+)\.json$/
const fnamePattern = /^npm-ls(?<args>.*?)_npm(?<npm>\d+)_node(?<node>\d+)_(?<os>.+)\.json$/

/** @type {Object.<string, Object.<string, string[]>>} */
const files = {}
Expand All @@ -41,33 +41,40 @@ for (const dirDemoResE of readdirSync(dirDemoRes)) {
if (!fnameMatch) {
continue
}
if (!Object.hasOwn(files, fnameMatch.groups.npm)) {
files[fnameMatch.groups.npm] = {}
let _t = files
if (!Object.hasOwn(_t, fnameMatch.groups.args)) {
_t[fnameMatch.groups.args] = {}
}
if (!Object.hasOwn(files[fnameMatch.groups.npm], fnameMatch.groups.os)) {
files[fnameMatch.groups.npm][fnameMatch.groups.os] = []
_t = _t[fnameMatch.groups.args]
if (!Object.hasOwn(_t, fnameMatch.groups.npm)) {
_t[fnameMatch.groups.npm] = {}
}
files[fnameMatch.groups.npm][fnameMatch.groups.os].push(
join(dirResults, dirResultsE)
)
_t = _t[fnameMatch.groups.npm]
if (!Object.hasOwn(_t, fnameMatch.groups.os)) {
_t[fnameMatch.groups.os] = []
}
_t = _t[fnameMatch.groups.os]
_t.push(join(dirResults, dirResultsE))
}
} catch (e) {
continue
}
}

for (const filesByOs of Object.values(files)) {
for (const filePaths of Object.values(filesByOs)) {
const fileHashes = new Set()
for (const filePath of filePaths) {
const fileHash = await hashFile(filePath)
if (fileHashes.has(fileHash)) {
console.info('DELETE:', fileHash, filePath)
unlinkSync(filePath)
continue
for (const filesByAs of Object.values(files)) {
for (const filesByOs of Object.values(filesByAs)) {
for (const filePaths of Object.values(filesByOs)) {
const fileHashes = new Set()
for (const filePath of filePaths) {
const fileHash = await hashFile(filePath)
if (fileHashes.has(fileHash)) {
console.info('DELETE:', fileHash, filePath)
unlinkSync(filePath)
continue
}
fileHashes.add(fileHash)
console.info('KEEP:', fileHash, filePath)
}
fileHashes.add(fileHash)
console.info('KEEP:', fileHash, filePath)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/_data/npm-ls_demo-results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
const { sync: glob } = require('fast-glob')

const fileGlob = '*/CI_results/*.json'
const filePattern = /\/(?<subject>[^/]+?)\/CI_results\/npm-ls_npm(?<npm>.+?)_node(?<node>.+?)_(?<os>.+?).json$/i
const filePattern = /\/(?<subject>[^/]+?)\/CI_results\/npm-ls(?<args>.*?)_npm(?<npm>.+?)_node(?<node>.+?)_(?<os>.+?).json$/i
/** @type {import('fast-glob').OptionsInternal} */
const globOptions = { absolute: true, caseSensitiveMatch: false, cwd: __dirname, deep: 3, onlyFiles: true, unique: true }

let cached

/**
* @return {{path:string, subject?:string, npm?:string, node?:string, os?:string}[]}
* @return {{path:string, subject?:string, args?:string, npm?:string, node?:string, os?:string}[]}
*/
function index () {
if (cached === undefined) {
Expand Down
11 changes: 8 additions & 3 deletions tests/_data/npm-ls_replacement/demo-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ process.exitCode = Number(process.env.CT_EXIT_CODE || 0)

const index = require('../npm-ls_demo-results').index()

const { CT_SUBJECT: subject, CT_NPM: npm, CT_NODE: node, CT_OS: os } = process.env
const matches = index.filter(i => i.subject === subject && i.npm === npm && i.node === node && i.os === os)
assert.strictEqual(matches.length, 1, 'did not find exactly 1 match')
const { CT_SUBJECT: subject, CT_ARGS: args = '', CT_NPM: npm, CT_NODE: node, CT_OS: os } = process.env
const matches = index.filter(i =>
i.subject === subject &&
i.args === args &&
i.npm === npm &&
i.node === node &&
i.os === os)
assert.strictEqual(matches.length, 1, `did not find exactly 1 match: ${JSON.stringify(matches)}`)

const { path } = matches[0]

Expand Down
9 changes: 6 additions & 3 deletions tests/integration/cli.from-collected.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ describe('integration.cli.from-collected', () => {
describe.each(useCases)('$subject', (ud) => {
mkdirSync(join(tmpRootRun, ud.subject))

test.each(demoCases)('$subject npm$npm node$node $os', async (dd) => {
const expectedOutSnap = resolve(demoResultsRoot, ud.subject, `${dd.subject}_npm${dd.npm}_node${dd.node}_${dd.os}.snap.json`)
const logFileBase = join(tmpRootRun, ud.subject, `${dd.subject}_npm${dd.npm}_node${dd.node}_${dd.os}`)
test.each(demoCases)('$subject $args npm$npm node$node $os', async (dd) => {
const expectedOutSnap = resolve(demoResultsRoot, ud.subject, `${dd.subject}${dd.args}_npm${dd.npm}_node${dd.node}_${dd.os}.snap.json`)
const logFileBase = join(tmpRootRun, ud.subject, `${dd.subject}${dd.args}_npm${dd.npm}_node${dd.node}_${dd.os}`)
const cwd = resolve(projectTestRootPath, '_data', 'dummy_projects')

const { res, outFile, errFile } = runCLI([
Expand All @@ -205,12 +205,15 @@ describe('integration.cli.from-collected', () => {
'--package-lock-only',
// case-specific args
...ud.args,
// explicit args
...(dd.args === '' ? [] : dd.args.split(' ')),
'--',
// just some dummy project
join('with-lockfile', 'package.json')
], logFileBase, cwd, {
CT_VERSION: `${dd.npm}.99.0`,
CT_SUBJECT: dd.subject,
CT_ARGS: dd.args,
CT_NPM: dd.npm,
CT_NODE: dd.node,
CT_OS: dd.os,
Expand Down

0 comments on commit a0882cc

Please sign in to comment.