summaryrefslogtreecommitdiff
path: root/deps/npm/test/need-npm5-update/move-no-clobber-dest-node-modules.js
blob: a00c720243ed7c3c47e20abb246cb5d43128d56f (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
var path = require('path')
var test = require('tap').test
var fs = require('graceful-fs')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var base = path.join(__dirname, path.basename(__filename, '.js'))
var common = require('../common-tap.js')

function fixturepath () {
  return path.join(base, path.join.apply(path, arguments))
}

function File (contents) {
  return {
    type: 'file',
    contents: typeof contents === 'object'
      ? JSON.stringify(contents)
      : contents
  }
}

function Dir (contents) {
  return {
    type: 'dir',
    contents: contents || {}
  }
}

function createFixtures (filename, fixture) {
  if (fixture.type === 'dir') {
    mkdirp.sync(filename)
    Object.keys(fixture.contents).forEach(function (content) {
      createFixtures(path.resolve(filename, content), fixture.contents[content])
    })
  } else if (fixture.type === 'file') {
    fs.writeFileSync(filename, fixture.contents)
  } else {
    throw new Error('Unknown fixture type: ' + fixture.type)
  }
}

var fixtures = Dir({
// The fixture modules
  'moda@1.0.1': Dir({
    'package.json': File({
      name: 'moda',
      version: '1.0.1'
    })
  }),
  'modb@1.0.0': Dir({
    'package.json': File({
      name: 'modb',
      version: '1.0.0',
      bundleDependencies: ['modc'],
      dependencies: {
        modc: fixturepath('modc@1.0.0')
      }
    })
  }),
  'modc@1.0.0': Dir({
    'package.json': File({
      name: 'modc',
      version: '1.0.0'
    })
  }),
  // The local config
  'package.json': File({
    dependencies: {
      moda: fixturepath('moda@1.0.1'),
      modb: fixturepath('modb@1.0.0')
    }
  }),
  'node_modules': Dir({
    'moda': Dir({
      'package.json': File({
        name: 'moda',
        version: '1.0.0',
        _requested: { rawSpec: fixturepath('moda@1.0.0') },
        dependencies: {
          modb: fixturepath('modb@1.0.0')
        },
        bundleDependencies: [ 'modb' ]
      })
    })
  })
})

function setup () {
  cleanup()
  createFixtures(base, fixtures)
}

function cleanup () {
  rimraf.sync(base)
}

function exists (t, filepath, msg) {
  try {
    fs.statSync(filepath)
    t.pass(msg)
    return true
  } catch (ex) {
    t.fail(msg, {found: null, wanted: 'exists', compare: 'fs.stat(' + filepath + ')'})
    return false
  }
}

test('setup', function (t) {
  setup()
  common.npm('install', {cwd: fixturepath('modb@1.0.0')}, function (err, code, stdout, stderr) {
    if (err) throw err
    t.is(code, 0, 'modb')
    common.npm('install', {
      cwd: fixturepath('node_modules', 'moda')
    }, function (err, code, stdout, stderr) {
      if (err) throw err
      t.is(code, 0, 'installed moda')
      t.done()
    })
  })
})

test('no-clobber', function (t) {
  common.npm('install', {cwd: base}, function (err, code, stdout, stderr) {
    if (err) throw err
    t.is(code, 0, 'installed ok')
    exists(t, fixturepath('node_modules', 'moda'), 'moda')
    exists(t, fixturepath('node_modules', 'modb'), 'modb')
    exists(t, fixturepath('node_modules', 'modb', 'node_modules', 'modc'), 'modc')
    t.done()
  })
})

test('cleanup', function (t) {
  cleanup()
  t.pass()
  t.done()
})