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

Add support for file-based configuration #75

Merged
merged 4 commits into from Mar 9, 2020

Conversation

mtlewis
Copy link
Contributor

@mtlewis mtlewis commented Mar 5, 2020

Description

This PR adds support for loading lockfile-lint configuration from a file, rather than supplying it on the command line. It uses cosmiconfig, which gives us a bunch of sensible configuration file support for free. Command-line options always override options specified in config files.

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)

Related Issue

Welp - I didn't notice that only PRs with open issues would be accepted before I worked on this, so I created one (#74). Sorry for not quite following the guidelines!

Motivation and Context

See #74.

How Has This Been Tested?

So far, I've only tested this manually. I'll be happy to circle back and add tests if there's a positive reaction to #74.

Checklist:

  • I have updated the documentation (if required).
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I added a picture of a cute animal cause it's fun

b88475796z1_20161206174812_000g7ge7mhd2-0-4el676zv61zhvnrpcn2_ct677x380

@lirantal lirantal self-requested a review March 6, 2020 08:28
@lirantal lirantal added the enhancement New feature or request label Mar 6, 2020
@lirantal
Copy link
Owner

lirantal commented Mar 6, 2020

This is wonderful @mtlewis, thanks for working on this ❤️
Lovely picture too!

I'll review it shortly

packages/lockfile-lint/README.md Outdated Show resolved Hide resolved

let fileConfig
if (cosmiconfigResult) {
fileConfig = validateConfig(cosmiconfigResult.config)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtlewis is this a synchronous operation? what happens if it throws?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's synchronous since we're using cosmiconfigSync. I hadn't spotted that it could throw though, thanks for pointing it out. Newest version of the code now handles errors, ignoring file-based config and logging an error.

@lirantal
Copy link
Owner

lirantal commented Mar 6, 2020

@mtlewis I'm happy to land this.
Are you able to add the relevant tests to match up with code coverage requirement? that's why the build is failing now.

@codecov-io
Copy link

codecov-io commented Mar 6, 2020

Codecov Report

Merging #75 into master will increase coverage by 1.79%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #75      +/-   ##
==========================================
+ Coverage   95.08%   96.87%   +1.79%     
==========================================
  Files          11       11              
  Lines         183      192       +9     
  Branches       29       31       +2     
==========================================
+ Hits          174      186      +12     
+ Misses          8        5       -3     
  Partials        1        1
Impacted Files Coverage Δ
packages/lockfile-lint/src/config.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8e3cdbe...76df197. Read the comment docs.

@mtlewis mtlewis force-pushed the cosmiconfig branch 2 times, most recently from bdbe3c1 to 81459e3 Compare March 6, 2020 17:38
@mtlewis
Copy link
Contributor Author

mtlewis commented Mar 6, 2020

Hey @lirantal, thanks for your review! I've added some tests. I also did a bit of refactoring of the PR, since the way it was before made it harder to retain the argument validation that's configured in yargs. Instead, we now load the cosmiconfig first, then pass it into yargs as the base config. This allows us to keep the configuration rules in yargs. Let me know what you think!

@mtlewis mtlewis changed the title (wip) Add support for file-based configuration Add support for file-based configuration Mar 6, 2020
@mtlewis
Copy link
Contributor Author

mtlewis commented Mar 6, 2020

By the way - I'm not sure coverage is currently working in cli.js. I believe I've covered all the new code in cli.js in tests, but both before and after this change, the detected coverage in cli.js is 0. I suspect this could be something to do with the fact that the tests for cli.js are currently executing in a separate process.

I guess the reason CI is failing in this PR is that cli.js now has more lines of code, which means its 0% coverage contributes more to the overall number.

@lirantal
Copy link
Owner

lirantal commented Mar 7, 2020

Yep, I see what you mean there about the cli.js not being covered in unit tests (and indeed in a different process they aren't being counted for).

What if you move this code

const {cosmiconfigSync} = require('cosmiconfig')
let cosmiconfigResult
try {
cosmiconfigResult = cosmiconfigSync('lockfile-lint').search()
} catch (err) {
debug(`error encountered while loading configuration: ${err}`)
}
let fileConfig = {}
if (cosmiconfigResult) {
fileConfig = cosmiconfigResult.config
debug(
`loaded the following config from ${cosmiconfigResult.filepath}: ${JSON.stringify(fileConfig)}`
)
}
to its own config.js module, add unit test coverage to it, and then require it in cli.js?

Tip: to better understand what lines of code are missing coverage, you can run yarn run coverage:view

@mtlewis
Copy link
Contributor Author

mtlewis commented Mar 9, 2020

Hey @lirantal thanks for the feedback and suggestion - how about this: 76df197? Since the whole of cli.js was previously uncovered, I figured it might be good to refactor the whole thing so that it's testable without launching a child process, and then separate out the testing of the option parsing/validation from the cli tests in cli.js.

@lirantal
Copy link
Owner

lirantal commented Mar 9, 2020

Ok, taking a look ;-)

@lirantal
Copy link
Owner

lirantal commented Mar 9, 2020

this looks overall good, thanks @mtlewis 🙏

@lirantal lirantal merged commit e183593 into lirantal:master Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants