diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2015-04-08 03:43:16 +0300 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2015-04-09 07:40:17 +0300 |
commit | f40ecaad68f77abc50eb6f5b224e31dec3d250fc (patch) | |
tree | b00e7730c88a0677f29ca0928b1b7b6fdb5762b8 | |
parent | a8089268d5f3d57f42dbaba02ff6437da5121191 (diff) |
test: add common.makeGitRepo for DRYness
-rw-r--r-- | test/common-tap.js | 75 | ||||
-rw-r--r-- | test/tap/add-remote-git-fake-windows.js | 113 | ||||
-rw-r--r-- | test/tap/add-remote-git-file.js | 27 | ||||
-rw-r--r-- | test/tap/add-remote-git-shrinkwrap.js | 21 | ||||
-rw-r--r-- | test/tap/add-remote-git.js | 105 | ||||
-rw-r--r-- | test/tap/git-dependency-install-link.js | 21 | ||||
-rw-r--r-- | test/tap/version-message-config.js | 62 | ||||
-rw-r--r-- | test/tap/version-update-shrinkwrap.js | 289 |
8 files changed, 280 insertions, 433 deletions
diff --git a/test/common-tap.js b/test/common-tap.js index 27c828f16..3537df687 100644 --- a/test/common-tap.js +++ b/test/common-tap.js @@ -6,21 +6,22 @@ if (!global.setImmediate || !require('timers').setImmediate) { } } -var spawn = require("child_process").spawn -var path = require("path") +var spawn = require('child_process').spawn +var path = require('path') var port = exports.port = 1337 -exports.registry = "http://localhost:" + port -process.env.npm_config_loglevel = "error" +exports.registry = 'http://localhost:' + port +process.env.npm_config_loglevel = 'error' -var npm_config_cache = path.resolve(__dirname, "npm_cache") +var npm_config_cache = path.resolve(__dirname, 'npm_cache') process.env.npm_config_cache = exports.npm_config_cache = npm_config_cache -process.env.npm_config_userconfig = exports.npm_config_userconfig = path.join(__dirname, "fixtures", "config", "userconfig") -process.env.npm_config_globalconfig = exports.npm_config_globalconfig = path.join(__dirname, "fixtures", "config", "globalconfig") -process.env.random_env_var = "foo" +process.env.npm_config_userconfig = exports.npm_config_userconfig = path.join(__dirname, 'fixtures', 'config', 'userconfig') +process.env.npm_config_globalconfig = exports.npm_config_globalconfig = path.join(__dirname, 'fixtures', 'config', 'globalconfig') +process.env.random_env_var = 'foo' -var bin = exports.bin = require.resolve("../bin/npm-cli.js") -var once = require("once") +var bin = exports.bin = require.resolve('../bin/npm-cli.js') +var chain = require('slide').chain +var once = require('once') exports.npm = function (cmd, opts, cb) { cb = once(cb) @@ -32,23 +33,53 @@ exports.npm = function (cmd, opts, cb) { opts.env.npm_config_cache = npm_config_cache } - var stdout = "" - , stderr = "" - , node = process.execPath - , child = spawn(node, cmd, opts) + var stdout = '' + var stderr = '' + var node = process.execPath + var child = spawn(node, cmd, opts) - if (child.stderr) child.stderr.on("data", function (chunk) { - stderr += chunk - }) + if (child.stderr) { + child.stderr.on('data', function (chunk) { + stderr += chunk + }) + } - if (child.stdout) child.stdout.on("data", function (chunk) { - stdout += chunk - }) + if (child.stdout) { + child.stdout.on('data', function (chunk) { + stdout += chunk + }) + } - child.on("error", cb) + child.on('error', cb) - child.on("close", function (code) { + child.on('close', function (code) { cb(null, code, stdout, stderr) }) return child } + +exports.makeGitRepo = function (params, cb) { + // git must be called after npm.load because it uses config + var git = require('../lib/utils/git.js') + + var root = params.path || process.cwd() + var user = params.user || 'PhantomFaker' + var email = params.email || 'nope@not.real' + var added = params.added || ['package.json'] + var message = params.message || 'stub repo' + + var opts = { cwd: root, env: { PATH: process.env.PATH }} + var commands = [ + git.chainableExec(['init'], opts), + git.chainableExec(['config', 'user.name', user], opts), + git.chainableExec(['config', 'user.email', email], opts), + git.chainableExec(['add'].concat(added), opts), + git.chainableExec(['commit', '-m', message], opts) + ] + + if (Array.isArray(params.commands)) { + commands = commands.concat(params.commands) + } + + chain(commands, cb) +} diff --git a/test/tap/add-remote-git-fake-windows.js b/test/tap/add-remote-git-fake-windows.js index b665f752a..0e0d539fb 100644 --- a/test/tap/add-remote-git-fake-windows.js +++ b/test/tap/add-remote-git-fake-windows.js @@ -1,23 +1,22 @@ -var fs = require("fs") -var resolve = require("path").resolve +var fs = require('fs') +var resolve = require('path').resolve -var chain = require("slide").chain -var osenv = require("osenv") -var mkdirp = require("mkdirp") -var rimraf = require("rimraf") -var test = require("tap").test +var osenv = require('osenv') +var mkdirp = require('mkdirp') +var rimraf = require('rimraf') +var test = require('tap').test -var npm = require("../../lib/npm.js") -var common = require("../common-tap.js") +var npm = require('../../lib/npm.js') +var common = require('../common-tap.js') -var pkg = resolve(__dirname, "add-remote-git") -var repo = resolve(__dirname, "add-remote-git-repo") +var pkg = resolve(__dirname, 'add-remote-git') +var repo = resolve(__dirname, 'add-remote-git-repo') var daemon var daemonPID var git -test("setup", function (t) { +test('setup', function (t) { bootstrap() setup(function (er, r) { if (er) { @@ -33,21 +32,21 @@ test("setup", function (t) { }) }) -test("install from repo on 'Windows'", function (t) { +test('install from repo on \'Windows\'', function (t) { // before we confuse everything by switching the platform - require("../../lib/install.js") - require("../../lib/unbuild.js") - process.platform = "win32" + require('../../lib/install.js') + require('../../lib/unbuild.js') + process.platform = 'win32' process.chdir(pkg) - npm.commands.install(".", [], function (er) { - t.ifError(er, "npm installed via git") + npm.commands.install('.', [], function (er) { + t.ifError(er, 'npm installed via git') t.end() }) }) -test("clean", function (t) { - daemon.on("close", function () { +test('clean', function (t) { + daemon.on('close', function () { cleanup() t.end() }) @@ -55,83 +54,73 @@ test("clean", function (t) { }) var pjParent = JSON.stringify({ - name : "parent", - version : "1.2.3", - dependencies : { - "child" : "git://localhost:1233/child.git" + name: 'parent', + version: '1.2.3', + dependencies: { + child: 'git://localhost:1233/child.git' } -}, null, 2) + "\n" +}, null, 2) + '\n' var pjChild = JSON.stringify({ - name : "child", - version : "1.0.3" -}, null, 2) + "\n" + name: 'child', + version: '1.0.3' +}, null, 2) + '\n' function bootstrap () { rimraf.sync(pkg) mkdirp.sync(pkg) - fs.writeFileSync(resolve(pkg, "package.json"), pjParent) + fs.writeFileSync(resolve(pkg, 'package.json'), pjParent) } function setup (cb) { rimraf.sync(repo) mkdirp.sync(repo) - fs.writeFileSync(resolve(repo, "package.json"), pjChild) - npm.load({ registry : common.registry, loglevel : "silent" }, function () { + fs.writeFileSync(resolve(repo, 'package.json'), pjChild) + npm.load({ registry: common.registry, loglevel: 'silent' }, function () { // some really cheesy monkeypatching - require("module")._cache[require.resolve("which")] = { - exports : function (_, cb) { cb() } + require('module')._cache[require.resolve('which')] = { + exports: function (_, cb) { cb() } } - git = require("../../lib/utils/git.js") + git = require('../../lib/utils/git.js') function startDaemon (cb) { // start git server var d = git.spawn( [ - "daemon", - "--verbose", - "--listen=localhost", - "--export-all", - "--base-path=.", - "--port=1233" + 'daemon', + '--verbose', + '--listen=localhost', + '--export-all', + '--base-path=.', + '--port=1233' ], { - cwd : pkg, - env : process.env, - stdio : ["pipe", "pipe", "pipe"] + cwd: pkg, + env: process.env, + stdio: ['pipe', 'pipe', 'pipe'] } ) - d.stderr.on("data", childFinder) + d.stderr.on('data', childFinder) function childFinder (c) { var cpid = c.toString().match(/^\[(\d+)\]/) if (cpid[1]) { - this.removeListener("data", childFinder) + this.removeListener('data', childFinder) cb(null, [d, cpid[1]]) } } } - var opts = { - cwd : repo, - env : process.env - } - - chain( - [ - git.chainableExec(["init"], opts), - git.chainableExec(["config", "user.name", "PhantomFaker"], opts), - git.chainableExec(["config", "user.email", "nope@not.real"], opts), - git.chainableExec(["add", "package.json"], opts), - git.chainableExec(["commit", "-m", "stub package"], opts), + common.makeGitRepo({ + path: repo, + commands: [ git.chainableExec( - ["clone", "--bare", repo, "child.git"], - { cwd : pkg, env : process.env } + ['clone', '--bare', repo, 'child.git'], + { cwd: pkg, env: process.env } ), startDaemon - ], - cb - ) + ] + }, cb) }) } diff --git a/test/tap/add-remote-git-file.js b/test/tap/add-remote-git-file.js index ca4a33cfc..fc102faac 100644 --- a/test/tap/add-remote-git-file.js +++ b/test/tap/add-remote-git-file.js @@ -2,7 +2,6 @@ var fs = require('fs') var resolve = require('path').resolve var url = require('url') -var chain = require('slide').chain var osenv = require('osenv') var mkdirp = require('mkdirp') var rimraf = require('rimraf') @@ -62,25 +61,13 @@ function setup (cb) { npm.load({ registry: common.registry, loglevel: 'silent' }, function () { git = require('../../lib/utils/git.js') - var opts = { - cwd: repo, - env: process.env - } - - chain( - [ - git.chainableExec(['init'], opts), - git.chainableExec(['config', 'user.name', 'PhantomFaker'], opts), - git.chainableExec(['config', 'user.email', 'nope@not.real'], opts), - git.chainableExec(['add', 'package.json'], opts), - git.chainableExec(['commit', '-m', 'stub package'], opts), - git.chainableExec( - ['clone', '--bare', repo, 'child.git'], - { cwd: pkg, env: process.env } - ) - ], - cb - ) + common.makeGitRepo({ + path: repo, + commands: [git.chainableExec( + ['clone', '--bare', repo, 'child.git'], + { cwd: pkg, env: process.env } + )] + }, cb) }) } diff --git a/test/tap/add-remote-git-shrinkwrap.js b/test/tap/add-remote-git-shrinkwrap.js index 555dca213..94951e9a9 100644 --- a/test/tap/add-remote-git-shrinkwrap.js +++ b/test/tap/add-remote-git-shrinkwrap.js @@ -1,7 +1,6 @@ var fs = require('fs') var resolve = require('path').resolve -var chain = require('slide').chain var osenv = require('osenv') var mkdirp = require('mkdirp') var rimraf = require('rimraf') @@ -141,26 +140,16 @@ function setup (cb) { } } - var opts = { - cwd: repo, - env: process.env - } - - chain( - [ - git.chainableExec(['init'], opts), - git.chainableExec(['config', 'user.name', 'PhantomFaker'], opts), - git.chainableExec(['config', 'user.email', 'nope@not.real'], opts), - git.chainableExec(['add', 'package.json'], opts), - git.chainableExec(['commit', '-m', 'stub package'], opts), + common.makeGitRepo({ + path: repo, + commands: [ git.chainableExec( ['clone', '--bare', repo, 'child.git'], { cwd: pkg, env: process.env } ), startDaemon - ], - cb - ) + ] + }, cb) }) } diff --git a/test/tap/add-remote-git.js b/test/tap/add-remote-git.js index 325ac2ed3..269d0cac7 100644 --- a/test/tap/add-remote-git.js +++ b/test/tap/add-remote-git.js @@ -1,26 +1,25 @@ -var fs = require("fs") -var resolve = require("path").resolve +var fs = require('fs') +var resolve = require('path').resolve -var chain = require("slide").chain -var osenv = require("osenv") -var mkdirp = require("mkdirp") -var rimraf = require("rimraf") -var test = require("tap").test +var osenv = require('osenv') +var mkdirp = require('mkdirp') +var rimraf = require('rimraf') +var test = require('tap').test -var npm = require("../../lib/npm.js") -var common = require("../common-tap.js") +var npm = require('../../lib/npm.js') +var common = require('../common-tap.js') -var pkg = resolve(__dirname, "add-remote-git") -var repo = resolve(__dirname, "add-remote-git-repo") +var pkg = resolve(__dirname, 'add-remote-git') +var repo = resolve(__dirname, 'add-remote-git-repo') var daemon var daemonPID var git -test("setup", function (t) { +test('setup', function (t) { bootstrap() setup(function (er, r) { - t.ifError(er, "git started up successfully") + t.ifError(er, 'git started up successfully') if (!er) { daemon = r[r.length - 2] @@ -31,17 +30,17 @@ test("setup", function (t) { }) }) -test("install from repo", function (t) { +test('install from repo', function (t) { process.chdir(pkg) - npm.commands.install(".", [], function (er) { - t.ifError(er, "npm installed via git") + npm.commands.install('.', [], function (er) { + t.ifError(er, 'npm installed via git') t.end() }) }) -test("clean", function (t) { - daemon.on("close", function () { +test('clean', function (t) { + daemon.on('close', function () { cleanup() t.end() }) @@ -49,77 +48,67 @@ test("clean", function (t) { }) var pjParent = JSON.stringify({ - name : "parent", - version : "1.2.3", - dependencies : { - "child" : "git://localhost:1234/child.git" + name: 'parent', + version: '1.2.3', + dependencies: { + child: 'git://localhost:1234/child.git' } -}, null, 2) + "\n" +}, null, 2) + '\n' var pjChild = JSON.stringify({ - name : "child", - version : "1.0.3" -}, null, 2) + "\n" + name: 'child', + version: '1.0.3' +}, null, 2) + '\n' function bootstrap () { mkdirp.sync(pkg) - fs.writeFileSync(resolve(pkg, "package.json"), pjParent) + fs.writeFileSync(resolve(pkg, 'package.json'), pjParent) } function setup (cb) { mkdirp.sync(repo) - fs.writeFileSync(resolve(repo, "package.json"), pjChild) - npm.load({ registry : common.registry, loglevel : "silent" }, function () { - git = require("../../lib/utils/git.js") + fs.writeFileSync(resolve(repo, 'package.json'), pjChild) + npm.load({ registry: common.registry, loglevel: 'silent' }, function () { + git = require('../../lib/utils/git.js') function startDaemon (cb) { // start git server var d = git.spawn( [ - "daemon", - "--verbose", - "--listen=localhost", - "--export-all", - "--base-path=.", - "--port=1234" + 'daemon', + '--verbose', + '--listen=localhost', + '--export-all', + '--base-path=.', + '--port=1234' ], { - cwd : pkg, - env : process.env, - stdio : ["pipe", "pipe", "pipe"] + cwd: pkg, + env: process.env, + stdio: ['pipe', 'pipe', 'pipe'] } ) - d.stderr.on("data", childFinder) + d.stderr.on('data', childFinder) function childFinder (c) { var cpid = c.toString().match(/^\[(\d+)\]/) if (cpid[1]) { - this.removeListener("data", childFinder) + this.removeListener('data', childFinder) cb(null, [d, cpid[1]]) } } } - var opts = { - cwd : repo, - env : process.env - } - - chain( - [ - git.chainableExec(["init"], opts), - git.chainableExec(["config", "user.name", "PhantomFaker"], opts), - git.chainableExec(["config", "user.email", "nope@not.real"], opts), - git.chainableExec(["add", "package.json"], opts), - git.chainableExec(["commit", "-m", "stub package"], opts), + common.makeGitRepo({ + path: repo, + commands: [ git.chainableExec( - ["clone", "--bare", repo, "child.git"], - { cwd : pkg, env : process.env } + ['clone', '--bare', repo, 'child.git'], + { cwd: pkg, env: process.env } ), startDaemon - ], - cb - ) + ] + }, cb) }) } diff --git a/test/tap/git-dependency-install-link.js b/test/tap/git-dependency-install-link.js index 2d382dd56..92938b342 100644 --- a/test/tap/git-dependency-install-link.js +++ b/test/tap/git-dependency-install-link.js @@ -1,7 +1,6 @@ var fs = require('fs') var resolve = require('path').resolve -var chain = require('slide').chain var osenv = require('osenv') var mkdirp = require('mkdirp') var rimraf = require('rimraf') @@ -154,26 +153,16 @@ function setup (cb) { } } - var opts = { - cwd: repo, - env: process.env - } - - chain( - [ - git.chainableExec(['init'], opts), - git.chainableExec(['config', 'user.name', 'PhantomFaker'], opts), - git.chainableExec(['config', 'user.email', 'nope@not.real'], opts), - git.chainableExec(['add', 'package.json'], opts), - git.chainableExec(['commit', '-m', 'stub package'], opts), + common.makeGitRepo({ + path: repo, + commands: [ git.chainableExec( ['clone', '--bare', repo, 'child.git'], { cwd: pkg, env: process.env } ), startDaemon - ], - cb - ) + ] + }, cb) }) } diff --git a/test/tap/version-message-config.js b/test/tap/version-message-config.js index 7ce3b4c6b..fca0d5d9a 100644 --- a/test/tap/version-message-config.js +++ b/test/tap/version-message-config.js @@ -2,7 +2,6 @@ var common = require('../common-tap.js') var fs = require('fs') var path = require('path') -var chain = require('slide').chain var mkdirp = require('mkdirp') var osenv = require('osenv') var rimraf = require('rimraf') @@ -25,44 +24,33 @@ test('npm version <semver> with message config', function (t) { npm.load({ prefix: pkg, userconfig: npmrc }, function () { var git = require('../../lib/utils/git.js') - var opts = { cwd: pkg, env: { PATH: process.env.PATH } } - chain( - [ - git.chainableExec(['init'], opts), - git.chainableExec(['config', 'user.name', 'PhantomFaker'], opts), - git.chainableExec(['config', 'user.email', 'nope@not.real'], opts), - git.chainableExec(['add', 'package.json'], opts), - git.chainableExec(['add', '.npmrc'], opts), - git.chainableExec(['commit', '-m', 'stub package'], opts) - ], - function (er) { - t.ifErr(er, 'git bootstrap ran without error') + common.makeGitRepo({ path: pkg }, function (er) { + t.ifErr(er, 'git bootstrap ran without error') - common.npm( - [ - 'version', - 'patch', - '--loglevel', 'silent' - // package config is picked up from env - ], - opts, - function (err, code, stdout, stderr) { - t.ifError(err, 'npm version ran without issue') - t.notOk(code, 'exited with a non-error code') - t.notOk(stderr, 'no error output') + common.npm( + [ + 'version', + 'patch', + '--loglevel', 'silent' + // package config is picked up from env + ], + { cwd: pkg, env: { PATH: process.env.PATH } }, + function (err, code, stdout, stderr) { + t.ifError(err, 'npm version ran without issue') + t.notOk(code, 'exited with a non-error code') + t.notOk(stderr, 'no error output') - git.whichAndExec( - ['log'], - { cwd: pkg, env: process.env }, - function (er, log, stderr) { - t.ok(log.match(/:bookmark: 0\.1\.3/g), 'config was picked up by version') - t.end() - } - ) - } - ) - } - ) + git.whichAndExec( + ['log'], + { cwd: pkg, env: process.env }, + function (er, log, stderr) { + t.ok(log.match(/:bookmark: 0\.1\.3/g), 'config was picked up by version') + t.end() + } + ) + } + ) + }) }) }) diff --git a/test/tap/version-update-shrinkwrap.js b/test/tap/version-update-shrinkwrap.js index 204c7323b..acf6cca8e 100644 --- a/test/tap/version-update-shrinkwrap.js +++ b/test/tap/version-update-shrinkwrap.js @@ -1,230 +1,115 @@ -var common = require("../common-tap.js") -var test = require("tap").test -var npm = require("../../") -var osenv = require("osenv") -var path = require("path") -var fs = require("fs") -var rimraf = require("rimraf") -var mkdirp = require("mkdirp") -var which = require("which") -var spawn = require("child_process").spawn - -var pkg = path.resolve(__dirname, "version-shrinkwrap") -var cache = path.resolve(pkg, "cache") - -test("npm version <semver> updates shrinkwrap - no git", function (t) { +var fs = require('fs') +var path = require('path') + +var mkdirp = require('mkdirp') +var osenv = require('osenv') +var rimraf = require('rimraf') +var test = require('tap').test + +var npm = require('../../') +var common = require('../common-tap.js') + +var pkg = path.resolve(__dirname, 'version-shrinkwrap') +var cache = path.resolve(pkg, 'cache') + +test('npm version <semver> updates shrinkwrap - no git', function (t) { setup() - npm.load({ cache: pkg + "/cache", registry: common.registry }, function () { - npm.commands.version(["patch"], function(err) { - if (err) return t.fail("Error perform version patch") - var shrinkwrap = require(path.resolve(pkg, "npm-shrinkwrap.json")) - t.equal(shrinkwrap.version, "0.0.1", "got expected version") + npm.load({ cache: pkg + '/cache', registry: common.registry }, function () { + npm.commands.version(['patch'], function (err) { + if (err) return t.fail('Error perform version patch') + var shrinkwrap = require(path.resolve(pkg, 'npm-shrinkwrap.json')) + t.equal(shrinkwrap.version, '0.0.1', 'got expected version') t.end() }) }) }) -test("npm version <semver> updates git works with no shrinkwrap", function (t) { +test('npm version <semver> updates git works with no shrinkwrap', function (t) { setup() + rimraf.sync(path.resolve(pkg, 'npm-shrinkwrap.json')) - rimraf.sync(path.resolve(pkg, "npm-shrinkwrap.json")) - - var opts = { - cache : cache, - registry : common.registry - } - npm.load(opts, function () { - npm.config.set("sign-git-tag", false) - which("git", function (err, git) { - if (err) t.fail("Git not installed, or which git command error") + npm.config.set('sign-git-tag', false) - initRepo() + common.makeGitRepo({ + path: pkg, + added: ['package.json'] + }, version) - function initRepo () { - var init = spawn(git, ["init"]) - init.stdout.pipe(process.stdout) - init.on("exit", function (code) { - t.notOk(code, "git init exited without issue") - - configName() - }) - } - - function configName () { - var namer = spawn(git, ["config", "user.name", "Phantom Faker"]) - namer.stdout.pipe(process.stdout) - namer.on("exit", function (code) { - t.notOk(code, "git config user.name exited without issue") - - configEmail() - }) - } + function version (er, stdout, stderr) { + t.ifError(er, 'git repo initialized without issue') + t.notOk(stderr, 'no error output') - function configEmail () { - var emailer = spawn(git, ["config", "user.email", "nope@not.real"]) - emailer.stdout.pipe(process.stdout) - emailer.on("exit", function (code) { - t.notOk(code, "git config user.email exited without issue") - - addAll() - }) - } - - function addAll () { - var emailer = spawn(git, ["add", "package.json"]) - emailer.stdout.pipe(process.stdout) - emailer.on("exit", function (code) { - t.notOk(code, "git add package.json exited without issue") - - commit() - }) - - } + npm.commands.version(['patch'], checkCommit) + } - function commit () { - var emailer = spawn(git, ["commit", "-m", "test setup"]) - emailer.stdout.pipe(process.stdout) - emailer.on("exit", function (code) { - t.notOk(code, "git commit -m 'test setup' exited without issue") + function checkCommit (er) { + t.ifError(er, 'version command ran without error') - version() - }) + var shrinkwrap = require(path.resolve(pkg, 'npm-shrinkwrap.json')) + t.equal(shrinkwrap.version, '0.0.1', 'got expected version') - } + var opts = { cwd: pkg, env: { PATH: process.env.PATH }} + var git = require('../../lib/utils/git.js') + git.whichAndExec( + ['show', 'HEAD', '--name-only'], + opts, + function (er, stdout, stderr) { + t.ifError(er, 'git show ran without issues') + t.notOk(stderr, 'no error output') - function version () { - npm.commands.version(["patch"], checkCommit) - } + var lines = stdout.split('\n') + t.notEqual(lines.indexOf('package.json'), -1, 'package.json commited') + t.equal(lines.indexOf('npm-shrinkwrap.json'), -1, 'npm-shrinkwrap.json not present') - function checkCommit (er) { - t.ifError(er, "version command ran without error") - - var shrinkwrap = require(path.resolve(pkg, "npm-shrinkwrap.json")) - t.equal(shrinkwrap.version, "0.0.1", "got expected version") - - var shower = spawn(git, ["show", "HEAD", "--name-only"]) - var out = "", eout = "" - shower.stdout.on("data", function (d) { - out += d.toString() - }) - shower.stderr.on("data", function (d) { - eout += d.toString() - }) - shower.on("exit", function (code) { - t.notOk(code, "git show HEAD exited without issue") - t.notOk(err, "git show produced no error output") - - var lines = out.split("\n") - t.notEqual(lines.indexOf("package.json"), -1, "package.json commited") - t.equal(lines.indexOf("npm-shrinkwrap.json"), -1, "npm-shrinkwrap.json not present") - - t.end() - }) + t.end() } - }) - }) + ) + } }) -test("npm version <semver> updates shrinkwrap and updates git", function (t) { +test('npm version <semver> updates shrinkwrap and updates git', function (t) { setup() - var opts = { - cache : cache, - registry : common.registry - } - npm.load(opts, function () { - npm.config.set("sign-git-tag", false) - which("git", function (err, git) { - t.ifError(err, "git found") - - initRepo() - - function initRepo () { - var init = spawn(git, ["init"]) - init.stdout.pipe(process.stdout) - init.on("exit", function (code) { - t.notOk(code, "git init exited without issue") - - configName() - }) - } - - function configName () { - var namer = spawn(git, ["config", "user.name", "Phantom Faker"]) - namer.stdout.pipe(process.stdout) - namer.on("exit", function (code) { - t.notOk(code, "git config user.name exited without issue") - - configEmail() - }) - } - - function configEmail () { - var emailer = spawn(git, ["config", "user.email", "nope@not.real"]) - emailer.stdout.pipe(process.stdout) - emailer.on("exit", function (code) { - t.notOk(code, "git config user.email exited without issue") - - addAll() - }) - } + npm.config.set('sign-git-tag', false) - function addAll () { - var emailer = spawn(git, ["add", "package.json", "npm-shrinkwrap.json"]) - emailer.stdout.pipe(process.stdout) - emailer.on("exit", function (code) { - t.notOk(code, "git add package.json npm-shrinkwrap.json exited without issue") + common.makeGitRepo({ + path: pkg, + added: ['package.json', 'npm-shrinkwrap.json'] + }, version) - commit() - }) + function version (er, stdout, stderr) { + t.ifError(er, 'git repo initialized without issue') + t.notOk(stderr, 'no error output') - } + npm.commands.version(['patch'], checkCommit) + } - function commit () { - var emailer = spawn(git, ["commit", "-m", "test setup"]) - emailer.stdout.pipe(process.stdout) - emailer.on("exit", function (code) { - t.notOk(code, "git commit -m 'test setup' exited without issue") + function checkCommit (er) { + t.ifError(er, 'version command ran without error') - version() - }) + var shrinkwrap = require(path.resolve(pkg, 'npm-shrinkwrap.json')) + t.equal(shrinkwrap.version, '0.0.1', 'got expected version') - } + var git = require('../../lib/utils/git.js') + var opts = { cwd: pkg, env: { PATH: process.env.PATH }} + git.whichAndExec( + ['show', 'HEAD', '--name-only'], + opts, + function (er, stdout, stderr) { + t.ifError(er, 'git show ran without issues') + t.notOk(stderr, 'no error output') - function version () { - npm.commands.version(["patch"], checkCommit) - } + var lines = stdout.split('\n') + t.notEqual(lines.indexOf('package.json'), -1, 'package.json commited') + t.notEqual(lines.indexOf('npm-shrinkwrap.json'), -1, 'npm-shrinkwrap.json commited') - function checkCommit (er) { - t.ifError(er, "version command ran without error") - - var shrinkwrap = require(path.resolve(pkg, "npm-shrinkwrap.json")) - t.equal(shrinkwrap.version, "0.0.1", "got expected version") - - var shower = spawn(git, ["show", "HEAD", "--name-only"]) - var out = "", eout = "" - shower.stdout.on("data", function (d) { - out += d.toString() - }) - shower.stderr.on("data", function (d) { - eout += d.toString() - }) - shower.on("exit", function (code) { - t.notOk(code, "git show HEAD exited without issue") - t.notOk(err, "git show produced no error output") - - var lines = out.split("\n") - t.notEqual(lines.indexOf("package.json"), -1, "package.json commited") - t.notEqual(lines.indexOf("npm-shrinkwrap.json"), -1, "npm-shrinkwrap.json commited") - - t.end() - }) + t.end() } - }) - }) + ) + } }) -test("cleanup", function(t) { +test('cleanup', function (t) { // windows fix for locked files process.chdir(osenv.tmpdir()) @@ -232,18 +117,18 @@ test("cleanup", function(t) { t.end() }) -function setup() { +function setup () { rimraf.sync(pkg) mkdirp.sync(pkg) mkdirp.sync(cache) var contents = { - author: "Nathan Bowser && Faiq Raza", - name: "version-with-shrinkwrap-test", - version: "0.0.0", - description: "Test for version with shrinkwrap update" + author: 'Nathan Bowser && Faiq Raza', + name: 'version-with-shrinkwrap-test', + version: '0.0.0', + description: 'Test for version with shrinkwrap update' } - fs.writeFileSync(path.resolve(pkg, "package.json"), JSON.stringify(contents), "utf8") - fs.writeFileSync(path.resolve(pkg, "npm-shrinkwrap.json"), JSON.stringify(contents), "utf8") + fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify(contents), 'utf8') + fs.writeFileSync(path.resolve(pkg, 'npm-shrinkwrap.json'), JSON.stringify(contents), 'utf8') process.chdir(pkg) } |