Skip to content

Commit

Permalink
fixup: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Apr 8, 2021
1 parent eb71ee8 commit 4b25921
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 15 deletions.
5 changes: 2 additions & 3 deletions lib/base-command.js
Expand Up @@ -27,17 +27,16 @@ class BaseCommand {
else
usage = `${usage}${this.constructor.usage.map(u => `npm ${this.constructor.name} ${u}`).join('\n')}`

if (this.constructor.params) {
if (this.constructor.params)
usage = `${usage}\n\nOptions:\n${this.wrappedParams}`
}

// Mostly this just appends aliases, this could be more clear
usage = usageUtil(this.constructor.name, usage)
usage = `${usage}\n\nRun "npm help ${this.constructor.name}" for more info`
return usage
}

get wrappedParams() {
get wrappedParams () {
let results = ''
let line = ''

Expand Down
9 changes: 4 additions & 5 deletions lib/version.js
Expand Up @@ -71,7 +71,7 @@ class Version extends BaseCommand {
}
}

async versionWorkspaces(args, filters) {
async versionWorkspaces (args, filters) {
switch (args.length) {
case 0:
return this.listWorkspaces(filters)
Expand Down Expand Up @@ -100,7 +100,7 @@ class Version extends BaseCommand {
const version = await libnpmversion(args[0], {
...this.npm.flatOptions,
'git-tag-version': false,
path
path,
})
this.npm.output(`${prefix}${version}`)
}
Expand Down Expand Up @@ -130,17 +130,16 @@ class Version extends BaseCommand {
const results = {}
const workspaces =
await getWorkspaces(filters, { path: this.npm.localPrefix })
for (const [name, path] of workspaces) {
for (const [, path] of workspaces) {
const pj = resolve(path, 'package.json')
// getWorkspaces has already parsed this so we know it won't error
const pkg = await readFile(pj, 'utf8')
.then(data => JSON.parse(data))
.catch(() => ({}))

if (pkg.name && pkg.version)
results[pkg.name] = pkg.version
}
return this.list(results)

}
}

Expand Down
1 change: 1 addition & 0 deletions lib/workspaces/get-workspaces.js
Expand Up @@ -6,6 +6,7 @@ const rpj = require('read-package-json-fast')
// Returns an Map of paths to workspaces indexed by workspace name
// { foo => '/path/to/foo' }
const getWorkspaces = async (filters, { path }) => {
// TODO we need a better error to be bubbled up here if this rpj call fails
const pkg = await rpj(resolve(path, 'package.json'))
const workspaces = await mapWorkspaces({ cwd: path, pkg })
const res = filters.length ? new Map() : workspaces
Expand Down
15 changes: 10 additions & 5 deletions tap-snapshots/test-lib-dist-tag.js-TAP.test.js
Expand Up @@ -16,7 +16,8 @@ npm dist-tag rm <pkg> <tag>
npm dist-tag ls [<pkg>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] [-ws|--workspaces]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
alias: dist-tags
Expand All @@ -34,7 +35,8 @@ npm dist-tag rm <pkg> <tag>
npm dist-tag ls [<pkg>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] [-ws|--workspaces]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
alias: dist-tags
Expand All @@ -61,7 +63,8 @@ npm dist-tag rm <pkg> <tag>
npm dist-tag ls [<pkg>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] [-ws|--workspaces]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
alias: dist-tags
Expand All @@ -85,7 +88,8 @@ npm dist-tag rm <pkg> <tag>
npm dist-tag ls [<pkg>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] [-ws|--workspaces]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
alias: dist-tags
Expand Down Expand Up @@ -139,7 +143,8 @@ npm dist-tag rm <pkg> <tag>
npm dist-tag ls [<pkg>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] [-ws|--workspaces]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
alias: dist-tags
Expand Down
7 changes: 5 additions & 2 deletions tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js
Expand Up @@ -325,7 +325,8 @@ All commands:
npm dist-tag ls [<pkg>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] [-ws|--workspaces]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
alias: dist-tags
Expand Down Expand Up @@ -632,7 +633,9 @@ All commands:
npm pack [[<@scope>/]<pkg>...]
Options:
[--dry-run] [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]] [-ws|--workspaces]
[--dry-run]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
Run "npm help pack" for more info
Expand Down
172 changes: 172 additions & 0 deletions test/lib/version.js
Expand Up @@ -6,6 +6,7 @@ let result = []

const noop = () => null
const config = {
'git-tag-version': true,
'tag-version-prefix': 'v',
json: false,
}
Expand Down Expand Up @@ -158,3 +159,174 @@ t.test('with one arg', t => {
t.end()
})
})

t.test('workspaces', t => {
t.teardown(() => {
npm.localPrefix = ''
npm.prefix = ''
})

t.test('no args, all workspaces', t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'workspaces-test',
version: '1.0.0',
workspaces: ['workspace-a', 'workspace-b'],
}, null, 2),
'workspace-a': {
'package.json': JSON.stringify({
name: 'workspace-a',
version: '1.0.0',
}),
},
'workspace-b': {
'package.json': JSON.stringify({
name: 'workspace-b',
version: '1.0.0',
}),
},
})
npm.localPrefix = testDir
npm.prefix = testDir
const version = new Version(npm)
version.execWorkspaces([], [], err => {
if (err)
throw err
t.same(result, [{
'workspaces-test': '1.0.0',
'workspace-a': '1.0.0',
'workspace-b': '1.0.0',
npm: '1.0.0',
}], 'outputs includes main package and workspace versions')
t.end()
})
})

