summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/test/tap/git-prepare.js
blob: 1a61056b4beda1485fb726551575ee8fdfe83156 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict'

const fs = require('fs')
const path = require('path')

const osenv = require('osenv')
const rimraf = require('rimraf')
const test = require('tap').test
const mr = require('npm-registry-mock')

const npm = require('../../lib/npm.js')
const common = require('../common-tap.js')

const testdir = path.resolve(__dirname, path.basename(__filename, '.js'))
const repo = path.join(testdir, 'repo')
const prefix = path.join(testdir, 'prefix')
const cache = path.join(testdir, 'cache')

var Tacks = require('tacks')
var Dir = Tacks.Dir
var File = Tacks.File

let daemon
let daemonPID
let git
let mockRegistry

process.env.npm_config_prefix = prefix

const fixture = new Tacks(Dir({
  repo: Dir({}),
  prefix: Dir({}),
  cache: Dir({}),
  deps: Dir({
    parent: Dir({
      'package.json': File({
        name: 'parent',
        version: '1.2.3',
        dependencies: {
          'child': 'git://localhost:1234/child.git'
        }
      })
    }),
    child: Dir({
      'package.json': File({
        name: 'child',
        version: '1.0.3',
        main: 'dobuild.js',
        scripts: {
          'prepublish': 'exit 123',
          'prepare': 'writer build-artifact'
        },
        devDependencies: {
          writer: 'file:' + path.join(testdir, 'deps', 'writer')
        }
      })
    }),
    writer: Dir({
      'package.json': File({
        name: 'writer',
        version: '1.0.0',
        bin: 'writer.js'
      }),
      'writer.js': File(`#!/usr/bin/env node\n
        require('fs').writeFileSync(process.argv[2], 'hello, world!')
        `)
    })
  })
}))

test('setup', function (t) {
  bootstrap()
  setup(function (er, r) {
    t.ifError(er, 'git started up successfully')

    if (!er) {
      daemon = r[r.length - 2]
      daemonPID = r[r.length - 1]
    }

    mr({
      port: common.port
    }, function (er, server) {
      t.ifError(er, 'started mock registry')
      mockRegistry = server

      t.end()
    })
  })
})

test('install from git repo with prepare script', function (t) {
  const parent = path.join(testdir, 'deps', 'parent')

  common.npm([
    'install',
    '--no-save',
    '--registry', common.registry,
    '--cache', cache,
    '--loglevel', 'error'
  ], {
    cwd: parent
  }, function (err, code, stdout, stderr) {
    if (err) { throw err }
    t.equal(code, 0, 'exited successfully')
    t.equal(stderr, '', 'no actual output on stderr')

    const target = path.join(parent, 'node_modules', 'child', 'build-artifact')
    fs.readFile(target, 'utf8', (err, data) => {
      if (err) { throw err }
      t.equal(data, 'hello, world!', 'build artifact written for git dep')
      t.end()
    })
  })
})

test('clean', function (t) {
  mockRegistry.close()
  daemon.on('close', function () {
    cleanup()
    t.end()
  })
  process.kill(daemonPID)
})

function bootstrap () {
  fixture.create(testdir)
}

function setup (cb) {
  npm.load({
    prefix: testdir,
    loglevel: 'silent'
  }, function () {
    git = require('../../lib/utils/git.js')

    function startDaemon (cb) {
      // start git server
      const d = git.spawn(
        [
          'daemon',
          '--verbose',
          '--listen=localhost',
          '--export-all',
          '--base-path=.',
          '--reuseaddr',
          '--port=1234'
        ],
        {
          cwd: repo,
          env: process.env
        }
      )
      d.stderr.on('data', childFinder)

      function childFinder (c) {
        const cpid = c.toString().match(/^\[(\d+)\]/)
        if (cpid[1]) {
          this.removeListener('data', childFinder)
          cb(null, [d, cpid[1]])
        }
      }
    }

    const childPath = path.join(testdir, 'deps', 'child')
    common.makeGitRepo({
      path: childPath,
      commands: [
        git.chainableExec([
          'clone', '--bare', childPath, 'child.git'
        ], { cwd: repo, env: process.env }),
        startDaemon
      ]
    }, cb)
  })
}

function cleanup () {
  process.chdir(osenv.tmpdir())
  rimraf.sync(testdir)
}