Skip to content

Commit

Permalink
version: strip tag-version-prefix from 'from-git' target tag
Browse files Browse the repository at this point in the history
PR-URL: npm#10717
Credit: @ekmartin
  • Loading branch information
ekmartin authored and iarna committed Jan 21, 2016
1 parent 9ed2849 commit e9f1ad8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ function parseLastGitTag (cb) {
}

var tag = stdout.trim()
var prefix = npm.config.get('tag-version-prefix')
// Strip the prefix from the start of the tag:
if (tag.indexOf(prefix) === 0) tag = tag.slice(prefix.length)
var version = semver.valid(tag)
if (!version) return cb(new Error(tag + ' is not a valid version'))
cb(null, version)
Expand Down
74 changes: 68 additions & 6 deletions test/tap/version-from-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ var cache = path.resolve(pkg, 'cache')

var json = { name: 'cat', version: '0.1.2' }

test('npm version from-git with a valid tag creates new commit', function (t) {
test('npm version from-git with a valid tag creates a new commit', function (t) {
var version = '1.2.3'
var tag = 'v' + version
setup()
createTag(t, tag, runVersion)
createTag(t, version, runVersion)

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
Expand All @@ -40,16 +39,15 @@ test('npm version from-git with a valid tag creates new commit', function (t) {
function checkCommit (er, log, stderr) {
t.ifError(er, 'git log ran without issue')
t.notOk(stderr, 'no error output')
t.ok(log.indexOf(version) !== -1, 'commited from subdirectory')
t.ok(log.indexOf(version) !== -1, 'commit was created')
t.end()
}
})

test('npm version from-git with a valid tag updates the package.json version', function (t) {
var version = '1.2.3'
var tag = 'v' + version
setup()
createTag(t, tag, runVersion)
createTag(t, version, runVersion)

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
Expand All @@ -68,6 +66,70 @@ test('npm version from-git with a valid tag updates the package.json version', f
}
})

test('npm version from-git strips tag-version-prefix', function (t) {
var version = '1.2.3'
var prefix = 'custom-'
var tag = prefix + version
setup()
createTag(t, tag, runVersion)

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-tag', false)
npm.config.set('tag-version-prefix', prefix)
npm.commands.version(['from-git'], checkVersion)
}

function checkVersion (er) {
var git = require('../../lib/utils/git.js')
t.ifError(er, 'version command ran without error')
git.whichAndExec(
['log', '--pretty=medium'],
{ cwd: pkg, env: process.env },
checkCommit
)
}

function checkCommit (er, log, stderr) {
t.ifError(er, 'git log ran without issue')
t.notOk(stderr, 'no error output')
t.ok(log.indexOf(tag) === -1, 'commit should not include prefix')
t.ok(log.indexOf(version) !== -1, 'commit should include version')
t.end()
}
})

test('npm version from-git only strips tag-version-prefix if it is a prefix', function (t) {
var prefix = 'test'
var version = '1.2.3-' + prefix
setup()
createTag(t, version, runVersion)

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-tag', false)
npm.config.set('tag-version-prefix', prefix)
npm.commands.version(['from-git'], checkVersion)
}

function checkVersion (er) {
var git = require('../../lib/utils/git.js')
t.ifError(er, 'version command ran without error')
git.whichAndExec(
['log'],
{ cwd: pkg, env: process.env },
checkCommit
)
}

function checkCommit (er, log, stderr) {
t.ifError(er, 'git log ran without issue')
t.notOk(stderr, 'no error output')
t.ok(log.indexOf(version) !== -1, 'commit should include the full version')
t.end()
}
})

test('npm version from-git with an existing version', function (t) {
var tag = 'v' + json.version
setup()
Expand Down

0 comments on commit e9f1ad8

Please sign in to comment.