Skip to content
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

Merged
merged 2 commits into from
Feb 3, 2018
Merged

feat(cli): add "--config" option support #261

merged 2 commits into from
Feb 3, 2018

Conversation

lc-soft
Copy link
Contributor

@lc-soft lc-soft commented Feb 1, 2018

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

// path/to/my/custom/config/commitlint.config.js
module.exports = {

};
echo "your commit message here" | commitlint --config path/to/my/custom/config/commitlint.config.js # pass

How Has This Been Tested?

Please see the above example.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
    • Test if config in parent directories is ignored if configPath is provided (should)
    • Test the options.file parameter in core.load tests
  • All new and existing tests passed.

Copy link
Contributor

@marionebl marionebl left a 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 in core.load tests

const explorer = cosmiconfig('commitlint', {
rcExtensions: true
});

const local = await explorer.load(cwd);
const local = await explorer.load(cwd, filepath);
Copy link
Contributor

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

@@ -78,12 +78,12 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
}, preset);
};

async function loadConfig(cwd) {
async function loadConfig(cwd, filepath) {
Copy link
Contributor

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);
}

Copy link
Contributor Author

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])

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);
Copy link
Contributor Author

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.

image

@lc-soft
Copy link
Contributor Author

lc-soft commented Feb 1, 2018

How to run commitlint/cli test only?

@@ -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';
Copy link
Contributor Author

@lc-soft lc-soft Feb 1, 2018

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');

@lc-soft
Copy link
Contributor Author

lc-soft commented Feb 1, 2018

How to run 1commitlint/cli1 test only?

npx yarn run lerna run test --stream --scope @commitlint/cli 

@lc-soft
Copy link
Contributor Author

lc-soft commented Feb 1, 2018

@marionebl

Test if config in parent directories is ignored if configPath is provided (should)

I added file @commitlint/cli/fixtures/specify-config-file/commitlint.config.js.

Test the options.file parameter in core.load tests

I do not understand this sentence, can you describe in detail?

@marionebl
Copy link
Contributor

Test the options.file parameter in core.load tests
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

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');
Copy link
Contributor Author

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.

Copy link
Contributor Author

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';

Copy link
Contributor

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)
	});

Copy link
Contributor Author

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
	});

@marionebl
Copy link
Contributor

marionebl commented Feb 3, 2018

Thanks for your patience 👍

@marionebl marionebl merged commit 2c03ec6 into conventional-changelog:master Feb 3, 2018
@gabemeola gabemeola mentioned this pull request May 3, 2018
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants