~webapps/unity-js-scopes/node.js

« back to all changes in this revision

Viewing changes to deps/npm/test/tap/add-remote-git-fake-windows.js

  • Committer: Marcus Tomlinson
  • Date: 2015-11-13 07:59:04 UTC
  • Revision ID: marcus.tomlinson@canonical.com-20151113075904-h0swczmoq1rvstfc
Node v4 (stable)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var fs = require('fs')
 
2
var resolve = require('path').resolve
 
3
 
 
4
var osenv = require('osenv')
 
5
var mkdirp = require('mkdirp')
 
6
var rimraf = require('rimraf')
 
7
var test = require('tap').test
 
8
 
 
9
var npm = require('../../lib/npm.js')
 
10
var common = require('../common-tap.js')
 
11
 
 
12
var pkg = resolve(__dirname, 'add-remote-git')
 
13
var repo = resolve(__dirname, 'add-remote-git-repo')
 
14
 
 
15
var daemon
 
16
var daemonPID
 
17
var git
 
18
 
 
19
var pjParent = JSON.stringify({
 
20
  name: 'parent',
 
21
  version: '1.2.3',
 
22
  dependencies: {
 
23
    child: 'git://localhost:1233/child.git'
 
24
  }
 
25
}, null, 2) + '\n'
 
26
 
 
27
var pjChild = JSON.stringify({
 
28
  name: 'child',
 
29
  version: '1.0.3'
 
30
}, null, 2) + '\n'
 
31
 
 
32
 
 
33
test('setup', function (t) {
 
34
  bootstrap()
 
35
  setup(function (er, r) {
 
36
    if (er) {
 
37
      throw er
 
38
    }
 
39
 
 
40
    if (!er) {
 
41
      daemon = r[r.length - 2]
 
42
      daemonPID = r[r.length - 1]
 
43
    }
 
44
 
 
45
    t.end()
 
46
  })
 
47
})
 
48
 
 
49
test('install from repo on \'Windows\'', function (t) {
 
50
  // before we confuse everything by switching the platform
 
51
  require('../../lib/install.js')
 
52
  require('../../lib/unbuild.js')
 
53
  process.platform = 'win32'
 
54
  process.chdir(pkg)
 
55
  npm.commands.install('.', [], function (er) {
 
56
    t.ifError(er, 'npm installed via git')
 
57
 
 
58
    t.end()
 
59
  })
 
60
})
 
61
 
 
62
test('clean', function (t) {
 
63
  daemon.on('close', function () {
 
64
    cleanup()
 
65
    t.end()
 
66
  })
 
67
  process.kill(daemonPID)
 
68
})
 
69
 
 
70
function bootstrap () {
 
71
  rimraf.sync(pkg)
 
72
  mkdirp.sync(pkg)
 
73
  fs.writeFileSync(resolve(pkg, 'package.json'), pjParent)
 
74
}
 
75
 
 
76
function setup (cb) {
 
77
  rimraf.sync(repo)
 
78
  mkdirp.sync(repo)
 
79
  fs.writeFileSync(resolve(repo, 'package.json'), pjChild)
 
80
  npm.load({ registry: common.registry, loglevel: 'silent' }, function () {
 
81
    // some really cheesy monkeypatching
 
82
    require('module')._cache[require.resolve('which')] = {
 
83
      exports: function (_, cb) { cb() }
 
84
    }
 
85
    git = require('../../lib/utils/git.js')
 
86
 
 
87
    function startDaemon (cb) {
 
88
      // start git server
 
89
      var d = git.spawn(
 
90
        [
 
91
          'daemon',
 
92
          '--verbose',
 
93
          '--listen=localhost',
 
94
          '--export-all',
 
95
          '--base-path=.',
 
96
          '--port=1233'
 
97
        ],
 
98
        {
 
99
          cwd: pkg,
 
100
          env: process.env,
 
101
          stdio: ['pipe', 'pipe', 'pipe']
 
102
        }
 
103
      )
 
104
      d.stderr.on('data', childFinder)
 
105
 
 
106
      function childFinder (c) {
 
107
        var cpid = c.toString().match(/^\[(\d+)\]/)
 
108
        if (cpid[1]) {
 
109
          this.removeListener('data', childFinder)
 
110
          cb(null, [d, cpid[1]])
 
111
        }
 
112
      }
 
113
    }
 
114
 
 
115
    common.makeGitRepo({
 
116
      path: repo,
 
117
      commands: [
 
118
        git.chainableExec(
 
119
          ['clone', '--bare', repo, 'child.git'],
 
120
          { cwd: pkg, env: process.env }
 
121
        ),
 
122
        startDaemon
 
123
      ]
 
124
    }, cb)
 
125
  })
 
126
}
 
127
 
 
128
function cleanup () {
 
129
  process.chdir(osenv.tmpdir())
 
130
  rimraf.sync(repo)
 
131
  rimraf.sync(pkg)
 
132
}