summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/test/tap/bundled-dependencies.js
blob: c6d67e3c503a57a70835e6c3840a93843f9540c3 (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
'use strict'
var test = require('tap').test
var common = require('../common-tap.js')
var path = require('path')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var fs = require('graceful-fs')
var tar = require('tar')
var basepath = path.resolve(__dirname, path.basename(__filename, '.js'))
var fixturepath = path.resolve(basepath, 'npm-test-bundled-deps')
var targetpath = path.resolve(basepath, 'target')
var Tacks = require('tacks')
var File = Tacks.File
var Dir = Tacks.Dir

test('basic bundling', function (t) {
  var fixture = new Tacks(
    Dir({
      'package.json': File({
        name: 'npm-test-files',
        version: '1.2.5',
        bundledDependencies: [
          'addme'
        ]
      }),
      node_modules: Dir({
        addme: Dir({
          'index.js': File('')
        }),
        iggyme: Dir({
          'index.js': File('')
        })
      })
    })
  )
  withFixture(t, fixture, function (done) {
    t.ok(fileExists('node_modules/addme'), 'bundled dep included')
    t.notOk(fileExists('node_modules/iggyme'), 'unspecified dep not included')
    done()
  })
})

test('scoped dep bundling', function (t) {
  var fixture = new Tacks(
    Dir({
      'package.json': File({
        name: 'npm-test-files',
        version: '1.2.5',
        bundledDependencies: [
          '@foo/addme'
        ]
      }),
      node_modules: Dir({
        '@foo': Dir({
          addme: Dir({
            'index.js': File('')
          }),
          iggyme: Dir({
            'index.js': File('')
          })
        })
      })
    })
  )
  withFixture(t, fixture, function (done) {
    t.ok(fileExists('node_modules/@foo/addme'), 'bundled dep included')
    t.notOk(
      fileExists('node_modules/@foo/iggyme'),
      'unspecified dep not included')
    done()
  })
})

function fileExists (file) {
  try {
    return !!fs.statSync(path.resolve(targetpath, 'package', file))
  } catch (_) {
    return false
  }
}

function withFixture (t, fixture, tester) {
  fixture.create(fixturepath)
  mkdirp.sync(targetpath)
  common.npm(['pack', fixturepath], {cwd: basepath}, extractAndCheck)
  function extractAndCheck (err, code) {
    if (err) throw err
    t.is(code, 0, 'pack went ok')
    extractTarball(checkTests)
  }
  function checkTests (err) {
    if (err) throw err
    tester(removeAndDone)
  }
  function removeAndDone (err) {
    if (err) throw err
    fixture.remove(fixturepath)
    rimraf.sync(basepath)
    t.done()
  }
}

function extractTarball (cb) {
  // Unpack to disk so case-insensitive filesystems are consistent
  tar.extract({
    file: path.join(basepath, 'npm-test-files-1.2.5.tgz'),
    cwd: targetpath
  }).then(cb, cb)
}