summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/version-from-git.js
blob: 6f2c794ce2b9d3ed66a12c6402a67ff5caf1afe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var common = require('../common-tap.js')
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('../../lib/npm.js')

var pkg = path.resolve(__dirname, 'version-from-git')
var packagePath = path.resolve(pkg, 'package.json')
var cache = path.resolve(pkg, 'cache')

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

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

  function runVersion (er) {
    t.ifError(er, 'git tag ran without error')
    npm.config.set('sign-git-tag', false)
    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 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'
  setup()
  createTag(t, version, runVersion)

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

  function checkManifest (er) {
    t.ifError(er, 'npm run version ran without error')
    fs.readFile(path.resolve(pkg, 'package.json'), 'utf8', function (er, data) {
      t.ifError(er, 'read manifest without error')
      var manifest = JSON.parse(data)
      t.equal(manifest.version, version, 'updated the package.json version')
      t.done()
    })
  }
})

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()
  createTag(t, tag, runVersion)

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

  function checkVersion (er) {
    t.equal(er.message, 'Version not changed')
    t.done()
  }
})

test('npm version from-git with an invalid version tag', function (t) {
  var tag = 'invalidversion'
  setup()
  createTag(t, tag, runVersion)

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

  function checkVersion (er) {
    t.equal(er.message, tag + ' is not a valid version')
    t.done()
  }
})

test('npm version from-git without any versions', function (t) {
  setup()
  createGitRepo(t, runVersion)

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

  function checkVersion (er) {
    t.equal(er.message, 'No tags found')
    t.done()
  }
})

test('cleanup', function (t) {
  cleanup()
  t.end()
})

function cleanup () {
  // windows fix for locked files
  process.chdir(osenv.tmpdir())
  rimraf.sync(pkg)
}

function setup () {
  cleanup()
  mkdirp.sync(cache)
  process.chdir(pkg)
  fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8')
}

function createGitRepo (t, cb) {
  npm.load({ cache: cache }, function (er) {
    t.ifError(er, 'npm load ran without issue')
    common.makeGitRepo({
      path: pkg,
      added: ['package.json']
    }, cb)
  })
}

function createTag (t, tag, cb) {
  var opts = { cwd: pkg, env: { PATH: process.env.PATH } }
  npm.load({ cache: cache }, function (er) {
    t.ifError(er, 'npm load ran without issue')

    // git must be called after npm.load because it uses config
    var git = require('../../lib/utils/git.js')
    common.makeGitRepo({
      path: pkg,
      added: ['package.json'],
      commands: [git.chainableExec(['tag', tag, '-am', tag], opts)]
    }, cb)
  })
}