• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var fs = require('fs')
2var resolve = require('path').resolve
3
4var mkdirp = require('mkdirp')
5var rimraf = require('rimraf')
6var test = require('tap').test
7var readJson = require('read-package-json')
8var mr = require('npm-registry-mock')
9
10var npm = require('../../lib/npm.js')
11var common = require('../common-tap.js')
12
13var pkg = resolve(common.pkg, 'package')
14var repo = resolve(common.pkg, 'repo')
15var prefix = resolve(common.pkg, 'prefix')
16var cache = common.cache
17
18var daemon
19var daemonPID
20var git
21var mockRegistry
22
23var EXEC_OPTS = {
24  registry: common.registry,
25  cwd: pkg,
26  cache: cache
27}
28process.env.npm_config_prefix = prefix
29
30var pjParent = JSON.stringify({
31  name: 'parent',
32  version: '1.2.3',
33  dependencies: {
34    'child': 'git://localhost:' + common.gitPort + '/child.git'
35  }
36}, null, 2) + '\n'
37
38var pjChild = JSON.stringify({
39  name: 'child',
40  version: '1.0.3'
41}, null, 2) + '\n'
42
43test('setup', function (t) {
44  t.test('bootstrap', t => bootstrap(t.end))
45  t.test('setup', t => setup(function (er, r) {
46    t.ifError(er, 'git started up successfully')
47
48    if (!er) {
49      daemon = r[r.length - 2]
50      daemonPID = r[r.length - 1]
51    }
52
53    mr({
54      port: common.port
55    }, function (er, server) {
56      t.ifError(er, 'started mock registry')
57      mockRegistry = server
58
59      t.end()
60    })
61  }))
62  t.end()
63})
64
65test('install from git repo [no --link]', function (t) {
66  process.chdir(pkg)
67
68  common.npm(['install'], EXEC_OPTS, function (err, code, stdout, stderr) {
69    t.ifError(err, 'npm install failed')
70
71    t.dissimilar(stderr, /Command failed:/, 'expect git to succeed')
72    t.dissimilar(stderr, /version not found/, 'should not go to repository')
73
74    readJson(resolve(pkg, 'node_modules', 'child', 'package.json'), function (err, data) {
75      t.ifError(err, 'error reading child package.json')
76
77      t.equal(data && data.version, '1.0.3')
78      t.end()
79    })
80  })
81})
82
83test('install from git repo [with --link]', function (t) {
84  process.chdir(pkg)
85  rimraf.sync(resolve(pkg, 'node_modules'))
86
87  common.npm(['install', '--link'], EXEC_OPTS, function (err, code, stdout, stderr) {
88    t.ifError(err, 'npm install --link failed')
89    t.equal(code, 0, 'npm install --link returned non-0 code')
90
91    t.dissimilar(stderr, /Command failed:/, 'expect git to succeed')
92    t.dissimilar(stderr, /version not found/, 'should not go to repository')
93    t.equal(stderr, '', 'no actual output on stderr')
94
95    readJson(resolve(pkg, 'node_modules', 'child', 'package.json'), function (err, data) {
96      t.ifError(err, 'error reading child package.json')
97
98      t.equal(data && data.version, '1.0.3')
99      t.end()
100    })
101  })
102})
103
104test('clean', function (t) {
105  mockRegistry.close()
106  daemon.on('close', t.end)
107  process.kill(daemonPID)
108})
109
110function bootstrap (cb) {
111  rimraf(repo, () => {
112    rimraf(pkg, () => {
113      mkdirp.sync(pkg)
114      mkdirp.sync(cache)
115
116      fs.writeFileSync(resolve(pkg, 'package.json'), pjParent)
117      cb()
118    })
119  })
120}
121
122function setup (cb) {
123  mkdirp.sync(repo)
124  fs.writeFileSync(resolve(repo, 'package.json'), pjChild)
125  npm.load({
126    link: true,
127    prefix: pkg,
128    loglevel: 'silent'
129  }, function () {
130    git = require('../../lib/utils/git.js')
131
132    function startDaemon (cb) {
133      // start git server
134      var d = git.spawn(
135        [
136          'daemon',
137          '--verbose',
138          '--listen=localhost',
139          '--export-all',
140          '--base-path=.',
141          '--reuseaddr',
142          '--port=' + common.gitPort
143        ],
144        {
145          cwd: pkg,
146          env: process.env,
147          stdio: ['pipe', 'pipe', 'pipe']
148        }
149      )
150      d.stderr.on('data', childFinder)
151
152      function childFinder (c) {
153        var cpid = c.toString().match(/^\[(\d+)\]/)
154        if (cpid[1]) {
155          this.removeListener('data', childFinder)
156          cb(null, [d, cpid[1]])
157        }
158      }
159    }
160
161    common.makeGitRepo({
162      path: repo,
163      commands: [
164        git.chainableExec(
165          ['clone', '--bare', repo, 'child.git'],
166          { cwd: pkg, env: process.env }
167        ),
168        startDaemon
169      ]
170    }, cb)
171  })
172}
173