Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cli can now output json with the
--json
flag (#76)
* add command line option to have output be in JSON format * ensure reporter files are pushed, update test describe blocks * Adds docker-compose.test for running unit tests * update readme for options * dd json output option: fix build because of inproper test setup * raise error when attempting buildReport on Reporter object * cleanup CLI options, remove unused code and duplcated tests * dd json output option: remove function that was no longer being used * dd json output option: dry out usage messages * dd json output option: fix cli_reporter test that failed in docker-compose, remove extra line * indentation
- Loading branch information
1 parent
9482bd9
commit c4b13d2
Showing
9 changed files
with
456 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: '2' | ||
services: | ||
dockerfilelint: | ||
build: . | ||
image: dockerfilelint | ||
entrypoint: npm test | ||
volumes: | ||
- ./bin:/dockerfilelint/bin | ||
- ./lib:/dockerfilelint/lib | ||
- ./test:/dockerfilelint/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// { | ||
// "file": "file", | ||
// "issues_count": 3, | ||
// "issues" : [ | ||
// { | ||
// "line": 4, | ||
// "content": "FROM ...", | ||
// "category": "clarity", | ||
// "title": "...", | ||
// "description": "...." | ||
// }, ..., {} | ||
// ] | ||
// } | ||
|
||
'use strict'; | ||
|
||
const Reporter = require('./reporter'); | ||
|
||
class JsonReporter extends Reporter { | ||
constructor (opts) { | ||
super(opts); | ||
|
||
this.json = {}; | ||
} | ||
|
||
// build a report object for data given via addFile | ||
buildReport () { | ||
let self = this; | ||
let totalIssues = 0; | ||
let reportFiles = []; | ||
|
||
Object.keys(self.fileReports).forEach((file) => { | ||
let fileReport = self.fileReports[file]; | ||
let linesWithItems = Object.keys(fileReport.itemsByLine); | ||
|
||
let jsonReport = { file: file, issues_count: fileReport.uniqueIssues, issues: [] }; | ||
|
||
if (linesWithItems.length === 0) { | ||
reportFiles.push(jsonReport); | ||
return; | ||
} | ||
|
||
totalIssues += fileReport.uniqueIssues; | ||
|
||
linesWithItems.forEach((lineNum) => { | ||
let lineContent = fileReport.contentArray[parseInt(lineNum, 10) - 1]; | ||
|
||
fileReport.itemsByLine[lineNum].forEach((item) => { | ||
let lineIssueJson = { | ||
line: lineNum, | ||
content: lineContent, | ||
category: item.category, | ||
title: item.title, | ||
description: item.description | ||
}; | ||
|
||
jsonReport.issues.push(lineIssueJson); | ||
}); | ||
}); | ||
|
||
reportFiles.push(jsonReport); | ||
}); | ||
|
||
this.json.files = reportFiles; | ||
this.json.totalIssues = totalIssues; | ||
|
||
return { toString: () => JSON.stringify(this.json), totalIssues: totalIssues }; | ||
} | ||
} | ||
|
||
module.exports = JsonReporter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const notDeepStrictEqual = require('assert').notDeepStrictEqual; | ||
|
||
class Reporter { | ||
constructor (opts) { | ||
this.fileReports = {}; | ||
} | ||
|
||
// group file items by line for easy reporting | ||
addFile (file, fileContent, items) { | ||
let self = this; | ||
if (!file) return self; | ||
let fileReport = self.fileReports[file] || { | ||
itemsByLine: {}, | ||
uniqueIssues: 0, | ||
contentArray: (fileContent || '').replace('\r', '').split('\n') | ||
}; | ||
let ibl = fileReport.itemsByLine; | ||
[].concat(items).forEach((item) => { | ||
if (ibl[String(item.line)]) { | ||
try { | ||
ibl[String(item.line)].forEach((lineItem) => { | ||
notDeepStrictEqual(item, lineItem); | ||
}); | ||
ibl[String(item.line)].push(item); | ||
fileReport.uniqueIssues = fileReport.uniqueIssues + 1; | ||
} catch (err) { | ||
// ignore duplicate | ||
} | ||
} else { | ||
ibl[String(item.line)] = [ item ]; | ||
fileReport.uniqueIssues = fileReport.uniqueIssues + 1; | ||
} | ||
}); | ||
self.fileReports[file] = fileReport; | ||
return self; | ||
} | ||
|
||
// build a report object for data given via addFile | ||
buildReport () { | ||
// TO BE OVERRIDDEN BY SUB CLASSES | ||
throw new Error("#buildReport() must be defined in a child class"); | ||
} | ||
} | ||
|
||
module.exports = Reporter; |
Oops, something went wrong.