-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
270 lines (250 loc) · 10.5 KB
/
action.yml
File metadata and controls
270 lines (250 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
name: "Test Rust Library and Upload Coverage"
description: "Runs tests for a Rust library using Tarpaulin and uploads the coverage to Codecov."
branding:
icon: "check"
color: "green"
inputs:
rust-project-path:
description: "Directory containing Cargo.toml (crate root or workspace root)"
required: false
default: "."
rust-toolchain:
description: "Rust toolchain to use for building and testing (e.g., stable, nightly, 1.75.0, nightly-2024-02-08)"
required: false
default: "stable"
target:
description: "The target platform for the Rust compiler"
required: false
default: ""
install-rust-toolchain:
description: "Whether to install the specified Rust toolchain"
required: false
default: "true"
setup-rust-cache:
description: "Whether to set up Rust caching"
required: false
default: "true"
cache-workspace-crates:
description: "Whether rust-cache should cache workspace crates"
required: false
default: "true"
cache-directories:
description: >-
Additional directories to cache, relative to rust-project-path unless
absolute. The rustdoc directory is cached automatically.
required: false
default: ""
use-tarpaulin:
description: "Whether to use Tarpaulin for code coverage. If false, only runs tests."
required: false
default: "true"
use-binstall:
description: "Whether to use cargo-binstall for installing components like tarpaulin. If false, uses cargo install."
required: false
default: "true"
install-binstall:
description: "Whether to install cargo-binstall. If false, assumes it is already available in the environment."
required: false
default: "true"
upload-coverage:
description: "Whether to upload coverage to Codecov"
required: false
default: "true"
codecov-token:
description: "Codecov token for uploading coverage"
required: false
codecov-flags:
description: "Flags to pass to Codecov"
required: false
default: "unittests"
codecov-name:
description: "Custom defined name for the upload"
required: false
default: "codecov-umbrella"
features:
description: "Space-separated list of features to enable during testing"
required: false
default: ""
no-default-features:
description: "Disable default features during testing"
required: false
default: "false"
use-cross:
description: "Use cross-rs for testing. If false, use cargo."
required: false
default: "false"
additional-test-args:
description: "Additional arguments to pass to the cargo test command"
required: false
default: ""
additional-tarpaulin-args:
description: "Additional arguments to pass to the cargo tarpaulin command"
required: false
default: ""
pre-test-command:
description: "Bash command to run before executing tests"
required: false
default: ""
packages:
description: "Multi-line list of package names to test (one per line). If empty, tests all packages in workspace (--all)"
required: false
default: ""
runs:
using: "composite"
steps:
- name: Extract features and additional test args for cache in friendly to cache-key manner
shell: bash
run: |
echo "SAFE_FEATURES=$(echo ${{ inputs.features }} | tr ',' '-' | tr -d ' ')" >> $GITHUB_ENV
SAFE_ADDITIONAL_TEST_ARGS=$(echo "${{ inputs.additional-test-args }}" | tr -dc '[:alnum:],_-' | tr ',' '-')
if [ -z "$SAFE_ADDITIONAL_TEST_ARGS" ]; then
SAFE_ADDITIONAL_TEST_ARGS="no-test-args"
fi
echo "SAFE_ADDITIONAL_TEST_ARGS=${SAFE_ADDITIONAL_TEST_ARGS}" >> $GITHUB_ENV
# Process packages input - convert multiline to space-separated, clean whitespace
SAFE_PACKAGES=$(echo "${{ inputs.packages }}" | tr -d '\r' | tr '\n\t' ' ' | tr -s ' ' | sed 's/^ *//;s/ *$//')
echo "SAFE_PACKAGES=${SAFE_PACKAGES}" >> $GITHUB_ENV
if [ -z "$SAFE_PACKAGES" ]; then
SAFE_PACKAGES_KEY="all-packages"
else
# rust-cache rejects commas in cache keys, so use a cache-safe separator.
SAFE_PACKAGES_KEY=$(echo "$SAFE_PACKAGES" | tr ' ' '+')
fi
echo "SAFE_PACKAGES_KEY=${SAFE_PACKAGES_KEY}" >> $GITHUB_ENV
# Build packages argument once and reuse across steps
if [ -n "$SAFE_PACKAGES" ]; then
PACKAGES_ARG=""
for pkg in $SAFE_PACKAGES; do
if [ -n "$pkg" ]; then
if [ -z "$PACKAGES_ARG" ]; then
PACKAGES_ARG="-p $pkg"
else
PACKAGES_ARG="$PACKAGES_ARG -p $pkg"
fi
fi
done
else
PACKAGES_ARG="--all"
fi
echo "PACKAGES_ARG=${PACKAGES_ARG}" >> $GITHUB_ENV
# rust-cache prunes rustdoc output by default, so resolve the automatic
# rustdoc path plus any caller-provided cache directories here.
- name: Normalize rust-cache directories
if: inputs.setup-rust-cache == 'true'
shell: bash
run: python3 "${{ github.action_path }}/scripts/resolve_cache_directories.py"
env:
INPUT_RUST_PROJECT_PATH: ${{ inputs.rust-project-path }}
INPUT_TARGET: ${{ inputs.target }}
INPUT_CACHE_DIRECTORIES: ${{ inputs.cache-directories }}
- name: Install Rust Toolchain
if: inputs.install-rust-toolchain == 'true'
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: ${{ inputs.rust-toolchain }}
target: ${{ inputs.target }}
cache: false
components: "clippy,rustfmt"
# Needs to be done after installing toolchain, don't move!
- name: Setup Rust Caching
if: inputs.setup-rust-cache == 'true'
uses: Swatinem/rust-cache@v2
with:
key:
test-${{ inputs.rust-toolchain }}-${{ inputs.rust-project-path }}-${{ inputs.target }}-${{ env.SAFE_FEATURES
}}-${{ inputs.no-default-features }}-${{ inputs.use-cross }}-${{ env.SAFE_PACKAGES_KEY
}}-${{ env.SAFE_ADDITIONAL_TEST_ARGS }}-${{ inputs.cache-workspace-crates }}
cache-on-failure: true
cache-all-crates: true
cache-workspace-crates: ${{ inputs.cache-workspace-crates }}
cache-bin: ${{ inputs.use-binstall == 'true' && 'false' || 'true' }} # Disable binary caching when using binstall to avoid conflicts
workspaces: |
${{ inputs.rust-project-path }} -> target
cache-directories: ${{ env.RUST_CACHE_DIRECTORIES }}
- name: Install gcc-multilib (if on Ubuntu based System, and Target is not Host)
if: inputs.use-cross == 'false' && runner.os == 'Linux'
shell: bash
run: |
# Get host triple
HOST_TARGET=$(rustc -vV | sed -n 's|host: ||p')
echo "Host target: $HOST_TARGET"
echo "Target: ${{ inputs.target }}"
if [ "$HOST_TARGET" != "${{ inputs.target }}" ]; then
echo "Target is different from host. Installing gcc-multilib and g++-multilib..."
sudo apt-get update || true
sudo apt-get install -y gcc-multilib g++-multilib || true
else
echo "Target is the same as host. Skipping gcc-multilib installation."
fi
- name: Install cross
if: inputs.use-cross == 'true'
shell: bash
run: RUSTFLAGS="" cargo install cross --git https://github.com/cross-rs/cross
- name: Install cargo-binstall
if: inputs.install-binstall == 'true'
uses: cargo-bins/cargo-binstall@main
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Install Tarpaulin (via binstall)
if: inputs.use-cross == 'false' && inputs.use-tarpaulin == 'true' && inputs.use-binstall == 'true'
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
# The force here is kinda forced for back compat reasons, else cargo might not pick up the binary;
# while binstall thinks it's there due to caching. Current versions wouldn't cache preinstalled cargo-tarpaulin
# but they may reside from older CI runs in other repos.
cargo +${{ inputs.rust-toolchain }} binstall cargo-tarpaulin --force
- name: Install Tarpaulin (via cargo install)
if: inputs.use-cross == 'false' && inputs.use-tarpaulin == 'true' && inputs.use-binstall == 'false'
shell: bash
run: |
cargo +${{ inputs.rust-toolchain }} install cargo-tarpaulin
- name: Run Pre-Test Command
if: inputs.pre-test-command != ''
shell: bash
run: ${{ inputs.pre-test-command }}
- name: Run Tarpaulin
if: inputs.use-cross == 'false' && inputs.use-tarpaulin == 'true'
shell: bash
working-directory: ${{ inputs.rust-project-path }}
run: |
cargo +${{ inputs.rust-toolchain }} tarpaulin $PACKAGES_ARG --out xml --engine llvm \
--features "${{ inputs.features }}" \
${{ inputs.no-default-features == 'true' && '--no-default-features' || '' }} \
${{ inputs.target != '' && format('--target "{0}"', inputs.target) || '' }} \
--skip-clean \
${{ inputs.additional-test-args }} \
${{ inputs.additional-tarpaulin-args }}
- name: Run Tests with cargo
if: inputs.use-cross == 'false' && inputs.use-tarpaulin == 'false'
shell: bash
working-directory: ${{ inputs.rust-project-path }}
run: |
cargo +${{ inputs.rust-toolchain }} test $PACKAGES_ARG \
--features "${{ inputs.features }}" \
${{ inputs.no-default-features == 'true' && '--no-default-features' || '' }} \
${{ inputs.target != '' && format('--target "{0}"', inputs.target) || '' }} \
${{ inputs.additional-test-args }}
- name: Run Tests with cross
if: inputs.use-cross == 'true'
shell: bash
working-directory: ${{ inputs.rust-project-path }}
# the RUSTFLAGS are a hack around cross build errors on certain setups
# https://github.com/cross-rs/cross/issues/1561
run: |
RUSTFLAGS="" cross +${{ inputs.rust-toolchain }} test $PACKAGES_ARG \
--features "${{ inputs.features }}" \
${{ inputs.no-default-features == 'true' && '--no-default-features' || '' }} \
${{ inputs.target != '' && format('--target "{0}"', inputs.target) || '' }} \
${{ inputs.additional-test-args }}
- name: Upload Coverage to Codecov
if: inputs.upload-coverage == 'true' && inputs.use-cross == 'false' && inputs.use-tarpaulin == 'true'
uses: codecov/codecov-action@v6
with:
files: ${{ inputs.rust-project-path }}/cobertura.xml
flags: ${{ inputs.codecov-flags }}
name: ${{ inputs.codecov-name }}
token: ${{ inputs.codecov-token }}
fail_ci_if_error: true
verbose: true