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: Add support for yaml configuration file #1054

Merged
merged 10 commits into from Apr 16, 2019
@@ -18,18 +18,45 @@ function guessCWD (cwd) {
return cwd
}

function moduleExists (name) {
try {
return typeof require.resolve(name) !== 'undefined'
} catch (e) { return false }
This conversation was marked as resolved by c-bandy

This comment has been minimized.

Copy link
@coreyfarrell

coreyfarrell Apr 5, 2019

Member

I missed this before, please add line-feeds around the return false and tell nyc to ignore the unreachable code:

try {
  return typeof require.resolve(name) !== 'undefined';
} catch (e) {
  /* istanbul ignore next */
  return false;
}
}

Config.loadConfig = function (argv, cwd) {
const rcOptions = [
argv.nycrcPath || '.nycrc',
'.nycrc.json',
'.nycrc.yml',
'.nycrc.yaml',
'nyc.config.js'
]
const rcPath = findUp.sync(rcOptions, { cwd: guessCWD(cwd) })
let config = {}

if (rcPath) {
if (rcPath.toLowerCase().endsWith('.js')) {
const rcPathLower = rcPath.toLowerCase()
if (rcPathLower.endsWith('.js')) {
config = require(rcPath)
} else if (/.+\.y(a?)ml$/.test(rcPathLower)) {
const installedYamlParser = ['js-yaml', 'yaml'].find((name) => moduleExists(name))
This conversation was marked as resolved by c-bandy

This comment has been minimized.

Copy link
@bcoe

bcoe Apr 9, 2019

Member

I'd honestly rather converge on one yaml parser, seems like overkill to support multiple flavors.

what's the reason to not just support js-yaml, it seems to be the more popular.

This comment has been minimized.

Copy link
@JaKXz

JaKXz Apr 10, 2019

Member

@isaacs mentioned supporting both in a previous comment

switch (installedYamlParser) {
case 'js-yaml':
config = require('js-yaml').load(
fs.readFileSync(rcPath, 'utf8')
)
break
case 'yaml':
This conversation was marked as resolved by c-bandy

This comment has been minimized.

Copy link
@coreyfarrell

coreyfarrell Apr 5, 2019

Member

Need to tell istanbul to ignore the yaml case as well since testing will always find js-yaml.

config = require('yaml').parse(
fs.readFileSync(rcPath, 'utf8')
)
break
/* istanbul ignore next */
default:
console.error('you need to install a yaml parser to use a yaml configuration file')
process.exit(1)
}
} else {
config = JSON.parse(
fs.readFileSync(rcPath, 'utf-8')

Some generated files are not rendered by default. Learn more.

@@ -97,6 +97,7 @@
"coveralls": "^3.0.3",
"glob": "^7.1.3",
"is-windows": "^1.0.2",
"js-yaml": "^3.13.0",
"lodash": "^4.17.11",
"newline-regex": "^0.2.1",
"requirejs": "^2.3.6",
@@ -107,6 +108,7 @@
"strip-indent": "^2.0.0",
"tap": "^12.6.1",
"which": "^1.3.1",
"yaml": "^1.4.0",
This conversation was marked as resolved by c-bandy

This comment has been minimized.

Copy link
@coreyfarrell

coreyfarrell Apr 5, 2019

Member

No point installing yaml dev dependency, it'll never be tested (js-yaml is found first).

"zero-fill": "^2.2.3"
},
"engines": {
@@ -0,0 +1,2 @@
exclude:
- ignore.js
@@ -0,0 +1,2 @@
exclude:
- ignore.js
@@ -488,6 +488,48 @@ describe('the nyc cli', function () {
})
})

it('loads configuration from .nycrc.yml', function (done) {
var args = [bin, '--nycrc-path', './.nycrc.yml', process.execPath, './index.js']

var proc = spawn(process.execPath, args, {
cwd: cwd,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdout.should.match(/SF:.*index\.js/)
stdout.should.not.match(/SF:.*ignore\.js/)
done()
})
})

it('loads configuration from .nycrc.yaml', function (done) {
var args = [bin, '--nycrc-path', './.nycrc.yaml', process.execPath, './index.js']

var proc = spawn(process.execPath, args, {
cwd: cwd,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdout.should.match(/SF:.*index\.js/)
stdout.should.not.match(/SF:.*ignore\.js/)
done()
})
})

it('allows .nycrc configuration to be overridden with command line args', function (done) {
var args = [bin, '--exclude=foo.js', process.execPath, './index.js']

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.