t.test('no args, single workspaces', t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'workspaces-test',
version: '1.0.0',
workspaces: ['workspace-a', 'workspace-b'],
}, null, 2),
'workspace-a': {
'package.json': JSON.stringify({
name: 'workspace-a',
version: '1.0.0',
}),
},
'workspace-b': {
'package.json': JSON.stringify({
name: 'workspace-b',
version: '1.0.0',
}),
},
})
npm.localPrefix = testDir
npm.prefix = testDir
const version = new Version(npm)
version.execWorkspaces([], ['workspace-a'], err => {
if (err)
throw err
t.same(result, [{
'workspaces-test': '1.0.0',
'workspace-a': '1.0.0',
npm: '1.0.0',
}], 'outputs includes main package and requested workspace versions')
t.end()
})
})

t.test('no args, all workspaces, workspace with missing name or version', t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'workspaces-test',
version: '1.0.0',
workspaces: ['workspace-a', 'workspace-b', 'workspace-c'],
}, null, 2),
'workspace-a': {
'package.json': JSON.stringify({
name: 'workspace-a',
version: '1.0.0',
}),
},
'workspace-b': {
'package.json': JSON.stringify({
name: 'workspace-b',
}),
},
'workspace-c': {
'package.json': JSON.stringify({
version: '1.0.0',
}),
},
})
npm.localPrefix = testDir
npm.prefix = testDir
const version = new Version(npm)
version.execWorkspaces([], [], err => {
if (err)
throw err
t.same(result, [{
'workspaces-test': '1.0.0',
'workspace-a': '1.0.0',
npm: '1.0.0',
}], 'outputs includes main package and valid workspace versions')
t.end()
})
})

t.test('with one arg, all workspaces', t => {
const libNpmVersionArgs = []
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'workspaces-test',
version: '1.0.0',
workspaces: ['workspace-a', 'workspace-b'],
}, null, 2),
'workspace-a': {
'package.json': JSON.stringify({
name: 'workspace-a',
version: '1.0.0',
}),
},
'workspace-b': {
'package.json': JSON.stringify({
name: 'workspace-b',
version: '1.0.0',
}),
},
})
const Version = requireInject('../../lib/version.js', {
...mocks,
libnpmversion: (arg, opts) => {
libNpmVersionArgs.push([arg, opts])
return '2.0.0'
},
})
npm.localPrefix = testDir
npm.prefix = testDir
const version = new Version(npm)

version.execWorkspaces(['major'], [], err => {
if (err)
throw err
t.same(result, ['workspace-a', 'v2.0.0', 'workspace-b', 'v2.0.0'], 'outputs the new version for only the workspaces prefixed by the tagVersionPrefix')
t.end()
})
})

t.test('too many args', t => {
version.execWorkspaces(['foo', 'bar'], [], err => {
t.match(
err,
'npm version',
'should throw usage instructions error'
)

t.end()
})
})

t.end()
})

0 comments on commit 4b25921

Please sign in to comment.