1var common = require('../common-tap.js') 2var test = require('tap').test 3var npm = require('../../') 4var fs = require('fs') 5var which = require('which') 6var spawn = require('child_process').spawn 7 8var pkg = common.pkg 9var cache = common.cache 10 11test('npm version <semver> with working directory not clean', function (t) { 12 process.chdir(pkg) 13 npm.load({ cache: cache, registry: common.registry, prefix: pkg }, function () { 14 which('git', function (err, git) { 15 t.ifError(err, 'git found') 16 17 function addPackageJSON (_cb) { 18 var data = JSON.stringify({ name: 'blah', version: '0.1.2' }) 19 fs.writeFile('package.json', data, function () { 20 var child = spawn(git, ['add', 'package.json']) 21 child.on('exit', function () { 22 var child2 = spawn(git, ['commit', 'package.json', '-m', 'init']) 23 var out = '' 24 child2.stdout.on('data', function (d) { 25 out += d.toString() 26 }) 27 child2.on('exit', function () { 28 return _cb(out) 29 }) 30 }) 31 }) 32 } 33 34 common.makeGitRepo({path: pkg}, function () { 35 addPackageJSON(function () { 36 var data = JSON.stringify({ name: 'blah', version: '0.1.3' }) 37 fs.writeFile('package.json', data, function () { 38 npm.commands.version(['patch'], function (err) { 39 if (!err) { 40 t.fail('should fail on non-clean working directory') 41 } else { 42 t.ok(err.message.match(/Git working directory not clean./)) 43 t.ok(err.message.match(/M package.json/)) 44 } 45 t.end() 46 }) 47 }) 48 }) 49 }) 50 }) 51 }) 52}) 53 54test('npm version <semver> --force with working directory not clean', function (t) { 55 common.npm( 56 [ 57 '--force', 58 '--no-sign-git-commit', 59 '--no-sign-git-tag', 60 '--registry', common.registry, 61 '--prefix', pkg, 62 'version', 63 'patch' 64 ], 65 { cwd: pkg, env: {PATH: process.env.PATH} }, 66 function (err, code, stdout, stderr) { 67 t.ifError(err, 'npm version ran without issue') 68 t.notOk(code, 'exited with a non-error code') 69 var errorLines = stderr.trim().split('\n') 70 .map(function (line) { 71 return line.trim() 72 }) 73 .filter(function (line) { 74 return !line.indexOf('using --force') 75 }) 76 t.notOk(errorLines.length, 'no error output') 77 t.end() 78 }) 79}) 80