• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var common = require('../common-tap.js')
2
3var resolve = require('path').resolve
4var fs = require('graceful-fs')
5var test = require('tap').test
6var mkdirp = require('mkdirp')
7var rimraf = require('rimraf')
8var isWindows = require('../../lib/utils/is-windows.js')
9
10var pkg = resolve(common.pkg, 'package')
11var dep = resolve(common.pkg, 'test-linked')
12var glb = resolve(common.pkg, 'test-global')
13var lnk = resolve(common.pkg, 'test-global-link')
14
15var EXEC_OPTS = { cwd: pkg }
16
17var index = "module.exports = function () { console.log('whoop whoop') }"
18
19var fixture = {
20  name: '@test/linked',
21  version: '1.0.0',
22  bin: {
23    linked: './index.js'
24  }
25}
26
27test('setup', function (t) {
28  cleanup()
29  setup()
30
31  t.end()
32})
33
34test('install and link', function (t) {
35  common.npm(
36    [
37      '--global',
38      '--prefix', lnk,
39      '--loglevel', 'silent',
40      '--json',
41      'install', '../test-linked'
42    ],
43    EXEC_OPTS,
44    function (er, code, stdout, stderr) {
45      t.ifError(er, "test-linked install didn't explode")
46      t.notOk(code, 'test-linked install also failed')
47      t.notOk(stderr, 'no log output')
48
49      verify(t, stdout)
50
51      // again, to make sure unlinking works properlyt
52      common.npm(
53        [
54          '--global',
55          '--prefix', lnk,
56          '--loglevel', 'silent',
57          '--json',
58          'install', '../test-linked'
59        ],
60        EXEC_OPTS,
61        function (er, code, stdout, stderr) {
62          t.ifError(er, "test-linked install didn't explode")
63          t.notOk(code, 'test-linked install also failed')
64          t.notOk(stderr, 'no log output')
65
66          verify(t, stdout)
67
68          fs.readdir(pkg, function (er, files) {
69            t.ifError(er, 'package directory is still there')
70            t.deepEqual(files, ['node_modules'], 'only stub modules dir remains')
71
72            t.end()
73          })
74        }
75      )
76    }
77  )
78})
79
80test('cleanup', function (t) {
81  cleanup()
82
83  t.end()
84})
85
86function resolvePath () {
87  return resolve.apply(null, Array.prototype.slice.call(arguments)
88    .filter(function (arg) { return arg }))
89}
90
91function verify (t, stdout) {
92  var result = JSON.parse(stdout)
93  var pkgPath = resolvePath(lnk, !isWindows && 'lib', 'node_modules', '@test', 'linked')
94  if (result.added.length) {
95    t.is(result.added.length, 1, 'added the module')
96    t.is(result.added[0].path, pkgPath, 'in the right location')
97  } else {
98    t.is(result.updated.length, 1, 'updated the module')
99    t.is(result.updated[0].path, pkgPath, 'in the right location')
100  }
101}
102
103function cleanup () {
104  rimraf.sync(pkg)
105  rimraf.sync(dep)
106  rimraf.sync(lnk)
107  rimraf.sync(glb)
108}
109
110function setup () {
111  mkdirp.sync(pkg)
112  mkdirp.sync(glb)
113  fs.symlinkSync(glb, lnk, 'junction')
114  // so it doesn't try to install into npm's own node_modules
115  mkdirp.sync(resolve(pkg, 'node_modules'))
116  mkdirp.sync(dep)
117  fs.writeFileSync(resolve(dep, 'package.json'), JSON.stringify(fixture))
118  fs.writeFileSync(resolve(dep, 'index.js'), index)
119}
120