• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var Bluebird = require('bluebird')
3var test = require('tap').test
4var path = require('path')
5var mkdirp = require('mkdirp')
6var rimraf = require('rimraf')
7var fs = require('graceful-fs')
8var common = require('../common-tap')
9
10var base = common.pkg
11var modA = path.resolve(base, 'modA')
12var modB = path.resolve(base, 'modB')
13var modC = path.resolve(base, 'modC')
14var testNormal = path.resolve(base, 'testNormal')
15var testGlobal = path.resolve(base, 'testGlobal')
16var testLegacy = path.resolve(base, 'testLegacy')
17
18var json = {
19  'name': 'test-tree-style',
20  'version': '1.0.0',
21  'dependencies': {
22    'modA': modA + '-1.0.0.tgz'
23  }
24}
25
26var modAJson = {
27  'name': 'modA',
28  'version': '1.0.0',
29  'dependencies': {
30    'modB': modB + '-1.0.0.tgz'
31  }
32}
33
34var modBJson = {
35  'name': 'modB',
36  'version': '1.0.0',
37  'dependencies': {
38    'modC': modC + '-1.0.0.tgz'
39  }
40}
41
42var modCJson = {
43  'name': 'modC',
44  'version': '1.0.0'
45}
46
47function modJoin () {
48  var modules = Array.prototype.slice.call(arguments)
49  return modules.reduce(function (a, b) {
50    return path.resolve(a, 'node_modules', b)
51  })
52}
53
54function writeJson (mod, data) {
55  fs.writeFileSync(path.resolve(mod, 'package.json'), JSON.stringify(data))
56}
57
58function setup () {
59  cleanup()
60  ;[modA, modB, modC, testNormal, testGlobal, testLegacy].forEach(function (mod) {
61    mkdirp.sync(mod)
62  })
63  writeJson(modA, modAJson)
64  writeJson(modB, modBJson)
65  writeJson(modC, modCJson)
66  ;[testNormal, testGlobal, testLegacy].forEach(function (mod) { writeJson(mod, json) })
67}
68
69function cleanup () {
70  rimraf.sync(base)
71}
72
73test('setup', function (t) {
74  setup()
75  return Bluebird.try(() => {
76    return common.npm(['pack', 'file:modC'], {cwd: base})
77  }).spread((code) => {
78    t.is(code, 0, 'pack modC')
79    return common.npm(['pack', 'file:modB'], {cwd: base})
80  }).spread((code) => {
81    t.is(code, 0, 'pack modB')
82    return common.npm(['pack', 'file:modA'], {cwd: base})
83  }).spread((code) => {
84    t.is(code, 0, 'pack modA')
85  })
86})
87
88function exists (t, filepath, msg) {
89  try {
90    fs.statSync(filepath)
91    t.pass(msg)
92    return true
93  } catch (ex) {
94    t.fail(msg, {found: null, wanted: 'exists', compare: 'fs.stat(' + filepath + ')'})
95    return false
96  }
97}
98
99test('tree-style', function (t) {
100  t.plan(12)
101  common.npm(['install'], {cwd: testNormal}, function (err, code, stdout, stderr) {
102    if (err) throw err
103    t.is(code, 0, 'normal install; result code')
104    t.is(stderr, '', 'normal install; no errors')
105    exists(t, modJoin(testNormal, 'modA'), 'normal install; module A')
106    exists(t, modJoin(testNormal, 'modB'), 'normal install; module B')
107    exists(t, modJoin(testNormal, 'modC'), 'normal install; module C')
108  })
109  common.npm(['install', '--global-style'], {cwd: testGlobal}, function (err, code, stdout, stderr) {
110    if (err) throw err
111    t.is(code, 0, 'global-style install; result code')
112    t.is(stderr, '', 'global-style install; no errors')
113    exists(t, modJoin(testGlobal, 'modA', 'modB'), 'global-style install; module B')
114    exists(t, modJoin(testGlobal, 'modA', 'modC'), 'global-style install; module C')
115  })
116  common.npm(['install', '--legacy-bundling'], {cwd: testLegacy}, function (err, code, stdout, stderr) {
117    if (err) throw err
118    t.is(code, 0, 'legacy-bundling install; result code')
119    t.is(stderr, '', 'legacy-bundling install; no errors')
120    exists(t, modJoin(testLegacy, 'modA', 'modB', 'modC'), 'legacy-bundling install; module C')
121  })
122})
123
124test('cleanup', function (t) {
125  cleanup()
126  t.end()
127})
128