refactor and simplify [AzureDevOpsTests] (#7076)
* refactor: simplify AzureDevOpsTests service tests via helpers * refactor: apply standard service class patterns to AzureDevOpsTests class Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
@@ -2,11 +2,24 @@ import Joi from 'joi'
|
||||
import {
|
||||
testResultQueryParamSchema,
|
||||
renderTestResultBadge,
|
||||
documentation as commonDocumentation,
|
||||
} from '../test-results.js'
|
||||
import AzureDevOpsBase from './azure-devops-base.js'
|
||||
|
||||
const commonAttrs = {
|
||||
keywords: ['vso', 'vsts', 'azure-devops'],
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
branch: 'master',
|
||||
},
|
||||
queryParams: {
|
||||
passed_label: 'passed',
|
||||
failed_label: 'failed',
|
||||
skipped_label: 'skipped',
|
||||
compact_message: null,
|
||||
},
|
||||
documentation: `
|
||||
<p>
|
||||
To obtain your own badge, you need to get 3 pieces of information:
|
||||
@@ -26,19 +39,7 @@ const commonAttrs = {
|
||||
Optionally, you can specify a named branch:
|
||||
<code>https://img.shields.io/azure-devops/tests/ORGANIZATION/PROJECT/DEFINITION_ID/NAMED_BRANCH.svg</code>.
|
||||
</p>
|
||||
<p>
|
||||
You may change the "passed", "failed" and "skipped" text on this badge by supplying query parameters <code>&passed_label=</code>, <code>&failed_label=</code> and <code>&skipped_label=</code> respectively.
|
||||
<br>
|
||||
There is also a <code>&compact_message</code> query parameter, which will default to displaying ✔, ✘ and ➟, separated by a horizontal bar |.
|
||||
<br>
|
||||
For example, if you want to use a different terminology:
|
||||
<br>
|
||||
<code>/azure-devops/tests/ORGANIZATION/PROJECT/DEFINITION_ID.svg?passed_label=good&failed_label=bad&skipped_label=n%2Fa</code>
|
||||
<br>
|
||||
Or, use symbols:
|
||||
<br>
|
||||
<code>/azure-devops/tests/ORGANIZATION/PROJECT/DEFINITION_ID.svg?compact_message&passed_label=%F0%9F%8E%89&failed_label=%F0%9F%92%A2&skipped_label=%F0%9F%A4%B7</code>
|
||||
</p>
|
||||
${commonDocumentation}
|
||||
`,
|
||||
}
|
||||
|
||||
@@ -71,29 +72,6 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
static examples = [
|
||||
{
|
||||
title: 'Azure DevOps tests',
|
||||
pattern: ':organization/:project/:definitionId',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
failed: 1,
|
||||
skipped: 1,
|
||||
total: 22,
|
||||
}),
|
||||
...commonAttrs,
|
||||
},
|
||||
{
|
||||
title: 'Azure DevOps tests (branch)',
|
||||
pattern: ':organization/:project/:definitionId/:branch',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
branch: 'master',
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
failed: 1,
|
||||
@@ -104,15 +82,6 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
},
|
||||
{
|
||||
title: 'Azure DevOps tests (compact)',
|
||||
pattern: ':organization/:project/:definitionId',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
},
|
||||
queryParams: {
|
||||
compact_message: null,
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
failed: 1,
|
||||
@@ -124,16 +93,11 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
},
|
||||
{
|
||||
title: 'Azure DevOps tests with custom labels',
|
||||
pattern: ':organization/:project/:definitionId',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
},
|
||||
queryParams: {
|
||||
passed_label: 'good',
|
||||
failed_label: 'bad',
|
||||
skipped_label: 'n/a',
|
||||
compact_message: null,
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
@@ -172,15 +136,16 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
})
|
||||
}
|
||||
|
||||
async handle(
|
||||
{ organization, project, definitionId, branch },
|
||||
{
|
||||
compact_message: compactMessage,
|
||||
passed_label: passedLabel,
|
||||
failed_label: failedLabel,
|
||||
skipped_label: skippedLabel,
|
||||
}
|
||||
) {
|
||||
static transform({ aggregatedResultsAnalysis }) {
|
||||
const { totalTests: total, resultsByOutcome } = aggregatedResultsAnalysis
|
||||
const passed = resultsByOutcome.Passed ? resultsByOutcome.Passed.count : 0
|
||||
const failed = resultsByOutcome.Failed ? resultsByOutcome.Failed.count : 0
|
||||
// assume the rest has been skipped
|
||||
const skipped = total - passed - failed
|
||||
return { passed, failed, skipped, total }
|
||||
}
|
||||
|
||||
async fetchTestResults({ organization, project, definitionId, branch }) {
|
||||
const errorMessages = {
|
||||
404: 'build pipeline or test result summary not found',
|
||||
}
|
||||
@@ -193,8 +158,7 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
)
|
||||
|
||||
// https://dev.azure.com/azuredevops-powershell/azuredevops-powershell/_apis/test/ResultSummaryByBuild?buildId=20
|
||||
|
||||
const json = await this.fetch({
|
||||
return await this.fetch({
|
||||
url: `https://dev.azure.com/${organization}/${project}/_apis/test/ResultSummaryByBuild`,
|
||||
options: {
|
||||
qs: { buildId },
|
||||
@@ -202,24 +166,24 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
schema: buildTestResultSummarySchema,
|
||||
errorMessages,
|
||||
})
|
||||
}
|
||||
|
||||
const total = json.aggregatedResultsAnalysis.totalTests
|
||||
|
||||
let passed = 0
|
||||
const passedTests = json.aggregatedResultsAnalysis.resultsByOutcome.Passed
|
||||
if (passedTests) {
|
||||
passed = passedTests.count
|
||||
async handle(
|
||||
{ organization, project, definitionId, branch },
|
||||
{
|
||||
compact_message: compactMessage,
|
||||
passed_label: passedLabel,
|
||||
failed_label: failedLabel,
|
||||
skipped_label: skippedLabel,
|
||||
}
|
||||
|
||||
let failed = 0
|
||||
const failedTests = json.aggregatedResultsAnalysis.resultsByOutcome.Failed
|
||||
if (failedTests) {
|
||||
failed = failedTests.count
|
||||
}
|
||||
|
||||
// assume the rest has been skipped
|
||||
const skipped = total - passed - failed
|
||||
const isCompact = compactMessage !== undefined
|
||||
) {
|
||||
const json = await this.fetchTestResults({
|
||||
organization,
|
||||
project,
|
||||
definitionId,
|
||||
branch,
|
||||
})
|
||||
const { passed, failed, skipped, total } = this.constructor.transform(json)
|
||||
return this.constructor.render({
|
||||
passed,
|
||||
failed,
|
||||
@@ -228,7 +192,7 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
passedLabel,
|
||||
failedLabel,
|
||||
skippedLabel,
|
||||
isCompact,
|
||||
isCompact: compactMessage !== undefined,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,149 +1,27 @@
|
||||
import Joi from 'joi'
|
||||
import {
|
||||
isDefaultTestTotals,
|
||||
isDefaultCompactTestTotals,
|
||||
isCustomTestTotals,
|
||||
isCustomCompactTestTotals,
|
||||
} from '../test-validators.js'
|
||||
import { createServiceTester } from '../tester.js'
|
||||
export const t = await createServiceTester()
|
||||
|
||||
const org = 'azuredevops-powershell'
|
||||
const project = 'azuredevops-powershell'
|
||||
const definitionId = 1
|
||||
const nonExistentDefinitionId = 9999
|
||||
const buildId = 20
|
||||
const uriPrefix = `/${org}/${project}`
|
||||
const azureDevOpsApiBaseUri = `https://dev.azure.com/${org}/${project}/_apis`
|
||||
const mockBadgeUriPath = `${uriPrefix}/${definitionId}`
|
||||
const mockBadgeUri = `${mockBadgeUriPath}.json`
|
||||
const mockBranchBadgeUri = `${mockBadgeUriPath}/master.json`
|
||||
const mockLatestBuildApiUriPath = `/build/builds?definitions=${definitionId}&%24top=1&statusFilter=completed&api-version=5.0-preview.4`
|
||||
const mockLatestBranchBuildApiUriPath = `/build/builds?definitions=${definitionId}&%24top=1&statusFilter=completed&api-version=5.0-preview.4&branchName=refs%2Fheads%2Fmaster`
|
||||
const mockNonExistentBuildApiUriPath = `/build/builds?definitions=${nonExistentDefinitionId}&%24top=1&statusFilter=completed&api-version=5.0-preview.4`
|
||||
const mockTestResultSummaryApiUriPath = `/test/ResultSummaryByBuild?buildId=${buildId}`
|
||||
const latestBuildResponse = {
|
||||
count: 1,
|
||||
value: [{ id: buildId }],
|
||||
}
|
||||
const mockEmptyTestResultSummaryResponse = {
|
||||
aggregatedResultsAnalysis: {
|
||||
totalTests: 0,
|
||||
resultsByOutcome: {},
|
||||
},
|
||||
}
|
||||
const mockTestResultSummaryResponse = {
|
||||
aggregatedResultsAnalysis: {
|
||||
totalTests: 3,
|
||||
resultsByOutcome: {
|
||||
Passed: {
|
||||
count: 1,
|
||||
},
|
||||
Failed: {
|
||||
count: 1,
|
||||
},
|
||||
Skipped: {
|
||||
count: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
const mockTestResultSummarySetup = nock =>
|
||||
nock(azureDevOpsApiBaseUri)
|
||||
.get(mockLatestBuildApiUriPath)
|
||||
.reply(200, latestBuildResponse)
|
||||
.get(mockTestResultSummaryApiUriPath)
|
||||
.reply(200, mockTestResultSummaryResponse)
|
||||
const mockBranchTestResultSummarySetup = nock =>
|
||||
nock(azureDevOpsApiBaseUri)
|
||||
.get(mockLatestBranchBuildApiUriPath)
|
||||
.reply(200, latestBuildResponse)
|
||||
.get(mockTestResultSummaryApiUriPath)
|
||||
.reply(200, mockTestResultSummaryResponse)
|
||||
|
||||
const expectedDefaultAzureDevOpsTestTotals = '1 passed, 1 failed, 1 skipped'
|
||||
const expectedCompactAzureDevOpsTestTotals = '✔ 1 | ✘ 1 | ➟ 1'
|
||||
const expectedCustomAzureDevOpsTestTotals = '1 good, 1 bad, 1 n/a'
|
||||
const expectedCompactCustomAzureDevOpsTestTotals = '💃 1 | 🤦♀️ 1 | 🤷 1'
|
||||
|
||||
function getLabelRegex(label, isCompact) {
|
||||
return isCompact ? `(?:${label} [0-9]*)` : `(?:[0-9]* ${label})`
|
||||
}
|
||||
|
||||
function isAzureDevOpsTestTotals(
|
||||
passedLabel,
|
||||
failedLabel,
|
||||
skippedLabel,
|
||||
isCompact
|
||||
) {
|
||||
const passedRegex = getLabelRegex(passedLabel, isCompact)
|
||||
const failedRegex = getLabelRegex(failedLabel, isCompact)
|
||||
const skippedRegex = getLabelRegex(skippedLabel, isCompact)
|
||||
const separator = isCompact ? ' | ' : ', '
|
||||
const regexStrings = [
|
||||
`^${passedRegex}$`,
|
||||
`^${failedRegex}$`,
|
||||
`^${skippedRegex}$`,
|
||||
`^${passedRegex}${separator}${failedRegex}$`,
|
||||
`^${failedRegex}${separator}${skippedRegex}$`,
|
||||
`^${passedRegex}${separator}${skippedRegex}$`,
|
||||
`^${passedRegex}${separator}${failedRegex}${separator}${skippedRegex}$`,
|
||||
`^no tests$`,
|
||||
]
|
||||
|
||||
return Joi.alternatives().try(
|
||||
...regexStrings.map(regexStr => Joi.string().regex(new RegExp(regexStr)))
|
||||
)
|
||||
}
|
||||
|
||||
const isDefaultAzureDevOpsTestTotals = isAzureDevOpsTestTotals(
|
||||
'passed',
|
||||
'failed',
|
||||
'skipped'
|
||||
)
|
||||
const isCompactAzureDevOpsTestTotals = isAzureDevOpsTestTotals(
|
||||
'✔',
|
||||
'✘',
|
||||
'➟',
|
||||
true
|
||||
)
|
||||
const isCustomAzureDevOpsTestTotals = isAzureDevOpsTestTotals(
|
||||
'good',
|
||||
'bad',
|
||||
'n\\/a'
|
||||
)
|
||||
const isCompactCustomAzureDevOpsTestTotals = isAzureDevOpsTestTotals(
|
||||
'💃',
|
||||
'🤦♀️',
|
||||
'🤷',
|
||||
true
|
||||
)
|
||||
|
||||
t.create('unknown build definition')
|
||||
.get(`${uriPrefix}/${nonExistentDefinitionId}.json`)
|
||||
.get(`/swellaby/opensource/99999999.json`)
|
||||
.expectBadge({ label: 'tests', message: 'build pipeline not found' })
|
||||
|
||||
t.create('404 latest build error response')
|
||||
.get(mockBadgeUri)
|
||||
.get('/swellaby/fake/14.json')
|
||||
.intercept(nock =>
|
||||
nock(azureDevOpsApiBaseUri).get(mockLatestBuildApiUriPath).reply(404)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'tests',
|
||||
message: 'build pipeline or test result summary not found',
|
||||
})
|
||||
|
||||
t.create('no build response')
|
||||
.get(`${uriPrefix}/${nonExistentDefinitionId}.json`)
|
||||
.intercept(nock =>
|
||||
nock(azureDevOpsApiBaseUri).get(mockNonExistentBuildApiUriPath).reply(200, {
|
||||
count: 0,
|
||||
value: [],
|
||||
})
|
||||
)
|
||||
.expectBadge({ label: 'tests', message: 'build pipeline not found' })
|
||||
|
||||
t.create('no test result summary response')
|
||||
.get(mockBadgeUri)
|
||||
.intercept(nock =>
|
||||
nock(azureDevOpsApiBaseUri)
|
||||
.get(mockLatestBuildApiUriPath)
|
||||
.reply(200, latestBuildResponse)
|
||||
.get(mockTestResultSummaryApiUriPath)
|
||||
nock('https://dev.azure.com/swellaby/fake/_apis')
|
||||
.get('/build/builds')
|
||||
.query({
|
||||
definitions: 14,
|
||||
$top: 1,
|
||||
statusFilter: 'completed',
|
||||
'api-version': '5.0-preview.4',
|
||||
})
|
||||
.reply(404)
|
||||
)
|
||||
.expectBadge({
|
||||
@@ -151,113 +29,73 @@ t.create('no test result summary response')
|
||||
message: 'build pipeline or test result summary not found',
|
||||
})
|
||||
|
||||
t.create('invalid test result summary response')
|
||||
.get(mockBadgeUri)
|
||||
t.create('no test result summary response')
|
||||
.get('/swellaby/fake/14.json')
|
||||
.intercept(nock =>
|
||||
nock(azureDevOpsApiBaseUri)
|
||||
.get(mockLatestBuildApiUriPath)
|
||||
.reply(200, latestBuildResponse)
|
||||
.get(mockTestResultSummaryApiUriPath)
|
||||
.reply(200, {})
|
||||
nock('https://dev.azure.com/swellaby/fake/_apis')
|
||||
.get('/build/builds')
|
||||
.query({
|
||||
definitions: 14,
|
||||
$top: 1,
|
||||
statusFilter: 'completed',
|
||||
'api-version': '5.0-preview.4',
|
||||
})
|
||||
.reply(200, { count: 1, value: [{ id: 1234 }] })
|
||||
.get('/test/ResultSummaryByBuild')
|
||||
.query({ buildId: 1234 })
|
||||
.reply(404)
|
||||
)
|
||||
.expectBadge({ label: 'tests', message: 'invalid response data' })
|
||||
|
||||
t.create('no tests in test result summary response')
|
||||
.get(mockBadgeUri)
|
||||
.intercept(nock =>
|
||||
nock(azureDevOpsApiBaseUri)
|
||||
.get(mockLatestBuildApiUriPath)
|
||||
.reply(200, latestBuildResponse)
|
||||
.get(mockTestResultSummaryApiUriPath)
|
||||
.reply(200, mockEmptyTestResultSummaryResponse)
|
||||
)
|
||||
.expectBadge({ label: 'tests', message: 'no tests' })
|
||||
|
||||
t.create('test status')
|
||||
.get(mockBadgeUri)
|
||||
.intercept(mockTestResultSummarySetup)
|
||||
.expectBadge({
|
||||
label: 'tests',
|
||||
message: expectedDefaultAzureDevOpsTestTotals,
|
||||
message: 'build pipeline or test result summary not found',
|
||||
})
|
||||
|
||||
t.create('no build response')
|
||||
.get(`/swellaby/opensource/174.json`)
|
||||
.expectBadge({ label: 'tests', message: 'build pipeline not found' })
|
||||
|
||||
t.create('no tests in test result summary response')
|
||||
.get('/swellaby/opensource/14.json')
|
||||
.expectBadge({ label: 'tests', message: 'no tests' })
|
||||
|
||||
t.create('test status').get('/swellaby/opensource/25.json').expectBadge({
|
||||
label: 'tests',
|
||||
message: isDefaultTestTotals,
|
||||
})
|
||||
|
||||
t.create('test status on branch')
|
||||
.get(mockBranchBadgeUri)
|
||||
.intercept(mockBranchTestResultSummarySetup)
|
||||
.get('/swellaby/opensource/25/master.json')
|
||||
.expectBadge({
|
||||
label: 'tests',
|
||||
message: expectedDefaultAzureDevOpsTestTotals,
|
||||
message: isDefaultTestTotals,
|
||||
})
|
||||
|
||||
t.create('test status with compact message')
|
||||
.get(mockBadgeUri, {
|
||||
.get('/swellaby/opensource/25.json', {
|
||||
qs: {
|
||||
compact_message: null,
|
||||
},
|
||||
})
|
||||
.intercept(mockTestResultSummarySetup)
|
||||
.expectBadge({
|
||||
label: 'tests',
|
||||
message: expectedCompactAzureDevOpsTestTotals,
|
||||
message: isDefaultCompactTestTotals,
|
||||
})
|
||||
|
||||
t.create('test status with custom labels')
|
||||
.get(mockBadgeUri, {
|
||||
.get('/swellaby/opensource/25.json', {
|
||||
qs: {
|
||||
passed_label: 'good',
|
||||
failed_label: 'bad',
|
||||
skipped_label: 'n/a',
|
||||
},
|
||||
})
|
||||
.intercept(mockTestResultSummarySetup)
|
||||
.expectBadge({
|
||||
label: 'tests',
|
||||
message: expectedCustomAzureDevOpsTestTotals,
|
||||
message: isCustomTestTotals,
|
||||
})
|
||||
|
||||
t.create('test status with compact message and custom labels')
|
||||
.get(mockBadgeUri, {
|
||||
qs: {
|
||||
compact_message: null,
|
||||
passed_label: '💃',
|
||||
failed_label: '🤦♀️',
|
||||
skipped_label: '🤷',
|
||||
},
|
||||
})
|
||||
.intercept(mockTestResultSummarySetup)
|
||||
.expectBadge({
|
||||
label: 'tests',
|
||||
message: expectedCompactCustomAzureDevOpsTestTotals,
|
||||
})
|
||||
|
||||
t.create('live test status')
|
||||
.get(mockBadgeUri)
|
||||
.expectBadge({ label: 'tests', message: isDefaultAzureDevOpsTestTotals })
|
||||
|
||||
t.create('live test status on branch')
|
||||
.get(mockBranchBadgeUri)
|
||||
.expectBadge({ label: 'tests', message: isDefaultAzureDevOpsTestTotals })
|
||||
|
||||
t.create('live test status with compact message')
|
||||
.get(mockBadgeUri, {
|
||||
qs: {
|
||||
compact_message: null,
|
||||
},
|
||||
})
|
||||
.expectBadge({ label: 'tests', message: isCompactAzureDevOpsTestTotals })
|
||||
|
||||
t.create('live test status with custom labels')
|
||||
.get(mockBadgeUri, {
|
||||
qs: {
|
||||
passed_label: 'good',
|
||||
failed_label: 'bad',
|
||||
skipped_label: 'n/a',
|
||||
},
|
||||
})
|
||||
.expectBadge({ label: 'tests', message: isCustomAzureDevOpsTestTotals })
|
||||
|
||||
t.create('live test status with compact message and custom labels')
|
||||
.get(mockBadgeUri, {
|
||||
.get('/swellaby/opensource/25.json', {
|
||||
qs: {
|
||||
compact_message: null,
|
||||
passed_label: '💃',
|
||||
@@ -267,5 +105,5 @@ t.create('live test status with compact message and custom labels')
|
||||
})
|
||||
.expectBadge({
|
||||
label: 'tests',
|
||||
message: isCompactCustomAzureDevOpsTestTotals,
|
||||
message: isCustomCompactTestTotals,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user