Skip to content

Commit f16ca36

Browse files
committed
Revise tests
1 parent 9e9d7b9 commit f16ca36

15 files changed

Lines changed: 142 additions & 266 deletions

src/commands/fix/cmd-fix.test.mts

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@ describe('socket fix', async () => {
3535
--markdown Output result as markdown
3636
--range-style Define how dependency version ranges are updated in package.json (default 'preserve').
3737
Available styles:
38-
* caret - Use ^ range for compatible updates (e.g. ^1.2.3)
39-
* gt - Use > to allow any newer version (e.g. >1.2.3)
40-
* gte - Use >= to allow any newer version (e.g. >=1.2.3)
41-
* lt - Use < to allow only lower versions (e.g. <1.2.3)
42-
* lte - Use <= to allow only lower versions (e.g. <=1.2.3)
4338
* pin - Use the exact version (e.g. 1.2.3)
4439
* preserve - Retain the existing version range style as-is
45-
* tilde - Use ~ range for patch/minor updates (e.g. ~1.2.3)
4640
4741
Examples
4842
$ socket fix
@@ -198,71 +192,6 @@ describe('socket fix', async () => {
198192
},
199193
)
200194

201-
cmdit(
202-
[
203-
'fix',
204-
'--dry-run',
205-
'--purl',
206-
'pkg:npm/lodash@3.9.2',
207-
'--config',
208-
'{"apiToken":"fakeToken"}',
209-
],
210-
'should accept valid PURL with version',
211-
async cmd => {
212-
const { code, stdout } = await invokeNpm(binCliPath, cmd)
213-
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Not saving"`)
214-
expect(code, 'should exit with code 0').toBe(0)
215-
},
216-
)
217-
218-
cmdit(
219-
['fix', '--purl', 'pkg:npm/lodash', '--config', '{"apiToken":"fakeToken"}'],
220-
'should fail with PURL without version',
221-
async cmd => {
222-
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
223-
const output = stdout + stderr
224-
expect(output).toContain('is missing a version and will be ignored')
225-
expect(output).toContain('No valid --purl values provided')
226-
expect(code, 'should exit with non-zero code').not.toBe(0)
227-
},
228-
)
229-
230-
cmdit(
231-
[
232-
'fix',
233-
'--dry-run',
234-
'--purl',
235-
'pkg:npm/lodash@3.9.2,pkg:npm/axios@1.8.0',
236-
'--config',
237-
'{"apiToken":"fakeToken"}',
238-
],
239-
'should accept multiple PURLs as comma-separated values',
240-
async cmd => {
241-
const { code, stdout } = await invokeNpm(binCliPath, cmd)
242-
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Not saving"`)
243-
expect(code, 'should exit with code 0').toBe(0)
244-
},
245-
)
246-
247-
cmdit(
248-
[
249-
'fix',
250-
'--dry-run',
251-
'--purl',
252-
'pkg:npm/lodash@3.9.2',
253-
'--purl',
254-
'pkg:npm/axios@1.8.0',
255-
'--config',
256-
'{"apiToken":"fakeToken"}',
257-
],
258-
'should accept multiple --purl flags',
259-
async cmd => {
260-
const { code, stdout } = await invokeNpm(binCliPath, cmd)
261-
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Not saving"`)
262-
expect(code, 'should exit with code 0').toBe(0)
263-
},
264-
)
265-
266195
cmdit(
267196
[
268197
'fix',
@@ -371,25 +300,6 @@ describe('socket fix', async () => {
371300
},
372301
)
373302

374-
cmdit(
375-
[
376-
'fix',
377-
'--purl',
378-
'pkg:npm/lodash@4.17.20',
379-
'--config',
380-
'{"apiToken":"fake-token"}',
381-
],
382-
'should handle specific PURL for lodash version',
383-
async cmd => {
384-
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
385-
const output = stdout + stderr
386-
expect(output).toContain(
387-
'Unable to resolve a Socket account organization',
388-
)
389-
expect(code, 'should exit with non-zero code').not.toBe(0)
390-
},
391-
)
392-
393303
cmdit(
394304
['fix', '--id', 'CVE-2021-23337', '--config', '{"apiToken":"fake-token"}'],
395305
'should handle CVE ID conversion for lodash vulnerability',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import nock from 'nock'
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
33

4-
import { convertIdsToGhsas } from '../src/commands/fix/handle-fix.mts'
4+
import { convertIdsToGhsas } from './handle-fix.mts'
55

66
describe('Socket fix --id functionality', () => {
77
beforeEach(() => {

src/commands/json/cmd-json.test.mts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,16 @@ describe('socket json', async () => {
108108
)
109109

110110
cmdit(
111-
['json', './sjtest', '--dry-run', '--config', '{"apiToken":"fakeToken"}'],
111+
[
112+
'json',
113+
'./fixtures/commands/json',
114+
'--dry-run',
115+
'--config',
116+
'{"apiToken":"fakeToken"}',
117+
],
112118
'should print a socket.json when found',
113119
async cmd => {
114120
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
115-
// (Remove carriage returns on Windows)
116121
expect(stdout.replace(/(?:\\r|\\x0d)/g, '')).toMatchInlineSnapshot(`
117122
"{
118123
" _____ _ _ ": "Local config file for Socket CLI tool ( https://npmjs.org/socket ), to work with https://socket.dev",

test/socket-cdxgen.test.mts renamed to src/commands/manifest/cdxgen-integration.test.mts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,25 @@ import { LOG_SYMBOLS } from '@socketsecurity/registry/lib/logger'
77
import { spawn } from '@socketsecurity/registry/lib/spawn'
88
import { stripAnsi } from '@socketsecurity/registry/lib/strings'
99

10-
import { npmFixturesPath } from './utils.mts'
11-
import constants from '../src/constants.mts'
12-
13-
const __filename = fileURLToPath(import.meta.url)
14-
const __dirname = path.dirname(__filename)
15-
16-
const testPath = __dirname
10+
import { testPath } from '../../../test/utils.mts'
11+
import constants from '../../constants.mts'
1712

1813
type PromiseSpawnOptions = Exclude<Parameters<typeof spawn>[2], undefined> & {
1914
encoding?: BufferEncoding | undefined
2015
}
2116

22-
const spawnOpts: PromiseSpawnOptions = {
23-
cwd: npmFixturesPath,
24-
env: {
25-
...process.env,
26-
...constants.processEnv,
27-
SOCKET_CLI_DEBUG: '1',
28-
},
29-
}
30-
3117
describe('Socket manifest cdxgen command', async () => {
3218
const { binCliPath } = constants
3319

20+
const spawnOpts: PromiseSpawnOptions = {
21+
cwd: path.join(testPath, 'fixtures/commands/cdxgen'),
22+
env: {
23+
...process.env,
24+
...constants.processEnv,
25+
SOCKET_CLI_DEBUG: '1',
26+
},
27+
}
28+
3429
it(
3530
'should forwards known commands to cdxgen',
3631
{

src/commands/manifest/cmd-manifest-conda.test.mts

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect } from 'vitest'
22

33
import constants from '../../../src/constants.mts'
4-
import { cmdit, invokeNpm } from '../../../test/utils.mts'
4+
import { cmdit, invokeNpm, testPath } from '../../../test/utils.mts'
55

66
describe('socket manifest conda', async () => {
77
const { binCliPath } = constants
@@ -10,7 +10,12 @@ describe('socket manifest conda', async () => {
1010
['manifest', 'conda', '--help', '--config', '{}'],
1111
'should support --help',
1212
async cmd => {
13-
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
13+
const { code, stderr, stdout } = await invokeNpm(
14+
binCliPath,
15+
cmd,
16+
{},
17+
testPath,
18+
)
1419
expect(stdout).toMatchInlineSnapshot(
1520
`
1621
"[beta] Convert a Conda environment.yml file to a python requirements.txt
@@ -61,7 +66,12 @@ describe('socket manifest conda', async () => {
6166
['manifest', 'conda', '--dry-run', '--config', '{}'],
6267
'should require args with just dry-run',
6368
async cmd => {
64-
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
69+
const { code, stderr, stdout } = await invokeNpm(
70+
binCliPath,
71+
cmd,
72+
{},
73+
testPath,
74+
)
6575
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
6676
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
6777
"
@@ -82,25 +92,33 @@ describe('socket manifest conda', async () => {
8292
[
8393
'manifest',
8494
'conda',
85-
'two',
86-
'three', // this triggers the error
95+
'fixtures/commands/manifest/conda',
96+
'--stdout',
8797
'--config',
8898
'{}',
8999
],
90100
'should print raw text without flags',
91101
async cmd => {
92-
const { stderr, stdout } = await invokeNpm(binCliPath, cmd)
93-
expect(stdout).toMatchInlineSnapshot(`""`)
102+
const { code, stderr, stdout } = await invokeNpm(
103+
binCliPath,
104+
cmd,
105+
{},
106+
testPath,
107+
)
108+
expect(stdout).toMatchInlineSnapshot(`
109+
"qgrid==1.3.0
110+
mplstereonet
111+
pyqt5
112+
gempy==2.1.0"
113+
`)
94114
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
95115
"
96116
_____ _ _ /---------------
97117
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
98118
|__ | * | _| '_| -_| _| | Node: <redacted>, API token: <redacted>, org: <redacted>
99119
|_____|___|___|_,_|___|_|.dev | Command: \`socket manifest conda\`, cwd: <redacted>
100120
101-
\\xd7 Input error: Please review the input requirements and try again
102-
103-
\\xd7 Can only accept one DIR (make sure to escape spaces!) (received 2)"
121+
\\u203c Warning: This will approximate your Conda dependencies using PyPI. We do not yet officially support Conda. Use at your own risk."
104122
`)
105123
},
106124
)
@@ -109,25 +127,32 @@ describe('socket manifest conda', async () => {
109127
[
110128
'manifest',
111129
'conda',
112-
'two',
113-
'three', // this triggers the error
130+
'fixtures/commands/manifest/conda',
114131
'--json',
132+
'--stdout',
115133
'--config',
116134
'{}',
117135
],
118136
'should print a json blurb with --json flag',
119137
async cmd => {
120-
const { stderr, stdout } = await invokeNpm(binCliPath, cmd)
138+
const { stderr, stdout } = await invokeNpm(
139+
binCliPath,
140+
cmd,
141+
{},
142+
testPath,
143+
)
121144
expect(stdout).toMatchInlineSnapshot(`
122145
"{
123-
"ok": false,
124-
"message": "Input error",
125-
"data": "Please review the input requirements and try again\\n\\n \\xd7 Can only accept one DIR (make sure to escape spaces!) (received 2)"
146+
"ok": true,
147+
"data": {
148+
"content": "name: my_stuff\\n\\nchannels:\\n - conda-thing\\n - defaults\\ndependencies:\\n - python=3.8\\n - pandas=1.3.4\\n - numpy=1.19.0\\n - scipy\\n - mkl-service\\n - libpython\\n - m2w64-toolchain\\n - pytest\\n - requests\\n - pip\\n - pip:\\n - qgrid==1.3.0\\n - mplstereonet\\n - pyqt5\\n - gempy==2.1.0\\n",
149+
"pip": "qgrid==1.3.0\\nmplstereonet\\npyqt5\\ngempy==2.1.0"
150+
}
126151
}"
127152
`)
128153
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
129154
"
130-
"
155+
\\u203c Warning: This will approximate your Conda dependencies using PyPI. We do not yet officially support Conda. Use at your own risk."
131156
`)
132157
},
133158
)
@@ -136,21 +161,35 @@ describe('socket manifest conda', async () => {
136161
[
137162
'manifest',
138163
'conda',
139-
'two',
140-
'three', // this triggers the error
164+
'fixtures/commands/manifest/conda',
141165
'--markdown',
166+
'--stdout',
142167
'--config',
143168
'{}',
144169
],
145170
'should print a markdown blurb with --markdown flag',
146171
async cmd => {
147-
const { stderr, stdout } = await invokeNpm(binCliPath, cmd)
148-
expect(stdout).toMatchInlineSnapshot(`""`)
172+
const { code, stderr, stdout } = await invokeNpm(
173+
binCliPath,
174+
cmd,
175+
{},
176+
testPath,
177+
)
178+
expect(stdout).toMatchInlineSnapshot(`
179+
"# Converted Conda file
180+
181+
This is the Conda \`environment.yml\` file converted to python \`requirements.txt\`:
182+
183+
\`\`\`file=requirements.txt
184+
qgrid==1.3.0
185+
mplstereonet
186+
pyqt5
187+
gempy==2.1.0
188+
\`\`\`"
189+
`)
149190
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
150191
"
151-
\\xd7 Input error: Please review the input requirements and try again
152-
153-
\\xd7 Can only accept one DIR (make sure to escape spaces!) (received 2)"
192+
\\u203c Warning: This will approximate your Conda dependencies using PyPI. We do not yet officially support Conda. Use at your own risk."
154193
`)
155194
},
156195
)

test/socket-npm.test.mts renamed to src/commands/npm/socket-npm-integration.test.mts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import { describe, expect, it } from 'vitest'
66
import { isDebug } from '@socketsecurity/registry/lib/debug'
77
import { spawn, spawnSync } from '@socketsecurity/registry/lib/spawn'
88

9-
import { npmFixturesPath } from './utils.mts'
10-
import constants from '../src/constants.mts'
9+
import { testPath } from '../../../test/utils.mts'
10+
import constants from '../../constants.mts'
1111

12-
const __filename = fileURLToPath(import.meta.url)
13-
const __dirname = path.dirname(__filename)
14-
15-
const testPath = __dirname
12+
const npmFixturesPath = path.join(testPath, 'fixtures/commands/npm')
1613

1714
// These aliases are defined in package.json.
1815
for (const npmDir of ['npm9', 'npm10', 'npm11']) {

0 commit comments

Comments
 (0)