1var common = require('../common-tap.js') 2var fs = require('fs') 3var path = require('path') 4 5var mkdirp = require('mkdirp') 6var rimraf = require('rimraf') 7var test = require('tap').test 8 9var npm = require('../../lib/npm.js') 10 11var pkg = common.pkg 12var packagePath = path.resolve(pkg, 'package.json') 13var cache = common.cache 14 15var json = { name: 'cat', version: '0.1.2' } 16 17test('npm version from-git with a valid tag creates a new commit', function (t) { 18 var version = '1.2.3' 19 setup() 20 createTag(t, version, runVersion) 21 22 function runVersion (er) { 23 t.ifError(er, 'git tag ran without error') 24 npm.config.set('sign-git-commit', false) 25 npm.config.set('sign-git-tag', false) 26 npm.commands.version(['from-git'], checkVersion) 27 } 28 29 function checkVersion (er) { 30 var git = require('../../lib/utils/git.js') 31 t.ifError(er, 'version command ran without error') 32 git.whichAndExec( 33 ['log'], 34 { cwd: pkg, env: process.env }, 35 checkCommit 36 ) 37 } 38 39 function checkCommit (er, log, stderr) { 40 t.ifError(er, 'git log ran without issue') 41 t.notOk(stderr, 'no error output') 42 t.ok(log.indexOf(version) !== -1, 'commit was created') 43 t.end() 44 } 45}) 46 47test('npm version from-git with a valid tag updates the package.json version', function (t) { 48 var version = '1.2.3' 49 setup() 50 createTag(t, version, runVersion) 51 52 function runVersion (er) { 53 t.ifError(er, 'git tag ran without error') 54 npm.config.set('sign-git-commit', false) 55 npm.config.set('sign-git-tag', false) 56 npm.commands.version(['from-git'], checkManifest) 57 } 58 59 function checkManifest (er) { 60 t.ifError(er, 'npm run version ran without error') 61 fs.readFile(path.resolve(pkg, 'package.json'), 'utf8', function (er, data) { 62 t.ifError(er, 'read manifest without error') 63 var manifest = JSON.parse(data) 64 t.equal(manifest.version, version, 'updated the package.json version') 65 t.done() 66 }) 67 } 68}) 69 70test('npm version from-git strips tag-version-prefix', function (t) { 71 var version = '1.2.3' 72 var prefix = 'custom-' 73 var tag = prefix + version 74 setup() 75 createTag(t, tag, runVersion) 76 77 function runVersion (er) { 78 t.ifError(er, 'git tag ran without error') 79 npm.config.set('sign-git-commit', false) 80 npm.config.set('sign-git-tag', false) 81 npm.config.set('tag-version-prefix', prefix) 82 npm.commands.version(['from-git'], checkVersion) 83 } 84 85 function checkVersion (er) { 86 var git = require('../../lib/utils/git.js') 87 t.ifError(er, 'version command ran without error') 88 git.whichAndExec( 89 ['log', '--pretty=medium'], 90 { cwd: pkg, env: process.env }, 91 checkCommit 92 ) 93 } 94 95 function checkCommit (er, log, stderr) { 96 t.ifError(er, 'git log ran without issue') 97 t.notOk(stderr, 'no error output') 98 t.ok(log.indexOf(tag) === -1, 'commit should not include prefix') 99 t.ok(log.indexOf(version) !== -1, 'commit should include version') 100 t.end() 101 } 102}) 103 104test('npm version from-git only strips tag-version-prefix if it is a prefix', function (t) { 105 var prefix = 'test' 106 var version = '1.2.3-' + prefix 107 setup() 108 createTag(t, version, runVersion) 109 110 function runVersion (er) { 111 t.ifError(er, 'git tag ran without error') 112 npm.config.set('sign-git-commit', false) 113 npm.config.set('sign-git-tag', false) 114 npm.config.set('tag-version-prefix', prefix) 115 npm.commands.version(['from-git'], checkVersion) 116 } 117 118 function checkVersion (er) { 119 var git = require('../../lib/utils/git.js') 120 t.ifError(er, 'version command ran without error') 121 git.whichAndExec( 122 ['log'], 123 { cwd: pkg, env: process.env }, 124 checkCommit 125 ) 126 } 127 128 function checkCommit (er, log, stderr) { 129 t.ifError(er, 'git log ran without issue') 130 t.notOk(stderr, 'no error output') 131 t.ok(log.indexOf(version) !== -1, 'commit should include the full version') 132 t.end() 133 } 134}) 135 136test('npm version from-git with an existing version', function (t) { 137 var tag = 'v' + json.version 138 setup() 139 createTag(t, tag, runVersion) 140 141 function runVersion (er) { 142 t.ifError(er, 'git tag ran without error') 143 npm.config.set('sign-git-commit', false) 144 npm.config.set('sign-git-tag', false) 145 npm.commands.version(['from-git'], checkVersion) 146 } 147 148 function checkVersion (er) { 149 t.like(er.message, /Version not changed/) 150 t.done() 151 } 152}) 153 154test('npm version from-git with an invalid version tag', function (t) { 155 var tag = 'invalidversion' 156 setup() 157 createTag(t, tag, runVersion) 158 159 function runVersion (er) { 160 t.ifError(er, 'git tag ran without error') 161 npm.config.set('sign-git-commit', false) 162 npm.config.set('sign-git-tag', false) 163 npm.commands.version(['from-git'], checkVersion) 164 } 165 166 function checkVersion (er) { 167 t.equal(er.message, tag + ' is not a valid version') 168 t.done() 169 } 170}) 171 172test('npm version from-git without any versions', function (t) { 173 setup() 174 createGitRepo(t, runVersion) 175 176 function runVersion (er) { 177 t.ifError(er, 'created git repo without errors') 178 npm.config.set('sign-git-commit', false) 179 npm.config.set('sign-git-tag', false) 180 npm.commands.version(['from-git'], checkVersion) 181 } 182 183 function checkVersion (er) { 184 t.equal(er.message, 'No tags found') 185 t.done() 186 } 187}) 188 189function setup () { 190 process.chdir(__dirname) 191 rimraf.sync(pkg) 192 mkdirp.sync(pkg) 193 process.chdir(pkg) 194 fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8') 195} 196 197function createGitRepo (t, cb) { 198 npm.load({ cache: cache }, function (er) { 199 t.ifError(er, 'npm load ran without issue') 200 common.makeGitRepo({ 201 path: pkg, 202 added: ['package.json'] 203 }, cb) 204 }) 205} 206 207function createTag (t, tag, cb) { 208 var opts = { cwd: pkg, env: { PATH: process.env.PATH } } 209 npm.load({ cache: cache }, function (er) { 210 t.ifError(er, 'npm load ran without issue') 211 212 // git must be called after npm.load because it uses config 213 var git = require('../../lib/utils/git.js') 214 common.makeGitRepo({ 215 path: pkg, 216 added: ['package.json'], 217 commands: [git.chainableExec(['tag', tag, '-am', tag], opts)] 218 }, cb) 219 }) 220} 221