Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Changed require configuration to path relative to current working dir… #427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/config/index.js
Original file line number Diff line number Diff line change
@@ -53,6 +53,14 @@ Config.prototype.isCoverageEnabled = function() {
return this.system.coverage.enabled;
};

Config.isRelativePath = function(file) {
return file.indexOf('./') === 0 || file.indexOf('/') !== 0;
};

Config.getAbsPath = function(file) {
return process.cwd() + '/' + file;
};

function readConfig(filePath) {
filePath = filePath || getDefaultConfig();

@@ -81,7 +89,11 @@ function getDefaultConfig() {

function requireModule(file) {
try {
return require(file);
if (Config.isRelativePath(file)) {
return require(Config.getAbsPath(file));
} else {
return require(file);
}
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new GeminiError('Config file does not exist: ' + file);
20 changes: 20 additions & 0 deletions test/functional/config.test.js
Original file line number Diff line number Diff line change
@@ -51,6 +51,16 @@ describe('config', function() {
return new Config(configPath('notExists.js'));
}, GeminiError);
});

it('should read relative to current working directory', function() {
assert.deepPropertyVal(
new Config(configPath('validConfig.js').replace(process.cwd() + '/', '')),
'system.projectRoot', '/it/works');

assert.deepPropertyVal(
new Config(configPath('validConfig.js').replace(process.cwd() + '/', './')),
'system.projectRoot', '/it/works');
});
});

describe('.json', function() {
@@ -64,6 +74,16 @@ describe('config', function() {
return new Config(configPath('notExists.json'));
}, GeminiError);
});

it('should read relative to current working directory', function() {
assert.deepPropertyVal(
new Config(configPath('validConfig.json').replace(process.cwd() + '/', '')),
'system.projectRoot', '/it/works');

assert.deepPropertyVal(
new Config(configPath('validConfig.json').replace(process.cwd() + '/', './')),
'system.projectRoot', '/it/works');
});
});
});
});
22 changes: 22 additions & 0 deletions test/unit/config-options/config-options.test.js
Original file line number Diff line number Diff line change
@@ -866,4 +866,26 @@ describe('config', function() {
});
});
});

describe('util functions', function() {
it('should return true if path is relative', function() {
assert.strictEqual(Config.isRelativePath('./foo/bar'), true);
assert.strictEqual(Config.isRelativePath('foo/bar'), true);
});

it('should return false if path is absolute', function() {
assert.strictEqual(Config.isRelativePath('/foo/bar'), false);
});

it('should return absolute path by pass relative', function() {
var relativePath = 'foo/bar/gemini.config';
var relativePathWithLeadedDot = './foo/bar/gemini.config';
assert.strictEqual(
Config.getAbsPath(relativePath),
process.cwd() + '/' + relativePath);
assert.strictEqual(
Config.getAbsPath(relativePathWithLeadedDot),
process.cwd() + '/' + relativePathWithLeadedDot);
});
});
});