summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/add-remote-git-submodule.js
blob: 43b30f7a65bcb5a86c29929d8f108a92e92aff5e (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
var fs = require('fs')
var resolve = require('path').resolve

var osenv = require('osenv')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var test = require('tap').test

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

var pkg = common.pkg
var repos = pkg + '-repos'
var subwt = resolve(repos, 'subwt')
var topwt = resolve(repos, 'topwt')
var suburl = 'git://localhost:' + common.gitPort + '/sub.git'
var topurl = 'git://localhost:' + common.gitPort + '/top.git'

var daemon
var daemonPID
var git

var pjParent = JSON.stringify({
  name: 'parent',
  version: '1.2.3',
  dependencies: {
    child: topurl
  }
}, null, 2) + '\n'

var pjChild = JSON.stringify({
  name: 'child',
  version: '1.0.3'
}, null, 2) + '\n'

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

test('install from repo', function (t) {
  bootstrap(t)
  npm.commands.install('.', [], function (er) {
    t.ifError(er, 'npm installed via git')
    t.end()
  })
})

test('has file in submodule', function (t) {
  bootstrap(t)
  npm.commands.install('.', [], function (er) {
    t.ifError(er, 'npm installed via git')
    var fooPath = resolve('node_modules', 'child', 'subpath', 'foo.txt')
    fs.stat(fooPath, function (er) {
      t.ifError(er, 'file in submodule exists')
      t.end()
    })
  })
})

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

function bootstrap (t) {
  process.chdir(osenv.tmpdir())
  rimraf.sync(pkg)
  mkdirp.sync(pkg)
  process.chdir(pkg)
  fs.writeFileSync('package.json', pjParent)
}

function setup (cb) {
  rimraf.sync(pkg)
  rimraf.sync(repos)

  mkdirp.sync(topwt)
  fs.writeFileSync(resolve(topwt, 'package.json'), pjChild)
  mkdirp.sync(subwt)
  fs.writeFileSync(resolve(subwt, 'foo.txt'), 'This is provided by submodule')
  npm.load({ registry: common.registry, loglevel: 'silent' }, function () {
    git = require('../../lib/utils/git.js')

    function startDaemon (cb) {
      // start git server
      var d = git.spawn(
        [
          'daemon',
          '--verbose',
          '--listen=localhost',
          '--export-all',
          '--base-path=.',
          '--reuseaddr',
          '--port=' + common.gitPort
        ],
        {
          cwd: repos,
          env: process.env,
          stdio: ['pipe', 'pipe', 'pipe']
        }
      )
      d.stderr.on('data', childFinder)

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

    var env = { PATH: process.env.PATH }
    var topopt = { cwd: topwt, env: env }
    var reposopt = { cwd: repos, env: env }
    common.makeGitRepo({
      path: subwt,
      message: 'subwt repo: ' + subwt,
      added: ['foo.txt'],
      commands: [
        git.chainableExec(['clone', '--bare', subwt, 'sub.git'], reposopt),
        startDaemon,
        [common.makeGitRepo, {
          path: topwt,
          message: 'topwt repo: ' + topwt,
          commands: [
            git.chainableExec(['submodule', 'add', suburl, 'subpath'], topopt),
            git.chainableExec(['commit', '-m', 'added submodule'], topopt),
            git.chainableExec(['clone', '--bare', topwt, 'top.git'], reposopt)
          ]
        }]
      ]
    }, cb)
  })
}

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