• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const BB = require('bluebird')
4
5const test = require('tap').test
6const common = require('../common-tap')
7const fs = BB.promisifyAll(require('graceful-fs'))
8const path = require('path')
9const rimraf = BB.promisify(require('rimraf'))
10const Tacks = require('tacks')
11
12const Dir = Tacks.Dir
13const File = Tacks.File
14
15const testDir = common.pkg
16const tmp = path.join(testDir, 'tmp')
17const cache = common.cache
18
19test('basic pack', (t) => {
20  const fixture = new Tacks(new Dir({
21    'package.json': new File({
22      name: 'generic-package',
23      version: '90000.100001.5'
24    })
25  }))
26  return rimraf(testDir)
27    .then(() => fixture.create(testDir))
28    .then(() => common.npm([
29      'pack',
30      '--loglevel', 'notice',
31      '--cache', cache,
32      '--tmp', tmp,
33      '--prefix', testDir,
34      '--no-global'
35    ], {
36      cwd: testDir
37    }))
38    .spread((code, stdout, stderr) => {
39      t.equal(code, 0, 'npm pack exited ok')
40      t.match(stderr, /notice\s+\d+[a-z]+\s+package\.json/gi, 'mentions package.json')
41      t.match(stdout, /generic-package-90000\.100001\.5\.tgz/ig, 'found pkg')
42      return fs.statAsync(
43        path.join(testDir, 'generic-package-90000.100001.5.tgz')
44      )
45    })
46    .then((stat) => t.ok(stat, 'tarball written to cwd'))
47    .then(() => rimraf(testDir))
48})
49
50test('pack with bundled', (t) => {
51  const fixture = new Tacks(new Dir({
52    'package.json': new File({
53      name: 'generic-package',
54      version: '90000.100001.5',
55      dependencies: {
56        '@bundle/dep': '^1.0.0',
57        'regular-dep': '^1.0.0'
58      },
59      bundleDependencies: [
60        '@bundle/dep',
61        'regular-dep'
62      ]
63    }),
64    'node_modules': new Dir({
65      'regular-dep': new Dir({
66        'package.json': new File({
67          name: 'regular-dep',
68          version: '1.0.0'
69        })
70      }),
71      '@bundle': new Dir({
72        'dep': new Dir({
73          'package.json': new File({
74            name: '@bundle/dep',
75            version: '1.0.0'
76          })
77        })
78      })
79    })
80  }))
81  return rimraf(testDir)
82    .then(() => fixture.create(testDir))
83    .then(() => common.npm([
84      'pack',
85      '--loglevel', 'notice',
86      '--cache', cache,
87      '--tmp', tmp,
88      '--prefix', testDir,
89      '--no-global'
90    ], {
91      cwd: testDir
92    }))
93    .spread((code, stdout, stderr) => {
94      t.equal(code, 0, 'npm pack exited ok')
95      t.match(stderr, /notice\s+\d+[a-z]+\s+package\.json/gi, 'mentions package.json')
96      t.match(stderr, /notice\s+regular-dep/, 'regular dep mentioned')
97      t.match(stderr, /notice\s+@bundle\/dep/, 'bundled dep mentioned')
98    })
99    .then(() => rimraf(testDir))
100})
101
102test('pack --dry-run', (t) => {
103  const fixture = new Tacks(new Dir({
104    'package.json': new File({
105      name: 'generic-package',
106      version: '90000.100001.5'
107    })
108  }))
109  return rimraf(testDir)
110    .then(() => fixture.create(testDir))
111    .then(() => common.npm([
112      'pack',
113      '--dry-run',
114      '--loglevel', 'notice',
115      '--cache', cache,
116      '--tmp', tmp,
117      '--prefix', testDir,
118      '--no-global'
119    ], {
120      cwd: testDir
121    }))
122    .spread((code, stdout, stderr) => {
123      t.equal(code, 0, 'npm pack exited ok')
124      t.match(stdout, /generic-package-90000\.100001\.5\.tgz/ig, 'found pkg')
125      return fs.statAsync(
126        path.join(testDir, 'generic-package-90000.100001.5.tgz')
127      )
128        .then(
129          () => { throw new Error('should have failed') },
130          (err) => t.equal(err.code, 'ENOENT', 'no tarball written!')
131        )
132    })
133    .then(() => rimraf(testDir))
134})
135
136test('pack --json', (t) => {
137  const fixture = new Tacks(new Dir({
138    'package.json': new File({
139      name: 'generic-package',
140      version: '90000.100001.5'
141    })
142  }))
143  return rimraf(testDir)
144    .then(() => fixture.create(testDir))
145    .then(() => common.npm([
146      'pack',
147      '--dry-run',
148      '--json',
149      '--loglevel', 'notice',
150      '--cache', cache,
151      '--tmp', tmp,
152      '--prefix', testDir,
153      '--no-global'
154    ], {
155      cwd: testDir
156    }))
157    .spread((code, stdout, stderr) => {
158      t.equal(code, 0, 'npm pack exited ok')
159      t.equal(stderr.trim(), '', 'no notice output')
160      t.similar(JSON.parse(stdout), [{
161        filename: 'generic-package-90000.100001.5.tgz',
162        files: [{path: 'package.json'}],
163        entryCount: 1
164      }], 'pack details output as valid json')
165    })
166    .then(() => rimraf(testDir))
167})
168
169test('postpack', (t) => {
170  const fixture = new Tacks(new Dir({
171    'package.json': new File({
172      name: 'generic-package',
173      version: '90000.100001.5',
174      scripts: {
175        postpack: 'node -e "var fs = require(\'fs\'); fs.openSync(\'postpack-step\', \'w+\'); if (!fs.existsSync(\'generic-package-90000.100001.5.tgz\')) { throw new Error(\'tar archive does not exist on postpack\') }"'
176      }
177    })
178  }))
179
180  return rimraf(testDir)
181    .then(() => fixture.create(testDir))
182    .then(() => common.npm([
183      'pack',
184      '--loglevel', 'notice',
185      '--cache', cache,
186      '--tmp', tmp,
187      '--prefix', testDir,
188      '--no-global'
189    ], {
190      cwd: testDir
191    }))
192    .spread((code, stdout, stderr) => {
193      t.equal(code, 0, 'npm pack exited ok')
194      return fs.statAsync(
195        path.join(testDir, 'postpack-step')
196      )
197    })
198    .then(() => rimraf(testDir))
199})
200