Skip to content

Commit

Permalink
enable retryable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
harshanarayana committed Mar 28, 2021
1 parent 87280f3 commit e3752c8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
19 changes: 18 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {execaCommandRunner, getIssueNumber} from '../src/utils/generic'
import {execaCommandRunner, getIssueNumber, getNumberRange} from '../src/utils/generic'
import * as fs from 'fs'
import {promisify} from 'util'

Expand Down Expand Up @@ -36,3 +36,20 @@ test('Long Running Command With Timeout', async () => {
)
expect(state).toBeGreaterThan(0)
}, 300000)

test('Retry Some Commands', async () => {
const retry = getNumberRange(10)
for (let attempt = 0; attempt <= retry.length; attempt++) {
console.log(`Attempt ${attempt}`)
const state = await execaCommandRunner(
'bash',
['invlidcommand'],
new Map<string, string>(),
null,
null,
null,
null
)
expect(state).toBeGreaterThan(0)
}
})
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ inputs:
required: false
description: 'Force Install the Test tool even if the Env does not need it'
default: "false"
test-failure-retry:
required: false
description: 'Number of Retries to be performed on a failed tests. This is to account for some flaky tests'
default: '0'
linter-infra-tool:
required: false
description: 'Tool used to perming linting operation on the repository'
Expand Down
26 changes: 21 additions & 5 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions src/test-infra/tox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class ToxInfra implements TestInfra {
version: string
additionalArgs: string
argMap: Map<string, string> = new Map<string, string>()
retry: number

constructor(version: string, force: boolean, additionalArg: string) {
this.name = 'tox'
this.version = version
this.force = force
this.retry = parseInt(core.getInput('test-failure-retry'))
this.additionalArgs = additionalArg
this.argMap = argToMap(additionalArg)
for (const k of this.argMap.keys()) {
Expand Down Expand Up @@ -127,12 +129,21 @@ class ToxInfra implements TestInfra {
additionalArg.push(...[k, v])
}
}
const state = await commandRunner('tox', additionalArg, true, null, null)

if (state !== 0) {
throw new Error(`Tox Environment ${this.envName()} run completed with an exit code ${state}`)
if (isNaN(this.retry) || this.retry < 1) {
this.retry = 1
}
const retry = [...Array(this.retry).keys()]
for (let attempt = 1; attempt <= retry.length; attempt++) {
core.startGroup(`[Attempt ${attempt}] Run Unit Tests using Tox`)
const state = await commandRunner('tox', additionalArg, true, null, null)
core.endGroup()
if (state === 0) {
return Promise.resolve(state)
} else if (attempt >= retry.length) {
throw new Error(`Tox Environment ${this.envName()} run completed with an exit code ${state}`)
}
}
return state
return Promise.resolve(1)
}

async setVersion(): Promise<number> {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,7 @@ export async function getIssueNumber(jsonString: string): Promise<number> {
const issueNumber = jsonPath.JSONPath({path: issuePathFilter, json: JSON.parse(jsonString)})[0]
return Promise.resolve(parseInt(issueNumber.toString()))
}

export function getNumberRange(count: number): number[] {
return [...Array(count).keys()]
}

0 comments on commit e3752c8

Please sign in to comment.