• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var mkdirp = require('mkdirp')
2var osenv = require('osenv')
3var path = require('path')
4var rimraf = require('rimraf')
5var test = require('tap').test
6var writeFileSync = require('fs').writeFileSync
7
8var common = require('../common-tap.js')
9
10var link = path.join(__dirname, 'rmlinked')
11var linkDep = path.join(link, 'node_modules', 'baz')
12var linkInstall = path.join(__dirname, 'rmlinked-install')
13var linkRoot = path.join(__dirname, 'rmlinked-root')
14
15var config = 'prefix = ' + linkRoot
16var configPath = path.join(link, '_npmrc')
17
18var OPTS = {
19  env: {
20    'npm_config_userconfig': configPath
21  }
22}
23
24var linkedJSON = {
25  name: 'foo',
26  version: '1.0.0',
27  description: '',
28  main: 'index.js',
29  scripts: {
30    test: 'echo "Error: no test specified" && exit 1'
31  },
32  dependencies: {
33    'baz': '1.0.0'
34  },
35  author: '',
36  license: 'ISC'
37}
38
39var linkedDepJSON = {
40  name: 'baz',
41  version: '1.0.0',
42  description: '',
43  main: 'index.js',
44  scripts: {
45    test: 'echo "Error: no test specified" && exit 1'
46  },
47  author: '',
48  license: 'ISC'
49}
50
51var installJSON = {
52  name: 'bar',
53  version: '1.0.0',
54  description: '',
55  main: 'index.js',
56  scripts: {
57    test: 'echo "Error: no test specified" && exit 1'
58  },
59  dependencies: {
60    'foo': '1.0.0'
61  },
62  author: '',
63  license: 'ISC'
64}
65
66test('setup', function (t) {
67  setup()
68  common.npm(['ls', '-g', '--depth=0'], OPTS, function (err, code, stdout, stderr) {
69    if (err) throw err
70    t.comment(stdout)
71    t.comment(stderr)
72    t.is(code, 0, 'ls -g')
73    t.notMatch(stdout, /UNMET DEPENDENCY foo@/, "foo isn't in global")
74    t.end()
75  })
76})
77
78test('creates global link', function (t) {
79  process.chdir(link)
80  common.npm(['link'], OPTS, function (err, code, stdout, stderr) {
81    if (err) throw err
82    t.is(code, 0, 'link')
83    t.comment(stdout)
84    t.comment(stderr)
85    common.npm(['ls', '-g'], OPTS, function (err, code, stdout, stderr) {
86      if (err) throw err
87      t.comment(stdout)
88      t.comment(stderr)
89      t.is(code, 0, 'ls -g')
90      t.equal(stderr, '', 'got expected stderr')
91      t.match(stdout, /foo@1.0.0/, 'creates global link ok')
92      t.end()
93    })
94  })
95})
96
97test('uninstall the global linked package', function (t) {
98  process.chdir(osenv.tmpdir())
99  common.npm(['uninstall', '-g', 'foo'], OPTS, function (err, code, stdout, stderr) {
100    if (err) throw err
101    t.is(code, 0, 'uninstall -g foo')
102    t.comment(stdout)
103    t.comment(stderr)
104    process.chdir(link)
105    common.npm(['ls'], OPTS, function (err, code, stdout) {
106      if (err) throw err
107      t.is(code, 0, 'ls')
108      t.comment(stdout)
109      t.comment(stderr)
110      t.match(stdout, /baz@1.0.0/, "uninstall didn't remove dep")
111      t.end()
112    })
113  })
114})
115
116test('cleanup', function (t) {
117  cleanup()
118  t.end()
119})
120
121function cleanup () {
122  process.chdir(osenv.tmpdir())
123  try { rimraf.sync(linkRoot) } catch (e) { }
124  try { rimraf.sync(linkDep) } catch (e) { }
125  try { rimraf.sync(link) } catch (e) { }
126  try { rimraf.sync(linkInstall) } catch (e) { }
127}
128
129function setup () {
130  cleanup()
131  mkdirp.sync(linkRoot)
132  mkdirp.sync(link)
133  writeFileSync(
134    path.join(link, 'package.json'),
135    JSON.stringify(linkedJSON, null, 2)
136  )
137  mkdirp.sync(linkDep)
138  writeFileSync(
139    path.join(linkDep, 'package.json'),
140    JSON.stringify(linkedDepJSON, null, 2)
141  )
142  mkdirp.sync(linkInstall)
143  writeFileSync(
144    path.join(linkInstall, 'package.json'),
145    JSON.stringify(installJSON, null, 2)
146  )
147  writeFileSync(configPath, config)
148}
149