Skip to content

Commit 0f7a63c

Browse files
authored
Create test file for each command, move dryrun tests there (#363)
1 parent 46480cd commit 0f7a63c

38 files changed

+2490
-836
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"test:unit:coverage": "vitest run --coverage",
6767
"test-ci": "run-s test:*",
6868
"testu": "cross-env SOCKET_CLI_NO_API_TOKEN=1 run-s test:prepare; npm run test:unit:update --",
69+
"testuf": "SOCKET_CLI_NO_API_TOKEN=1 npm run test:unit:update --",
6970
"update": "run-p --aggregate-output update:**",
7071
"update:deps": "npx --yes npm-check-updates"
7172
},
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 analytics', async () => {
11+
// Lazily access constants.rootBinPath.
12+
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)
13+
14+
cmdit(['analytics', '--help'], 'should support --help', async cmd => {
15+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
16+
expect(stdout).toMatchInlineSnapshot(
17+
`
18+
"Look up analytics data
19+
20+
Usage
21+
$ socket analytics --scope=<scope> --time=<time filter>
22+
23+
Default parameters are set to show the organization-level analytics over the
24+
last 7 days.
25+
26+
Options
27+
--dryRun Do input validation for a command and exit 0 when input is ok
28+
--file Path to a local file to save the output. Only valid with --json/--markdown. Defaults to stdout.
29+
--help Print this help.
30+
--json Output result as json
31+
--markdown Output result as markdown
32+
--repo Name of the repository. Only valid when scope=repo
33+
--scope Scope of the analytics data - either 'org' or 'repo', default: org
34+
--time Time filter - either 7, 30 or 90, default: 7
35+
36+
Examples
37+
$ socket analytics --scope=org --time=7
38+
$ socket analytics --scope=org --time=30
39+
$ socket analytics --scope=repo --repo=test-repo --time=30"
40+
`
41+
)
42+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
43+
"
44+
_____ _ _ /---------------
45+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
46+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
47+
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
48+
`)
49+
50+
expect(code, 'help should exit with code 2').toBe(2)
51+
expect(stderr, 'header should include command (without params)').toContain(
52+
'`socket analytics`'
53+
)
54+
})
55+
56+
cmdit(
57+
['analytics', '--dry-run'],
58+
'should require args with just dry-run',
59+
async cmd => {
60+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
61+
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
62+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
63+
"
64+
_____ _ _ /---------------
65+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
66+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
67+
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>"
68+
`)
69+
70+
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
71+
}
72+
)
73+
})
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 audit-log', async () => {
11+
// Lazily access constants.rootBinPath.
12+
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)
13+
14+
cmdit(['audit-log', '--help'], 'should support --help', async cmd => {
15+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
16+
expect(stdout).toMatchInlineSnapshot(
17+
`
18+
"Look up the audit log for an organization
19+
20+
Usage
21+
$ socket audit-log <org slug>
22+
23+
This feature requires an Enterprise Plan. To learn more about getting access
24+
to this feature and many more, please visit https://socket.dev/pricing
25+
26+
Options
27+
--dryRun Do input validation for a command and exit 0 when input is ok
28+
--help Print this help.
29+
--json Output result as json
30+
--markdown Output result as markdown
31+
--page Page number - default is 1
32+
--perPage Results per page - default is 30
33+
--type Type of log event
34+
35+
Examples
36+
$ socket audit-log FakeOrg"
37+
`
38+
)
39+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
40+
"
41+
_____ _ _ /---------------
42+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
43+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
44+
|_____|___|___|_,_|___|_|.dev | Command: \`socket audit-log\`, cwd: <redacted>"
45+
`)
46+
47+
expect(code, 'help should exit with code 2').toBe(2)
48+
expect(stderr, 'header should include command (without params)').toContain(
49+
'`socket audit-log`'
50+
)
51+
})
52+
53+
cmdit(
54+
['audit-log', '--dry-run'],
55+
'should require args with just dry-run',
56+
async cmd => {
57+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
58+
expect(stdout).toMatchInlineSnapshot(`""`)
59+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
60+
"
61+
_____ _ _ /---------------
62+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
63+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
64+
|_____|___|___|_,_|___|_|.dev | Command: \`socket audit-log\`, cwd: <redacted>
65+
66+
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[37mInput error\\x1b[39m\\x1b[49m: Please provide the required fields:
67+
68+
- Org name as the first argument \\x1b[31m(missing!)\\x1b[39m"
69+
`)
70+
71+
expect(code, 'dry-run should exit with code 2 if missing input').toBe(2)
72+
}
73+
)
74+
75+
cmdit(
76+
['audit-log', 'fakeorg', '--dry-run'],
77+
'should require args with just dry-run',
78+
async cmd => {
79+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
80+
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
81+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
82+
"
83+
_____ _ _ /---------------
84+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
85+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
86+
|_____|___|___|_,_|___|_|.dev | Command: \`socket audit-log\`, cwd: <redacted>"
87+
`)
88+
89+
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
90+
}
91+
)
92+
})
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 cdxgen', async () => {
11+
// Lazily access constants.rootBinPath.
12+
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)
13+
14+
cmdit(['cdxgen', '--help'], 'should support --help', async cmd => {
15+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
16+
expect(stdout).toMatchInlineSnapshot(
17+
`
18+
"cdxgen [command]
19+
20+
Commands:
21+
cdxgen completion Generate bash/zsh completion
22+
23+
Options:
24+
-o, --output Output file. Default bom.json [default: "bom.json"]
25+
-t, --type Project type. Please refer to https://cyclonedx.github.io/cdxgen/#/PROJECT_TYPES for supported languages/platforms. [array]
26+
--exclude-type Project types to exclude. Please refer to https://cyclonedx.github.io/cdxgen/#/PROJECT_TYPES for supported languages/platforms.
27+
-r, --recurse Recurse mode suitable for mono-repos. Defaults to true. Pass --no-recurse to disable. [boolean] [default: true]
28+
-p, --print Print the SBOM as a table with tree. [boolean]
29+
-c, --resolve-class Resolve class names for packages. jars only for now. [boolean]
30+
--deep Perform deep searches for components. Useful while scanning C/C++ apps, live OS and oci images. [boolean]
31+
--server-url Dependency track url. Eg: https://deptrack.cyclonedx.io
32+
--skip-dt-tls-check Skip TLS certificate check when calling Dependency-Track. [boolean] [default: false]
33+
--api-key Dependency track api key
34+
--project-group Dependency track project group
35+
--project-name Dependency track project name. Default use the directory name
36+
--project-version Dependency track project version [string] [default: ""]
37+
--project-id Dependency track project id. Either provide the id or the project name and version together [string]
38+
--parent-project-id Dependency track parent project id [string]
39+
--required-only Include only the packages with required scope on the SBOM. Would set compositions.aggregate to incomplete unless --no-auto-compositions is passed. [boolean]
40+
--fail-on-error Fail if any dependency extractor fails. [boolean]
41+
--no-babel Do not use babel to perform usage analysis for JavaScript/TypeScript projects. [boolean]
42+
--generate-key-and-sign Generate an RSA public/private key pair and then sign the generated SBOM using JSON Web Signatures. [boolean]
43+
--server Run cdxgen as a server [boolean]
44+
--server-host Listen address [default: "127.0.0.1"]
45+
--server-port Listen port [default: "9090"]
46+
--install-deps Install dependencies automatically for some projects. Defaults to true but disabled for containers and oci scans. Use --no-install-deps to disable this feature. [boolean] [default: true]
47+
--validate Validate the generated SBOM using json schema. Defaults to true. Pass --no-validate to disable. [boolean] [default: true]
48+
--evidence Generate SBOM with evidence for supported languages. [boolean] [default: false]
49+
--spec-version CycloneDX Specification version to use. Defaults to 1.6 [number] [default: 1.6]
50+
--filter Filter components containing this word in purl or component.properties.value. Multiple values allowed. [array]
51+
--only Include components only containing this word in purl. Useful to generate BOM with first party components alone. Multiple values allowed. [array]
52+
--author The person(s) who created the BOM. Set this value if you're intending the modify the BOM and claim authorship. [array] [default: "OWASP Foundation"]
53+
--profile BOM profile to use for generation. Default generic. [choices: "appsec", "research", "operational", "threat-modeling", "license-compliance", "generic", "machine-learning", "ml", "deep-learning", "ml-deep", "ml-tiny"] [default: "generic"]
54+
--exclude Additional glob pattern(s) to ignore [array]
55+
--include-formulation Generate formulation section with git metadata and build tools. Defaults to false. [boolean] [default: false]
56+
--include-crypto Include crypto libraries as components. [boolean] [default: false]
57+
--standard The list of standards which may consist of regulations, industry or organizational-specific standards, maturity models, best practices, or any other requirements which can be evaluated against or attested to. [array] [choices: "asvs-5.0", "asvs-4.0.3", "bsimm-v13", "masvs-2.0.0", "nist_ssdf-1.1", "pcissc-secure-slc-1.1", "scvs-1.0.0", "ssaf-DRAFT-2023-11"]
58+
--min-confidence Minimum confidence needed for the identity of a component from 0 - 1, where 1 is 100% confidence. [number] [default: 0]
59+
--technique Analysis technique to use [array] [choices: "auto", "source-code-analysis", "binary-analysis", "manifest-analysis", "hash-comparison", "instrumentation", "filename"]
60+
--auto-compositions Automatically set compositions when the BOM was filtered. Defaults to true [boolean] [default: true]
61+
-h, --help Show help [boolean]
62+
-v, --version Show version number [boolean]
63+
64+
Examples:
65+
cdxgen -t java . Generate a Java SBOM for the current directory
66+
cdxgen -t java -t js . Generate a SBOM for Java and JavaScript in the current directory
67+
cdxgen -t java --profile ml . Generate a Java SBOM for machine learning purposes.
68+
cdxgen -t python --profile research . Generate a Python SBOM for appsec research.
69+
cdxgen --server Run cdxgen as a server
70+
71+
for documentation, visit https://cyclonedx.github.io/cdxgen"
72+
`
73+
)
74+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
75+
"
76+
_____ _ _ /---------------
77+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
78+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
79+
|_____|___|___|_,_|___|_|.dev | Command: \`socket cdxgen\`, cwd: <redacted>"
80+
`)
81+
82+
// expect(code, 'help should exit with code 2').toBe(2)
83+
expect(code, 'help should exit with code 2').toBe(0) // cdxgen special case
84+
expect(stderr, 'header should include command (without params)').toContain(
85+
'`socket cdxgen`'
86+
)
87+
})
88+
89+
// cdxgen does not support --dry-run
90+
// cmdit(
91+
// ['cdxgen', '--dry-run'],
92+
// 'should require args with just dry-run',
93+
// async cmd => {
94+
// const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
95+
// expect(stdout).toMatchInlineSnapshot(`""`)
96+
// expect(`\n ${stderr}`).toMatchInlineSnapshot(`
97+
// "
98+
// _____ _ _ /---------------
99+
// | __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
100+
// |__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
101+
// |_____|___|___|_,_|___|_|.dev | Command: \`socket cdxgen\`, cwd: <redacted>
102+
//
103+
// \\x1b[31m\\xd7\\x1b[39m Unknown argument: --dry-run"
104+
// `)
105+
//
106+
// expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
107+
// }
108+
// )
109+
})
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 dependencies', async () => {
11+
// Lazily access constants.rootBinPath.
12+
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)
13+
14+
cmdit(['dependencies', '--help'], 'should support --help', async cmd => {
15+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
16+
expect(stdout).toMatchInlineSnapshot(
17+
`
18+
"Search for any dependency that is being used in your organization
19+
20+
Usage
21+
socket dependencies
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+
--limit Maximum number of dependencies returned
28+
--markdown Output result as markdown
29+
--offset Page number
30+
31+
Examples
32+
socket dependencies --limit 20 --offset 10"
33+
`
34+
)
35+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
36+
"
37+
_____ _ _ /---------------
38+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
39+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
40+
|_____|___|___|_,_|___|_|.dev | Command: \`socket dependencies\`, cwd: <redacted>"
41+
`)
42+
43+
expect(code, 'help should exit with code 2').toBe(2)
44+
expect(stderr, 'header should include command (without params)').toContain(
45+
'`socket dependencies`'
46+
)
47+
})
48+
49+
cmdit(
50+
['dependencies', '--dry-run'],
51+
'should require args with just dry-run',
52+
async cmd => {
53+
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
54+
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
55+
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
56+
"
57+
_____ _ _ /---------------
58+
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
59+
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
60+
|_____|___|___|_,_|___|_|.dev | Command: \`socket dependencies\`, cwd: <redacted>"
61+
`)
62+
63+
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
64+
}
65+
)
66+
})

0 commit comments

Comments
 (0)