Compare commits

...

2 Commits

Author SHA1 Message Date
Nick Fields
616fa81820 Use spawn not exec to run commands (#88)
* minor: use spawn to stream larger output rather than exec which buffers it

* test: verify distinct error code is returned from large output test

* test: breakout additional integration tests to run in parallel

* test: dont pass/fail PRs for coverage yet
2022-08-03 23:02:05 -04:00
Nick Fields
a25f198007 Setup tests (#87)
* test: move timeout tests to their own job to speed things up slightly

* test: add comment about timeout tests

* test: fix needs in cd job

* test: add jest configuration and first test

* test: setup codecov to track coverage
2022-08-03 10:19:55 -04:00
11 changed files with 7922 additions and 133 deletions

13
.config/jest.config.js Normal file
View File

@@ -0,0 +1,13 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
rootDir: '..',
testEnvironment: 'node',
testMatch: ['<rootDir>/src/**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest',
},
verbose: true,
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{js,ts,jsx,tsx}'],
};

12
.github/codecov.yml vendored Normal file
View File

@@ -0,0 +1,12 @@
# see https://docs.codecov.com/docs/codecovyml-reference
codecov:
require_ci_to_pass: false
comment:
layout: 'diff, flags'
behavior: default
require_changes: true
coverage:
# don't pass/fail PRs for coverage yet
status:
project: off
patch: off

View File

@@ -4,10 +4,181 @@ on:
jobs: jobs:
# runs on branch pushes only # runs on branch pushes only
ci: ci_unit:
name: Run Tests name: Run Unit Tests
if: startsWith(github.ref, 'refs/heads') if: startsWith(github.ref, 'refs/heads')
runs-on: ubuntu-18.04 runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Run Unit Tests
run: npm test
- uses: codecov/codecov-action@v3
with:
directory: ./coverage/
verbose: true
ci_integration_envvar:
name: Run Integration Env Var Tests
if: startsWith(github.ref, 'refs/heads')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: env-vars-passed-through
uses: ./
env:
NODE_OPTIONS: '--max_old_space_size=3072'
with:
timeout_minutes: 1
max_attempts: 2
command: node -e 'console.log(process.env.NODE_OPTIONS)'
ci_integration_large_output:
name: Run Integration Large Output Tests
if: startsWith(github.ref, 'refs/heads')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Test 100MiB of output can be processed
id: large-output
continue-on-error: true
uses: ./
with:
max_attempts: 1
timeout_minutes: 5
command: 'make -C ./test-data/large-output bytes-102400'
- name: Assert test had expected result
uses: nick-invision/assert-action@v1
with:
expected: failure
actual: ${{ steps.large-output.outcome }}
- name: Assert exit code is expected
uses: nick-invision/assert-action@v1
with:
expected: 2
actual: ${{ steps.large-output.outputs.exit_code }}
ci_integration_retry_on_exit_code:
name: Run Integration retry_on_exit_code Tests
if: startsWith(github.ref, 'refs/heads')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: retry_on_exit_code (with expected error code)
id: retry_on_exit_code_expected
uses: ./
continue-on-error: true
with:
timeout_minutes: 1
retry_on_exit_code: 2
max_attempts: 3
command: node -e "process.exit(2)"
- uses: nick-invision/assert-action@v1
with:
expected: failure
actual: ${{ steps.retry_on_exit_code_expected.outcome }}
- uses: nick-invision/assert-action@v1
with:
expected: 3
actual: ${{ steps.retry_on_exit_code_expected.outputs.total_attempts }}
- name: retry_on_exit_code (with unexpected error code)
id: retry_on_exit_code_unexpected
uses: ./
continue-on-error: true
with:
timeout_minutes: 1
retry_on_exit_code: 2
max_attempts: 3
command: node -e "process.exit(1)"
- uses: nick-invision/assert-action@v1
with:
expected: failure
actual: ${{ steps.retry_on_exit_code_unexpected.outcome }}
- uses: nick-invision/assert-action@v1
with:
expected: 1
actual: ${{ steps.retry_on_exit_code_unexpected.outputs.total_attempts }}
ci_integration_continue_on_error:
name: Run Integration continue_on_error Tests
if: startsWith(github.ref, 'refs/heads')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: happy-path (continue_on_error)
id: happy_path_continue_on_error
uses: ./
with:
command: node -e "process.exit(0)"
timeout_minutes: 1
continue_on_error: true
- name: sad-path (continue_on_error)
id: sad_path_continue_on_error
uses: ./
with:
command: node -e "process.exit(33)"
timeout_minutes: 1
continue_on_error: true
- name: Verify continue_on_error returns correct exit code on success
uses: nick-invision/assert-action@v1
with:
expected: 0
actual: ${{ steps.happy_path_continue_on_error.outputs.exit_code }}
- name: Verify continue_on_error exits with correct outcome on success
uses: nick-invision/assert-action@v1
with:
expected: success
actual: ${{ steps.happy_path_continue_on_error.outcome }}
- name: Verify continue_on_error returns correct exit code on error
uses: nick-invision/assert-action@v1
with:
expected: 33
actual: ${{ steps.sad_path_continue_on_error.outputs.exit_code }}
- name: Verify continue_on_error exits with successful outcome when an error occurs
uses: nick-invision/assert-action@v1
with:
expected: success
actual: ${{ steps.sad_path_continue_on_error.outcome }}
ci_integration:
name: Run Integration Tests
if: startsWith(github.ref, 'refs/heads')
runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
@@ -78,42 +249,6 @@ jobs:
command: node -e "process.exit(1)" command: node -e "process.exit(1)"
on_retry_command: node -e "console.log('this is a retry command')" on_retry_command: node -e "console.log('this is a retry command')"
- name: retry_on_exit_code (with expected error code)
id: retry_on_exit_code_expected
uses: ./
continue-on-error: true
with:
timeout_minutes: 1
retry_on_exit_code: 2
max_attempts: 3
command: node -e "process.exit(2)"
- uses: nick-invision/assert-action@v1
with:
expected: failure
actual: ${{ steps.retry_on_exit_code_expected.outcome }}
- uses: nick-invision/assert-action@v1
with:
expected: 3
actual: ${{ steps.retry_on_exit_code_expected.outputs.total_attempts }}
- name: retry_on_exit_code (with unexpected error code)
id: retry_on_exit_code_unexpected
uses: ./
continue-on-error: true
with:
timeout_minutes: 1
retry_on_exit_code: 2
max_attempts: 3
command: node -e "process.exit(1)"
- uses: nick-invision/assert-action@v1
with:
expected: failure
actual: ${{ steps.retry_on_exit_code_unexpected.outcome }}
- uses: nick-invision/assert-action@v1
with:
expected: 1
actual: ${{ steps.retry_on_exit_code_unexpected.outputs.total_attempts }}
- name: on-retry-cmd (on-retry fails) - name: on-retry-cmd (on-retry fails)
id: on-retry-cmd-fails id: on-retry-cmd-fails
uses: ./ uses: ./
@@ -141,41 +276,6 @@ jobs:
expected: failure expected: failure
actual: ${{ steps.sad_path_error.outcome }} actual: ${{ steps.sad_path_error.outcome }}
- name: happy-path (continue_on_error)
id: happy_path_continue_on_error
uses: ./
with:
command: node -e "process.exit(0)"
timeout_minutes: 1
continue_on_error: true
- name: sad-path (continue_on_error)
id: sad_path_continue_on_error
uses: ./
with:
command: node -e "process.exit(33)"
timeout_minutes: 1
continue_on_error: true
- name: Verify continue_on_error returns correct exit code on success
uses: nick-invision/assert-action@v1
with:
expected: 0
actual: ${{ steps.happy_path_continue_on_error.outputs.exit_code }}
- name: Verify continue_on_error exits with correct outcome on success
uses: nick-invision/assert-action@v1
with:
expected: success
actual: ${{ steps.happy_path_continue_on_error.outcome }}
- name: Verify continue_on_error returns correct exit code on error
uses: nick-invision/assert-action@v1
with:
expected: 33
actual: ${{ steps.sad_path_continue_on_error.outputs.exit_code }}
- name: Verify continue_on_error exits with successful outcome when an error occurs
uses: nick-invision/assert-action@v1
with:
expected: success
actual: ${{ steps.sad_path_continue_on_error.outcome }}
- name: retry_on (timeout) fails early if error encountered - name: retry_on (timeout) fails early if error encountered
id: retry_on_timeout_fail id: retry_on_timeout_fail
uses: ./ uses: ./
@@ -220,7 +320,39 @@ jobs:
expected: 2 expected: 2
actual: ${{ steps.retry_on_error.outputs.exit_code }} actual: ${{ steps.retry_on_error.outputs.exit_code }}
# timeout tests (takes longer to run so run last) - name: sad-path (wrong shell for OS)
id: wrong_shell
uses: ./
continue-on-error: true
with:
timeout_minutes: 1
max_attempts: 2
shell: cmd
command: 'dir'
- uses: nick-invision/assert-action@v1
with:
expected: 2
actual: ${{ steps.wrong_shell.outputs.total_attempts }}
- uses: nick-invision/assert-action@v1
with:
expected: failure
actual: ${{ steps.wrong_shell.outcome }}
# timeout tests take longer to run so run in parallel
ci_integration_timeout:
name: Run Integration Timeout Tests
if: startsWith(github.ref, 'refs/heads')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: sad-path (timeout) - name: sad-path (timeout)
id: sad_path_timeout id: sad_path_timeout
uses: ./ uses: ./
@@ -295,24 +427,6 @@ jobs:
expected: failure expected: failure
actual: ${{ steps.sad_path_timeout.outcome }} actual: ${{ steps.sad_path_timeout.outcome }}
- name: sad-path (wrong shell for OS)
id: wrong_shell
uses: ./
continue-on-error: true
with:
timeout_minutes: 1
max_attempts: 2
shell: cmd
command: 'dir'
- uses: nick-invision/assert-action@v1
with:
expected: 2
actual: ${{ steps.wrong_shell.outputs.total_attempts }}
- uses: nick-invision/assert-action@v1
with:
expected: failure
actual: ${{ steps.wrong_shell.outcome }}
ci_windows: ci_windows:
name: Run Windows Tests name: Run Windows Tests
if: startsWith(github.ref, 'refs/heads') if: startsWith(github.ref, 'refs/heads')
@@ -368,9 +482,9 @@ jobs:
# runs on push to master only # runs on push to master only
cd: cd:
name: Publish Action name: Publish Action
needs: ci needs: [ci_integration, ci_integration_timeout, ci_windows]
if: github.ref == 'refs/heads/master' if: github.ref == 'refs/heads/master'
runs-on: ubuntu-18.04 runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2

4
dist/index.js vendored
View File

@@ -778,8 +778,8 @@ function runCmd(attempt) {
done = false; done = false;
(0, core_1.debug)("Running command ".concat(COMMAND, " on ").concat(OS, " using shell ").concat(executable)); (0, core_1.debug)("Running command ".concat(COMMAND, " on ").concat(OS, " using shell ").concat(executable));
child = attempt > 1 && NEW_COMMAND_ON_RETRY child = attempt > 1 && NEW_COMMAND_ON_RETRY
? (0, child_process_1.exec)(NEW_COMMAND_ON_RETRY, { shell: executable }) ? (0, child_process_1.spawn)(NEW_COMMAND_ON_RETRY, { shell: executable })
: (0, child_process_1.exec)(COMMAND, { shell: executable }); : (0, child_process_1.spawn)(COMMAND, { shell: executable });
(_a = child.stdout) === null || _a === void 0 ? void 0 : _a.on('data', function (data) { (_a = child.stdout) === null || _a === void 0 ? void 0 : _a.on('data', function (data) {
process.stdout.write(data); process.stdout.write(data);
}); });

7649
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,7 +8,8 @@
"local": "npm run prepare && node -r dotenv/config ./dist/index.js", "local": "npm run prepare && node -r dotenv/config ./dist/index.js",
"prepare": "ncc build src/index.ts && husky install", "prepare": "ncc build src/index.ts && husky install",
"style:base": "prettier --config ./.config/.prettierrc.yml --ignore-path ./.config/.prettierignore --write ", "style:base": "prettier --config ./.config/.prettierrc.yml --ignore-path ./.config/.prettierignore --write ",
"style": "npm run style:base -- ." "style": "npm run style:base -- .",
"test": "jest -c ./.config/jest.config.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -31,6 +32,7 @@
"@commitlint/config-conventional": "^16.2.1", "@commitlint/config-conventional": "^16.2.1",
"@semantic-release/changelog": "^6.0.1", "@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1", "@semantic-release/git": "^10.0.1",
"@types/jest": "^28.1.6",
"@types/milliseconds": "0.0.30", "@types/milliseconds": "0.0.30",
"@types/node": "^16.11.7", "@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^5.32.0", "@typescript-eslint/eslint-plugin": "^5.32.0",
@@ -40,11 +42,14 @@
"eslint": "^8.21.0", "eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0", "eslint-config-prettier": "^8.5.0",
"husky": "^8.0.1", "husky": "^8.0.1",
"jest": "^28.1.3",
"lint-staged": "^13.0.3", "lint-staged": "^13.0.3",
"prettier": "^2.7.1", "prettier": "^2.7.1",
"semantic-release": "19.0.3", "semantic-release": "19.0.3",
"ts-jest": "^28.0.7",
"ts-node": "9.0.0", "ts-node": "9.0.0",
"typescript": "^4.7.4" "typescript": "^4.7.4",
"yaml-lint": "^1.7.0"
}, },
"lint-staged": { "lint-staged": {
"**/*.ts": [ "**/*.ts": [
@@ -53,6 +58,9 @@
], ],
"**/*.{md,yaml,yml}": [ "**/*.{md,yaml,yml}": [
"npm run style:base --" "npm run style:base --"
],
"**/*.{yaml,yml}": [
"npx yamllint "
] ]
} }
} }

