diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2021-05-21 20:22:51 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-05-27 17:24:12 +0300 |
commit | 4a4fbe33c51413adcd558b4af6f1e204b1b87e41 (patch) | |
tree | af8624d65414bfcf89c68e5ebfcc2aa5b8170201 /test | |
parent | 64b13dd1082b6ca7eac4e8e329bfdd8cd8daf157 (diff) |
fix(publish): skip private workspaces
Allow users to publish all workspaces with `npm publish --ws` while also
skipping any workspace that might have been intentionally marked as
private, using `"private": true` in its package.json file.
Fixes: https://github.com/npm/cli/issues/3268
PR-URL: https://github.com/npm/cli/pull/3285
Credit: @ruyadorno
Close: #3285
Reviewed-by: @wraithgar
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/publish.js | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/test/lib/publish.js b/test/lib/publish.js index 80f3f7ccc..e34f00b47 100644 --- a/test/lib/publish.js +++ b/test/lib/publish.js @@ -617,3 +617,148 @@ t.test('workspaces', (t) => { }) t.end() }) + +t.test('private workspaces', (t) => { + const testDir = t.testdir({ + 'package.json': JSON.stringify({ + name: 'workspaces-project', + version: '1.0.0', + workspaces: ['packages/*'], + }), + packages: { + a: { + 'package.json': JSON.stringify({ + name: '@npmcli/a', + version: '1.0.0', + private: true, + }), + }, + b: { + 'package.json': JSON.stringify({ + name: '@npmcli/b', + version: '1.0.0', + }), + }, + }, + }) + + const publishes = [] + const outputs = [] + t.beforeEach(() => { + npm.config.set('json', false) + outputs.length = 0 + publishes.length = 0 + }) + const mocks = { + '../../lib/utils/tar.js': { + getContents: (manifest) => ({ + id: manifest._id, + }), + logTar: () => {}, + }, + libnpmpublish: { + publish: (manifest, tarballData, opts) => { + if (manifest.private) { + throw Object.assign( + new Error('private pkg'), + { code: 'EPRIVATE' } + ) + } + publishes.push(manifest) + }, + }, + } + const npm = mockNpm({ + output: (o) => { + outputs.push(o) + }, + }) + npm.localPrefix = testDir + npm.config.getCredentialsByURI = (uri) => { + return { token: 'some.registry.token' } + } + + t.test('with color', t => { + const Publish = t.mock('../../lib/publish.js', { + ...mocks, + npmlog: { + notice () {}, + verbose () {}, + warn (title, msg) { + t.equal(title, 'publish', 'should use publish warn title') + t.match( + msg, + 'Skipping workspace \u001b[32m@npmcli/a\u001b[39m, marked as \u001b[1mprivate\u001b[22m', + 'should display skip private workspace warn msg' + ) + }, + }, + }) + const publish = new Publish(npm) + + npm.color = true + publish.execWorkspaces([], [], (err) => { + t.notOk(err) + t.matchSnapshot(publishes, 'should publish all non-private workspaces') + t.matchSnapshot(outputs, 'should output all publishes') + npm.color = false + t.end() + }) + }) + + t.test('colorless', t => { + const Publish = t.mock('../../lib/publish.js', { + ...mocks, + npmlog: { + notice () {}, + verbose () {}, + warn (title, msg) { + t.equal(title, 'publish', 'should use publish warn title') + t.equal( + msg, + 'Skipping workspace @npmcli/a, marked as private', + 'should display skip private workspace warn msg' + ) + }, + }, + }) + const publish = new Publish(npm) + + publish.execWorkspaces([], [], (err) => { + t.notOk(err) + t.matchSnapshot(publishes, 'should publish all non-private workspaces') + t.matchSnapshot(outputs, 'should output all publishes') + t.end() + }) + }) + + t.test('unexpected error', t => { + const Publish = t.mock('../../lib/publish.js', { + ...mocks, + libnpmpublish: { + publish: (manifest, tarballData, opts) => { + if (manifest.private) + throw new Error('ERR') + + publishes.push(manifest) + }, + }, + npmlog: { + notice () {}, + verbose () {}, + }, + }) + const publish = new Publish(npm) + + publish.execWorkspaces([], [], (err) => { + t.match( + err, + /ERR/, + 'should throw unexpected error' + ) + t.end() + }) + }) + + t.end() +}) |