Skip to content

Commit

Permalink
Fix ignoreMissing when using cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed Jun 6, 2015
1 parent 86d9791 commit c2553d8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -136,7 +136,7 @@ Deps.prototype.resolve = function (id, parent, cb) {
var opts = self.options;

if (xhas(self.cache, parent.id, 'deps', id)
&& self.cache[parent.id].deps) {
&& self.cache[parent.id].deps[id]) {
var file = self.cache[parent.id].deps[id];
var pkg = self.pkgCache[file];
if (pkg) return cb(null, file, pkg);
Expand Down
60 changes: 60 additions & 0 deletions test/ignore_missing.js
@@ -0,0 +1,60 @@
var parser = require('../');
var test = require('tap').test;
var fs = require('fs');
var path = require('path');

var files = {
main: path.join(__dirname, '/ignore_missing/main.js'),
other: path.join(__dirname, '/ignore_missing/other.js')
};

var sources = Object.keys(files).reduce(function (acc, file) {
acc[file] = fs.readFileSync(files[file], 'utf8');
return acc;
}, {});

test('ignoreMissing', function (t) {
t.plan(1);
var p = parser({ignoreMissing: true});
p.end({file: files.main, entry: true});

var rows = [];
p.on('data', function (row) { rows.push(row) });
p.on('end', function () {
t.same(rows.sort(cmp), [
{
id: files.main,
file: files.main,
source: sources.main,
entry: true,
deps: { './other': files.other }
},
{
id: files.other,
file: files.other,
source: sources.other,
deps: { 'missingModule': undefined }
}
].sort(cmp));
});
});

test('ignoreMissing off', function (t) {
t.plan(1);
var p = parser();
p.end({file: files.main, entry: true});

var rows = [];
p.on('data', function (row) { rows.push(row) });
p.on('error', function (err) {
t.match(
String(err),
/Cannot find module 'missingModule'/
);
});
p.on('end', function () {
t.fail('should have errored');
});
});

function cmp (a, b) { return a.id < b.id ? -1 : 1 }
1 change: 1 addition & 0 deletions test/ignore_missing/main.js
@@ -0,0 +1 @@
require('./other');
1 change: 1 addition & 0 deletions test/ignore_missing/other.js
@@ -0,0 +1 @@
require('missingModule');
52 changes: 52 additions & 0 deletions test/ignore_missing_cache.js
@@ -0,0 +1,52 @@
var parser = require('../');
var test = require('tap').test;
var fs = require('fs');
var path = require('path');

var files = {
main: path.join(__dirname, '/ignore_missing/main.js'),
other: path.join(__dirname, '/ignore_missing/other.js')
};

var sources = Object.keys(files).reduce(function (acc, file) {
acc[file] = fs.readFileSync(files[file], 'utf8');
return acc;
}, {});

var cache = {};
cache[files.main] = {
source: sources.main,
deps: { './other': files.other }
};
cache[files.other] = {
source: sources.other,
deps: { 'missingModule': undefined }
};

test('ignoreMissing with cache', function (t) {
t.plan(1);
var p = parser({ cache: cache, ignoreMissing: true });
p.end({file: files.main, entry: true});

var rows = [];
p.on('data', function (row) { rows.push(row) });
p.on('end', function () {
t.same(rows.sort(cmp), [
{
id: files.main,
file: files.main,
source: sources.main,
entry: true,
deps: { './other': files.other }
},
{
id: files.other,
file: files.other,
source: sources.other,
deps: { 'missingModule': undefined }
}
].sort(cmp));
});
});

function cmp (a, b) { return a.id < b.id ? -1 : 1 }

0 comments on commit c2553d8

Please sign in to comment.