Skip to content

Commit d6e0871

Browse files
committed
[Tests] add coverage for a malformed package.json
See #89
1 parent c2b7070 commit d6e0871

File tree

7 files changed

+112
-2
lines changed

7 files changed

+112
-2
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@
4949
},
5050
},
5151
],
52+
53+
"ignorePatterns": [
54+
"./test/resolver/malformed_package_json/package.json",
55+
],
5256
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"prepublishOnly": "safe-publish-latest && cp node_modules/is-core-module/core.json ./lib/ ||:",
1818
"prepublish": "not-in-publish || npm run prepublishOnly",
1919
"prelint": "eclint check $(git ls-files | grep -vE 'node_modules|\\.git')",
20-
"lint": "eslint --ext=js,mjs .",
20+
"lint": "eslint --ext=js,mjs --no-eslintrc -c .eslintrc .",
2121
"pretests-only": "cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async",
2222
"tests-only": "tape test/*.js",
2323
"pretest": "npm run lint",

test/resolver.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,58 @@ test('absolute paths', function (t) {
483483
);
484484
});
485485
});
486+
487+
test('malformed package.json', function (t) {
488+
/* eslint operator-linebreak: ["error", "before"], function-paren-newline: "off" */
489+
t.plan(
490+
(3 * 3) // 3 sets of 3 assertions in the final callback
491+
+ 2 // 1 readPackage call with malformed package.json
492+
);
493+
494+
var basedir = path.join(__dirname, 'resolver/malformed_package_json');
495+
var expected = path.join(basedir, 'index.js');
496+
497+
resolve('./index.js', { basedir: basedir }, function (err, res, pkg) {
498+
t.error(err, 'no error');
499+
t.equal(res, expected, 'malformed package.json is silently ignored');
500+
t.equal(pkg, undefined, 'malformed package.json gives an undefined `pkg` argument');
501+
});
502+
503+
resolve(
504+
'./index.js',
505+
{
506+
basedir: basedir,
507+
packageFilter: function (pkg, pkgfile, dir) {
508+
t.fail('should not reach here');
509+
}
510+
},
511+
function (err, res, pkg) {
512+
t.error(err, 'with packageFilter: no error');
513+
t.equal(res, expected, 'with packageFilter: malformed package.json is silently ignored');
514+
t.equal(pkg, undefined, 'with packageFilter: malformed package.json gives an undefined `pkg` argument');
515+
}
516+
);
517+
518+
resolve(
519+
'./index.js',
520+
{
521+
basedir: basedir,
522+
readPackage: function (readFile, pkgfile, cb) {
523+
t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
524+
readFile(pkgfile, function (err, result) {
525+
try {
526+
cb(null, JSON.parse(result));
527+
} catch (e) {
528+
t.ok(e instanceof SyntaxError, 'readPackage: malformed package.json parses as a syntax error');
529+
cb(null);
530+
}
531+
});
532+
}
533+
},
534+
function (err, res, pkg) {
535+
t.error(err, 'with readPackage: no error');
536+
t.equal(res, expected, 'with readPackage: malformed package.json is silently ignored');
537+
t.equal(pkg, undefined, 'with readPackage: malformed package.json gives an undefined `pkg` argument');
538+
}
539+
);
540+
});

test/resolver/malformed_package_json/index.js

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{

test/resolver_sync.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,54 @@ test('absolute paths', function (t) {
383383

384384
t.end();
385385
});
386+
387+
test('malformed package.json', function (t) {
388+
t.plan(5);
389+
390+
var basedir = path.join(__dirname, 'resolver/malformed_package_json');
391+
var expected = path.join(basedir, 'index.js');
392+
393+
t.equal(
394+
resolve.sync('./index.js', { basedir: basedir }),
395+
expected,
396+
'malformed package.json is silently ignored'
397+
);
398+
399+
var res1 = resolve.sync(
400+
'./index.js',
401+
{
402+
basedir: basedir,
403+
packageFilter: function (pkg, pkgfile, dir) {
404+
t.fail('should not reach here');
405+
}
406+
}
407+
);
408+
409+
t.equal(
410+
res1,
411+
expected,
412+
'with packageFilter: malformed package.json is silently ignored'
413+
);
414+
415+
var res2 = resolve.sync(
416+
'./index.js',
417+
{
418+
basedir: basedir,
419+
readPackageSync: function (readFileSync, pkgfile) {
420+
t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
421+
var result = String(readFileSync(pkgfile));
422+
try {
423+
return JSON.parse(result);
424+
} catch (e) {
425+
t.ok(e instanceof SyntaxError, 'readPackageSync: malformed package.json parses as a syntax error');
426+
}
427+
}
428+
}
429+
);
430+
431+
t.equal(
432+
res2,
433+
expected,
434+
'with readPackageSync: malformed package.json is silently ignored'
435+
);
436+
});

0 commit comments

Comments
 (0)