• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var path = require('path')
3var test = require('tap').test
4var fs = require('graceful-fs')
5var mkdirp = require('mkdirp')
6var rimraf = require('rimraf')
7var base = path.join(__dirname, path.basename(__filename, '.js'))
8var common = require('../common-tap.js')
9
10function fixturepath () {
11  return path.join(base, path.join.apply(path, arguments))
12}
13
14function File (contents) {
15  return {
16    type: 'file',
17    contents: typeof contents === 'object'
18      ? JSON.stringify(contents)
19      : contents
20  }
21}
22
23function Dir (contents) {
24  return {
25    type: 'dir',
26    contents: contents || {}
27  }
28}
29
30function createFixtures (filename, fixture) {
31  if (fixture.type === 'dir') {
32    mkdirp.sync(filename)
33    Object.keys(fixture.contents).forEach(function (content) {
34      createFixtures(path.resolve(filename, content), fixture.contents[content])
35    })
36  } else if (fixture.type === 'file') {
37    fs.writeFileSync(filename, fixture.contents)
38  } else {
39    throw new Error('Unknown fixture type: ' + fixture.type)
40  }
41}
42
43var fixtures = Dir({
44// The fixture modules
45  'moda@1.0.1': Dir({
46    'package.json': File({
47      name: 'moda',
48      version: '1.0.1'
49    })
50  }),
51  'modb@1.0.0': Dir({
52    'package.json': File({
53      name: 'modb',
54      version: '1.0.0',
55      bundleDependencies: ['modc'],
56      dependencies: {
57        modc: fixturepath('modc@1.0.0')
58      }
59    })
60  }),
61  'modc@1.0.0': Dir({
62    'package.json': File({
63      name: 'modc',
64      version: '1.0.0'
65    })
66  }),
67  // The local config
68  'package.json': File({
69    dependencies: {
70      moda: fixturepath('moda@1.0.1'),
71      modb: fixturepath('modb@1.0.0')
72    }
73  }),
74  'node_modules': Dir({
75    'moda': Dir({
76      'package.json': File({
77        name: 'moda',
78        version: '1.0.0',
79        _requested: { rawSpec: fixturepath('moda@1.0.0') },
80        dependencies: {
81          modb: fixturepath('modb@1.0.0')
82        },
83        bundleDependencies: [ 'modb' ]
84      })
85    })
86  })
87})
88
89function setup () {
90  cleanup()
91  createFixtures(base, fixtures)
92}
93
94function cleanup () {
95  rimraf.sync(base)
96}
97
98function exists (t, filepath, msg) {
99  try {
100    fs.statSync(filepath)
101    t.pass(msg)
102    return true
103  } catch (ex) {
104    t.fail(msg, {found: null, wanted: 'exists', compare: 'fs.stat(' + filepath + ')'})
105    return false
106  }
107}
108
109test('setup', function (t) {
110  setup()
111  common.npm('install', {cwd: fixturepath('modb@1.0.0')}, function (err, code, stdout, stderr) {
112    if (err) throw err
113    t.is(code, 0, 'modb')
114    common.npm('install', {
115      cwd: fixturepath('node_modules', 'moda')
116    }, function (err, code, stdout, stderr) {
117      if (err) throw err
118      t.is(code, 0, 'installed moda')
119      t.done()
120    })
121  })
122})
123
124test('no-clobber', function (t) {
125  common.npm('install', {cwd: base}, function (err, code, stdout, stderr) {
126    if (err) throw err
127    t.is(code, 0, 'installed ok')
128    exists(t, fixturepath('node_modules', 'moda'), 'moda')
129    exists(t, fixturepath('node_modules', 'modb'), 'modb')
130    exists(t, fixturepath('node_modules', 'modb', 'node_modules', 'modc'), 'modc')
131    t.done()
132  })
133})
134
135test('cleanup', function (t) {
136  cleanup()
137  t.pass()
138  t.done()
139})
140