• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var test = require('tap').test
3var common = require('../common-tap.js')
4var path = require('path')
5var rimraf = require('rimraf')
6var mkdirp = require('mkdirp')
7var basepath = path.resolve(__dirname, path.basename(__filename, '.js'))
8var fixturepath = path.resolve(basepath, 'npm-test-array-bin')
9var modulepath = path.resolve(basepath, 'node_modules')
10var installedpath = path.resolve(modulepath, 'npm-test-array-bin')
11var Tacks = require('tacks')
12var File = Tacks.File
13var Dir = Tacks.Dir
14var fixture = new Tacks(
15  Dir({
16    bin: Dir({
17      'array-bin': File(
18        '#!/usr/bin/env node\n' +
19        "console.log('test ran ok')\n"
20      )
21    }),
22    'package.json': File({
23      name: 'npm-test-array-bin',
24      version: '1.2.5',
25      bin: [
26        'bin/array-bin'
27      ],
28      scripts: {
29        test: 'node test.js'
30      }
31    }),
32    'test.js': File(
33      "require('child_process').exec('array-bin', { env: process.env },\n" +
34      '  function (err, stdout, stderr) {\n' +
35      "    if (err && err.code) throw new Error('exited badly with code = ' + err.code)\n" +
36      '    console.log(stdout)\n' +
37      '    console.error(stderr)\n' +
38      '  }\n' +
39      ')\n'
40    )
41  })
42)
43test('setup', function (t) {
44  setup()
45  t.done()
46})
47test('array-bin', function (t) {
48  common.npm(['install', fixturepath], {cwd: basepath}, installCheckAndTest)
49  function installCheckAndTest (err, code, stdout, stderr) {
50    if (err) throw err
51    t.is(code, 0, 'install went ok')
52    t.equal(stderr, '', 'no error output')
53    common.npm(['test'], {cwd: installedpath}, testCheckAndRemove)
54  }
55  function testCheckAndRemove (err, code, stdout, stderr) {
56    t.ifError(err, 'npm test on array bin')
57    t.equal(code, 0, 'exited OK')
58    t.equal(stderr.trim(), '', 'no error output')
59    t.match(stdout, /test ran ok/, 'child script ran properly')
60    common.npm(['rm', fixturepath], {cwd: basepath}, removeCheckAndDone)
61  }
62  function removeCheckAndDone (err, code, stdout, stderr) {
63    if (err) throw err
64    t.is(code, 0, 'remove went ok')
65    t.done()
66  }
67})
68test('cleanup', function (t) {
69  cleanup()
70  t.done()
71})
72function setup () {
73  cleanup()
74  fixture.create(fixturepath)
75  mkdirp.sync(modulepath)
76}
77function cleanup () {
78  fixture.remove(fixturepath)
79  rimraf.sync(basepath)
80}
81