From c76f04ac28ddf2ae4df4b3ce0aec684a118de1b5 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Tue, 23 Mar 2021 11:59:38 -0700 Subject: fix(set-script): add completion PR-URL: https://github.com/npm/cli/pull/2925 Credit: @Yash-Singh1 Close: #2925 Reviewed-by: @ruyadorno --- test/lib/set-script.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'test') diff --git a/test/lib/set-script.js b/test/lib/set-script.js index b6b6e2519..dc00fd374 100644 --- a/test/lib/set-script.js +++ b/test/lib/set-script.js @@ -2,6 +2,43 @@ const test = require('tap') const requireInject = require('require-inject') const parseJSON = require('json-parse-even-better-errors') +test.test('completion', t => { + const SetScript = requireInject('../../lib/set-script.js') + const emptyDir = t.testdir() + t.test('already have a script name', async t => { + const setScript = new SetScript({localPrefix: emptyDir}) + const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run', 'x']}}}) + t.equal(res, undefined) + t.end() + }) + t.test('no package.json', async t => { + const setScript = new SetScript({localPrefix: emptyDir}) + const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}}) + t.strictSame(res, []) + t.end() + }) + t.test('has package.json, no scripts', async t => { + const localPrefix = t.testdir({ + 'package.json': JSON.stringify({}), + }) + const setScript = new SetScript({localPrefix}) + const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}}) + t.strictSame(res, []) + t.end() + }) + t.test('has package.json, with scripts', async t => { + const localPrefix = t.testdir({ + 'package.json': JSON.stringify({ + scripts: { hello: 'echo hello', world: 'echo world' }, + }), + }) + const setScript = new SetScript({localPrefix}) + const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}}) + t.strictSame(res, ['hello', 'world']) + t.end() + }) + t.end() +}) test.test('fails on invalid arguments', (t) => { const SetScript = requireInject('../../lib/set-script.js', { npmlog: {}, -- cgit v1.2.3