• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2const path = require('path')
3const test = require('tap').test
4const Tacks = require('tacks')
5const File = Tacks.File
6const Dir = Tacks.Dir
7const common = require('../common-tap.js')
8const fs = require('fs')
9
10const basedir = common.pkg
11const testdir = path.join(basedir, 'testdir')
12const cachedir = common.cache
13const globaldir = path.join(basedir, 'global')
14const tmpdir = path.join(basedir, 'tmp')
15const optionaldir = path.join(testdir, 'optional')
16const devdir = path.join(testdir, 'dev')
17
18const env = common.newEnv().extend({
19  npm_config_cache: cachedir,
20  npm_config_tmp: tmpdir,
21  npm_config_prefix: globaldir,
22  npm_config_registry: common.registry,
23  npm_config_loglevel: 'error'
24})
25
26/**
27 * NOTE: Tarball Fixtures
28 * They contain package.json files with dependencies like the following:
29 * a-1.0.0.tgz: package/package.json
30 * {
31 *   "name":"a",
32 *   "version":"1.0.0",
33 *   "dependencies":{
34 *     "b":"./b-1.0.0.tgz"
35 *   }
36 * }
37 * example-1.0.0.tgz: package/package.json
38 * {
39 *   "name":"example",
40 *   "version":"1.0.0",
41 *   "dependencies":{
42 *     "a":"./a-1.0.0.tgz",
43 *     "b":"./b-1.0.0.tgz"
44 *   }
45 * }
46 */
47const fixture = new Tacks(Dir({
48  cache: Dir(),
49  global: Dir(),
50  tmp: Dir(),
51  testdir: Dir({
52    'a-1.0.0.tgz': File(Buffer.from(
53      '1f8b0800000000000003edcfc10a83300c06e09df71492f35653567bf06d' +
54      'a2067163b558b7c3c4775f54f0e4654c18837e973f4da0249eca1bd59cfa' +
55      '25d535b4eeb03344b4c6245bfd8946995d328b5a5b3bd55264464beebdc8' +
56      '9647e8a99355befd67b92559f34f0ce0e8ce9003c1099edc85a675f2d20a' +
57      '154aa762cfae6257361c201fa090994a8bf33c577dfd82713cfefa86288a' +
58      'a2e8736f68a0ff4400080000',
59      'hex'
60    )),
61    'b-1.0.0.tgz': File(Buffer.from(
62      '1f8b08000000000000032b484cce4e4c4fd52f80d07a59c5f9790c540606' +
63      '06066626260ad8c4c1c0d85c81c1d8d4ccc0d0d0cccc00a80ec830353103' +
64      'd2d4760836505a5c925804740aa5e640bca200a78708a856ca4bcc4d55b2' +
65      '524a52d2512a4b2d2acecccf03f20cf50cf40c946ab906da79a360148c82' +
66      '51300a680400106986b400080000',
67      'hex'
68    )),
69    dev: Dir({
70      'package.json': File({
71        name: 'dev',
72        version: '1.0.0',
73        devDependencies: {
74          example: '../example-1.0.0.tgz'
75        }
76      })
77    }),
78    'example-1.0.0.tgz': File(Buffer.from(
79      '1f8b0800000000000003ed8fc10a83300c863def2924e7ada6587bf06daa' +
80      '06719bb55837c6c4775fa6307670a70963d0ef92f02584fcce94275353e2' +
81      '962a8ebeb3d1c620a2562a5ef34f64aae328cd344aa935f21e379962875b' +
82      '3fb2c6c50fa6e757bebdb364895ff54f18c19a962007ba99d69d09f670a5' +
83      'de379d6527050a645391235b912d1bf2908f607826127398e762a8efbc53' +
84      'ccae7873d3b4fb75ba402010087ce2014747c9d500080000',
85      'hex'
86    )),
87    optional: Dir({
88      'package.json': File({
89        name: 'optional',
90        version: '1.0.0',
91        optionalDependencies: {
92          example: '../example-1.0.0.tgz'
93        }
94      })
95    })
96  })
97}))
98
99function setup () {
100  cleanup()
101  fixture.create(basedir)
102}
103
104function cleanup () {
105  fixture.remove(basedir)
106}
107
108test('setup', function (t) {
109  setup()
110  return common.fakeRegistry.listen()
111})
112
113test('optional dependency identification', function (t) {
114  return common.npm(['install', '--no-optional'], {cwd: optionaldir, env}).then(([code, stdout, stderr]) => {
115    t.is(code, 0, 'no error code')
116    t.is(stderr, '', 'no error output')
117    t.notOk(fs.existsSync(path.join(optionaldir, 'node_modules')), 'did not install anything')
118    t.similar(JSON.parse(fs.readFileSync(path.join(optionaldir, 'package-lock.json'), 'utf8')), {
119      dependencies: {
120        a: {
121          version: 'file:../a-1.0.0.tgz',
122          optional: true
123        },
124        b: {
125          version: 'file:../b-1.0.0.tgz',
126          optional: true
127        },
128        example: {
129          version: 'file:../example-1.0.0.tgz',
130          optional: true
131        }
132      }
133    }, 'locks dependencies as optional')
134  })
135})
136
137test('development dependency identification', function (t) {
138  return common.npm(['install', '--only=prod'], {cwd: devdir, env}).then(([code, stdout, stderr]) => {
139    t.is(code, 0, 'no error code')
140    t.is(stderr, '', 'no error output')
141    t.notOk(fs.existsSync(path.join(devdir, 'node_modules')), 'did not install anything')
142    t.similar(JSON.parse(fs.readFileSync(path.join(devdir, 'package-lock.json'), 'utf8')), {
143      dependencies: {
144        a: {
145          version: 'file:../a-1.0.0.tgz',
146          dev: true
147        },
148        b: {
149          version: 'file:../b-1.0.0.tgz',
150          dev: true
151        },
152        example: {
153          version: 'file:../example-1.0.0.tgz',
154          dev: true
155        }
156      }
157    }, 'locks dependencies as dev')
158  })
159})
160
161test('default dependency identification', function (t) {
162  return common.npm(['install'], {cwd: optionaldir, env}).then(([code, stdout, stderr]) => {
163    t.is(code, 0, 'no error code')
164    t.is(stderr, '', 'no error output')
165    t.similar(JSON.parse(fs.readFileSync(path.join(optionaldir, 'package-lock.json'), 'utf8')), {
166      dependencies: {
167        a: {
168          version: 'file:../a-1.0.0.tgz',
169          optional: true
170        },
171        b: {
172          version: 'file:../b-1.0.0.tgz',
173          optional: true
174        },
175        example: {
176          version: 'file:../example-1.0.0.tgz',
177          optional: true
178        }
179      }
180    }, 'locks dependencies as optional')
181  })
182})
183
184test('cleanup', function (t) {
185  common.fakeRegistry.close()
186  cleanup()
187  t.done()
188})
189