View File

@@ -1,7 +1,11 @@
# these are the bare minimum envvars required
INPUT_TIMEOUT_MINUTES=1 INPUT_TIMEOUT_MINUTES=1
INPUT_MAX_ATTEMPTS=3 INPUT_MAX_ATTEMPTS=3
INPUT_COMMAND="node -e 'process.exit(99)'" INPUT_COMMAND="node -e 'process.exit(99)'"
INPUT_RETRY_WAIT_SECONDS=10 INPUT_CONTINUE_ON_ERROR=false
SHELL=pwsh
INPUT_POLLING_INTERVAL_SECONDS=1 # these are optional
INPUT_RETRY_ON=any #INPUT_RETRY_WAIT_SECONDS=10
#SHELL=pwsh
#INPUT_POLLING_INTERVAL_SECONDS=1
#INPUT_RETRY_ON=any

View File

@@ -1,5 +1,5 @@
import { getInput, error, warning, info, debug, setOutput } from '@actions/core'; import { getInput, error, warning, info, debug, setOutput } from '@actions/core';
import { exec, execSync } from 'child_process'; import { execSync, spawn } from 'child_process';
import ms from 'milliseconds'; import ms from 'milliseconds';
import kill from 'tree-kill'; import kill from 'tree-kill';
@@ -137,8 +137,8 @@ async function runCmd(attempt: number) {
debug(`Running command ${COMMAND} on ${OS} using shell ${executable}`); debug(`Running command ${COMMAND} on ${OS} using shell ${executable}`);
const child = const child =
attempt > 1 && NEW_COMMAND_ON_RETRY attempt > 1 && NEW_COMMAND_ON_RETRY
? exec(NEW_COMMAND_ON_RETRY, { shell: executable }) ? spawn(NEW_COMMAND_ON_RETRY, { shell: executable })
: exec(COMMAND, { shell: executable }); : spawn(COMMAND, { shell: executable });
child.stdout?.on('data', (data) => { child.stdout?.on('data', (data) => {
process.stdout.write(data); process.stdout.write(data);

17
src/util.test.ts Normal file
View File

@@ -0,0 +1,17 @@
import 'jest';
import { getHeapStatistics } from 'v8';
import { wait } from './util';
// mocks the setTimeout function, see https://jestjs.io/docs/timer-mocks
jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');
describe('util', () => {
test('wait', async () => {
const waitTime = 1000;
wait(waitTime);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), waitTime);
});
});

View File

@@ -0,0 +1,13 @@
SHELL = bash
# this tests fix for the following issues
# https://github.com/nick-fields/retry/issues/76
# https://github.com/nick-fields/retry/issues/84
bytes-%:
for i in {1..$*}; do cat kibibyte.txt; done; exit 2
.PHONY: bytes-%
lines-%:
for i in {1..$*}; do echo a; done; exit 2
.PHONY: lines-%

View File

@@ -0,0 +1,13 @@
1: 0000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2: 0081 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
3: 0162 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4: 243 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
5: 324 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
6: 405 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
7: 486 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
8: 567 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
9: 648 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
a: 729 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
b: 810 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
c: 891 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
d: 972 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa