• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* eslint-disable camelcase */
2var child_process = require('child_process')
3var readdir = require('graceful-fs').readdirSync
4var resolve = require('path').resolve
5
6var rimraf = require('rimraf')
7var test = require('tap').test
8var which = require('which')
9
10var GITHUB_ACTIONS = process.env.GITHUB_ACTIONS
11var common = require('../common-tap.js')
12var escapeArg = require('../../lib/utils/escape-arg.js')
13var Tacks = require('tacks')
14var Dir = Tacks.Dir
15var File = Tacks.File
16
17var fixture = new Tacks(Dir({
18  deps: Dir({
19    gitch: Dir({
20      '.npmignore': File(
21        't.js\n'
22      ),
23      '.gitignore': File(
24        'node_modules/\n'
25      ),
26      'a.js': File(
27        "console.log('hi');"
28      ),
29      't.js': File(
30        "require('tap').test(function (t) { t.pass('I am a test!'); t.end(); });"
31      ),
32      'package.json': File({
33        name: 'gitch',
34        version: '1.0.0',
35        private: true,
36        main: 'a.js'
37      })
38    })
39  }),
40  'node_modules': Dir({
41  })
42}))
43
44var testdir = common.pkg
45var cachedir = common.cache
46var dep = resolve(testdir, 'deps', 'gitch')
47var packname = 'gitch-1.0.0.tgz'
48var packed = resolve(testdir, packname)
49var modules = resolve(testdir, 'node_modules')
50var installed = resolve(modules, 'gitch')
51var expected = [
52  'a.js',
53  'package.json'
54].sort()
55
56var NPM_OPTS = {
57  cwd: testdir,
58  env: common.newEnv().extend({
59    npm_config_cache: cachedir
60  })
61}
62
63function exec (todo, opts, cb) {
64  console.log('    # EXEC:', todo)
65  child_process.exec(todo, opts, cb)
66}
67
68test('setup', function (t) {
69  setup(function (er) {
70    t.ifError(er, 'setup ran OK')
71
72    t.end()
73  })
74})
75
76test('npm pack directly from directory', function (t) {
77  packInstallTest(dep, t)
78})
79
80test('npm pack via git', function (t) {
81  var urlPath = dep
82    .replace(/\\/g, '/') // fixup slashes for Windows
83    .replace(/^\/+/, '') // remove any leading slashes
84  packInstallTest('git+file:///' + urlPath, t)
85})
86
87test('cleanup', function (t) {
88  cleanup()
89
90  t.end()
91})
92
93function packInstallTest (spec, t) {
94  console.log('    # pack', spec)
95  common.npm(
96    [
97      '--loglevel', 'error',
98      'pack', spec
99    ],
100    NPM_OPTS,
101    function (err, code, stdout, stderr) {
102      if (err) throw err
103      t.is(code, 0, 'npm pack exited cleanly')
104      t.is(stderr, '', 'npm pack ran silently')
105      t.is(stdout.trim(), packname, 'got expected package name')
106
107      common.npm(
108        [
109          '--loglevel', 'error',
110          'install', packed
111        ],
112        NPM_OPTS,
113        function (err, code, stdout, stderr) {
114          if (err) throw err
115          t.is(code, 0, 'npm install exited cleanly')
116          t.is(stderr, '', 'npm install ran silently')
117
118          var actual = readdir(installed).sort()
119          t.isDeeply(actual, expected, 'no unexpected files in packed directory')
120
121          rimraf(packed, function () {
122            t.end()
123          })
124        }
125      )
126    }
127  )
128}
129
130function cleanup () {
131  fixture.remove(testdir)
132  rimraf.sync(testdir)
133}
134
135function setup (cb) {
136  cleanup()
137
138  fixture.create(testdir)
139
140  common.npm(
141    [
142      '--loglevel', 'error',
143      'cache', 'clean', '--force'
144    ],
145    NPM_OPTS,
146    function (er, code, _, stderr) {
147      if (er) return cb(er)
148      if (stderr) return cb(new Error('npm cache clean error: ' + stderr))
149      if (code) return cb(new Error('npm cache nonzero exit: ' + code))
150
151      which('git', function found (er, gitPath) {
152        if (er) return cb(er)
153
154        var git = escapeArg(gitPath)
155        var extraOpts = GITHUB_ACTIONS ? ' --initial-branch=main' : ''
156
157        exec(git + ' init' + extraOpts, {cwd: dep}, init)
158
159        function init (er, _, stderr) {
160          if (er) return cb(er)
161          if (stderr) return cb(new Error('git init error: ' + stderr))
162
163          exec(git + " config user.name 'Phantom Faker'", {cwd: dep}, user)
164        }
165
166        function user (er, _, stderr) {
167          if (er) return cb(er)
168          if (stderr) return cb(new Error('git config error: ' + stderr))
169
170          exec(git + ' config user.email nope@not.real', {cwd: dep}, email)
171        }
172
173        function email (er, _, stderr) {
174          if (er) return cb(er)
175          if (stderr) return cb(new Error('git config error: ' + stderr))
176
177          exec(git + ' config core.autocrlf input', {cwd: dep}, autocrlf)
178        }
179
180        function autocrlf (er, _, stderr) {
181          if (er) return cb(er)
182          if (stderr) return cb(new Error('git config error: ' + stderr))
183
184          exec(git + ' add .', {cwd: dep}, addAll)
185        }
186
187        function addAll (er, _, stderr) {
188          if (er) return cb(er)
189          if (stderr) return cb(new Error('git add . error: ' + stderr))
190
191          exec(git + ' commit -m boot --no-gpg-sign', {cwd: dep}, commit)
192        }
193
194        function commit (er, _, stderr) {
195          if (er) return cb(er)
196          if (stderr) return cb(new Error('git commit error: ' + stderr))
197          cb()
198        }
199      })
200    }
201  )
202}
203