readGraph-test.js (5793B)
1 var vows = require("vows"), 2 assert = require("assert"), 3 smash = require("../"); 4 5 var suite = vows.describe("smash.readGraph"); 6 7 suite.addBatch({ 8 "readGraph": { 9 "on a file with no imports": { 10 topic: function() { 11 smash.readGraph(["test/data/foo.js"], this.callback); 12 }, 13 "returns only the input file": function(imports) { 14 assert.deepEqual(imports, { 15 "test/data/foo.js": [] 16 }); 17 } 18 }, 19 "on a file with imports with trailing comments": { 20 topic: function() { 21 smash.readGraph(["test/data/trailing-comment-import.js"], this.callback); 22 }, 23 "returns the empty array": function(imports) { 24 assert.deepEqual(imports, { 25 "test/data/trailing-comment-import.js": ["test/data/foo.js", "test/data/bar.js"], 26 "test/data/foo.js": [], 27 "test/data/bar.js": [] 28 }); 29 } 30 }, 31 "on a file with invalid import syntax": { 32 topic: function() { 33 var callback = this.callback; 34 smash.readGraph(["test/data/invalid-import-syntax.js"], function(error) { 35 callback(null, error); 36 }); 37 }, 38 "throws an error with the expected message": function(error) { 39 assert.deepEqual(error.message, "invalid import: test/data/invalid-import-syntax.js:0: import foo;"); 40 } 41 }, 42 "on a file with that imports a file that does not exist": { 43 topic: function() { 44 var callback = this.callback; 45 smash.readGraph(["test/data/imports-not-found.js"], function(error) { 46 callback(null, error); 47 }); 48 }, 49 "throws an error with the expected message": function(error) { 50 assert.deepEqual(error.message, "ENOENT, open 'test/data/not-found.js'"); 51 } 52 }, 53 "on a file with that imports a file that does not exist with --ignore-missing": { 54 topic: function() { 55 smash.readGraph(["test/data/imports-not-found.js"], {"ignore-missing": true}, this.callback); 56 }, 57 "returns the empty array": function(imports) { 58 assert.deepEqual(imports, { 59 "test/data/imports-not-found.js": ["test/data/not-found.js"], 60 "test/data/not-found.js": [] 61 }); 62 } 63 }, 64 "on a file with a commented-out import": { 65 topic: function() { 66 smash.readGraph(["test/data/commented-import.js"], this.callback); 67 }, 68 "ignores the commented-out input": function(imports) { 69 assert.deepEqual(imports, { 70 "test/data/commented-import.js": [] 71 }); 72 } 73 }, 74 "on a file with a not-commented-out import": { 75 topic: function() { 76 smash.readGraph(["test/data/not-commented-import.js"], this.callback); 77 }, 78 "does not ignore the not-commented-out import": function(imports) { 79 assert.deepEqual(imports, { 80 "test/data/not-commented-import.js": ["test/data/foo.js"], 81 "test/data/foo.js": [] 82 }); 83 } 84 }, 85 "on a file with one import": { 86 topic: function() { 87 smash.readGraph(["test/data/imports-foo.js"], this.callback); 88 }, 89 "returns the expected import followed by the input file": function(imports) { 90 assert.deepEqual(imports, { 91 "test/data/imports-foo.js": ["test/data/foo.js"], 92 "test/data/foo.js": [] 93 }); 94 } 95 }, 96 "on a file with multiple imports": { 97 topic: function() { 98 smash.readGraph(["test/data/imports-foo-bar-baz.js"], this.callback); 99 }, 100 "returns the imports in order of declaration": function(imports) { 101 assert.deepEqual(imports, { 102 "test/data/imports-foo-bar-baz.js": ["test/data/foo.js", "test/data/bar.js", "test/data/baz.js"], 103 "test/data/foo.js": [], 104 "test/data/bar.js": [], 105 "test/data/baz.js": [] 106 }); 107 } 108 }, 109 "on a file with nested imports": { 110 topic: function() { 111 smash.readGraph(["test/data/imports-imports-foo.js"], this.callback); 112 }, 113 "returns the imports in order of dependency": function(imports) { 114 assert.deepEqual(imports, { 115 "test/data/imports-imports-foo.js": ["test/data/imports-foo.js"], 116 "test/data/imports-foo.js": ["test/data/foo.js"], 117 "test/data/foo.js": [] 118 }); 119 } 120 }, 121 "on multiple input files": { 122 topic: function() { 123 smash.readGraph(["test/data/foo.js", "test/data/bar.js", "test/data/baz.js"], this.callback); 124 }, 125 "returns the expected imports": function(imports) { 126 assert.deepEqual(imports, { 127 "test/data/foo.js": [], 128 "test/data/bar.js": [], 129 "test/data/baz.js": [] 130 }); 131 } 132 }, 133 "with redundant input files": { 134 topic: function() { 135 smash.readGraph(["test/data/foo.js", "test/data/foo.js"], this.callback); 136 }, 137 "ignores the redundant imports": function(imports) { 138 assert.deepEqual(imports, { 139 "test/data/foo.js": [] 140 }); 141 } 142 }, 143 "when a file that imports itself": { 144 topic: function() { 145 smash.readGraph(["test/data/imports-self.js"], this.callback); 146 }, 147 "returns a self-import": function(imports) { 148 assert.deepEqual(imports, { 149 "test/data/imports-self.js": ["test/data/imports-self.js"] 150 }); 151 } 152 }, 153 "when circular imports are encountered": { 154 topic: function() { 155 smash.readGraph(["test/data/imports-circular-foo.js"], this.callback); 156 }, 157 "returns circular imports": function(imports) { 158 assert.deepEqual(imports, { 159 "test/data/imports-circular-foo.js": ["test/data/imports-circular-bar.js"], 160 "test/data/imports-circular-bar.js": ["test/data/imports-circular-foo.js"] 161 }); 162 } 163 } 164 } 165 }); 166 167 suite.export(module);