• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// verify that prepublish runs on install, pack, and publish
2var common = require('../common-tap')
3var test = require('tap').test
4var fs = require('graceful-fs')
5var join = require('path').join
6var mkdirp = require('mkdirp')
7var rimraf = require('rimraf')
8
9var pkg = common.pkg
10var tmp = join(pkg, 'tmp')
11var cache = common.cache
12
13test('setup', function (t) {
14  var n = 0
15  cleanup()
16  mkdirp(pkg, then())
17  mkdirp(cache, then())
18  mkdirp(tmp, then())
19  function then () {
20    n++
21    return function (er) {
22      if (er) throw er
23      if (--n === 0) next()
24    }
25  }
26
27  function next () {
28    fs.writeFile(join(pkg, 'package.json'), JSON.stringify({
29      name: 'npm-test-prepublish',
30      version: '1.2.5',
31      scripts: { prepublish: 'echo ok' }
32    }), 'ascii', function (er) {
33      if (er) throw er
34
35      t.pass('setup done')
36      t.end()
37    })
38  }
39})
40
41test('prepublish deprecation warning on `npm pack`', function (t) {
42  var env = {
43    'npm_config_cache': cache,
44    'npm_config_tmp': tmp,
45    'npm_config_prefix': pkg,
46    'npm_config_global': 'false'
47  }
48
49  for (var i in process.env) {
50    if (!/^npm_config_/.test(i)) {
51      env[i] = process.env[i]
52    }
53  }
54
55  common.npm([
56    'pack',
57    '--loglevel', 'warn'
58  ], { cwd: pkg, env: env }, function (err, code, stdout, stderr) {
59    t.equal(code, 0, 'pack finished successfully')
60    t.ifErr(err, 'pack finished successfully')
61
62    t.match(stderr, /`prepublish` scripts are deprecated/)
63    var c = stdout.trim()
64    var regex = new RegExp('' +
65      '> npm-test-prepublish@1.2.5 prepublish [^\\r\\n]+\\r?\\n' +
66      '> echo ok\\r?\\n' +
67      '\\r?\\n' +
68      'ok\\r?\\n' +
69      'npm-test-prepublish-1.2.5.tgz', 'ig')
70
71    t.match(c, regex)
72    t.end()
73  })
74})
75
76test('prepublish deprecation warning on `npm install`', function (t) {
77  var env = {
78    'npm_config_cache': cache,
79    'npm_config_tmp': tmp,
80    'npm_config_prefix': pkg,
81    'npm_config_global': 'false'
82  }
83
84  for (var i in process.env) {
85    if (!/^npm_config_/.test(i)) {
86      env[i] = process.env[i]
87    }
88  }
89
90  common.npm([
91    'install',
92    '--loglevel', 'warn'
93  ], { cwd: pkg, env: env }, function (err, code, stdout, stderr) {
94    t.equal(code, 0, 'pack finished successfully')
95    t.ifErr(err, 'pack finished successfully')
96
97    t.match(stderr, /`prepublish` scripts are deprecated/)
98    var c = stdout.trim()
99    var regex = new RegExp('' +
100      '> npm-test-prepublish@1.2.5 prepublish [^\\r\\n]+\\r?\\n' +
101      '> echo ok\\r?\\n' +
102      '\\r?\\n' +
103      'ok' +
104      '', 'ig')
105
106    t.match(c, regex)
107    t.end()
108  })
109})
110test('cleanup', function (t) {
111  cleanup()
112  t.pass('cleaned up')
113  t.end()
114})
115
116function cleanup () {
117  rimraf.sync(pkg)
118}
119