Skip to content

Commit e7677c9

Browse files
Maxim-Mazurokraineorshine
authored andcommitted
support doctorTest command that includes quotes
1 parent 7455026 commit e7677c9

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/lib/doctor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ const doctor = async (run: Run, options: Options): Promise<void> => {
114114
}
115115

116116
if (options.doctorTest) {
117-
const [testCommand, ...testArgs] = options.doctorTest.split(' ')
117+
const regexp = /"(.+?)"|'(.+?)'|[^ ]+/g
118+
const matches = options.doctorTest.matchAll(regexp)
119+
let groups: string[] = []
120+
// eslint-disable-next-line fp/no-loops
121+
for (const match of matches) {
122+
groups = [...groups, match[2] || match[1] || match[0]]
123+
}
124+
const [testCommand, ...testArgs] = groups
118125
await spawn(testCommand, testArgs, spawnOptions)
119126
} else {
120127
await npm(

test/doctor.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,54 @@ describe('doctor', function () {
304304
pkgUpgraded.should.containIgnoreCase('"ncu-test-v2": "~2.0.0"')
305305
})
306306

307+
it('custom test script with --doctorTest command that includes spaced words wrapped in quotes', async function () {
308+
// use dynamic import for ESM module
309+
const { default: stripAnsi } = await import('strip-ansi')
310+
const cwd = path.join(doctorTests, 'customtest2')
311+
const pkgPath = path.join(cwd, 'package.json')
312+
const lockfilePath = path.join(cwd, 'package-lock.json')
313+
const nodeModulesPath = path.join(cwd, 'node_modules')
314+
const echoPath = path.join(cwd, 'echo.js')
315+
const pkgOriginal = await fs.readFile(path.join(cwd, 'package.json'), 'utf-8')
316+
let stdout = ''
317+
let stderr = ''
318+
319+
try {
320+
await ncu(['--doctor', '-u', '--doctorTest', `node ${echoPath} '123 456'`], {
321+
cwd,
322+
stdout: function (data: string) {
323+
stdout += data
324+
},
325+
stderr: function (data: string) {
326+
stderr += data
327+
},
328+
})
329+
} catch (e) {}
330+
331+
const pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
332+
333+
// cleanup before assertions in case they fail
334+
await fs.writeFile(pkgPath, pkgOriginal)
335+
rimraf.sync(lockfilePath)
336+
rimraf.sync(nodeModulesPath)
337+
338+
// stderr should be empty
339+
stderr.should.equal('')
340+
341+
// stdout should include expected output
342+
stripAnsi(stdout).should.contain("'123 456'")
343+
344+
// package file should include upgrades
345+
pkgUpgraded.should.containIgnoreCase('"ncu-test-v2": "~2.0.0"')
346+
})
347+
307348
it('handle failed prepare script', async () => {
308349
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
309350
const pkgPath = path.join(tempDir, 'package.json')
310351
await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
311352

312353
/*
313-
- packagu.json
354+
- package.json
314355
- tsconfig.json
315356
- src/
316357
- index.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(process.argv)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"license": "MIT",
3+
"dependencies": {
4+
"ncu-test-v2": "~1.0.0"
5+
},
6+
"scripts": {
7+
"mytest": "echo Success"
8+
}
9+
}

0 commit comments

Comments
 (0)