|
| 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 score for one package which reflects all of its transitive dependencies as well |
| 19 | +
|
| 20 | + Usage |
| 21 | + $ socket package score <<ecosystem> <name> | <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 | + Requirements |
| 30 | + - quota: 100 |
| 31 | + - scope: \`packages:list\` |
| 32 | +
|
| 33 | + Show deep scoring details for one package. The score will reflect the package |
| 34 | + itself, any of its dependencies, and any of its transitive dependencies. |
| 35 | +
|
| 36 | + When you want to know whether to trust a package, this is the command to run. |
| 37 | +
|
| 38 | + See also the \`socket package shallow\` command, which returns the shallow |
| 39 | + score for any number of packages. That will not reflect the dependency scores. |
| 40 | +
|
| 41 | + Only a few ecosystems are supported like npm, golang, and maven. |
| 42 | +
|
| 43 | + A "purl" is a standard package name formatting: \`pkg:eco/name@version\` |
| 44 | + This command will automatically prepend "pkg:" when not present. |
| 45 | +
|
| 46 | + The version is optional but when given should be a direct match. |
| 47 | +
|
| 48 | + Examples |
| 49 | + $ socket package score npm babel-cli |
| 50 | + $ socket package score npm babel-cli@1.9.1 |
| 51 | + $ socket package score npm/babel-cli@1.9.1 |
| 52 | + $ socket package score pkg:npm/babel-cli@1.9.1" |
| 53 | + ` |
| 54 | + ) |
| 55 | + expect(`\n ${stderr}`).toMatchInlineSnapshot(` |
| 56 | + " |
| 57 | + _____ _ _ /--------------- |
| 58 | + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted> |
| 59 | + |__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted> |
| 60 | + |_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: <redacted>" |
| 61 | + `) |
| 62 | + |
| 63 | + expect(code, 'help should exit with code 2').toBe(2) |
| 64 | + expect(stderr, 'header should include command (without params)').toContain( |
| 65 | + '`socket package score`' |
| 66 | + ) |
| 67 | + }) |
| 68 | + |
| 69 | + cmdit( |
| 70 | + ['package', 'score', '--dry-run'], |
| 71 | + 'should require args with just dry-run', |
| 72 | + async cmd => { |
| 73 | + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) |
| 74 | + expect(stdout).toMatchInlineSnapshot(`""`) |
| 75 | + expect(`\n ${stderr}`).toMatchInlineSnapshot(` |
| 76 | + " |
| 77 | + _____ _ _ /--------------- |
| 78 | + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted> |
| 79 | + |__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted> |
| 80 | + |_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: <redacted> |
| 81 | +
|
| 82 | + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[37mInput error\\x1b[39m\\x1b[49m: Please provide the required fields: |
| 83 | +
|
| 84 | + - First parameter should be an ecosystem or the arg must be a purl \\x1b[31m(bad!)\\x1b[39m |
| 85 | +
|
| 86 | + - Expecting the package to check \\x1b[31m(missing!)\\x1b[39m" |
| 87 | + `) |
| 88 | + |
| 89 | + expect(code, 'dry-run should exit with code 2 if missing input').toBe(2) |
| 90 | + } |
| 91 | + ) |
| 92 | + |
| 93 | + cmdit( |
| 94 | + ['package', 'score', 'npm', 'babel', '--dry-run'], |
| 95 | + 'should require args with just dry-run', |
| 96 | + async cmd => { |
| 97 | + const { code, stderr, stdout } = await invokeNpm(entryPath, cmd) |
| 98 | + expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`) |
| 99 | + expect(`\n ${stderr}`).toMatchInlineSnapshot(` |
| 100 | + " |
| 101 | + _____ _ _ /--------------- |
| 102 | + | __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted> |
| 103 | + |__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted> |
| 104 | + |_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: <redacted>" |
| 105 | + `) |
| 106 | + |
| 107 | + expect(code, 'dry-run should exit with code 0 if input ok').toBe(0) |
| 108 | + } |
| 109 | + ) |
| 110 | +}) |
0 commit comments