From da269db98debaf076ab6e3490c1b61628e54081e Mon Sep 17 00:00:00 2001 From: Forrest L Norvell Date: Thu, 26 Feb 2015 22:35:54 -0800 Subject: run-script: tweak wording of output, add tests Also make tests conform to `standard` while I'm here, to make merging less painful later. --- lib/run-script.js | 9 +- test/tap/run-script.js | 220 +++++++++++++++++++++++++++++---------- test/tap/run-script/package.json | 14 --- 3 files changed, 172 insertions(+), 71 deletions(-) delete mode 100644 test/tap/run-script/package.json diff --git a/lib/run-script.js b/lib/run-script.js index cb797c7b8..7603e29e1 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -118,16 +118,17 @@ function list(cb) { var s = "\n " var prefix = " " - var header = "Available scripts in the %s package" - if (scripts.length) console.log(header + ":", d.name) + if (scripts.length) { + console.log("Lifecycle scripts included in %s:", d.name) + } scripts.forEach(function(script) { console.log(prefix + script + s + d.scripts[script]) }) if (!scripts.length && runScripts.length) { - console.log(header + " with run-scripts:", d.name) + console.log("Scripts available in %s via `npm run-script`:", d.name) } else if (runScripts.length) { - console.log("\nwith run-scripts:") + console.log("\navailable via `npm run-script`:") } runScripts.forEach(function(script) { console.log(prefix + script + s + d.scripts[script]) diff --git a/test/tap/run-script.js b/test/tap/run-script.js index d7681b9ac..fca9a76ff 100644 --- a/test/tap/run-script.js +++ b/test/tap/run-script.js @@ -1,12 +1,17 @@ -var common = require("../common-tap") - , test = require("tap").test - , path = require("path") - , rimraf = require("rimraf") - , mkdirp = require("mkdirp") - , pkg = path.resolve(__dirname, "run-script") - , cache = path.resolve(pkg, "cache") - , tmp = path.resolve(pkg, "tmp") - , opts = { cwd: pkg } +var fs = require('fs') +var path = require('path') + +var mkdirp = require('mkdirp') +var test = require('tap').test +var rimraf = require('rimraf') + +var common = require('../common-tap') + +var pkg = path.resolve(__dirname, 'run-script') +var cache = path.resolve(pkg, 'cache') +var tmp = path.resolve(pkg, 'tmp') + +var opts = { cwd: pkg } function testOutput (t, command, er, code, stdout, stderr) { var lines @@ -15,99 +20,208 @@ function testOutput (t, command, er, code, stdout, stderr) { throw er if (stderr) - throw new Error("npm " + command + " stderr: " + stderr.toString()) + throw new Error('npm ' + command + ' stderr: ' + stderr.toString()) - lines = stdout.trim().split("\n") - stdout = lines.filter(function(line) { - return line.trim() !== "" && line[0] !== ">" - }).join(";") + lines = stdout.trim().split('\n') + stdout = lines.filter(function (line) { + return line.trim() !== '' && line[0] !== '>' + }).join(';') t.equal(stdout, command) t.end() } +function writeMetadata (object) { + fs.writeFileSync( + path.resolve(pkg, 'package.json'), + JSON.stringify(object, null, 2) + '\n' + ) +} + function cleanup () { - rimraf.sync(cache) - rimraf.sync(tmp) + rimraf.sync(pkg) } -test("setup", function (t) { +test('setup', function (t) { cleanup() mkdirp.sync(cache) mkdirp.sync(tmp) + writeMetadata(fullyPopulated) t.end() }) -test("npm run-script", function (t) { - common.npm(["run-script", "start"], opts, testOutput.bind(null, t, "start")) +var fullyPopulated = { + 'name': 'runscript', + 'version': '1.2.3', + 'scripts': { + 'start': 'node -e "console.log(process.argv[1] || \'start\')"', + 'prewith-pre': 'node -e "console.log(process.argv[1] || \'pre\')"', + 'with-pre': 'node -e "console.log(process.argv[1] || \'main\')"', + 'with-post': 'node -e "console.log(process.argv[1] || \'main\')"', + 'postwith-post': 'node -e "console.log(process.argv[1] || \'post\')"', + 'prewith-both': 'node -e "console.log(process.argv[1] || \'pre\')"', + 'with-both': 'node -e "console.log(process.argv[1] || \'main\')"', + 'postwith-both': 'node -e "console.log(process.argv[1] || \'post\')"', + 'stop': 'node -e "console.log(process.argv[1] || \'stop\')"' + } +} + +var lifecycleOnly = { + name: 'scripted', + version: '1.2.3', + scripts: { + 'prestart': 'echo prestart' + } +} + +var directOnly = { + name: 'scripted', + version: '1.2.3', + scripts: { + 'whoa': 'echo whoa' + } +} + +var both = { + name: 'scripted', + version: '1.2.3', + scripts: { + 'prestart': 'echo prestart', + 'whoa': 'echo whoa' + } +} + +test('npm run-script start', function (t) { + common.npm(['run-script', 'start'], opts, testOutput.bind(null, t, 'start')) +}) + +test('npm run-script with args', function (t) { + common.npm(['run-script', 'start', '--', 'stop'], opts, testOutput.bind(null, t, 'stop')) }) -test("npm run-script with args", function (t) { - common.npm(["run-script", "start", "--", "stop"], opts, testOutput.bind(null, t, "stop")) +test('npm run-script with args that contain spaces', function (t) { + common.npm(['run-script', 'start', '--', 'hello world'], opts, testOutput.bind(null, t, 'hello world')) }) -test("npm run-script with args that contain spaces", function (t) { - common.npm(["run-script", "start", "--", "hello world"], opts, testOutput.bind(null, t, "hello world")) +test('npm run-script with args that contain single quotes', function (t) { + common.npm(['run-script', 'start', '--', 'they"re awesome'], opts, testOutput.bind(null, t, 'they"re awesome')) }) -test("npm run-script with args that contain single quotes", function (t) { - common.npm(["run-script", "start", "--", "they're awesome"], opts, testOutput.bind(null, t, "they're awesome")) +test('npm run-script with args that contain double quotes', function (t) { + common.npm(['run-script', 'start', '--', 'what"s "up"?'], opts, testOutput.bind(null, t, 'what"s "up"?')) }) -test("npm run-script with args that contain double quotes", function (t) { - common.npm(["run-script", "start", "--", "what's \"up\"?"], opts, testOutput.bind(null, t, "what's \"up\"?")) +test('npm run-script with pre script', function (t) { + common.npm(['run-script', 'with-post'], opts, testOutput.bind(null, t, 'main;post')) }) -test("npm run-script with pre script", function (t) { - common.npm(["run-script", "with-post"], opts, testOutput.bind(null, t, "main;post")) +test('npm run-script with post script', function (t) { + common.npm(['run-script', 'with-pre'], opts, testOutput.bind(null, t, 'pre;main')) }) -test("npm run-script with post script", function (t) { - common.npm(["run-script", "with-pre"], opts, testOutput.bind(null, t, "pre;main")) +test('npm run-script with both pre and post script', function (t) { + common.npm(['run-script', 'with-both'], opts, testOutput.bind(null, t, 'pre;main;post')) }) -test("npm run-script with both pre and post script", function (t) { - common.npm(["run-script", "with-both"], opts, testOutput.bind(null, t, "pre;main;post")) +test('npm run-script with both pre and post script and with args', function (t) { + common.npm(['run-script', 'with-both', '--', 'an arg'], opts, testOutput.bind(null, t, 'pre;an arg;post')) }) -test("npm run-script with both pre and post script and with args", function (t) { - common.npm(["run-script", "with-both", "--", "an arg"], opts, testOutput.bind(null, t, "pre;an arg;post")) +test('npm run-script explicitly call pre script with arg', function (t) { + common.npm(['run-script', 'prewith-pre', '--', 'an arg'], opts, testOutput.bind(null, t, 'an arg')) }) -test("npm run-script explicitly call pre script with arg", function (t) { - common.npm(["run-script", "prewith-pre", "--", "an arg"], opts, testOutput.bind(null, t, "an arg")) +test('npm run-script test', function (t) { + common.npm(['run-script', 'test'], opts, function (er, code, stdout, stderr) { + t.ifError(er, 'npm run-script test ran without issue') + t.notOk(stderr, 'should not generate errors') + t.end() + }) }) -test("npm run-script test", function (t) { - common.npm(["run-script", "test"], opts, function (er, code, stdout, stderr) { - t.ifError(er, "npm run-script test ran without issue") - t.notOk(stderr, "should not generate errors") +test('npm run-script env', function (t) { + common.npm(['run-script', 'env'], opts, function (er, code, stdout, stderr) { + t.ifError(er, 'using default env script') + t.notOk(stderr, 'should not generate errors') + t.ok(stdout.indexOf('npm_config_init_version') > 0, 'expected values in var list') t.end() }) }) -test("npm run-script env", function (t) { - common.npm(["run-script", "env"], opts, function (er, code, stdout, stderr) { - t.ifError(er, "using default env script") - t.notOk(stderr, "should not generate errors") - t.ok( stdout.indexOf("npm_config_init_version") > 0, "expected values in var list" ) +test('npm run-script nonexistent-script', function (t) { + common.npm(['run-script', 'nonexistent-script'], opts, function (er, code, stdout, stderr) { + t.ifError(er, 'npm run-script nonexistent-script did not cause npm to explode') + t.ok(stderr, 'should generate errors') t.end() }) }) -test("npm run-script nonexistent-script", function (t) { - common.npm(["run-script", "nonexistent-script"], opts, function (er, code, stdout, stderr) { - t.ifError(er, "npm run-script nonexistent-script did not cause npm to explode") - t.ok(stderr, "should generate errors") +test('npm run-script restart when there isn"t restart', function (t) { + common.npm(['run-script', 'restart'], opts, testOutput.bind(null, t, 'stop;start')) +}) + +test('npm run-script no-params (lifecycle only)', function (t) { + var expected = [ + 'Lifecycle scripts included in scripted:', + ' prestart', + ' echo prestart', + '' + ].join('\n') + + writeMetadata(lifecycleOnly) + + common.npm(['run-script'], opts, function (err, code, stdout, stderr) { + t.ifError(err, 'ran run-script without parameters without crashing') + t.notOk(code, 'npm exited without error code') + t.notOk(stderr, 'npm printed nothing to stderr') + t.equal(stdout, expected, 'got expected output') t.end() }) }) -test("npm run-script restart when there isn't restart", function (t) { - common.npm(["run-script", "restart"], opts, testOutput.bind(null, t, "stop;start")) +test('npm run-script no-params (direct only)', function (t) { + var expected = [ + 'Scripts available in scripted via `npm run-script`:', + ' whoa', + ' echo whoa', + '' + ].join('\n') + + writeMetadata(directOnly) + + common.npm(['run-script'], opts, function (err, code, stdout, stderr) { + t.ifError(err, 'ran run-script without parameters without crashing') + t.notOk(code, 'npm exited without error code') + t.notOk(stderr, 'npm printed nothing to stderr') + t.equal(stdout, expected, 'got expected output') + t.end() + }) +}) + +test('npm run-script no-params (direct only)', function (t) { + var expected = [ + 'Lifecycle scripts included in scripted:', + ' prestart', + ' echo prestart', + '', + 'available via `npm run-script`:', + ' whoa', + ' echo whoa', + '' + ].join('\n') + + writeMetadata(both) + + common.npm(['run-script'], opts, function (err, code, stdout, stderr) { + t.ifError(err, 'ran run-script without parameters without crashing') + t.notOk(code, 'npm exited without error code') + t.notOk(stderr, 'npm printed nothing to stderr') + t.equal(stdout, expected, 'got expected output') + t.end() + }) }) -test("cleanup", function (t) { +test('cleanup', function (t) { cleanup() t.end() }) diff --git a/test/tap/run-script/package.json b/test/tap/run-script/package.json deleted file mode 100644 index 761b15ffb..000000000 --- a/test/tap/run-script/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{"name":"runscript" -,"version":"1.2.3" -,"scripts":{ - "start":"node -e \"console.log(process.argv[1] || 'start')\"", - "prewith-pre":"node -e \"console.log(process.argv[1] || 'pre')\"", - "with-pre":"node -e \"console.log(process.argv[1] || 'main')\"", - "with-post":"node -e \"console.log(process.argv[1] || 'main')\"", - "postwith-post":"node -e \"console.log(process.argv[1] || 'post')\"", - "prewith-both":"node -e \"console.log(process.argv[1] || 'pre')\"", - "with-both":"node -e \"console.log(process.argv[1] || 'main')\"", - "postwith-both":"node -e \"console.log(process.argv[1] || 'post')\"", - "stop":"node -e \"console.log(process.argv[1] || 'stop')\"" - } -} -- cgit v1.2.3