summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/version-lifecycle.js
blob: 5d78b71d5934310ad70bd3db5782cac99d1e2d7b (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
var fs = require('graceful-fs')
var path = require('path')

var mkdirp = require('mkdirp')
var osenv = require('osenv')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap.js')
var npm = require('../../')
var pkg = path.resolve(__dirname, 'version-lifecycle')
var cache = path.resolve(pkg, 'cache')
var npmrc = path.resolve(pkg, './.npmrc')
var configContents = 'sign-git-tag=false\n'

test('npm version <semver> with failing preversion lifecycle script', function (t) {
  setup()
  fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify({
    author: 'Alex Wolfe',
    name: 'version-lifecycle',
    version: '0.0.0',
    description: 'Test for npm version if preversion script fails',
    scripts: {
      preversion: './fail.sh'
    }
  }), 'utf8')
  fs.writeFileSync(path.resolve(pkg, 'fail.sh'), 'exit 50', 'utf8')
  fs.chmodSync(path.resolve(pkg, 'fail.sh'), 448)
  npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
    var version = require('../../lib/version')
    version(['patch'], function (err) {
      t.ok(err)
      t.ok(err.message.match(/Exit status 50/))
      t.end()
    })
  })
})

test('npm version <semver> with failing version lifecycle script', function (t) {
  setup()
  fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify({
    author: 'Alex Wolfe',
    name: 'version-lifecycle',
    version: '0.0.0',
    description: 'Test for npm version if postversion script fails',
    scripts: {
      version: './fail.sh'
    }
  }), 'utf8')
  fs.writeFileSync(path.resolve(pkg, 'fail.sh'), 'exit 50', 'utf8')
  fs.chmodSync(path.resolve(pkg, 'fail.sh'), 448)
  npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
    var version = require('../../lib/version')
    version(['patch'], function (err) {
      t.ok(err)
      t.ok(err.message.match(/Exit status 50/))
      t.end()
    })
  })
})

test('npm version <semver> with failing postversion lifecycle script', function (t) {
  setup()
  fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify({
    author: 'Alex Wolfe',
    name: 'version-lifecycle',
    version: '0.0.0',
    description: 'Test for npm version if postversion script fails',
    scripts: {
      postversion: './fail.sh'
    }
  }), 'utf8')
  fs.writeFileSync(path.resolve(pkg, 'fail.sh'), 'exit 50', 'utf8')
  fs.chmodSync(path.resolve(pkg, 'fail.sh'), 448)
  npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
    var version = require('../../lib/version')
    version(['patch'], function (err) {
      t.ok(err)
      t.ok(err.message.match(/Exit status 50/))
      t.end()
    })
  })
})

test('npm version <semver> execution order', function (t) {
  setup()
  fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify({
    author: 'Alex Wolfe',
    name: 'version-lifecycle',
    version: '0.0.0',
    description: 'Test for npm version if postversion script fails',
    scripts: {
      preversion: './preversion.sh',
      version: './version.sh',
      postversion: './postversion.sh'
    }
  }), 'utf8')
  makeScript('preversion')
  makeScript('version')
  makeScript('postversion')
  npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
    common.makeGitRepo({path: pkg}, function (err, git) {
      t.ifError(err, 'git bootstrap ran without error')

      var version = require('../../lib/version')
      version(['patch'], function (err) {
        t.ifError(err, 'version command complete')

        t.equal('0.0.0', readPackage('preversion').version, 'preversion')
        t.deepEqual(readStatus('preversion', t), {
          'preversion-package.json': 'A'
        })

        t.equal('0.0.1', readPackage('version').version, 'version')
        t.deepEqual(readStatus('version', t), {
          'package.json': 'M',
          'preversion-package.json': 'A',
          'version-package.json': 'A'
        })

        t.equal('0.0.1', readPackage('postversion').version, 'postversion')
        t.deepEqual(readStatus('postversion', t), {
          'postversion-package.json': 'A'
        })
        t.end()
      })
    })
  })
})

test('cleanup', function (t) {
  process.chdir(osenv.tmpdir())
  rimraf.sync(pkg)
  t.end()
})

function setup () {
  mkdirp.sync(pkg)
  mkdirp.sync(path.join(pkg, 'node_modules'))
  mkdirp.sync(cache)
  fs.writeFileSync(npmrc, configContents, 'ascii')
  process.chdir(pkg)
}

function makeScript (lifecycle) {
  var contents = [
    'cp package.json ' + lifecycle + '-package.json',
    'git add ' + lifecycle + '-package.json',
    'git status --porcelain > ' + lifecycle + '-git.txt'
  ].join('\n')
  var scriptPath = path.join(pkg, lifecycle + '.sh')
  fs.writeFileSync(scriptPath, contents, 'utf-8')
  fs.chmodSync(scriptPath, 448)
}

function readPackage (lifecycle) {
  return JSON.parse(fs.readFileSync(path.join(pkg, lifecycle + '-package.json'), 'utf-8'))
}

function readStatus (lifecycle, t) {
  var status = {}
  fs.readFileSync(path.join(pkg, lifecycle + '-git.txt'), 'utf-8')
    .trim()
    .split('\n')
    .forEach(function (line) {
      line = line.trim()
      if (line && !line.match(/^\?\? /)) {
        var parts = line.split(/\s+/)
        t.equal(parts.length, 2, lifecycle + ' : git status has too many words : ' + line)
        status[parts[1].trim()] = parts[0].trim()
      }
    })
  return status
}