-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #642 from gemini-testing/FEI-25418.add_jsonl_reporter
Add jsonl reporter
- Loading branch information
Showing
26 changed files
with
1,087 additions
and
265 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,6 @@ | ||
module.exports = { | ||
SUCCESS: 'success', | ||
FAIL: 'fail', | ||
RETRY: 'retry', | ||
SKIPPED: 'skipped' | ||
}; |
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
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,100 @@ | ||
exports.initReporters = async (rawReporters, runner) => { | ||
await Promise.all([].concat(rawReporters).map((rawReporter) => applyReporter(rawReporter, runner))); | ||
}; | ||
|
||
const reporterHandlers = [ | ||
{ | ||
isMatched: (rawReporter) => typeof rawReporter === 'string' && isJSON(rawReporter), | ||
initReporter: (rawReporter) => initReporter(getReporterDefinition(rawReporter, JSON.parse)) | ||
}, | ||
{ | ||
isMatched: (rawReporter) => typeof rawReporter === 'string', | ||
initReporter: (rawReporter) => initReporter({...getReporterDefinition(rawReporter), type: rawReporter}) | ||
}, | ||
{ | ||
isMatched: (rawReporter) => typeof rawReporter === 'object', | ||
initReporter: (rawReporter) => initReporter(getReporterDefinition(rawReporter, (v) => v)) | ||
}, | ||
{ | ||
isMatched: (rawReporter) => typeof rawReporter === 'function', | ||
initReporter: (rawReporter) => { | ||
validateReporter(rawReporter); | ||
return rawReporter.create(getReporterDefinition(rawReporter)); | ||
} | ||
}, | ||
{ | ||
isMatched: () => true, | ||
initReporter: (rawReporter) => { | ||
throw new TypeError(`Specified reporter must be a string, object or function, but got: "${typeof rawReporter}"`); | ||
} | ||
} | ||
]; | ||
|
||
async function applyReporter(rawReporter, runner) { | ||
for (const handler of reporterHandlers) { | ||
if (!handler.isMatched(rawReporter)) { | ||
continue; | ||
} | ||
|
||
const reporter = await handler.initReporter(rawReporter); | ||
|
||
if (typeof reporter.attachRunner !== 'function') { | ||
throw new TypeError( | ||
'Initialized reporter must have an "attachRunner" function for subscribe on test result events' | ||
); | ||
} | ||
|
||
return reporter.attachRunner(runner); | ||
} | ||
} | ||
|
||
function initReporter(reporter) { | ||
let Reporter; | ||
|
||
try { | ||
Reporter = require(`./${reporter.type}`); | ||
} catch (e) { | ||
if (e.code === 'MODULE_NOT_FOUND') { | ||
throw new Error(`No such reporter: "${reporter.type}"`); | ||
} | ||
throw e; | ||
} | ||
|
||
validateReporter(Reporter); | ||
|
||
return Reporter.create(reporter); | ||
} | ||
|
||
function getReporterDefinition(rawReporter, parser) { | ||
if (!parser) { | ||
return {type: null, path: null}; | ||
} | ||
|
||
const {type, path} = parser(rawReporter); | ||
|
||
if (!type) { | ||
const strRawReporter = typeof rawReporter !== 'string' ? JSON.stringify(rawReporter) : rawReporter; | ||
throw new Error(`Failed to find required "type" field in reporter definition: "${strRawReporter}"`); | ||
} | ||
|
||
return {type, path}; | ||
} | ||
|
||
function validateReporter(Reporter) { | ||
if (typeof Reporter !== 'function') { | ||
throw new TypeError(`Imported reporter must be a function, but got: "${typeof Reporter}"`); | ||
} | ||
|
||
if (typeof Reporter.create !== 'function') { | ||
throw new TypeError('Imported reporter must have a "create" function for initialization'); | ||
} | ||
} | ||
|
||
function isJSON(str) { | ||
try { | ||
JSON.parse(str); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} |
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,21 @@ | ||
module.exports = class BaseInformer { | ||
static create(...args) { | ||
return new this(...args); | ||
} | ||
|
||
log() { | ||
throw new Error('Method must be implemented in child classes'); | ||
} | ||
|
||
warn() { | ||
throw new Error('Method must be implemented in child classes'); | ||
} | ||
|
||
error() { | ||
throw new Error('Method must be implemented in child classes'); | ||
} | ||
|
||
end() { | ||
throw new Error('Method must be implemented in child classes'); | ||
} | ||
}; |
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,22 @@ | ||
const BaseInformer = require('./base'); | ||
const logger = require('../../utils/logger'); | ||
|
||
module.exports = class ConsoleInformer extends BaseInformer { | ||
log(message) { | ||
logger.log(message); | ||
} | ||
|
||
warn(message) { | ||
logger.warn(message); | ||
} | ||
|
||
error(message) { | ||
logger.error(message); | ||
} | ||
|
||
end(message) { | ||
if (message) { | ||
logger.log(message); | ||
} | ||
} | ||
}; |
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,41 @@ | ||
const fs = require('fs'); | ||
const chalk = require('chalk'); | ||
const BaseInformer = require('./base'); | ||
const logger = require('../../utils/logger'); | ||
|
||
module.exports = class FileInformer extends BaseInformer { | ||
constructor(opts) { | ||
super(opts); | ||
|
||
this._fileStream = fs.createWriteStream(opts.path); | ||
this._reporterType = opts.type; | ||
|
||
logger.log(`Information with test results for report: "${opts.type}" will be saved to a file: "${opts.path}"`); | ||
} | ||
|
||
log(message) { | ||
this._fileStream.write(`${this._prepareMsg(message)}\n`); | ||
} | ||
|
||
warn(message) { | ||
this.log(message); | ||
} | ||
|
||
error(message) { | ||
this.log(message); | ||
} | ||
|
||
end(message) { | ||
if (message) { | ||
this._fileStream.end(`${this._prepareMsg(message)}\n`); | ||
} else { | ||
this._fileStream.end(); | ||
} | ||
} | ||
|
||
_prepareMsg(msg) { | ||
return typeof msg === 'object' | ||
? JSON.stringify(msg) | ||
: chalk.stripColor(msg); | ||
} | ||
}; |
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,12 @@ | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
|
||
exports.initInformer = async (opts) => { | ||
if (opts.path) { | ||
await fs.ensureDir(path.dirname(opts.path)); | ||
} | ||
|
||
const informerType = opts.path ? 'file' : 'console'; | ||
|
||
return require(`./${informerType}`).create(opts); | ||
}; |
Oops, something went wrong.