forked from broccolijs/broccoli
/
builder_test.js
157 lines (140 loc) · 4.58 KB
/
builder_test.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var test = require('tap').test
var broccoli = require('..')
var Builder = broccoli.Builder
var RSVP = require('rsvp')
var Promise = RSVP.Promise
RSVP.on('error', function(error) {
throw error
})
function countingTree (readFn) {
return {
read: function (readTree) {
this.readCount++
return readFn.call(this, readTree)
},
readCount: 0,
cleanup: function () { this.cleanupCount++ },
cleanupCount: 0
}
}
test('Builder', function (t) {
test('core functionality', function (t) {
t.end()
test('build', function (t) {
test('passes through string tree', function (t) {
var builder = new Builder('someDir')
builder.build().then(function (hash) {
t.equal(hash.directory, 'someDir')
t.end()
})
})
test('calls read on the given tree object', function (t) {
var builder = new Builder({
read: function (readTree) { return 'someDir' }
})
builder.build().then(function (hash) {
t.equal(hash.directory, 'someDir')
t.end()
})
})
t.end()
})
test('readTree deduplicates', function (t) {
var subtree = new countingTree(function (readTree) { return 'foo' })
var builder = new Builder({
read: function (readTree) {
return readTree(subtree).then(function (hash) {
var dirPromise = readTree(subtree) // read subtree again
t.ok(dirPromise.then, 'is promise, not string')
return dirPromise
})
}
})
builder.build().then(function (hash) {
t.equal(hash.directory, 'foo')
t.equal(subtree.readCount, 1)
t.end()
})
})
test('cleanup', function (t) {
test('is called on all trees called ever', function (t) {
var tree = countingTree(function (readTree) {
// Interesting edge case: Read subtree1 on the first read, subtree2 on
// the second
return readTree(this.readCount === 1 ? subtree1 : subtree2)
})
var subtree1 = countingTree(function (readTree) { return 'foo' })
var subtree2 = countingTree(function (readTree) { throw new Error('bar') })
var builder = new Builder(tree)
builder.build().then(function (hash) {
t.equal(hash.directory, 'foo')
builder.build().catch(function (err) {
t.equal(err.message, 'bar')
builder.cleanup()
t.equal(tree.cleanupCount, 1)
t.equal(subtree1.cleanupCount, 1)
t.equal(subtree2.cleanupCount, 1)
t.end()
})
})
})
t.end()
})
})
test('tree graph', function (t) {
var parent = countingTree(function (readTree) {
return readTree(child).then(function (dir) {
return new Promise(function (resolve, reject) {
setTimeout(function() { resolve('parentTreeDir') }, 30)
})
})
})
var child = countingTree(function (readTree) {
return readTree('srcDir').then(function (dir) {
return new Promise(function (resolve, reject) {
setTimeout(function() { resolve('childTreeDir') }, 20)
})
})
})
var timeEqual = function (a, b) {
t.equal(typeof a, 'number')
// do not run timing assertions in Travis builds
// the actual results of process.hrtime() are not
// reliable
if (process.env.CI !== 'true') {
t.ok(a >= b - 5e6 && a <= b + 5e6, a + ' should be within ' + b + ' +/- 5e6')
}
}
var builder = new Builder(parent)
builder.build().then(function (hash) {
t.equal(hash.directory, 'parentTreeDir')
var parentNode = hash.graph
t.equal(parentNode.directory, 'parentTreeDir')
t.equal(parentNode.tree, parent)
timeEqual(parentNode.totalTime, 50e6)
timeEqual(parentNode.selfTime, 30e6)
t.equal(parentNode.subtrees.length, 1)
var childNode = parentNode.subtrees[0]
t.equal(childNode.directory, 'childTreeDir')
t.equal(childNode.tree, child)
timeEqual(childNode.totalTime, 20e6)
timeEqual(childNode.selfTime, 20e6)
t.equal(childNode.subtrees.length, 1)
var leafNode = childNode.subtrees[0]
t.equal(leafNode.directory, 'srcDir')
t.equal(leafNode.tree, 'srcDir')
t.equal(leafNode.totalTime, 0)
t.equal(leafNode.selfTime, 0)
t.equal(leafNode.subtrees.length, 0)
t.end()
})
})
test('string tree callback', function (t) {
var builder = new Builder('fooDir')
builder.build(function willReadStringTree (dir) {
t.equal(dir, 'fooDir')
t.end()
})
})
t.end()
})