summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/lifecycle-INIT_CWD.js
blob: eec5c266eec98d28f00abd6655ab633c693318ec (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
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 common = require('../common-tap.js')

var pkg = path.resolve(__dirname, 'lifecycle-initcwd')
var subdir = path.resolve(pkg, 'subdir')

var json = {
  name: 'init-cwd',
  version: '1.0.0',
  scripts: {
    initcwd: 'echo "$INIT_CWD"'
  }
}

test('setup', function (t) {
  cleanup()
  mkdirp.sync(pkg)
  mkdirp.sync(subdir)
  fs.writeFileSync(
    path.join(pkg, 'package.json'),
    JSON.stringify(json, null, 2)
  )

  process.chdir(subdir)
  t.end()
})

test('make sure the env.INIT_CWD is correct', function (t) {
  common.npm(['run-script', 'initcwd'], {
    cwd: subdir
  }, function (er, code, stdout) {
    if (er) throw er
    t.equal(code, 0, 'exit code')
    stdout = stdout.trim().split(/\r|\n/).pop()
    var actual = stdout

    t.equal(actual, subdir)
    t.end()
  })
})

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

function cleanup () {
  process.chdir(osenv.tmpdir())
  rimraf.sync(subdir)
  rimraf.sync(pkg)
}