summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/git-npmignore.js
blob: 819e0a62852893f2e38aad8e3d9108cddd13c414 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var child_process = require('child_process')
var readdir = require('graceful-fs').readdirSync
var path = require('path')
var resolve = require('path').resolve

var rimraf = require('rimraf')
var test = require('tap').test
var which = require('which')

var common = require('../common-tap.js')
var escapeArg = require('../../lib/utils/escape-arg.js')
var Tacks = require('tacks')
var Dir = Tacks.Dir
var File = Tacks.File

var fixture = new Tacks(Dir({
  cache: Dir({}),
  deps: Dir({
    gitch: Dir({
      '.npmignore': File(
        't.js\n'
      ),
      '.gitignore': File(
        'node_modules/\n'
      ),
      'a.js': File(
        "console.log('hi');"
      ),
      't.js': File(
        "require('tap').test(function (t) { t.pass('I am a test!'); t.end(); });"
      ),
      'package.json': File({
        name: 'gitch',
        version: '1.0.0',
        private: true,
        main: 'a.js'
      })
    })
  }),
  'node_modules': Dir({
  })
}))

var testdir = resolve(__dirname, path.basename(__filename, '.js'))
var cachedir = resolve(testdir, 'cache')
var dep = resolve(testdir, 'deps', 'gitch')
var packname = 'gitch-1.0.0.tgz'
var packed = resolve(testdir, packname)
var modules = resolve(testdir, 'node_modules')
var installed = resolve(modules, 'gitch')
var expected = [
  'a.js',
  'package.json',
  '.npmignore'
].sort()

var NPM_OPTS = {
  cwd: testdir,
  env: common.newEnv().extend({
    npm_config_cache: cachedir
  })
}

function exec (todo, opts, cb) {
  console.log('    # EXEC:', todo)
  child_process.exec(todo, opts, cb)
}

test('setup', function (t) {
  setup(function (er) {
    t.ifError(er, 'setup ran OK')

    t.end()
  })
})

test('npm pack directly from directory', function (t) {
  packInstallTest(dep, t)
})

test('npm pack via git', function (t) {
  var urlPath = dep
    .replace(/\\/g, '/') // fixup slashes for Windows
    .replace(/^\/+/, '') // remove any leading slashes
  packInstallTest('git+file:///' + urlPath, t)
})

test('cleanup', function (t) {
  cleanup()

  t.end()
})

function packInstallTest (spec, t) {
  console.log('    # pack', spec)
  common.npm(
    [
      '--loglevel', 'error',
      'pack', spec
    ],
    NPM_OPTS,
    function (err, code, stdout, stderr) {
      if (err) throw err
      t.is(code, 0, 'npm pack exited cleanly')
      t.is(stderr, '', 'npm pack ran silently')
      t.is(stdout.trim(), packname, 'got expected package name')

      common.npm(
        [
          '--loglevel', 'error',
          'install', packed
        ],
        NPM_OPTS,
        function (err, code, stdout, stderr) {
          if (err) throw err
          t.is(code, 0, 'npm install exited cleanly')
          t.is(stderr, '', 'npm install ran silently')

          var actual = readdir(installed).sort()
          t.isDeeply(actual, expected, 'no unexpected files in packed directory')

          rimraf(packed, function () {
            t.end()
          })
        }
      )
    }
  )
}

function cleanup () {
  fixture.remove(testdir)
  rimraf.sync(testdir)
}

function setup (cb) {
  cleanup()

  fixture.create(testdir)

  common.npm(
    [
      '--loglevel', 'error',
      'cache', 'clean', '--force'
    ],
    NPM_OPTS,
    function (er, code, _, stderr) {
      if (er) return cb(er)
      if (stderr) return cb(new Error('npm cache clean error: ' + stderr))
      if (code) return cb(new Error('npm cache nonzero exit: ' + code))

      which('git', function found (er, gitPath) {
        if (er) return cb(er)

        var git = escapeArg(gitPath)

        exec(git + ' init', {cwd: dep}, init)

        function init (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error('git init error: ' + stderr))

          exec(git + " config user.name 'Phantom Faker'", {cwd: dep}, user)
        }

        function user (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error('git config error: ' + stderr))

          exec(git + ' config user.email nope@not.real', {cwd: dep}, email)
        }

        function email (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error('git config error: ' + stderr))

          exec(git + ' config core.autocrlf input', {cwd: dep}, autocrlf)
        }

        function autocrlf (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error('git config error: ' + stderr))

          exec(git + ' add .', {cwd: dep}, addAll)
        }

        function addAll (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error('git add . error: ' + stderr))

          exec(git + ' commit -m boot', {cwd: dep}, commit)
        }

        function commit (er, _, stderr) {
          if (er) return cb(er)
          if (stderr) return cb(new Error('git commit error: ' + stderr))
          cb()
        }
      })
    }
  )
}