Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
npm version: handle checkGit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Dec 22, 2014
1 parent c62be42 commit 6880233
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/version.js
Expand Up @@ -54,6 +54,8 @@ function version (args, silent, cb_) {
data.version = newVersion

checkGit(function (er, hasGit) {
if (er) return cb_(er)

write(data, "package.json", function (er) {
if (er) return cb_(er)

Expand Down
78 changes: 78 additions & 0 deletions test/tap/version-git-not-clean.js
@@ -0,0 +1,78 @@
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-git-not-clean")
var cache = path.resolve(pkg, "cache")

test("npm version <semver> with working directory not clean", function (t) {
setup()
npm.load({ cache: cache, registry: common.registry}, function () {
which("git", function (err, git) {
t.ifError(err, "git found on system")
function gitInit(_cb) {
var child = spawn(git, ["init"])
var out = ""
child.stdout.on("data", function (d) {
out += d.toString()
})
child.on("exit", function () {
return _cb(out)
})
}
function addPackageJSON(_cb) {
var data = JSON.stringify({ name: "blah", version: "0.1.2" })
fs.writeFile("package.json", data, function() {
var child = spawn(git, ["add", "package.json"])
child.on("exit", function () {
var child2 = spawn(git, ["commit", "package.json", "-m", "init"])
var out = ""
child2.stdout.on("data", function (d) {
out += d.toString()
})
child2.on("exit", function () {
return _cb(out)
})
})
})
}

gitInit(function() {
addPackageJSON(function() {
var data = JSON.stringify({ name: "blah", version: "0.1.3" })
fs.writeFile("package.json", data, function() {
npm.commands.version(["patch"], function (err) {
if (!err) {
t.fail("should fail on non-clean working directory")
} else {
t.equal(err.message, "Git working directory not clean.\nM package.json")
}
t.end()
})
})
})
})
})
})
})

test("cleanup", function (t) {
// windows fix for locked files
process.chdir(osenv.tmpdir())

rimraf.sync(pkg)
t.end()
})

function setup() {
mkdirp.sync(pkg)
mkdirp.sync(cache)
process.chdir(pkg)
}

0 comments on commit 6880233

Please sign in to comment.