Skip to content

Commit 9d52bce

Browse files
authored
chore: add tests and support coverage.json (#14)
* split cli to separate file * 🔥 commander from main code * move lcov into separate function * support coverage.json * move serializers to separate file * 🏁 add tests
1 parent 2140cfe commit 9d52bce

File tree

10 files changed

+2758
-212
lines changed

10 files changed

+2758
-212
lines changed

bin/css-coverage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
process.bin = process.title = 'css-coverage'
44

5-
require('../src/runCoverage')
5+
require('../src/cli')

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
"name": "css-coverage",
33
"version": "2.0.3",
44
"scripts": {
5+
"lint": "standard --fix",
56
"pretest": "sass --source-map ./test/test.scss ./test/test.css",
6-
"test": "./bin/css-coverage.js --html ./test/test.html --css ./test/test.css --lcov ./test/test.lcov --ignore-declarations 'move-to,move-foobar'",
7+
"test": "npm run-script test:unit && npm run-script test:cli",
8+
"test:unit": "jest",
9+
"test:cli": "./bin/css-coverage.js --html ./test/test.html --css ./test/test.css --lcov ./test/test.lcov --ignore-declarations 'move-to,move-foobar'",
710
"test:debug": "node --inspect-brk ./bin/css-coverage.js --html ./test/test.html --css ./test/test.css --lcov ./test/test.lcov --ignore-declarations 'move-to,move-foobar'",
8-
"posttest": "standard --fix"
11+
"posttest": "npm run-script lint"
912
},
1013
"dependencies": {
1114
"bunyan": "^1.8.12",
@@ -32,7 +35,9 @@
3235
"author": "Philip Schatz <phil@cnx.org>",
3336
"license": "MIT",
3437
"devDependencies": {
38+
"jest": "^24.7.1",
3539
"sass": "^1.17.0",
36-
"standard": "^12.0.1"
40+
"standard": "^12.0.1",
41+
"tmp": "^0.1.0"
3742
}
3843
}

src/cli.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const commander = require('commander')
4+
const { doStuff, logger } = require('./runCoverage')
5+
const { toJson, toLcov } = require('./serialize')
6+
7+
const STATUS_CODE = {
8+
ERROR: 111,
9+
OK: 0
10+
}
11+
12+
function parseFileName (filePath) {
13+
return path.resolve(process.cwd(), filePath)
14+
}
15+
16+
function parseTokenList (tokenString) {
17+
return tokenString.split(',').map(token => token.trim().toLowerCase())
18+
}
19+
20+
commander
21+
// .usage('[options]')
22+
.description(`Generate coverage info for a CSS file against an HTML file.
23+
24+
This supports loading sourcemaps by using the sourceMappingURL=FILENAME.map CSS comment.
25+
26+
Use the LOG_LEVEL environment variable for more verbose logging. Values: error,warn,info,debug,trace .`)
27+
.option('--html [path/to/file.html]', 'path to a local HTML file', parseFileName) // TODO: Support multiple
28+
.option('--css [path/to/file.css]', 'path to a local CSS file', parseFileName)
29+
.option('--lcov [path/to/output.lcov]', 'the LCOV output file', parseFileName)
30+
.option('--json [path/to/coverage.json]', 'the coverage.json file', parseFileName)
31+
.option('--ignore-source-map', 'disable loading the sourcemap if one is found')
32+
.option('--ignore-declarations [move-to,content]', 'A comma-separated list of declarations to ignore', parseTokenList)
33+
.parse(process.argv)
34+
35+
// Validate args
36+
if (!commander.html && !commander.css) {
37+
commander.help()
38+
}
39+
if (commander.html) {
40+
if (!fs.statSync(commander.html).isFile()) {
41+
console.error('ERROR: Invalid argument. HTML file not found at ' + commander.html)
42+
process.exit(STATUS_CODE.ERROR)
43+
}
44+
} else {
45+
console.error('ERROR: Missing argument. At least 1 HTML file must be specified')
46+
process.exit(STATUS_CODE.ERROR)
47+
}
48+
if (commander.css) {
49+
if (!fs.statSync(commander.css).isFile()) {
50+
console.error('ERROR: Invalid argument. CSS file not found at ' + commander.css)
51+
process.exit(STATUS_CODE.ERROR)
52+
}
53+
} else {
54+
console.error('ERROR: Missing argument. A CSS file must be specified')
55+
process.exit(STATUS_CODE.ERROR)
56+
}
57+
58+
doStuff(commander.css, commander.html, commander.ignoreDeclarations, commander.ignoreSourceMap)
59+
.then(({ coverage, sourceMapPath }) => {
60+
if (commander.lcov) {
61+
const str = toLcov(coverage, sourceMapPath, commander.lcov)
62+
fs.writeFileSync(commander.lcov, str)
63+
}
64+
if (commander.json) {
65+
const str = toJson(coverage, sourceMapPath, commander.json)
66+
fs.writeFileSync(commander.json, str)
67+
}
68+
69+
logger.debug('Done writing LCOV string')
70+
}, err => {
71+
logger.fatal(err)
72+
process.exit(STATUS_CODE.ERROR)
73+
})

0 commit comments

Comments
 (0)