-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Refactored lib/star.js - Fixes `npm unstar` by adding a lib/unstar.js alias/cmd - Add tests for lib/star.js and lib/unstar.js Fixes: npm/statusboard#174 PR-URL: #2204 Credit: @ruyadorno Close: #2204 Reviewed-by: @nlf
- Loading branch information
Showing
6 changed files
with
244 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const { usage, completion } = require('./star.js') | ||
const npm = require('./npm.js') | ||
|
||
const unstar = (args, cb) => { | ||
npm.config.set('star.unstar', true) | ||
return npm.commands.star(args, cb) | ||
} | ||
|
||
module.exports = Object.assign(unstar, { usage, completion }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
const requireInject = require('require-inject') | ||
const t = require('tap') | ||
|
||
let result = '' | ||
|
||
const noop = () => null | ||
const npm = { config: { get () {} }, flatOptions: { unicode: false } } | ||
const npmFetch = { json: noop } | ||
const npmlog = { error: noop, info: noop, verbose: noop } | ||
const mocks = { | ||
npmlog, | ||
'npm-registry-fetch': npmFetch, | ||
'../../lib/npm.js': npm, | ||
'../../lib/utils/output.js': (...msg) => { | ||
result += msg.join('\n') | ||
}, | ||
'../../lib/utils/get-identity.js': async () => 'foo', | ||
'../../lib/utils/usage.js': () => 'usage instructions', | ||
} | ||
|
||
const star = requireInject('../../lib/star.js', mocks) | ||
|
||
t.afterEach(cb => { | ||
npm.config = { get () {} } | ||
npm.flatOptions.unicode = false | ||
npmlog.info = noop | ||
result = '' | ||
cb() | ||
}) | ||
|
||
t.test('no args', t => { | ||
star([], err => { | ||
t.match( | ||
err, | ||
/usage instructions/, | ||
'should throw usage instructions' | ||
) | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('star a package', t => { | ||
t.plan(4) | ||
const pkgName = '@npmcli/arborist' | ||
npmFetch.json = async (uri, opts) => ({ | ||
_id: pkgName, | ||
_rev: 'hash', | ||
users: ( | ||
opts.method === 'PUT' | ||
? { foo: true } | ||
: {} | ||
), | ||
}) | ||
npmlog.info = (title, msg, id) => { | ||
t.equal(title, 'star', 'should use expected title') | ||
t.equal(msg, 'starring', 'should use expected msg') | ||
t.equal(id, pkgName, 'should use expected id') | ||
} | ||
star([pkgName], err => { | ||
if (err) | ||
throw err | ||
t.equal( | ||
result, | ||
'(*) @npmcli/arborist', | ||
'should output starred package msg' | ||
) | ||
}) | ||
}) | ||
|
||
t.test('unstar a package', t => { | ||
t.plan(4) | ||
const pkgName = '@npmcli/arborist' | ||
npm.config.get = key => key === 'star.unstar' | ||
npmFetch.json = async (uri, opts) => ({ | ||
_id: pkgName, | ||
_rev: 'hash', | ||
...(opts.method === 'PUT' | ||
? {} | ||
: { foo: true } | ||
), | ||
}) | ||
npmlog.info = (title, msg, id) => { | ||
t.equal(title, 'unstar', 'should use expected title') | ||
t.equal(msg, 'unstarring', 'should use expected msg') | ||
t.equal(id, pkgName, 'should use expected id') | ||
} | ||
star([pkgName], err => { | ||
if (err) | ||
throw err | ||
t.equal( | ||
result, | ||
'( ) @npmcli/arborist', | ||
'should output unstarred package msg' | ||
) | ||
}) | ||
}) | ||
|
||
t.test('unicode', async t => { | ||
t.test('star a package', t => { | ||
npm.flatOptions.unicode = true | ||
npmFetch.json = async (uri, opts) => ({}) | ||
star(['pkg'], err => { | ||
if (err) | ||
throw err | ||
t.equal( | ||
result, | ||
'\u2605 pkg', | ||
'should output unicode starred package msg' | ||
) | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('unstar a package', t => { | ||
npm.flatOptions.unicode = true | ||
npm.config.get = key => key === 'star.unstar' | ||
npmFetch.json = async (uri, opts) => ({}) | ||
star(['pkg'], err => { | ||
if (err) | ||
throw err | ||
t.equal( | ||
result, | ||
'\u2606 pkg', | ||
'should output unstarred package msg' | ||
) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
t.test('logged out user', t => { | ||
const star = requireInject('../../lib/star.js', { | ||
...mocks, | ||
'../../lib/utils/get-identity.js': async () => undefined, | ||
}) | ||
star(['@npmcli/arborist'], err => { | ||
t.match( | ||
err, | ||
/You need to be logged in/, | ||
'should throw login required error' | ||
) | ||
t.end() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const requireInject = require('require-inject') | ||
const t = require('tap') | ||
|
||
t.test('unstar', t => { | ||
t.plan(3) | ||
|
||
const unstar = requireInject('../../lib/unstar.js', { | ||
'../../lib/npm.js': { | ||
config: { | ||
set: (key, value) => { | ||
t.equal(key, 'star.unstar', 'should set unstar config value') | ||
t.equal(value, true, 'should set a truthy value') | ||
}, | ||
}, | ||
commands: { | ||
star: (args, cb) => { | ||
t.deepEqual(args, ['pkg'], 'should forward packages') | ||
cb() | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
unstar(['pkg'], err => { | ||
if (err) | ||
throw err | ||
}) | ||
}) |