Skip to content

Commit

Permalink
feat: Add convertToReportTestSuites()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesin11 committed Jul 23, 2020
1 parent 32ba351 commit 8698d88
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 8 deletions.
175 changes: 175 additions & 0 deletions __tests__/analyzer/analyzer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { convertToReportTestSuites, ReportTestSuites } from '../../src/analyzer/analyzer'
import { TestSuites } from 'junit2json'

describe('Analyzer', () => {
describe('convertToReportTestSuites', () => {
describe('Omit some properties', () => {
let testSuites: TestSuites

beforeEach(() => {
testSuites = {
tests: 1,
failures: 1,
testsuite: [{
name: 'testsuite',
tests: 1,
failures: 1,
testcase: [] // Assigning testcase at each testcase
}]
}
})

it('testcase.error', async () => {
const testCase = [{
name: 'testcase',
classname: 'test',
error: [{
inner: 'assert xxx',
}]
}]
testSuites.testsuite[0].testcase = testCase

expect(
convertToReportTestSuites(testSuites).testsuite[0].testcase
).not.toHaveProperty('error')
})

it('testcase.failure', async () => {
const testCase = [{
name: 'testcase',
classname: 'test',
failure: [{
inner: 'assert xxx',
}]
}]
testSuites.testsuite[0].testcase = testCase

expect(
convertToReportTestSuites(testSuites).testsuite[0].testcase
).not.toHaveProperty('failure')
})

it('testcase.system-out', async () => {
const testCase = [{
name: 'testcase',
classname: 'test',
"system-out": ['stdout']
}]
testSuites.testsuite[0].testcase = testCase

expect(
convertToReportTestSuites(testSuites).testsuite[0].testcase
).not.toHaveProperty('system-out')
})

it('testcase.system-err', async () => {
const testCase = [{
name: 'testcase',
classname: 'test',
"system-err": ['stderr']
}]
testSuites.testsuite[0].testcase = testCase

expect(
convertToReportTestSuites(testSuites).testsuite[0].testcase
).not.toHaveProperty('system-err')
})

it('testsuite.system-out', async () => {
const testSuite = [{
name: 'testsuite',
tests: 1,
testcase: [{
name: 'testcase',
classname: 'test',
}],
"system-out": ['stdout']
}]
testSuites.testsuite = testSuite

expect(
convertToReportTestSuites(testSuites).testsuite
).not.toHaveProperty('system-out')
})

it('testsuite.system-err', async () => {
const testSuite = [{
name: 'testsuite',
tests: 1,
testcase: [{
name: 'testcase',
classname: 'test',
}],
"system-err": ['stderr']
}]
testSuites.testsuite = testSuite

expect(
convertToReportTestSuites(testSuites).testsuite
).not.toHaveProperty('system-err')
})

it('testSuites has not failure', async () => {
const testSuites: TestSuites = {
tests: 1,
testsuite: [{
name: 'testsuite',
tests: 1,
testcase: [{
name: 'testcase',
classname: 'test',
}]
}]
}
const expected = JSON.parse(JSON.stringify(testSuites))
expected.testsuite[0].testcase[0].successCount = expect.anything()

expect(convertToReportTestSuites(testSuites)).toStrictEqual(expected)
})
})

describe('Add some properties', () => {
it('successCount = 1 when testcase is success', async () => {
const testSuites: TestSuites = {
tests: 1,
testsuite: [{
name: 'testsuite',
tests: 1,
testcase: [{
name: 'testcase',
classname: 'test',
}]
}]
}
const expected = JSON.parse(JSON.stringify(testSuites))
expected.testsuite[0].testcase[0].successCount = 1

expect(convertToReportTestSuites(testSuites)).toStrictEqual(expected)
})

it('successCount = 0 when testcase is failed', async () => {
const testSuites: TestSuites = {
tests: 1,
failures: 1,
testsuite: [{
name: 'testsuite',
tests: 1,
failures: 1,
testcase: [{
name: 'testcase',
classname: 'test',
failure: [{
inner: 'assert xxx'
}]
}]
}]
}
const expected = JSON.parse(JSON.stringify(testSuites))
delete expected.testsuite[0].testcase[0].failure
expected.testsuite[0].testcase[0].successCount = 0

expect(convertToReportTestSuites(testSuites)).toStrictEqual(expected)
})
})
})
})
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"junit2json": "2.0.0",
"lodash": "4.17.15",
"minimatch": "3.0.4",
"utility-types": "3.10.0",
"yargs": "15.3.1"
},
"devDependencies": {
Expand Down
27 changes: 25 additions & 2 deletions src/analyzer/analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { round } from "lodash"
import { TestSuites } from 'junit2json'
import { TestSuites, TestCase, TestSuite } from 'junit2json'
import { Overwrite, Assign } from 'utility-types'

export type Status = 'SUCCESS' | 'FAILURE' | 'ABORTED' | 'OTHER'
export type TestStatus = 'SUCCESS' | 'FAILURE'
Expand Down Expand Up @@ -61,11 +62,16 @@ export type TestReport = {
createdAt: Date,
branch: string,
service: string,
testSuites: TestSuites
testSuites: ReportTestSuites
status: TestStatus // = testSuites.failures > 0: 'FAILURE', else: 'SUCCESS'
successCount: 0 | 1 // = testSuites.failures > 0: 1, else: 1. For create average success rate in dashboard
}

// Omit properties that may contain free and huge text data.
export type ReportTestSuites = Overwrite<TestSuites, { testsuite: ReportTestSuite[] }>
export type ReportTestSuite = Overwrite<Omit<TestSuite, 'system-out' | 'system-err'>, { testcase: ReportTestCase[] }>
export type ReportTestCase = Assign<Omit<TestCase, 'error' | 'failure' | 'system-out' | 'system-err'>, { successCount: 0 | 1 }>

export type WorkflowParams = {
workflowId: string
workflowRunId: string
Expand All @@ -90,3 +96,20 @@ export const secRound = (sec: number) => {
const PRECISION = 3
return round(sec, PRECISION)
}

export const convertToReportTestSuites = (testSuites: TestSuites): ReportTestSuites => {
const filterd = JSON.parse(JSON.stringify(testSuites))
filterd.testsuite
.forEach((testSuite: TestSuite) => {
delete testSuite["system-out"]
delete testSuite["system-err"]
testSuite.testcase.forEach((testCase: TestCase) => {
(testCase as any).successCount = (testCase.failure || testCase.error) ? 0 : 1
delete testCase["system-out"]
delete testCase["system-err"]
delete testCase.failure
delete testCase.error
})
})
return filterd
}
4 changes: 2 additions & 2 deletions src/analyzer/circleci_analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sumBy, min, max, sortBy, first, last } from "lodash"
import { Status, diffSec, Analyzer, secRound, TestReport, WorkflowParams } from "./analyzer"
import { Status, diffSec, Analyzer, secRound, TestReport, WorkflowParams, convertToReportTestSuites } from "./analyzer"
import { WorkflowRun, SingleBuildResponse, CircleciStatus, TestResponse } from "../client/circleci_client"
import { RepositoryTagMap } from "../client/github_repository_client"
import { TestSuite, TestCase } from "junit2json"
Expand Down Expand Up @@ -190,7 +190,7 @@ export class CircleciAnalyzer implements Analyzer {
createdAt: workflowReport.createdAt,
branch: workflowReport.branch,
service: workflowReport.service,
testSuites,
testSuites: convertToReportTestSuites(testSuites),
status: (testSuites.failures > 0) ? 'FAILURE' : 'SUCCESS',
successCount: (testSuites.failures > 0) ? 0 : 1,
}]
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/github_analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods'
import { sumBy, min, max } from 'lodash'
import { Analyzer, diffSec, Status, TestReport, WorkflowParams } from './analyzer'
import { Analyzer, diffSec, Status, TestReport, WorkflowParams, convertToReportTestSuites } from './analyzer'
import { RepositoryTagMap } from '../client/github_repository_client'
import { TestSuites, parse } from 'junit2json'
import AdmZip from 'adm-zip'
Expand Down Expand Up @@ -169,7 +169,7 @@ export class GithubAnalyzer implements Analyzer {
createdAt: workflowReport.createdAt,
branch: workflowReport.branch,
service: workflowReport.service,
testSuites,
testSuites: convertToReportTestSuites(testSuites),
status: (testSuites.failures && testSuites.failures > 0) ? 'FAILURE' : 'SUCCESS',
successCount: (testSuites.failures && testSuites.failures > 0) ? 0 : 1,
})
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/jenkins_analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Status, Analyzer, secRound, TestReport, WorkflowParams } from "./analyzer"
import { Status, Analyzer, secRound, TestReport, WorkflowParams, convertToReportTestSuites } from "./analyzer"
import { WfapiRunResponse, JenkinsStatus, BuildResponse, CauseAction, GhprbParametersAction, BuildData, ParametersAction, Artifact } from "../client/jenkins_client"
import { sumBy, first } from "lodash"
import { parse } from "junit2json"
Expand Down Expand Up @@ -161,7 +161,7 @@ export class JenkinsAnalyzer implements Analyzer {
createdAt: workflowReport.createdAt,
branch: workflowReport.branch,
service: workflowReport.service,
testSuites,
testSuites: convertToReportTestSuites(testSuites),
status: (testSuites.failures && testSuites.failures > 0) ? 'FAILURE' : 'SUCCESS',
successCount: (testSuites.failures && testSuites.failures > 0) ? 0 : 1,
})
Expand Down

0 comments on commit 8698d88

Please sign in to comment.