Skip to content

Commit 54e73f4

Browse files
kgravesisaacs
authored andcommittedJun 30, 2016
adds .taprc file support #228
a few changes to .taprc file support #234 includes: - moves parsing of `process.env.TAP` back to where it was - restructures dotfile tests - updates a test that was copied from another, but not updated
1 parent f8964a0 commit 54e73f4

File tree

6 files changed

+136
-26
lines changed

6 files changed

+136
-26
lines changed
 

‎bin/run.js

+42-26
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,31 @@ var supportsColor = require('supports-color')
88
var nycBin = require.resolve('nyc/bin/nyc.js')
99
var glob = require('glob')
1010
var isexe = require('isexe')
11+
var osHomedir = require('os-homedir')
12+
var extend = require('deep-extend')
13+
var yaml = require('js-yaml')
14+
var parseRcFile = require('./utils').parseRcFile
1115

1216
var coverageServiceTest = process.env.COVERAGE_SERVICE_TEST === 'true'
1317

1418
// console.error(process.argv.join(' '))
1519
// console.error('CST=%j', process.env.COVERAGE_SERVICE_TEST)
1620
// console.error('CRT=%j', process.env.COVERALLS_REPO_TOKEN)
17-
// console.error('CCT=%j', process.env.CODECOV_TOKEN)
1821
if (coverageServiceTest) {
1922
console.log('COVERAGE_SERVICE_TEST')
2023
}
2124

2225
// Add new coverage services here.
2326
// it'll check for the environ named and pipe appropriately.
27+
//
28+
// Currently only Coveralls is supported, but the infrastructure
29+
// is left in place in case some noble soul fixes the codecov
30+
// module in the future. See https://github.com/tapjs/node-tap/issues/270
2431
var coverageServices = [
2532
{
2633
env: 'COVERALLS_REPO_TOKEN',
2734
bin: require.resolve('coveralls/bin/coveralls.js'),
2835
name: 'Coveralls'
29-
},
30-
{
31-
env: 'CODECOV_TOKEN',
32-
bin: require.resolve('codecov.io/bin/codecov.io.js'),
33-
name: 'Codecov'
3436
}
3537
]
3638

@@ -44,7 +46,17 @@ function main () {
4446
process.exit(1)
4547
}
4648

47-
var options = parseArgs(args)
49+
// set default args
50+
var defaults = constructDefaultArgs()
51+
52+
// parse dotfile
53+
var rcOptions = parseRcFile(osHomedir() + '/.taprc')
54+
55+
// supplement defaults with parsed rc options
56+
defaults = extend(defaults, rcOptions)
57+
58+
// parse args
59+
var options = parseArgs(args, defaults)
4860

