From 5eb52818c7bda2dc68781400343f848cb6aac4c0 Mon Sep 17 00:00:00 2001 From: nlf Date: Wed, 6 Apr 2022 12:55:12 -0700 Subject: [PATCH] chore: add tests for missing coverage --- test/bundle-missing-dep.js | 33 +++++++++++ test/bundled-cycle.js | 53 +++++++++++++++++ test/cannot-include-non-file-or-directory.js | 62 ++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 test/bundle-missing-dep.js create mode 100644 test/bundled-cycle.js create mode 100644 test/cannot-include-non-file-or-directory.js diff --git a/test/bundle-missing-dep.js b/test/bundle-missing-dep.js new file mode 100644 index 0000000..7cc9e1e --- /dev/null +++ b/test/bundle-missing-dep.js @@ -0,0 +1,33 @@ +'use strict' + +const t = require('tap') +const packlist = require('../') + +t.test('skips bundling deps with missing edges', async (t) => { + const pkg = t.testdir({ + 'package.json': JSON.stringify({ + name: 'test', + version: '1.0.0', + main: 'index.js', + // named in bundleDependencies, but not actually a dependency + bundleDependencies: ['history'], + }), + 'index.js': '', + node_modules: { + history: { + 'package.json': JSON.stringify({ + name: 'history', + version: '1.0.0', + main: 'index.js', + }), + 'index.js': '', + }, + }, + }) + + const files = await packlist({ path: pkg }) + t.same(files, [ + 'index.js', + 'package.json', + ]) +}) diff --git a/test/bundled-cycle.js b/test/bundled-cycle.js new file mode 100644 index 0000000..f8eb4a9 --- /dev/null +++ b/test/bundled-cycle.js @@ -0,0 +1,53 @@ +'use strict' + +const t = require('tap') +const packlist = require('../') + +t.test('correctly bundles cyclic deps', async (t) => { + const pkg = t.testdir({ + 'package.json': JSON.stringify({ + name: 'root', + version: '1.0.0', + main: 'index.js', + dependencies: { + a: '1.0.0', + }, + bundleDependencies: ['a'], + }), + 'index.js': '', + node_modules: { + a: { + 'package.json': JSON.stringify({ + name: 'a', + version: '1.0.0', + main: 'index.js', + dependencies: { + b: '1.0.0', + }, + }), + 'index.js': '', + }, + b: { + 'package.json': JSON.stringify({ + name: 'b', + version: '1.0.0', + main: 'index.js', + dependencies: { + a: '1.0.0', + }, + }), + 'index.js': '', + }, + }, + }) + + const files = await packlist({ path: pkg }) + t.same(files, [ + 'index.js', + 'node_modules/a/index.js', + 'node_modules/b/index.js', + 'node_modules/a/package.json', + 'node_modules/b/package.json', + 'package.json', + ]) +}) diff --git a/test/cannot-include-non-file-or-directory.js b/test/cannot-include-non-file-or-directory.js new file mode 100644 index 0000000..95fde7b --- /dev/null +++ b/test/cannot-include-non-file-or-directory.js @@ -0,0 +1,62 @@ +'use strict' + +const fs = require('fs') +const t = require('tap') +const { join } = require('path') + +t.test('cannot include something that exists but is neither a file nor a directory', async (t) => { + const pkg = t.testdir({ + 'package.json': JSON.stringify({ + name: 'root', + version: '1.0.0', + main: 'index.js', + files: ['lib', 'device'], + }), + 'index.js': '', + lib: { + socket: '', + }, + device: '', + }) + + // mock fs.lstat for ignore-walk so it tells us that lib/socket is not a file, dir, or symlink + const ignoreWalk = t.mock('ignore-walk', { + fs: { + ...fs, + lstat: (path, options, callback) => { + if (typeof options === 'function') { + callback = options + options = undefined + } + if (path === join(pkg, 'lib', 'socket')) { + return callback(null, { + isFile: () => false, + isDirectory: () => false, + isSymbolicLink: () => false, + }) + } + return fs.lstat(path, options, callback) + }, + }, + }) + + const packlist = t.mock('../', { + 'ignore-walk': ignoreWalk, + fs: { + ...fs, + lstatSync: (path) => { + if (path === join(pkg, 'device')) { + return { isFile: () => false, isDirectory: () => false } + } + + return fs.lstatSync(path) + }, + }, + }) + + const files = await packlist({ path: pkg }) + t.same(files, [ + 'index.js', + 'package.json', + ]) +})