From ed6b613dd34d0f4d73178c606750e15674fcf16f Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Wed, 19 Feb 2025 19:36:51 +0000 Subject: [PATCH] Add CI check for odd BufEncoder CAP Add a test file that has an odd capacity BufEncoder. Add a test file that has an even capacity BufEncoder. Add a CI check that the even capacity compiles and the odd capacity does not. --- .github/workflows/README.md | 1 + .github/workflows/rust.yaml | 11 ++++++ contrib/compile-tests.sh | 36 ++++++++++++++++++++ tests/compiletest/Cargo.toml | 8 +++++ tests/compiletest/src/main.rs | 7 ++++ tests/compiletests/even-capacity/Cargo.toml | 10 ++++++ tests/compiletests/even-capacity/src/main.rs | 5 +++ tests/compiletests/odd-capacity/Cargo.toml | 10 ++++++ tests/compiletests/odd-capacity/src/main.rs | 6 ++++ 9 files changed, 94 insertions(+) create mode 100755 contrib/compile-tests.sh create mode 100644 tests/compiletest/Cargo.toml create mode 100644 tests/compiletest/src/main.rs create mode 100644 tests/compiletests/even-capacity/Cargo.toml create mode 100644 tests/compiletests/even-capacity/src/main.rs create mode 100644 tests/compiletests/odd-capacity/Cargo.toml create mode 100644 tests/compiletests/odd-capacity/src/main.rs diff --git a/.github/workflows/README.md b/.github/workflows/README.md index 21f8272..ef4f912 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -24,3 +24,4 @@ Run from rust.yml unless stated otherwise. Unfortunately we are now exceeding th 10. `Arch32bit` 11. `Cross` 12. `Format` +13. `Compile tests` diff --git a/.github/workflows/rust.yaml b/.github/workflows/rust.yaml index b0b1bc6..311e8db 100644 --- a/.github/workflows/rust.yaml +++ b/.github/workflows/rust.yaml @@ -218,3 +218,14 @@ jobs: run: rustup component add rustfmt - name: "Check formatting" run: cargo +nightly fmt --all -- --check + + Compile: + name: Compile tests + runs-on: ubuntu-latest + steps: + - name: "Checkout repo" + uses: actions/checkout@v4 + - name: "Select toolchain" + uses: dtolnay/rust-toolchain@stable + - name: "Run compile tests" + run: ./contrib/compile-tests.sh diff --git a/contrib/compile-tests.sh b/contrib/compile-tests.sh new file mode 100755 index 0000000..8802b41 --- /dev/null +++ b/contrib/compile-tests.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +even_capacity() { + cd even-capacity + if cargo run > /dev/null 2>&1; then + cd .. + return 0 + else + cd .. + return 1 + fi +} + +odd_capacity() { + cd odd-capacity + if cargo run > /dev/null 2>&1; then + cd .. + return 0 + else + cd .. + return 1 + fi +} + +# Check that an even capacity BufEncoder compiles and an odd BufEncoder capacity fails to compile +cd tests/compiletests +if ! even_capacity; then + echo "even-capacity BufEncoder failed to compile" + exit 1 +elif odd_capacity; then + echo "odd-capacity BufEncoder compiled when it should not have" + exit 1 +else + echo "BufEncoder capacity compile tests passed" + exit 0 +fi diff --git a/tests/compiletest/Cargo.toml b/tests/compiletest/Cargo.toml new file mode 100644 index 0000000..c691770 --- /dev/null +++ b/tests/compiletest/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "compiletest" +version = "0.1.0" +edition = "2021" +rust-version = "1.63.0" + +[dependencies] +hex = { package = "hex-conservative", path = "../../" } diff --git a/tests/compiletest/src/main.rs b/tests/compiletest/src/main.rs new file mode 100644 index 0000000..1650880 --- /dev/null +++ b/tests/compiletest/src/main.rs @@ -0,0 +1,7 @@ +use hex::buf_encoder::BufEncoder; +use hex::Case; + +fn main() { odd_buffer(); } + +// This should fail to compile because the capacity size is odd. +fn odd_buffer() { let _encoder = BufEncoder::<3>::new(Case::Lower); } diff --git a/tests/compiletests/even-capacity/Cargo.toml b/tests/compiletests/even-capacity/Cargo.toml new file mode 100644 index 0000000..61797b8 --- /dev/null +++ b/tests/compiletests/even-capacity/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "even-capacity" +version = "0.1.0" +edition = "2021" +rust-version = "1.63.0" + +[workspace] + +[dependencies] +hex = { package = "hex-conservative", path = "../../../" } diff --git a/tests/compiletests/even-capacity/src/main.rs b/tests/compiletests/even-capacity/src/main.rs new file mode 100644 index 0000000..f22133c --- /dev/null +++ b/tests/compiletests/even-capacity/src/main.rs @@ -0,0 +1,5 @@ +use hex::buf_encoder::BufEncoder; +use hex::Case; + +// This should compile, ensuring that the compile error in odd-capacity is from the odd capacity. +fn main() { let _encoder = BufEncoder::<4>::new(Case::Lower); } diff --git a/tests/compiletests/odd-capacity/Cargo.toml b/tests/compiletests/odd-capacity/Cargo.toml new file mode 100644 index 0000000..ba07696 --- /dev/null +++ b/tests/compiletests/odd-capacity/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "odd-capacity" +version = "0.1.0" +edition = "2021" +rust-version = "1.63.0" + +[workspace] + +[dependencies] +hex = { package = "hex-conservative", path = "../../../" } diff --git a/tests/compiletests/odd-capacity/src/main.rs b/tests/compiletests/odd-capacity/src/main.rs new file mode 100644 index 0000000..4624b6b --- /dev/null +++ b/tests/compiletests/odd-capacity/src/main.rs @@ -0,0 +1,6 @@ +use hex::buf_encoder::BufEncoder; +use hex::Case; + +// This should fail to compile because the capacity size is odd. +// The only difference to `even-capacity` is the capacity size. +fn main() { let _encoder = BufEncoder::<3>::new(Case::Lower); }