Skip to content

Commit 8ef4c1f

Browse files
authored
Add tests for DependencyGraph (postcss#354)
* Make depGraph testable * Add tests for DependencyGraph
1 parent 8aa4f6e commit 8ef4c1f

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ const postcssrc = require('postcss-load-config')
1515
const reporter = require('postcss-reporter/lib/formatter')()
1616

1717
const argv = require('./lib/args')
18-
const depGraph = require('./lib/depGraph')
18+
const createDependencyGraph = require('./lib/DependencyGraph')
1919
const getMapfile = require('./lib/getMapfile')
20+
const depGraph = createDependencyGraph()
2021

2122
let input = argv._
2223
const { dir, output } = argv

lib/DependencyGraph.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
const path = require('path')
3+
const { DepGraph } = require('dependency-graph')
4+
5+
module.exports = function () {
6+
const graph = new DepGraph()
7+
return {
8+
add(message) {
9+
message.parent = path.resolve(message.parent)
10+
message.file = path.resolve(message.file)
11+
12+
graph.addNode(message.parent)
13+
graph.addNode(message.file)
14+
graph.addDependency(message.parent, message.file)
15+
return message
16+
},
17+
dependantsOf(node) {
18+
node = path.resolve(node)
19+
20+
if (graph.hasNode(node)) return graph.dependantsOf(node)
21+
return []
22+
},
23+
}
24+
}

lib/DependencyGraph.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
const test = require('ava')
3+
const path = require('path')
4+
const createDependencyGraph = require('./DependencyGraph.js')
5+
6+
function resolveArray(arr) {
7+
return arr.map((p) => path.resolve(p))
8+
}
9+
10+
test('tracks dependencies', (t) => {
11+
const graph = createDependencyGraph()
12+
graph.add({ file: 'aa', parent: 'a' })
13+
graph.add({ file: 'bb', parent: 'b' })
14+
graph.add({ file: 'ab', parent: 'a' })
15+
graph.add({ file: 'ab', parent: 'b' })
16+
t.deepEqual(graph.dependantsOf('aa'), resolveArray(['a']))
17+
t.deepEqual(graph.dependantsOf('bb'), resolveArray(['b']))
18+
t.deepEqual(graph.dependantsOf('ab'), resolveArray(['a', 'b']))
19+
t.deepEqual(graph.dependantsOf('nonexistent'), [])
20+
})

lib/depGraph.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"files": [
4949
"bin",
5050
"index.js",
51-
"lib"
51+
"lib",
52+
"!*.test.js"
5253
],
5354
"keywords": [
5455
"cli",

0 commit comments

Comments
 (0)