Skip to content

Commit 9d81b97

Browse files
authored
Merge pull request #32 from prog-time/issues-17
[FEATURE] Add cargo test CI snippet
2 parents 6e48679 + 2c9e83e commit 9d81b97

5 files changed

Lines changed: 115 additions & 0 deletions

File tree

CI/tests/cargo_test.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cargo_test:
2+
runs-on: ubuntu-latest
3+
steps:
4+
- uses: actions/checkout@v4
5+
6+
- name: Set up Rust toolchain
7+
uses: dtolnay/rust-toolchain@stable
8+
9+
- name: Cache Cargo registry and target
10+
uses: actions/cache@v4
11+
with:
12+
path: |
13+
~/.cargo/registry
14+
target/
15+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
16+
restore-keys: |
17+
${{ runner.os }}-cargo-
18+
19+
- name: Run cargo test
20+
run: |
21+
set -euo pipefail
22+
23+
if [[ ! -f "Cargo.toml" ]]; then
24+
echo "::error::No Cargo.toml found"
25+
exit 1
26+
fi
27+
28+
echo "ℹ️ Running cargo test..."
29+
cargo test --all-features --verbose \
30+
&& echo "✅ cargo tests passed" \
31+
|| { echo "❌ cargo tests failed"; exit 1; }

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ that projects compose into their own workflows.
3636
| SpotBugs | static_analysis | [CI/static_analysis/spotbugs.yml](https://github.com/prog-time/workflows/blob/main/CI/static_analysis/spotbugs.yml) |
3737
| mermaid-cli | build | [CI/build/mermaid.yml](https://github.com/prog-time/workflows/blob/main/CI/build/mermaid.yml) |
3838
| BATS | tests | [CI/tests/bats.yml](https://github.com/prog-time/workflows/blob/main/CI/tests/bats.yml) |
39+
| cargo test | tests | [CI/tests/cargo_test.yml](https://github.com/prog-time/workflows/blob/main/CI/tests/cargo_test.yml) |
3940
| go test | tests | [CI/tests/go_test.yml](https://github.com/prog-time/workflows/blob/main/CI/tests/go_test.yml) |
4041
| Jest | tests | [CI/tests/jest.yml](https://github.com/prog-time/workflows/blob/main/CI/tests/jest.yml) |
4142
| Laravel | tests | [CI/tests/laravel_tests.yml](https://github.com/prog-time/workflows/blob/main/CI/tests/laravel_tests.yml) |
@@ -104,6 +105,7 @@ Workflows/
104105
│ │ │ └── spotbugs.yml
105106
│ │ └── tests/
106107
│ │ ├── bats.yml
108+
│ │ ├── cargo_test.yml
107109
│ │ ├── go_test.yml
108110
│ │ ├── jest.yml
109111
│ │ ├── laravel_tests.yml
@@ -135,6 +137,7 @@ Workflows/
135137
│ │ ├── semgrep.sh
136138
│ │ └── trivy.sh
137139
│ └── tests/
140+
│ ├── cargo_test.sh
138141
│ └── jest.sh
139142
140143
├── CI/ # assembled output (ready to use)
@@ -163,6 +166,7 @@ Workflows/
163166
│ │ ├── semgrep.bats
164167
│ │ └── trivy.bats
165168
│ ├── tests/
169+
│ │ ├── cargo_test.bats
166170
│ │ └── jest.bats
167171
│ └── helpers/
168172
│ └── common.bash # shared test utilities (mocks, temp dirs)
@@ -277,6 +281,7 @@ shellcheck:
277281
| Snippet | What it runs |
278282
|---------|--------------|
279283
| `CI/tests/bats.yml` | BATS tests (`tests/` directory) |
284+
| `CI/tests/cargo_test.yml` | Rust test suite (`cargo test --all-features --verbose`) |
280285
| `CI/tests/go_test.yml` | Go test suite (`go test ./...`) |
281286
| `CI/tests/jest.yml` | JavaScript/TypeScript test suite (Jest) |
282287
| `CI/tests/laravel_tests.yml` | Laravel test suite (PHP 8.2, SQLite, parallel) |

scripts/CI/tests/cargo_test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cargo_test:
2+
runs-on: ubuntu-latest
3+
steps:
4+
- uses: actions/checkout@v4
5+
6+
- name: Set up Rust toolchain
7+
uses: dtolnay/rust-toolchain@stable
8+
9+
- name: Cache Cargo registry and target
10+
uses: actions/cache@v4
11+
with:
12+
path: |
13+
~/.cargo/registry
14+
target/
15+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
16+
restore-keys: |
17+
${{ runner.os }}-cargo-
18+
19+
- name: Run cargo test
20+
run: bash scripts/shell/tests/cargo_test.sh

scripts/shell/tests/cargo_test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [[ ! -f "Cargo.toml" ]]; then
5+
echo "::error::No Cargo.toml found"
6+
exit 1
7+
fi
8+
9+
echo "ℹ️ Running cargo test..."
10+
cargo test --all-features --verbose \
11+
&& echo "✅ cargo tests passed" \
12+
|| { echo "❌ cargo tests failed"; exit 1; }

tests/tests/cargo_test.bats

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bats
2+
3+
load "../helpers/common"
4+
5+
SCRIPT="$BATS_TEST_DIRNAME/../../scripts/shell/tests/cargo_test.sh"
6+
7+
setup() {
8+
setup_test_dir
9+
mkdir -p "$TEST_DIR/bin"
10+
export PATH="$TEST_DIR/bin:$PATH"
11+
}
12+
13+
teardown() {
14+
teardown_test_dir
15+
}
16+
17+
make_cargo_stub() {
18+
local exit_code="$1"
19+
cat > "$TEST_DIR/bin/cargo" <<EOF
20+
#!/usr/bin/env bash
21+
exit $exit_code
22+
EOF
23+
chmod +x "$TEST_DIR/bin/cargo"
24+
}
25+
26+
@test "no Cargo.toml: exits 1 with error annotation" {
27+
make_cargo_stub 0
28+
run bash "$SCRIPT"
29+
[ "$status" -eq 1 ]
30+
[[ "$output" == *"::error::No Cargo.toml found"* ]]
31+
}
32+
33+
@test "Cargo.toml present, tests pass: exits 0 with success message" {
34+
make_cargo_stub 0
35+
touch Cargo.toml
36+
run bash "$SCRIPT"
37+
[ "$status" -eq 0 ]
38+
[[ "$output" == *"✅ cargo tests passed"* ]]
39+
}
40+
41+
@test "Cargo.toml present, tests fail: exits 1 with failure message" {
42+
make_cargo_stub 1
43+
touch Cargo.toml
44+
run bash "$SCRIPT"
45+
[ "$status" -eq 1 ]
46+
[[ "$output" == *"❌ cargo tests failed"* ]]
47+
}

0 commit comments

Comments
 (0)