Skip to content

Commit

Permalink
update run-script snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed May 13, 2024
1 parent 0387c9d commit 5363c9f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 59 deletions.
36 changes: 26 additions & 10 deletions lib/commands/run-script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { log, output } = require('proc-log')
const { output } = require('proc-log')
const pkgJson = require('@npmcli/package-json')
const BaseCommand = require('../base-cmd.js')
const { outputError } = require('../utils/output-error.js')
const { getError } = require('../utils/error-message.js')

// TODO this is missing things like prepare, prepublishOnly, and dependencies
const cmdList = [
Expand Down Expand Up @@ -55,21 +57,33 @@ class RunScript extends BaseCommand {
async execWorkspaces (args) {
await this.setWorkspaces()

for (const [name, path] of this.workspaces.entries()) {
const ws = [...this.workspaces.entries()]
for (const [name, path] of ws) {
const last = name === ws.at(-1)[0]
if (!args.length) {
await this.#list(path, { workspace: name })
await this.#list(path, { workspace: name, newline: !last })
continue
}

const pkg = await pkgJson.normalize(path).then(p => p.content)
try {
await this.#run(args, { path, pkg })
} catch (err) {
log.error(`Lifecycle script \`${args[0]}\` failed with error:`)
log.error(err)
log.error(` in workspace: ${pkg._id || pkg.name}`)
log.error(` at location: ${path}`)
process.exitCode = 1
// Set command to null so that we get the full error parsing
// run-script will supress error messages due to isShellout
const error = getError(err, { npm: this.npm, command: null })
outputError({
...error,
error: [
['', `Lifecycle script \`${args[0]}\` failed with error:`],
['workspace', pkg._id || pkg.name],
...error.error,
],
})
if (!last) {
output.error('')
}
process.exitCode = error.exitCode
}
}
}
Expand Down Expand Up @@ -133,7 +147,7 @@ class RunScript extends BaseCommand {
}
}

async #list (path, { workspace } = {}) {
async #list (path, { workspace, newline } = {}) {
const { scripts = {}, name, _id } = await pkgJson.normalize(path).then(p => p.content)
const scriptEntries = Object.entries(scripts)

Expand Down Expand Up @@ -189,7 +203,9 @@ class RunScript extends BaseCommand {
}
}

output.standard('')
if (newline) {
output.standard('')
}
}
}

Expand Down
103 changes: 54 additions & 49 deletions tap-snapshots/test/lib/commands/run-script.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ postenv:echo after the env

exports[`test/lib/commands/run-script.js TAP list scripts warn json > json report 1`] = `
{
test: 'exit 2',
start: 'node server.js',
stop: 'node kill-server.js',
preenv: 'echo before the env',
postenv: 'echo after the env'
"test": "exit 2",
"start": "node server.js",
"stop": "node kill-server.js",
"preenv": "echo before the env",
"postenv": "echo after the env"
}
`

Expand All @@ -53,20 +53,34 @@ Scripts available in x@1.2.3 via \`npm run-script\`:

exports[`test/lib/commands/run-script.js TAP workspaces failed workspace run with succeeded runs > should log error msgs for each workspace script 1`] = `
Lifecycle script \`glorp\` failed with error:
Error: ERR
in workspace: a@1.0.0
at location: {CWD}/prefix/packages/a
workspace a@1.0.0
code ERR
ERR
`

exports[`test/lib/commands/run-script.js TAP workspaces list all scripts --json > must match snapshot 1`] = `
{ a: { glorp: 'echo a doing the glerp glop' } }
{ b: { glorp: 'echo b doing the glerp glop' } }
{
c: { test: 'exit 0', posttest: 'echo posttest', lorem: 'echo c lorem' }
"a": {
"glorp": "echo a doing the glerp glop"
},
"b": {
"glorp": "echo b doing the glerp glop"
},
"c": {
"test": "exit 0",
"posttest": "echo posttest",
"lorem": "echo c lorem"
},
"d": {
"test": "exit 0",
"posttest": "echo posttest"
},
"e": {
"test": "exit 0",
"start": "echo start something"
},
"noscripts": {}
}
{ d: { test: 'exit 0', posttest: 'echo posttest' } }
{ e: { test: 'exit 0', start: 'echo start something' } }
{ noscripts: {} }
`

exports[`test/lib/commands/run-script.js TAP workspaces list all scripts --parseable > must match snapshot 1`] = `
Expand Down Expand Up @@ -195,69 +209,60 @@ Scripts available in a@1.0.0 via \`npm run-script\`:

exports[`test/lib/commands/run-script.js TAP workspaces missing scripts in all workspaces > should log error msgs for each workspace script 1`] = `
Lifecycle script \`missing-script\` failed with error:
Error: Missing script: "missing-script"
workspace a@1.0.0
Missing script: "missing-script"
npm error
To see a list of scripts, run:
npm run
in workspace: a@1.0.0
at location: {CWD}/prefix/packages/a
Lifecycle script \`missing-script\` failed with error:
Error: Missing script: "missing-script"
workspace b@2.0.0
Missing script: "missing-script"
npm error
To see a list of scripts, run:
npm run
in workspace: b@2.0.0
at location: {CWD}/prefix/packages/b
Lifecycle script \`missing-script\` failed with error:
Error: Missing script: "missing-script"
workspace c@1.0.0
Missing script: "missing-script"
npm error
To see a list of scripts, run:
npm run
in workspace: c@1.0.0
at location: {CWD}/prefix/packages/c
Lifecycle script \`missing-script\` failed with error:
Error: Missing script: "missing-script"
workspace d@1.0.0
Missing script: "missing-script"
npm error
To see a list of scripts, run:
npm run
in workspace: d@1.0.0
at location: {CWD}/prefix/packages/d
Lifecycle script \`missing-script\` failed with error:
Error: Missing script: "missing-script"
workspace e
Missing script: "missing-script"
npm error
To see a list of scripts, run:
npm run
in workspace: e
at location: {CWD}/prefix/packages/e
Lifecycle script \`missing-script\` failed with error:
Error: Missing script: "missing-script"
workspace noscripts@1.0.0
Missing script: "missing-script"
npm error
To see a list of scripts, run:
npm run
in workspace: noscripts@1.0.0
at location: {CWD}/prefix/packages/noscripts
`

exports[`test/lib/commands/run-script.js TAP workspaces missing scripts in some workspaces > should log error msgs for each workspace script 1`] = `
Lifecycle script \`test\` failed with error:
Error: Missing script: "test"
workspace a@1.0.0
Missing script: "test"
npm error
To see a list of scripts, run:
npm run
in workspace: a@1.0.0
at location: {CWD}/prefix/packages/a
Lifecycle script \`test\` failed with error:
Error: Missing script: "test"
workspace b@2.0.0
Missing script: "test"
npm error
To see a list of scripts, run:
npm run
in workspace: b@2.0.0
at location: {CWD}/prefix/packages/b
`

exports[`test/lib/commands/run-script.js TAP workspaces single failed workspace run > should log error msgs for each workspace script 1`] = `
Lifecycle script \`test\` failed with error:
Error: err
in workspace: c@1.0.0
at location: {CWD}/prefix/packages/c
workspace c@1.0.0
err
`

0 comments on commit 5363c9f

Please sign in to comment.