-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for DependencyGraph (#354)
* Make depGraph testable * Add tests for DependencyGraph
- Loading branch information
Showing
5 changed files
with
48 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), []) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,8 @@ | |
"files": [ | ||
"bin", | ||
"index.js", | ||
"lib" | ||
"lib", | ||
"!*.test.js" | ||
], | ||
"keywords": [ | ||
"cli", | ||
|