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

Commit

Permalink
Changed require configuration to path relative to current working dir…
Browse files Browse the repository at this point in the history
…ectory

For example working directory is `/home/foo/project_with_gemini/gemini/`.
We use js or json default path configuration or use `-c` option.
Run `./gemini` or `./gemini -c .very_special_configuration.json`.
And we get configuration relative to current working directory:
/home/foo/project_with_gemini/gemini/.gemini.conf.json
/home/foo/project_with_gemini/gemini/.gemini.conf.js
/home/foo/project_with_gemini/gemini/.very_special_configuration.json
not in directory anywhere in the node_modules.
Fixed #418.
  • Loading branch information
Alexandr Ishchenko committed Apr 12, 2016
1 parent d11c52c commit a4fd607
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions test/functional/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Up @@ -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);
});
});
});

0 comments on commit a4fd607

Please sign in to comment.