Skip to content

Commit c972894

Browse files
authored
Add support for multiple test suites (#12)
* First pass tweaks * Add setup to skip tests and provide formatted file name * Fix format output file path/name * Fix plugin.yml * tweak syntax * Fix var sub * Fix sum command * Tidy up code and add docs * Code review changes * Remove quotes * Ensure DS_Store is ignored * Revert code review change * Add quotes to coverage/"${formatted_file_prefix}".*.json * Remove quotes * Fix readme
1 parent cb24da4 commit c972894

File tree

4 files changed

+164
-9
lines changed

4 files changed

+164
-9
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/macos
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=macos
3+
4+
### macOS ###
5+
# General
6+
.DS_Store
7+
.AppleDouble
8+
.LSOverride
9+
10+
# Icon must end with two \r
11+
Icon
12+
13+
14+
# Thumbnails
15+
._*
16+
17+
# Files that might appear in the root of a volume
18+
.DocumentRevisions-V100
19+
.fseventsd
20+
.Spotlight-V100
21+
.TemporaryItems
22+
.Trashes
23+
.VolumeIcon.icns
24+
.com.apple.timemachine.donotpresent
25+
26+
# Directories potentially created on remote AFP share
27+
.AppleDB
28+
.AppleDesktop
29+
Network Trash Folder
30+
Temporary Items
31+
.apdisk
32+
33+
# End of https://www.toptal.com/developers/gitignore/api/macos

README.md

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ to report coverage with the Code Climate test reporter
88

99
https://github.com/codeclimate/test-reporter
1010

11-
Plugin can handle single, or multiple (parallel), tests.
11+
Plugin can handle single, [parallel](https://docs.codeclimate.com/docs/configuring-test-coverage#parallel-tests) and [multiple](https://docs.codeclimate.com/docs/configuring-test-coverage#multiple-test-suites) test suites.
1212

1313
Also see: https://docs.codeclimate.com/docs/configuring-test-coverage
1414

@@ -20,14 +20,16 @@ This plugin will download build artifact(s) generated by a previous step, compil
2020
steps:
2121
- label: ":codeclimate: Report coverage"
2222
plugins:
23-
- jobready/codeclimate-test-reporter#v2.0:
23+
- jobready/codeclimate-test-reporter#v2.2:
2424
artifact: "coverage/.resultset.json"
2525
input_type: simplecov
2626
prefix: /app
2727
env:
2828
CC_TEST_REPORTER_ID:
2929
```
3030
31+
### Parallel tests
32+
3133
If you are running parallel builds you'll probably want to create uniquely
3234
named artifacts. That can be done by renaming the `.resultset.json` file
3335
after your tests have run:
@@ -36,6 +38,86 @@ after your tests have run:
3638
mv -v coverage/.resultset.json coverage/.resultset${BUILDKITE_PARALLEL_JOB}.json
3739
```
3840

41+
### Multiple test suites
42+
43+
If your repository has more than one test suite you'll need to format each artifact (coverage file) before the result can be summed and reported to codeclimate.
44+
45+
To do this you'll need to configure multiple steps persisting the formated codeclimate compatable configuration files as artifacts between steps.
46+
47+
This is best shown via an example;
48+
49+
```
50+
- label: ":rails: Tests"
51+
key: "rails-tests"
52+
command: "rails db:test:prepare && rails test"
53+
env:
54+
RUBY_OPT: "-W:deprecated"
55+
depends_on:
56+
- "build-base"
57+
plugins:
58+
- docker-compose#v3.7.0:
59+
run: base
60+
config: .buildkite/docker-compose.yml
61+
volumes:
62+
- "./coverage:/app/coverage"
63+
artifact_paths:
64+
- "coverage/**/*"
65+
66+
- label: ":codeclimate: Format Rails Coverage"
67+
key: "codeclimate-format-rails-report"
68+
plugins:
69+
- jobready/codeclimate-test-reporter#v2.2:
70+
artifact: "coverage/coverage.json"
71+
input_type: simplecov
72+
prefix: /app
73+
report: false
74+
file_prefix: "codeclimate.rails"
75+
depends_on:
76+
- "rails-tests"
77+
artifact_paths:
78+
- "coverage/codeclimate.**"
79+
80+
- label: ":react: Tests"
81+
key: "react-tests"
82+
command: "yarn test"
83+
depends_on:
84+
- "build-base"
85+
plugins:
86+
- docker-compose#v3.7.0:
87+
run: base
88+
config: .buildkite/docker-compose.yml
89+
volumes:
90+
- "./coverage:/app/coverage"
91+
artifact_paths:
92+
- "coverage/**/*"
93+
94+
- label: ":codeclimate: Format React Coverage"
95+
key: "codeclimate-format-react-report"
96+
plugins:
97+
- jobready/codeclimate-test-reporter#v2.2:
98+
artifact: "coverage/lcov.info"
99+
input_type: lcov
100+
prefix: /app
101+
report: false
102+
file_prefix: "codeclimate.react"
103+
depends_on:
104+
- "react-tests"
105+
artifact_paths:
106+
- "coverage/codeclimate.**"
107+
108+
- label: ":codeclimate: Report coverage"
109+
key: "codeclimate-coverage-report"
110+
plugins:
111+
- jobready/codeclimate-test-reporter#v2.2:
112+
artifact: "coverage/codeclimate**"
113+
format: false
114+
env:
115+
CC_TEST_REPORTER_ID:
116+
depends_on:
117+
- "codeclimate-format-rails-report"
118+
- "codeclimate-format-react-report"
119+
```
120+
39121
## Configuration
40122
41123
### `artifact` (required)
@@ -86,11 +168,26 @@ Set to true to enable cc-test-reporter --debug flag
86168
87169
Example: `true`
88170
171+
### `format` (optional, default `true`)
172+
173+
Controls if formatting of artifacts should be performed. Set to false when wanting to upload artifacts formatted in a previous step.
174+
175+
Example: `false`
176+
177+
### `report` (optional, default `true`)
178+
179+
Controls if coverage reporting to codeclimate should be performed. Set to false when needing to parse multiple test suites for later upload.
180+
181+
Example: `false`
182+
183+
### `file_prefix` (optional, default `codeclimate`)
184+
185+
Controls the name outputs files formatted by the codeclimate test reporter. Required to stop artifact names clashing when processing muiltple test suites.
186+
89187
### `CC_TEST_REPORTER_ID` (required)
90188
91189
The `CC_TEST_REPORTER_ID` environment variable must be configured.
92190
93-
94191
## Linting
95192
96193
To run the [Buildkite Plugin Linter](https://github.com/buildkite-plugins/buildkite-plugin-linter), run

hooks/command

100644100755
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ install_reporter() {
1515
}
1616

1717
download_artifacts() {
18-
printf -- "--- :buildkite: downloading artifact\\n"
18+
printf -- "--- :buildkite: Downloading artifact\\n"
1919
buildkite-agent artifact download "${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_ARTIFACT}" ./
2020
}
2121

2222
format_file() {
23-
printf -- "Formatting file: %s\\n" "$1"
23+
output_file="coverage/$formatted_file_prefix.$2.json"
24+
printf -- "Formatting file: %s to %s\\n" "$1" "$output_file"
2425
./cc-test-reporter format-coverage ${debug:+"-d"} \
2526
--input-type "${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_INPUT_TYPE}" \
2627
--prefix "${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_PREFIX}" \
27-
--output "coverage/codeclimate.$2.json" \
28+
--output "${output_file}" \
2829
"$1"
2930
}
3031

31-
report_coverage() {
32-
printf -- "--- :codeclimate: reporting coverage\\n"
32+
format_files() {
33+
if [ "$format" = false ] ; then
34+
return
35+
fi
36+
37+
printf -- "--- :codeclimate: Formatting coverage\\n"
3338

3439
count=1
3540
for f in ${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_ARTIFACT}; do
@@ -38,13 +43,22 @@ report_coverage() {
3843
done
3944

4045
wait
46+
}
47+
48+
report_coverage() {
49+
if [ "$report" = false ] ; then
50+
return
51+
fi
52+
53+
printf -- "--- :codeclimate: Reporting coverage\\n"
4154

4255
if [[ -v BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_PARTS ]] ; then
4356
SUM_PARTS="--parts ${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_PARTS}"
4457
else
4558
SUM_PARTS=""
4659
fi
47-
./cc-test-reporter sum-coverage ${debug:+"-d"} ${SUM_PARTS} coverage/codeclimate.*.json
60+
61+
./cc-test-reporter sum-coverage ${debug:+"-d"} ${SUM_PARTS} coverage/${formatted_file_prefix}.*.json
4862

4963
./cc-test-reporter upload-coverage ${debug:+"-d"}
5064
}
@@ -56,6 +70,11 @@ fi
5670
debug=""
5771
[[ -v BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_DEBUG ]] && debug="yes"
5872

73+
format=${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_FORMAT:-true}
74+
formatted_file_prefix=${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_FILE_PREFIX:-"codeclimate"}
75+
report=${BUILDKITE_PLUGIN_CODECLIMATE_TEST_REPORTER_REPORT:-true}
76+
5977
install_reporter
6078
download_artifacts
79+
format_files
6180
report_coverage

plugin.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ configuration:
1616
type: integer
1717
debug:
1818
type: string
19+
format:
20+
type: boolean
21+
report:
22+
type: boolean
23+
file_prefix:
24+
type: string
1925
required: [artifact, input_type]

0 commit comments

Comments
 (0)