|
| 1 | +import path from 'node:path' |
| 2 | + |
| 3 | +import { describe, expect } from 'vitest' |
| 4 | + |
| 5 | +import constants from '../../../dist/constants.js' |
| 6 | +import { cmdit, invokeNpm } from '../../../test/utils' |
| 7 | + |
| 8 | +const { CLI } = constants |
| 9 | + |
| 10 | +describe('socket package score', async () => { |
| 11 | + // Lazily access constants.rootBinPath. |
| 12 | + const entryPath = path.join(constants.rootBinPath, `${CLI}.js`) |
| 13 | + |
| 14 | + cmdit(['package', 'score', '--help'], 'should support --help', async cmd => { |
| 15 | + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) |
| 16 | + expect(stdout).toMatchInlineSnapshot( |
| 17 | + ` |
| 18 | + "Look up info regarding a package |
| 19 | +
|
| 20 | + Usage |
| 21 | + $ socket package score <<ecosystem> <name> [<name> ...] | <purl> [<purl> ...]> |
| 22 | +
|
| 23 | + Options |
| 24 | + --dryRun Do input validation for a command and exit 0 when input is ok |
| 25 | + --help Print this help. |
| 26 | + --json Output result as json |
| 27 | + --markdown Output result as markdown |
| 28 | +
|
| 29 | + Show scoring details for one or more packages. |
| 30 | + Only a few ecosystems are supported like npm, golang, and maven. |
| 31 | +
|
| 32 | + If the first arg is an ecosystem, remaining args that are not a "purl" are |
| 33 | + assumed to be scoped in that ecosystem. If the first arg is in "purl" form |
| 34 | + then all args must be in purl form ("package url": \`pkg:eco/name@version\`). |
| 35 | +
|
| 36 | + This command takes 100 quota units. |
| 37 | + This command requires \`packages:list\` scope access on your API token. |
| 38 | +
|
| 39 | + Examples |
| 40 | + $ socket package score npm webtorrent |
| 41 | + $ socket package score npm webtorrent@1.9.1 |
| 42 | + $ socket package score npm/webtorrent@1.9.1 |
| 43 | + $ socket package score maven webtorrent babel |
| 44 | + $ socket package score npm/webtorrent golang/babel |
| 45 | + $ socket package score npm npm/webtorrent@1.0.1 babel" |
| 46 | + ` |
| 47 | + ) |
| 48 | + expect(`\n ${stderr}`).toMatchInlineSnapshot(` |
| 49 | + " |
| 50 | + _____ _ _ /--------------- |
| 51 | + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted> |
| 52 | + |__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted> |
| 53 | + |_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: <redacted>" |
| 54 | + `) |
| 55 | + |
| 56 | + expect(code, 'help should exit with code 2').toBe(2) |
| 57 | + expect(stderr, 'header should include command (without params)').toContain( |
| 58 | + '`socket package score`' |
| 59 | + ) |
| 60 | + }) |
| 61 | + |
| 62 | + cmdit( |
| 63 | + ['package', 'score', '--dry-run'], |
| 64 | + 'should require args with just dry-run', |
| 65 | + async cmd => { |
| 66 | + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) |
| 67 | + expect(stdout).toMatchInlineSnapshot(`""`) |
| 68 | + expect(`\n ${stderr}`).toMatchInlineSnapshot(` |
| 69 | + " |
| 70 | + _____ _ _ /--------------- |
| 71 | + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted> |
| 72 | + |__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted> |
| 73 | + |_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: <redacted> |
| 74 | +
|
| 75 | + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[37mInput error\\x1b[39m\\x1b[49m: Please provide the required fields: |
| 76 | +
|
| 77 | + - Expecting an ecosystem \\x1b[31m(missing!)\\x1b[39m |
| 78 | +
|
| 79 | + - Expecting at least one package \\x1b[31m(missing!)\\x1b[39m" |
| 80 | + `) |
| 81 | + |
| 82 | + expect(code, 'dry-run should exit with code 2 if missing input').toBe(2) |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + cmdit( |
| 87 | + ['package', 'score', 'npm', 'babel', '--dry-run'], |
| 88 | + 'should require args with just dry-run', |
| 89 | + async cmd => { |
| 90 | + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) |
| 91 | + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) |
| 92 | + expect(`\n ${stderr}`).toMatchInlineSnapshot(` |
| 93 | + " |
| 94 | + _____ _ _ /--------------- |
| 95 | + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted> |
| 96 | + |__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted> |
| 97 | + |_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: <redacted>" |
| 98 | + `) |
| 99 | + |
| 100 | + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) |
| 101 | + } |
| 102 | + ) |
| 103 | +}) |
0 commit comments