summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/startstop.js
blob: d5a0026cee414fcdaf1cb0ae77dffbcdeea2f19f (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
var common = require('../common-tap')
  , test = require('tap').test
  , path = require('path')
  , spawn = require('child_process').spawn
  , rimraf = require('rimraf')
  , mkdirp = require('mkdirp')
  , pkg = __dirname + '/startstop'
  , cache = pkg + '/cache'
  , tmp = pkg + '/tmp'
  , node = process.execPath
  , npm = path.resolve(__dirname, '../../cli.js')

function run (command, t, parse) {
  var c = ''
    , e = ''
    , node = process.execPath
    , child = spawn(node, [npm, command], {
      cwd: pkg
    })

    child.stderr.on('data', function (chunk) {
      e += chunk
    })

    child.stdout.on('data', function (chunk) {
      c += chunk
    })

    child.stdout.on('end', function () {
      if (e) {
        throw new Error('npm ' + command + ' stderr: ' + e.toString())
      }
      if (parse) {
        // custom parsing function
        c = parse(c)
        t.equal(c.actual, c.expected)
        t.end()
        return
      }

      c = c.trim().split('\n')
      c = c[c.length - 1]
      t.equal(c, command)
      t.end()
    })

}

function cleanup () {
  rimraf.sync(pkg + '/cache')
  rimraf.sync(pkg + '/tmp')
}

test('setup', function (t) {
  cleanup()
  mkdirp.sync(pkg + '/cache')
  mkdirp.sync(pkg + '/tmp')
  t.end()

})

test('npm start', function (t) {
  run('start', t)
})

test('npm stop', function (t) {
  run('stop', t)
})

test('npm restart', function (t) {
  run ('restart', t, function (output) {
    output = output.split('\n').filter(function (val) {
      return val.match(/^s/)
    })
    return {actual: output, expected: output}
  })
})

test('cleanup', function (t) {
  cleanup()
  t.end()
})