• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var fs = require('graceful-fs')
3var path = require('path')
4var test = require('tap').test
5var mr = require('npm-registry-mock')
6var Tacks = require('tacks')
7var File = Tacks.File
8var Dir = Tacks.Dir
9var common = require('../common-tap.js')
10
11var basedir = path.join(__dirname, path.basename(__filename, '.js'))
12var testdir = path.join(basedir, 'testdir')
13var cachedir = path.join(basedir, 'cache')
14var globaldir = path.join(basedir, 'global')
15var tmpdir = path.join(basedir, 'tmp')
16var appdir = path.join(testdir, 'app')
17var installedModuleA = path.join(appdir, 'node_modules', 'module-a')
18var installedModuleB = path.join(installedModuleA, 'node_modules', 'module-b')
19var installedModuleC = path.join(appdir, 'node_modules', 'module-c')
20var installedModuleD = path.join(appdir, 'node_modules', 'module-d')
21
22var conf = {
23  cwd: appdir,
24  env: Object.assign({}, process.env, {
25    npm_config_cache: cachedir,
26    npm_config_tmp: tmpdir,
27    npm_config_prefix: globaldir,
28    npm_config_registry: common.registry,
29    npm_config_loglevel: 'warn'
30  })
31}
32
33var server
34var fixture = new Tacks(Dir({
35  cache: Dir(),
36  global: Dir(),
37  tmp: Dir(),
38  testdir: Dir({
39    app: Dir({
40      'npm-shrinkwrap.json': File({
41        name: 'app',
42        version: '1.0.0',
43        dependencies: {
44          'module-a': {
45            version: '1.0.0',
46            from: '../module-a',
47            resolved: 'file:../module-a',
48            dependencies: {
49              'module-b': {
50                version: '1.0.0',
51                from: '../module-b',
52                resolved: 'file:../module-b'
53              }
54            }
55          }
56        }
57      }),
58      'package.json': File({
59        name: 'app',
60        version: '1.0.0',
61        dependencies: {
62          'module-a': '../module-a'
63        },
64        devDependencies: {
65          'module-d': '../module-d'
66        }
67      })
68    }),
69    'module-a': Dir({
70      'package.json': File({
71        name: 'module-a',
72        version: '1.0.0',
73        dependencies: {
74          'module-b': 'file:' + path.join(testdir, 'module-b'),
75          'module-c': 'file:' + path.join(testdir, 'module-c')
76        }
77      })
78    }),
79    'module-b': Dir({
80      'package.json': File({
81        name: 'module-b',
82        version: '1.0.0'
83      })
84    }),
85    'module-c': Dir({
86      'package.json': File({
87        name: 'module-c',
88        version: '1.0.0'
89      })
90    }),
91    'module-d': Dir({
92      'package.json': File({
93        name: 'module-d',
94        version: '1.0.0'
95      })
96    })
97  })
98}))
99
100function exists (t, filepath, msg) {
101  try {
102    fs.statSync(filepath)
103    t.pass(msg)
104    return true
105  } catch (ex) {
106    t.fail(msg, {found: ex, wanted: 'exists', compare: 'fs.stat(' + filepath + ')'})
107    return false
108  }
109}
110
111function notExists (t, filepath, msg) {
112  try {
113    fs.statSync(filepath)
114    t.fail(msg, {found: true, wanted: 'does not exist', compare: 'fs.stat(' + filepath + ')'})
115    return true
116  } catch (ex) {
117    t.pass(msg)
118    return false
119  }
120}
121
122function setup () {
123  cleanup()
124  fixture.create(basedir)
125}
126
127function cleanup () {
128  fixture.remove(basedir)
129}
130
131test('setup', function (t) {
132  setup()
133  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
134    if (err) throw err
135    server = s
136    t.done()
137  })
138})
139
140test('example', function (t) {
141  common.npm(['install'], conf, function (err, code, stdout, stderr) {
142    if (err) throw err
143    t.is(code, 0, 'command ran ok')
144    t.comment('1> ' + stdout.trim())
145    t.comment('2> ' + stderr.trim())
146    exists(t, installedModuleA, 'module-a: dep in shrinkwrap installed')
147    exists(t, installedModuleB, 'module-b: nested dep from shrinkwrap nested')
148    notExists(t, installedModuleC, 'module-c: dependency not installed because not in shrinkwrap')
149    exists(t, installedModuleD, 'module-d: dev dependency installed despite not being in shrinkwrap')
150    t.done()
151  })
152})
153
154test('cleanup', function (t) {
155  server.close()
156  cleanup()
157  t.done()
158})
159