-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): add "--config" option support #261
feat(cli): add "--config" option support #261
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got some ideas how to make this test green. Also we will want to add some additional tests:
- Test if config in parent directories is ignored if
configPath
is provided (should) - Test the
options.file
parameter incore.load
tests
@commitlint/core/src/load.js
Outdated
const explorer = cosmiconfig('commitlint', { | ||
rcExtensions: true | ||
}); | ||
|
||
const local = await explorer.load(cwd); | ||
const local = await explorer.load(cwd, filepath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thing filepath
has to be passed as configPath
to the cosmiconfig constructor
@commitlint/core/src/load.js
Outdated
@@ -78,12 +78,12 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => { | |||
}, preset); | |||
}; | |||
|
|||
async function loadConfig(cwd) { | |||
async function loadConfig(cwd, filepath) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading cosmiconfig docs (yes they are a bit confusing...) this probably should be (untested)
async function loadConfig(cwd, configPath) {
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
configPath
});
const local = await explorer.load(cwd);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the configPath
is option of cosmiconfig
? I mistakenly thought you meant load([searchPath, configPath])
@commitlint/cli/src/cli.test.js
Outdated
const config = 'config/commitlint.config.js'; | ||
const actual = await cli([], {cwd, config})('foo: bar'); | ||
t.true(actual.stdout.includes('type must not be one of [foo]')); | ||
t.is(actual.code, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result was correct when I ran the command manually, but the test case did not pass. what happened?
echo "foo: bar" | npx yarn run commitlint --config @commitlint/cli/fixtures/custom-config-path/config/commitlint.config.js
yarn run v1.3.2
$ /home/lc/Projects/commitlint/node_modules/.bin/commitlint --config @commitlint/cli/fixtures/custom-config-path/config/commitlint.config.js
⧗ input: foo: bar
✖ type must not be one of [foo] [type-enum]
✖ found 1 problems, 0 warnings
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
How to run |
@commitlint/cli/src/cli.test.js
Outdated
@@ -59,6 +59,14 @@ test('should fail for input from stdin with rule from rc', async t => { | |||
t.is(actual.code, 1); | |||
}); | |||
|
|||
test('should work with --config option', async t => { | |||
const cwd = await git.bootstrap('fixtures/custom-config-path'); | |||
const config = 'config/commitlint.config.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const file = 'config/commitlint.config.js';
const actual = await cli(['--config', file], {cwd})('foo: bar');
npx yarn run lerna run test --stream --scope @commitlint/cli |
I added file
I do not understand this sentence, can you describe in detail? |
Please add a unit test for the new config in https://github.com/marionebl/commitlint/blob/master/%40commitlint/core/src/load.test.js |
@commitlint/core/src/load.test.js
Outdated
const file = 'config/commitlint.config.js'; | ||
const cwd = await git.bootstrap('fixtures/specify-config-file'); | ||
const actual = await load({rules: {foo: 'what'}}, {cwd, file}); | ||
t.is(actual.rules.foo, 'bar'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the path is not /home/lc/Projects/commitlint/@commitlint/fixtures/specify-config-file/config/commitlint.config.js'
?
load › rules should be loaded from specify config file
Rejected promise returned by test. Reason:
Error {
code: 'ENOENT',
errno: -2,
path: '/home/lc/Projects/commitlint/@commitlint/core/config/commitlint.config.js',
syscall: 'open',
message: 'ENOENT: no such file or directory, open \'/home/lc/Projects/commitlint/@commitlint/core/config/commitlint.config.js\'',
}
error Command failed with exit code 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change can work.
- const file = 'fconfig/commitlint.config.js';
+ const file = 'fixtures/specify-config-file/config/commitlint.config.js';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have to resolve the configPath
in core.load
relative to cwd
:
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
configPath: path.resolve(cwd, configPath)
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rejected promise returned by test. Reason:
TypeError {
message: 'Path must be a string. Received undefined',
}
loadConfig (src/load.js:81:1)
exports.default (src/load.js:14:16)
Test.<anonymous> (src/load.test.js:189:23)
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
- configPath: path.resolve(cwd, configPath)
+ configPath: configPath ? path.resolve(cwd, configPath) : null
});
Thanks for your patience 👍 |
Description
Add "--config" option support for cli, and add a simple test case, but this test case are not tested, need @marionebl to help me improve this test.
Motivation and Context
#241
Usage examples
How Has This Been Tested?
Please see the above example.
Types of changes
Checklist:
configPath
is provided (should)options.file
parameter incore.load
tests