Skip to content

Commit

Permalink
Add tests for DependencyGraph (#354)
Browse files Browse the repository at this point in the history
* Make depGraph testable

* Add tests for DependencyGraph
  • Loading branch information
RyanZim committed Oct 8, 2020
1 parent 8aa4f6e commit 8ef4c1f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ const postcssrc = require('postcss-load-config')
const reporter = require('postcss-reporter/lib/formatter')()

const argv = require('./lib/args')
const depGraph = require('./lib/depGraph')
const createDependencyGraph = require('./lib/DependencyGraph')
const getMapfile = require('./lib/getMapfile')
const depGraph = createDependencyGraph()

let input = argv._
const { dir, output } = argv
Expand Down
24 changes: 24 additions & 0 deletions lib/DependencyGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'
const path = require('path')
const { DepGraph } = require('dependency-graph')

module.exports = function () {
const graph = new DepGraph()
return {
add(message) {
message.parent = path.resolve(message.parent)
message.file = path.resolve(message.file)

graph.addNode(message.parent)
graph.addNode(message.file)
graph.addDependency(message.parent, message.file)
return message
},
dependantsOf(node) {
node = path.resolve(node)

if (graph.hasNode(node)) return graph.dependantsOf(node)
return []
},
}
}
20 changes: 20 additions & 0 deletions lib/DependencyGraph.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'
const test = require('ava')
const path = require('path')
const createDependencyGraph = require('./DependencyGraph.js')

function resolveArray(arr) {
return arr.map((p) => path.resolve(p))
}

test('tracks dependencies', (t) => {
const graph = createDependencyGraph()
graph.add({ file: 'aa', parent: 'a' })
graph.add({ file: 'bb', parent: 'b' })
graph.add({ file: 'ab', parent: 'a' })
graph.add({ file: 'ab', parent: 'b' })
t.deepEqual(graph.dependantsOf('aa'), resolveArray(['a']))
t.deepEqual(graph.dependantsOf('bb'), resolveArray(['b']))
t.deepEqual(graph.dependantsOf('ab'), resolveArray(['a', 'b']))
t.deepEqual(graph.dependantsOf('nonexistent'), [])
})
22 changes: 0 additions & 22 deletions lib/depGraph.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"files": [
"bin",
"index.js",
"lib"
"lib",
"!*.test.js"
],
"keywords": [
"cli",
Expand Down

0 comments on commit 8ef4c1f

Please sign in to comment.