Skip to content

Commit 0aa0afb

Browse files
committed
test: add test to getFilePath
1 parent 627f5ce commit 0aa0afb

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

test/library.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const nodeExternals = require('../index.js');
2+
const path = require('path');
23
const utils = require('../utils.js');
34
const testUtils = require('./test-utils.js');
45
const mockNodeModules = testUtils.mockNodeModules;
@@ -326,3 +327,11 @@ describe('validate options', function () {
326327
expect(results[0].correctTerm).to.be.equal('modulesDir');
327328
});
328329
});
330+
331+
describe('file path', function() {
332+
it('should return the correct file path', function() {
333+
expect(utils.getFilePath({ fileName: 'file.json' })).to.be.equal(path.resolve(process.cwd(), 'file.json'));
334+
expect(utils.getFilePath({})).to.be.equal(path.resolve(process.cwd(), 'package.json'));
335+
expect(utils.getFilePath({ fileName: 'c:\\test\\test.js' })).to.be.equal('c:\\test\\test.js');
336+
})
337+
})

utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ exports.readDir = function readDir(dirName) {
3838
}
3939
};
4040

41+
function getFilePath(options) {
42+
return path.resolve(process.cwd(), options.fileName || 'package.json');
43+
}
44+
exports.getFilePath = getFilePath;
45+
4146
exports.readFromPackageJson = function readFromPackageJson(options) {
4247
if (typeof options !== 'object') {
4348
options = {};
@@ -48,11 +53,7 @@ exports.readFromPackageJson = function readFromPackageJson(options) {
4853
// read the file
4954
let packageJson;
5055
try {
51-
const fileName = options.fileName || 'package.json';
52-
const packageJsonString = fs.readFileSync(
53-
path.resolve(process.cwd(), fileName),
54-
'utf8'
55-
);
56+
const packageJsonString = fs.readFileSync(getFilePath(options), 'utf8');
5657
packageJson = JSON.parse(packageJsonString);
5758
} catch (e) {
5859
return [];

0 commit comments

Comments
 (0)