-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a super straightforward framework for testing Trunk Actions in this repo. Instantiate a `TrunkActionDriver` the same as the other tests, and then call a `preCheck` if necessary, and then pass assertions to `testCallback` (parametrized since action functionality is diverse). In this callback either invoke `driver.runAction()` or use the simpleGit `driver.gitDriver()` and assert based on its error handler or based on `driver.readGitStderr()`. I've added 2 tests to demonstrate this: - `hello_world` (currently disabled for speed, especially since it's just a sample) - `commitlint` (only added a test for a standard failure case. We may want to expand the test coverage for it in the future, including via `trunk run`) We can backfill the remaining actions with tests whenever we feel the need to, but this will help us prevent any action regressions, particularly with commitlint. Example successful [run](https://github.com/trunk-io/plugins/actions/runs/7403264485/job/20142746117?pr=609)
- Loading branch information
1 parent
329e676
commit e6ed22c
Showing
13 changed files
with
404 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
name: Action Tests | ||
description: Run tests against plugin actions | ||
|
||
inputs: | ||
cli-version: | ||
description: Trunk cli version to run test against. Mutually exclusive with `cli-path` | ||
required: false | ||
cli-path: | ||
description: Trunk cli path to run test against. Overrides `cli-version` if set. | ||
required: false | ||
path: | ||
description: The cwd from which to run plugin commands | ||
required: false | ||
default: . | ||
append-args: | ||
description: Additional args to append to the test invocation | ||
required: false | ||
default: actions -- | ||
trunk-token: | ||
description: CI debugger api token | ||
required: true | ||
|
||
runs: | ||
# TODO(Tyler): See if this can be converted to a js action | ||
using: composite | ||
steps: | ||
- name: Setup node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
|
||
- name: Specify defaults | ||
run: | | ||
echo "CLI_PATH=${{ inputs.cli-path }}" >> "$GITHUB_ENV" | ||
|
||
case "$RUNNER_OS" in | ||
Windows) | ||
echo "PLATFORM_APPEND_ARGS=--maxWorkers=5" >> "$GITHUB_ENV" | ||
if [[ "${{ inputs.cli-path }}" == "" ]]; then | ||
echo "CLI_PATH=${{ github.workspace }}\trunk.ps1" >> "$GITHUB_ENV" | ||
fi | ||
;; | ||
esac | ||
shell: bash | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
shell: bash | ||
working-directory: ${{ inputs.path }} | ||
|
||
- name: Run action tests | ||
if: runner.os == 'Windows' | ||
run: npm test ${{ inputs.append-args }} ${{ env.PLATFORM_APPEND_ARGS }} --passWithNoTests --ci | ||
shell: bash | ||
working-directory: ${{ inputs.path }} | ||
env: | ||
PLUGINS_TEST_CLI_VERSION: ${{ inputs.cli-version }} | ||
PLUGINS_TEST_CLI_PATH: ${{ env.CLI_PATH }} | ||
|
||
- name: Run action tests | ||
if: runner.os != 'Windows' | ||
uses: trunk-io/[email protected] | ||
with: | ||
breakpoint-id: trunk-plugins-action-tests | ||
shell: bash | ||
working-directory: ${{ inputs.path }} | ||
trunk-token: ${{ inputs.trunk-token }} | ||
org: trunk-staging-org | ||
run: | ||
npm test ${{ inputs.append-args }} ${{ env.PLATFORM_APPEND_ARGS }} --passWithNoTests --ci | ||
env: | ||
PLUGINS_TEST_CLI_VERSION: ${{ inputs.cli-version }} | ||
PLUGINS_TEST_CLI_PATH: ${{ env.CLI_PATH }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* trunk-ignore-all(eslint/import/no-extraneous-dependencies) */ | ||
import { actionRunTest } from "tests"; | ||
import { TrunkActionDriver } from "tests/driver"; | ||
|
||
const preCheck = (driver: TrunkActionDriver) => { | ||
driver.writeFile( | ||
"commitlint.config.js", | ||
"module.exports = {extends: ['@commitlint/config-conventional']}", | ||
); | ||
}; | ||
|
||
const testCallback = async (driver: TrunkActionDriver) => { | ||
try { | ||
await driver.gitDriver?.commit( | ||
"Test commit", | ||
[], | ||
{ "--allow-empty": null }, | ||
(error, result) => { | ||
expect(error?.message).toContain("subject may not be empty [subject-empty]"); | ||
expect(error?.message).toContain("type may not be empty [type-empty]"); | ||
expect(result).toBeUndefined(); | ||
}, | ||
); | ||
|
||
// Commit step should throw | ||
expect(1).toBe(2); | ||
} catch (_err) { | ||
// Intentionally empty | ||
} | ||
}; | ||
|
||
actionRunTest({ | ||
actionName: "commitlint", | ||
syncGitHooks: true, | ||
testCallback, | ||
preCheck, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { actionRunTest } from "tests"; | ||
import { TrunkActionDriver } from "tests/driver"; | ||
|
||
const testCallback = async (driver: TrunkActionDriver) => { | ||
await driver.gitDriver?.commit("Test commit", [], { "--allow-empty": null }, (error, result) => { | ||
expect(error).toBeNull(); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
expect(driver.readGitStderr()).toContain("[34mHello[31m World"); | ||
}; | ||
|
||
// NOTE(Tyler): This test is currently skipped. To demonstrate its functionality, remove the skipTestIf | ||
actionRunTest({ | ||
actionName: "hello-world-python", | ||
syncGitHooks: true, | ||
testCallback, | ||
skipTestIf: () => true, | ||
}); |
Oops, something went wrong.