siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
243 lines (167 loc) • 8.04 kB
JavaScript
/*
Siesta 5.6.1
Copyright(c) 2009-2022 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license
*/
Class('Siesta.Launcher.Options.Base', {
does : [
Siesta.Launcher.Role.CanPrintWithLauncher,
Siesta.Launcher.Role.CanWorkWithNyc
],
has : {
launcher : { required : true },
options : { required : true, init : Joose.I.Object }
},
methods : {
initialize : function () {
this.options.WRAPPER = this
},
readFile : function () {
return this.launcher.readFile.apply(this.launcher, arguments)
},
printHelp : function () {
return this.launcher.printHelp.apply(this.launcher, arguments)
},
validate : function () {
var options = this.options
var argv = this.launcher.argv
if (argv.length && !options[ 'project-url' ]) options[ 'project-url' ] = argv.shift()
if (!options[ 'project-url' ]) {
this.printError("No project url is provided\n")
this.printHelp()
return false
}
var maxWorkers = options[ 'max-workers' ]
if (maxWorkers) {
maxWorkers = Number(maxWorkers)
if (isNaN(maxWorkers) || maxWorkers <= 0) {
this.printError("The `--max-workers` should be a valid positive number\n")
return false
}
options[ 'max-workers' ] = maxWorkers
}
if (options[ 'verbose' ] && maxWorkers == 1) {
if (options[ 'rerun-failed' ]) this.warn("The --rerun-failed option is disabled because of --verbose and --max-workers=1 combination")
options[ 'rerun-failed' ] = false
}
return this.validateReportOptions(options) && this.validateCoverageOptions(options)
},
validateReportOptions : function (options) {
var reportFormats = options[ 'report-format' ]
var reportFiles = options[ 'report-file' ]
// nothing to validate - validation passed
if (!reportFiles && !reportFormats) return true
var formatsIsArray = reportFormats instanceof Array
var filesIsArray = reportFiles instanceof Array
if (
formatsIsArray && !filesIsArray
|| !formatsIsArray && filesIsArray
|| formatsIsArray && filesIsArray && reportFormats.length != reportFiles.length
) {
this.printError([
'The number of --report-format and --report-file options does not match'
]);
return false
}
// at this stage, either both `reportFormats` and `reportFiles` are array or both are not
if (!filesIsArray) {
// here both are not
// `report-file-prefix` is deprecated
if (!reportFiles && options[ 'report-file-prefix' ]) {
reportFiles = options[ 'report-file-prefix' ]
if (!reportFiles.match(/\{browser\}/)) {
var match = /(.*?)\.([^.]*)$/.exec(reportFiles)
if (match)
reportFiles = match[ 1 ] + '{browser}.' + match[ 2 ]
else
reportFiles = reportFiles + '{browser}'
}
}
if (reportFormats && !reportFiles) {
this.printError([
'`--report-file` option is required, when `--report-format` option is specified'
]);
return false
}
reportFiles = [ reportFiles ]
reportFormats = [ reportFormats || 'json' ]
}
var me = this
var res = Joose.A.each(reportFormats, function (reportFormat, index) {
var res = me.validateSingleReportOptions(reportFormats[ index ], reportFiles[ index ])
if (res) {
reportFormats[ index ] = res.reportFormat
reportFiles[ index ] = res.reportFile
} else
return false
})
// save the expanded variables
options[ 'report-file' ] = reportFiles
// save the lowercased version
options[ 'report-format' ] = reportFormats
return res !== false
},
validateSingleReportOptions : function (reportFormat, reportFile) {
reportFormat = reportFormat.toLowerCase()
if (reportFormat != 'html' && reportFormat != 'json' && reportFormat != 'jsons' && reportFormat != 'junit') {
this.printError([
'Unrecognized report format: ' + reportFormat
]);
return false
}
return {
reportFile : this.prepareReportFileName(reportFile),
reportFormat : reportFormat
}
},
nycOptionsHasReporter : function (positionalArgs) {
return positionalArgs.some((arg) => { return /^--reporter/.test(arg) })
},
normalizeCoverageReportFormat : function (coverageReportFormats) {
if (!(coverageReportFormats instanceof Array)) coverageReportFormats = [ coverageReportFormats ]
Joose.A.each(coverageReportFormats, function (format, index, arr) {
var formats = format.split(/\+|,/)
if (formats.length > 1) coverageReportFormats[ index ] = formats
})
// flatten
coverageReportFormats = Array.prototype.concat.apply([], coverageReportFormats)
// map to lowercase
coverageReportFormats = Joose.A.map(coverageReportFormats, function (format) { return format.toLowerCase() })
// filter out empty entries
coverageReportFormats = Joose.A.grep(coverageReportFormats, function (format) { return Boolean(format) })
var knownCoverageReportFormats = [
'clover', 'cobertura', 'html', 'json-summary', 'json', 'lcov', 'lcovonly', 'none', 'teamcity', 'text-lcov', 'text-summary', 'text'
]
var me = this
Joose.A.each(coverageReportFormats, function (format) {
if (knownCoverageReportFormats.indexOf(format) == -1) {
me.warn([ 'Unrecognized coverage report format: ' + format ]);
}
})
return coverageReportFormats
},
validateCoverageOptions : function (options) {
var rawNycArgv = this.launcher.positionalGroups.nyc
var nycArgv = this.buildNycArgv(rawNycArgv)
var coverageReportFormats = this.normalizeCoverageReportFormat(
options[ 'coverage-report-format' ] || (this.nycOptionsHasReporter(rawNycArgv || []) ? nycArgv.reporter : [])
)
if (coverageReportFormats.length > 0) {
if (options[ 'coverage-report-dir' ]) nycArgv.reportDir = options[ 'coverage-report-dir' ]
nycArgv.reporter = coverageReportFormats
options.coverage = nycArgv
}
return true
},
getReportFileNameTemplateReplacer : function () {
var options = this.options
return function (m, match) {
return options[ match ]
}
},
prepareReportFileName : function (template) {
return template.replace(/\{(\w+)\}/, this.getReportFileNameTemplateReplacer())
}
}
})