Skip to content

Commit

Permalink
Merge pull request #22 from ctrf-io/fix/cli-args
Browse files Browse the repository at this point in the history
fixes #18
  • Loading branch information
Ma11hewThomas authored Dec 8, 2024
2 parents a0f491f + 0fb9821 commit 6434f2d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 52 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "newman-reporter-ctrf-json",
"version": "0.0.7",
"description": "Create a common JSON test report for Postman Newman tests",
"version": "0.0.8",
"description": "A Postman Newman JSON test reporter to create test results reports",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
Expand Down
112 changes: 64 additions & 48 deletions src/generate-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ interface ReporterConfigOptions {
repositoryUrl?: string
branchName?: string
testEnvironment?: string

// CLI-prefixed options
ctrfJsonOutputFile?: string
ctrfJsonOutputDir?: string
ctrfJsonMinimal?: boolean
ctrfJsonTestType?: string
ctrfJsonAppName?: string
ctrfJsonAppVersion?: string
ctrfJsonOsPlatform?: string
ctrfJsonOsRelease?: string
ctrfJsonOsVersion?: string
ctrfJsonBuildName?: string
ctrfJsonBuildNumber?: string
ctrfJsonBuildUrl?: string
ctrfJsonRepositoryName?: string
ctrfJsonRepositoryUrl?: string
ctrfJsonBranchName?: string
ctrfJsonTestEnvironment?: string
}

class GenerateCtrfReport {
Expand All @@ -43,53 +61,26 @@ class GenerateCtrfReport {
private readonly collectionRunOptions: NewmanRunOptions
) {
this.registerEvents()
const normalizedOptions = this.normalizeOptions(reporterOptions)
console.log('Normalized Options:', JSON.stringify(normalizedOptions))

this.reporterConfigOptions = {
outputFile:
reporterOptions?.outputFile ??
reporterOptions?.outputFile ??
this.defaultOutputFile,
outputDir:
reporterOptions?.outputDir ??
reporterOptions?.outputDir ??
this.defaultOutputDir,
minimal: this.parseBoolean(
reporterOptions?.minimal ?? reporterOptions?.minimal ?? 'false'
),
testType:
reporterOptions?.testType ?? reporterOptions?.testType ?? undefined,
appName:
reporterOptions?.appName ?? reporterOptions?.appName ?? undefined,
appVersion:
reporterOptions?.appVersion ?? reporterOptions?.appVersion ?? undefined,
osPlatform:
reporterOptions?.osPlatform ?? reporterOptions?.osPlatform ?? undefined,
osRelease:
reporterOptions?.osRelease ?? reporterOptions?.osRelease ?? undefined,
osVersion:
reporterOptions?.osVersion ?? reporterOptions?.osVersion ?? undefined,
buildName:
reporterOptions?.buildName ?? reporterOptions?.buildName ?? undefined,
buildNumber:
reporterOptions?.buildNumber ??
reporterOptions?.buildNumber ??
undefined,
buildUrl:
reporterOptions?.buildUrl ?? reporterOptions?.buildUrl ?? undefined,
repositoryName:
reporterOptions?.repositoryName ??
reporterOptions?.repositoryName ??
undefined,
repositoryUrl:
reporterOptions?.repositoryUrl ??
reporterOptions?.repositoryUrl ??
undefined,
branchName:
reporterOptions?.branchName ?? reporterOptions?.branchName ?? undefined,
testEnvironment:
reporterOptions?.testEnvironment ??
reporterOptions?.testEnvironment ??
undefined,
outputFile: normalizedOptions.outputFile ?? this.defaultOutputFile,
outputDir: normalizedOptions.outputDir ?? this.defaultOutputDir,
minimal: this.parseBoolean(normalizedOptions.minimal ?? 'false'),
testType: normalizedOptions.testType,
appName: normalizedOptions.appName,
appVersion: normalizedOptions.appVersion,
osPlatform: normalizedOptions.osPlatform,
osRelease: normalizedOptions.osRelease,
osVersion: normalizedOptions.osVersion,
buildName: normalizedOptions.buildName,
buildNumber: normalizedOptions.buildNumber,
buildUrl: normalizedOptions.buildUrl,
repositoryName: normalizedOptions.repositoryName,
repositoryUrl: normalizedOptions.repositoryUrl,
branchName: normalizedOptions.branchName,
testEnvironment: normalizedOptions.testEnvironment,
}

this.ctrfReport = {
Expand Down Expand Up @@ -189,6 +180,34 @@ class GenerateCtrfReport {
})
}

private normalizeOptions(
options: ReporterConfigOptions
): ReporterConfigOptions {
const normalized: ReporterConfigOptions = {}

normalized.outputFile = options.outputFile ?? options.ctrfJsonOutputFile
normalized.outputDir = options.outputDir ?? options.ctrfJsonOutputDir
normalized.minimal = options.minimal ?? options.ctrfJsonMinimal
normalized.testType = options.testType ?? options.ctrfJsonTestType
normalized.appName = options.appName ?? options.ctrfJsonAppName
normalized.appVersion = options.appVersion ?? options.ctrfJsonAppVersion
normalized.osPlatform = options.osPlatform ?? options.ctrfJsonOsPlatform
normalized.osRelease = options.osRelease ?? options.ctrfJsonOsRelease
normalized.osVersion = options.osVersion ?? options.ctrfJsonOsVersion
normalized.buildName = options.buildName ?? options.ctrfJsonBuildName
normalized.buildNumber = options.buildNumber ?? options.ctrfJsonBuildNumber
normalized.buildUrl = options.buildUrl ?? options.ctrfJsonBuildUrl
normalized.repositoryName =
options.repositoryName ?? options.ctrfJsonRepositoryName
normalized.repositoryUrl =
options.repositoryUrl ?? options.ctrfJsonRepositoryUrl
normalized.branchName = options.branchName ?? options.ctrfJsonBranchName
normalized.testEnvironment =
options.testEnvironment ?? options.ctrfJsonTestEnvironment

return normalized
}

private setFilename(filename: string): void {
if (filename.endsWith('.json')) {
this.filename = filename
Expand Down Expand Up @@ -222,9 +241,6 @@ class GenerateCtrfReport {
if (reporterConfigOptions.buildUrl !== undefined) {
this.ctrfEnvironment.buildUrl = reporterConfigOptions.buildUrl
}
if (reporterConfigOptions.buildUrl !== undefined) {
this.ctrfEnvironment.buildUrl = reporterConfigOptions.buildUrl
}
if (reporterConfigOptions.repositoryName !== undefined) {
this.ctrfEnvironment.repositoryName = reporterConfigOptions.repositoryName
}
Expand Down

0 comments on commit 6434f2d

Please sign in to comment.