summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npmlog/test/progress.js
blob: 97b13ded2c6660fcb4f57ec190315078b51dc174 (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
'use strict'

var test = require('tap').test
var log = require('../log.js')

var actions = []
log.gauge = {
  enable: function () {
    actions.push(['enable'])
  },
  disable: function () {
    actions.push(['disable'])
  },
  hide: function () {
    actions.push(['hide'])
  },
  show: function (name, completed) {
    actions.push(['show', name, completed])
  },
  pulse: function (name) {
    actions.push(['pulse', name])
  }
}

function didActions(t, msg, output) {
  var tests = []
  for (var ii = 0; ii < output.length; ++ ii) {
    for (var jj = 0; jj < output[ii].length; ++ jj) {
      tests.push({cmd: ii, arg: jj})
    }
  }
  t.is(actions.length, output.length, msg)
  tests.forEach(function (test) {
    t.is(actions[test.cmd] ? actions[test.cmd][test.arg] : null, 
         output[test.cmd][test.arg],
         msg + ': ' + output[test.cmd] + (test.arg ? ' arg #'+test.arg : ''))
  })
  actions = []
}


test('enableProgress', function (t) {
  t.plan(6)
  log.enableProgress()
  didActions(t, 'enableProgress', [ [ 'enable' ], [ 'show', undefined, 0 ] ])
  log.enableProgress()
  didActions(t, 'enableProgress again', [])
})

test('disableProgress', function (t) {
  t.plan(4)
  log.disableProgress()
  didActions(t, 'disableProgress', [ [ 'hide' ], [ 'disable' ] ])
  log.disableProgress()
  didActions(t, 'disableProgress again', [])
})

test('showProgress', function (t) {
  t.plan(5)
  log.showProgress('foo')
  didActions(t, 'showProgress disabled', [])
  log.enableProgress()
  actions = []
  log.showProgress('foo')
  didActions(t, 'showProgress', [ [ 'show', 'foo', 0 ] ])
})

test('clearProgress', function (t) {
  t.plan(3)
  log.clearProgress()
  didActions(t, 'clearProgress', [ [ 'hide' ] ])
  log.disableProgress()
  actions = []
  log.clearProgress()
  didActions(t, 'clearProgress disabled', [ ])
})

test("newItem", function (t) {
  t.plan(12)
  log.enableProgress()
  actions = []
  var a = log.newItem("test", 10)
  didActions(t, "newItem", [ [ 'show', undefined, 0 ] ])
  a.completeWork(5)
  didActions(t, "newItem:completeWork", [ [ 'show', 'test', 0.5 ] ])
  a.finish()
  didActions(t, "newItem:finish", [ [ 'show', 'test', 1 ] ])
})

// test that log objects proxy through. And test that completion status filters up
test("newGroup", function (t) {
  t.plan(23)
  var a = log.newGroup("newGroup")
  didActions(t, "newGroup", [ [ 'show', undefined, 0.5 ] ])
  a.warn("test", "this is a test")
  didActions(t, "newGroup:warn", [ [ 'pulse', 'test' ], [ 'hide' ], [ 'show', undefined, 0.5 ] ])
  var b = a.newItem("newGroup2", 10)
  didActions(t, "newGroup:newItem", [ [ 'show', 'newGroup', 0.5 ] ])
  b.completeWork(5)
  didActions(t, "newGroup:completeWork", [ [ 'show', 'newGroup2', 0.75 ] ])
  a.finish()
  didActions(t, "newGroup:finish", [ [ 'show', 'newGroup', 1 ] ])
})

test("newStream", function (t) {
  t.plan(13)
  var a = log.newStream("newStream", 10)
  didActions(t, "newStream", [ [ 'show', undefined, 0.6666666666666666 ] ])
  a.write("abcde")
  didActions(t, "newStream", [ [ 'show', 'newStream', 0.8333333333333333 ] ])
  a.write("fghij")
  didActions(t, "newStream", [ [ 'show', 'newStream', 1 ] ])
  t.is(log.tracker.completed(), 1, "Overall completion")
})