Skip to content

Commit a7b00cd

Browse files
committed
Add fail-build-on-error property
Certain test runners (e.g. the SalesForce DX [Apex test runner](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_apex.htm)) will not necessarily return a non-zero value when they encounter failing tests. This means that a build can pass with failing tests. If we are already using this plugin to annotate failing tests when we see them, we might as well hook into it to allow ourselves to fail the build then as well.
1 parent 21fa929 commit a7b00cd

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ There are two options for this:
4444
* `file`
4545
* displays: `MyClass::UnderTest text of the failed expectation in path/to/my_class/under_test.file_ext`
4646

47+
### `fail-build-on-error` (optional)
48+
Default: `false`
49+
50+
If this setting is true and any errors are found in the JUnit XML files during
51+
parsing, the annotation step will exit with a non-zero value, which should cause
52+
the build to fail.
53+
4754
## Developing
4855

4956
To test the junit parser (in Ruby) and plugin hooks (in Bash):

hooks/command

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@ if grep -q "<details>" "$annotation_path"; then
4040
echo "--- :buildkite: Creating annotation"
4141
# shellcheck disable=SC2002
4242
cat "$annotation_path" | buildkite-agent annotate --context junit --style error
43+
44+
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAIL_BUILD_ON_ERROR:-false}" =~ (true|on|1) ]]
45+
then
46+
echo "--- :boom: Failing build due to error"
47+
exit 1
48+
fi
4349
fi

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ configuration:
1414
enum:
1515
- classname
1616
- file
17+
fail-build-on-error:
18+
type: boolean
1719
required:
1820
- artifacts
1921
additionalProperties: false

tests/command.bats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load "$BATS_PATH/load.bash"
99

1010
@test "runs the annotator and creates the annotation" {
1111
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS="junits/*.xml"
12+
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAIL_BUILD_ON_ERROR=false
1213

1314
artifacts_tmp="tests/tmp/$PWD/junit-artifacts"
1415
annotation_tmp="tests/tmp/$PWD/junit-annotation"
@@ -33,10 +34,36 @@ load "$BATS_PATH/load.bash"
3334
unstub docker
3435
}
3536

37+
@test "returns an error if fail-build-on-error is true" {
38+
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS="junits/*.xml"
39+
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAIL_BUILD_ON_ERROR=true
40+
41+
artifacts_tmp="tests/tmp/$PWD/junit-artifacts"
42+
annotation_tmp="tests/tmp/$PWD/junit-annotation"
43+
44+
stub mktemp \
45+
"-d junit-annotate-plugin-artifacts-tmp.XXXXXXXXXX : mkdir -p $artifacts_tmp; echo $artifacts_tmp" \
46+
"-d junit-annotate-plugin-annotation-tmp.XXXXXXXXXX : mkdir -p $annotation_tmp; echo $annotation_tmp"
47+
48+
stub buildkite-agent "artifact download junits/*.xml /plugin/tests/tmp//plugin/junit-artifacts : echo Downloaded artifacts" \
49+
"annotate --context junit --style error : echo Annotation added"
50+
51+
stub docker "--log-level error run --rm --volume /plugin/tests/tmp//plugin/junit-artifacts:/junits --volume /plugin/hooks/../ruby:/src --env BUILDKITE_PLUGIN_JUNIT_ANNOTATE_JOB_UUID_FILE_PATTERN= --env BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAILURE_FORMAT= ruby:2.5-alpine /src/bin/annotate /junits : echo '<details>Failure</details>'"
52+
53+
run "$PWD/hooks/command"
54+
55+
assert_failure
56+
57+
unstub mktemp
58+
unstub buildkite-agent
59+
unstub docker
60+
}
61+
3662
@test "can pass through optional params" {
3763
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS="junits/*.xml"
3864
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_JOB_UUID_FILE_PATTERN="custom_(*)_pattern.xml"
3965
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAILURE_FORMAT="file"
66+
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAIL_BUILD_ON_ERROR=false
4067

4168
artifacts_tmp="tests/tmp/$PWD/junit-artifacts"
4269
annotation_tmp="tests/tmp/$PWD/junit-annotation"

0 commit comments

Comments
 (0)