4961
if (!options) {
5062
return
@@ -93,26 +105,34 @@ function main () {
93105
runTests(options)
94106
}
95107

96-
function parseArgs (args) {
97-
var options = {}
108+
function constructDefaultArgs () {
109+
var defaultArgs = {
110+
nodeArgs: [],
111+
nycArgs: [],
112+
timeout: process.env.TAP_TIMEOUT || 30,
113+
color: supportsColor,
114+
reporter: null,
115+
files: [],
116+
bail: false,
117+
saveFile: null,
118+
pipeToService: false,
119+
coverageReport: null,
120+
openBrowser: true
121+
}
98122

99-
options.nodeArgs = []
100-
options.nycArgs = []
101-
options.timeout = process.env.TAP_TIMEOUT || 30
102-
// coverage tools run slow.
103-
/* istanbul ignore else */
104123
if (global.__coverage__) {
105-
options.timeout = 240
124+
defaultArgs.timeout = 240
106125
}
107126

108-
options.color = supportsColor
109127
if (process.env.TAP_COLORS !== undefined) {
110-
options.color = !!(+process.env.TAP_COLORS)
128+
defaultArgs.color = !!(+process.env.TAP_COLORS)
111129
}
112-
options.reporter = null
113-
options.files = []
114-
options.bail = false
115-
options.saveFile = null
130+
131+
return defaultArgs
132+
}
133+
134+
function parseArgs (args, defaults) {
135+
var options = defaults || {}
116136

117137
var singleFlags = {
118138
b: 'bail',
@@ -131,7 +151,6 @@ function parseArgs (args) {
131151

132152
// If we're running under Travis-CI with a Coveralls.io token,
133153
// then it's a safe bet that we ought to output coverage.
134-
options.pipeToService = false
135154
for (var i = 0; i < coverageServices.length && !options.pipeToService; i++) {
136155
if (process.env[coverageServices[i].env]) {
137156
options.pipeToService = true
@@ -140,9 +159,6 @@ function parseArgs (args) {
140159

141160
var defaultCoverage = options.pipeToService
142161

143-
options.coverageReport = null
144-
options.openBrowser = true
145-
146162
for (i = 0; i < args.length; i++) {
147163
var arg = args[i]
148164
if (arg.charAt(0) !== '-' || arg === '-') {
@@ -428,7 +444,7 @@ function runCoverageReportOnly (options, code, signal) {
428444
// console.error('run coverage report', args)
429445

430446
var child
431-
// automatically hook into coveralls and/or codecov
447+
// automatically hook into coveralls
432448
if (options.coverageReport === 'text-lcov' && options.pipeToService) {
433449
// console.error('pipeToService')
434450
child = spawn(node, args, { stdio: [ 0, 'pipe', 2 ] })

‎bin/utils.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var fs = require('fs')
2+
var yaml = require('js-yaml')
3+
4+
function parseYaml (contents) {
5+
try {
6+
var parsed = yaml.safeLoad(contents)
7+
8+
// js-yaml parses an empty file as undefined, one with only comments as
9+
// null. We want to return an empty object in these cases.
10+
if (!parsed) {
11+
parsed = {}
12+
}
13+
14+
return parsed
15+
} catch(er) {
16+
// invalid yaml, fail gracefully
17+
return {}
18+
}
19+
}
20+
21+
function parseRcFile (path) {
22+
var parsed = {}
23+
24+
try {
25+
var contents = fs.readFileSync(path, 'utf8')
26+
parsed = parseYaml(contents)
27+
} catch(er) {
28+
// if no dotfile exists, fail gracefully
29+
parsed = {}
30+
}
31+
32+
return parsed
33+
}
34+
35+
module.exports = {
36+
parseYaml: parseYaml,
37+
parseRcFile: parseRcFile
38+
}

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
"codecov.io": "0.1.6",
1818
"coveralls": "^2.11.2",
1919
"deeper": "^2.1.0",
20+
"deep-extend": "0.4.1",
2021
"foreground-child": "^1.3.3",
2122
"glob": "^7.0.0",
2223
"isexe": "^1.0.0",
2324
"js-yaml": "^3.3.1",
2425
"nyc": "^6.6.1",
2526
"only-shallow": "^1.0.2",
2627
"opener": "^1.4.1",
28+
"os-homedir": "1.0.1",
2729
"readable-stream": "^2.0.2",
2830
"signal-exit": "^2.0.0",
2931
"stack-utils": "^0.4.0",

‎test/fixtures/invalid-rc-file.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdf: asdf: asdf

‎test/fixtures/valid-rc-file.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this is a comment
2+
timeout: 9999
3+
coverage: false
4+
coverageReport: false
5+
reporter: 'classic'

‎test/utils.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var fs = require('fs')
2+
var t = require('../')
3+
var utils = require('../bin/utils')
4+
5+
t.test('parseYaml', function (t) {
6+
t.test('returns empty object when not valid yaml', function (t) {
7+
t.strictSame(utils.parseYaml('a: b c: d'), {})
8+
t.end()
9+
})
10+
11+
t.test('returns empty object when empty file', function (t) {
12+
t.strictSame(utils.parseYaml(''), {})
13+
t.end()
14+
})
15+
16+
t.test('returns empty object when only comments', function (t) {
17+
t.strictSame(utils.parseYaml('# this is a comment'), {})
18+
t.end()
19+
})
20+
21+
t.end()
22+
})
23+
24+
t.test('parseRcFile', function (t) {
25+
t.test('when file does not exist, returns empty object', function (t) {
26+
t.strictSame(utils.parseRcFile('./this-file-does-not-exist'), {})
27+
t.end()
28+
})
29+
30+
t.test('returns empty object when not valid yaml', function (t) {
31+
t.strictSame(utils.parseRcFile('./test/fixtures/invalid-rc-file.yml'), {})
32+
t.end()
33+
})
34+
35+
t.test('parses when valid yaml', function (t) {
36+
var expected = {
37+
timeout: 9999,
38+
coverage: false,
39+
coverageReport: false,
40+
reporter: 'classic'
41+
}
42+
var actual = utils.parseRcFile('./test/fixtures/valid-rc-file.yml')
43+
t.strictSame(actual, expected)
44+
t.end()
45+
})
46+
47+
t.end()
48+
})

0 commit comments

Comments
 (0)
Please sign in to comment.