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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/config-util.js
Expand Up @@ -40,13 +40,23 @@ Config.loadConfig = function (argv, cwd) {
if (rcPathLower.endsWith('.js')) {
config = require(rcPath)
} else if (/.+\.y(a?)ml$/.test(rcPathLower)) {
if (moduleExists('js-yaml')) {
console.error('you need to install js-yaml to use a yaml configuration file')
process.exit(1)
const installedYamlParser = ['js-yaml', 'yaml'].find((name) => moduleExists(name))
furudean marked this conversation as resolved.
Show resolved Hide resolved
switch (installedYamlParser) {
case 'js-yaml':
config = require('js-yaml').load(
fs.readFileSync(rcPath, 'utf8')
)
break
case 'yaml':
furudean marked this conversation as resolved.
Show resolved Hide resolved
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)
}
config = require('js-yaml').load(
fs.readFileSync(rcPath, 'utf8')
)
} else {
config = JSON.parse(
fs.readFileSync(rcPath, 'utf-8')
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -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",
Expand All @@ -107,6 +108,7 @@
"strip-indent": "^2.0.0",
"tap": "^12.6.1",
"which": "^1.3.1",
"yaml": "^1.4.0",
furudean marked this conversation as resolved.
Show resolved Hide resolved
"zero-fill": "^2.2.3"
},
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc.yaml
@@ -0,0 +1,2 @@
exclude:
- ignore.js
2 changes: 2 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc.yml
@@ -0,0 +1,2 @@
exclude:
- ignore.js
42 changes: 42 additions & 0 deletions test/nyc-integration.js
Expand Up @@ -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']

Expand Down