summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/gently-rm-symlinked-global-dir.js
blob: 0a27dae5c5e9555e553b8f09876be754f99c5120 (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
var common = require('../common-tap.js')

var resolve = require('path').resolve
var fs = require('graceful-fs')
var test = require('tap').test
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var isWindows = require('../../lib/utils/is-windows.js')

var pkg = resolve(common.pkg, 'package')
var dep = resolve(common.pkg, 'test-linked')
var glb = resolve(common.pkg, 'test-global')
var lnk = resolve(common.pkg, 'test-global-link')

var EXEC_OPTS = { cwd: pkg }

var index = "module.exports = function () { console.log('whoop whoop') }"

var fixture = {
  name: '@test/linked',
  version: '1.0.0',
  bin: {
    linked: './index.js'
  }
}

test('setup', function (t) {
  cleanup()
  setup()

  t.end()
})

test('install and link', function (t) {
  common.npm(
    [
      '--global',
      '--prefix', lnk,
      '--loglevel', 'silent',
      '--json',
      'install', '../test-linked'
    ],
    EXEC_OPTS,
    function (er, code, stdout, stderr) {
      t.ifError(er, "test-linked install didn't explode")
      t.notOk(code, 'test-linked install also failed')
      t.notOk(stderr, 'no log output')

      verify(t, stdout)

      // again, to make sure unlinking works properlyt
      common.npm(
        [
          '--global',
          '--prefix', lnk,
          '--loglevel', 'silent',
          '--json',
          'install', '../test-linked'
        ],
        EXEC_OPTS,
        function (er, code, stdout, stderr) {
          t.ifError(er, "test-linked install didn't explode")
          t.notOk(code, 'test-linked install also failed')
          t.notOk(stderr, 'no log output')

          verify(t, stdout)

          fs.readdir(pkg, function (er, files) {
            t.ifError(er, 'package directory is still there')
            t.deepEqual(files, ['node_modules'], 'only stub modules dir remains')

            t.end()
          })
        }
      )
    }
  )
})

test('cleanup', function (t) {
  cleanup()

  t.end()
})

function resolvePath () {
  return resolve.apply(null, Array.prototype.slice.call(arguments)
    .filter(function (arg) { return arg }))
}

function verify (t, stdout) {
  var result = JSON.parse(stdout)
  var pkgPath = resolvePath(lnk, !isWindows && 'lib', 'node_modules', '@test', 'linked')
  if (result.added.length) {
    t.is(result.added.length, 1, 'added the module')
    t.is(result.added[0].path, pkgPath, 'in the right location')
  } else {
    t.is(result.updated.length, 1, 'updated the module')
    t.is(result.updated[0].path, pkgPath, 'in the right location')
  }
}

function cleanup () {
  rimraf.sync(pkg)
  rimraf.sync(dep)
  rimraf.sync(lnk)
  rimraf.sync(glb)
}

function setup () {
  mkdirp.sync(pkg)
  mkdirp.sync(glb)
  fs.symlinkSync(glb, lnk, 'junction')
  // so it doesn't try to install into npm's own node_modules
  mkdirp.sync(resolve(pkg, 'node_modules'))
  mkdirp.sync(dep)
  fs.writeFileSync(resolve(dep, 'package.json'), JSON.stringify(fixture))
  fs.writeFileSync(resolve(dep, 'index.js'), index)
}