diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1946b16..99e7ec1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,13 +28,8 @@ jobs: with: target: ${{ matrix.target }} - - name: Build example binary + - name: Build library run: cargo build --release $BUILD_STD - - name: Run example binary - run: (cargo run --release $BUILD_STD 2>&1 | tee ../run.log) || true - working-directory: example - - - name: Check log - run: | - grep -Pz 'panicked at example/src/main.rs:36:5:\npanic\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\ndropped: "string"\ncaught\npanicked at example/src/main.rs:46:5:\npanic\npanicked at example/src/main.rs:25:9:\npanic on drop\n( *\d+:.*\n)+thread panicked while processing panic\. aborting\.' run.log + - name: Run tests + run: cargo test --release $BUILD_STD diff --git a/Cargo.toml b/Cargo.toml index 9e60f08..ff64183 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,12 @@ description = "Unwinding library in Rust and for Rust" repository = "https://github.com/nbdd0121/unwinding/" [workspace] -members = ["cdylib", "example"] +members = [ + "cdylib", + "test_crates/throw_and_catch", + "test_crates/catch_std_exception", + "test_crates/std_catch_exception", +] [dependencies] gimli = { version = "0.28", default-features = false, features = ["read-core"] } diff --git a/example/Cargo.toml b/example/Cargo.toml deleted file mode 100644 index 6a6b603..0000000 --- a/example/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "example" -version = "0.1.0" -edition = "2018" - -[dependencies] -unwinding = { path = "../", features = ["system-alloc", "personality", "panic-handler"] } -libc = "0.2" diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" diff --git a/test_crates/catch_std_exception/Cargo.toml b/test_crates/catch_std_exception/Cargo.toml new file mode 100644 index 0000000..55d4982 --- /dev/null +++ b/test_crates/catch_std_exception/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "catch_std_exception" +version = "0.1.0" +edition = "2018" + +[dependencies] +unwinding = { path = "../../", features = ["panic"] } +libc = "0.2" diff --git a/test_crates/catch_std_exception/check.sh b/test_crates/catch_std_exception/check.sh new file mode 100755 index 0000000..455444b --- /dev/null +++ b/test_crates/catch_std_exception/check.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -o pipefail +trap "rm -f run.log" EXIT +cargo run --release $BUILD_STD 2>&1 | tee run.log +if [ $? -ne 134 ]; then + echo process is not aborted + exit 1 +fi +grep -Pz 'panicked at test_crates/catch_std_exception/src/main.rs:5:9:\nexplicit panic\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace' run.log diff --git a/test_crates/catch_std_exception/src/main.rs b/test_crates/catch_std_exception/src/main.rs new file mode 100644 index 0000000..e4fc9f1 --- /dev/null +++ b/test_crates/catch_std_exception/src/main.rs @@ -0,0 +1,7 @@ +extern crate unwinding; + +fn main() { + let _ = unwinding::panic::catch_unwind(|| { + panic!(); + }); +} diff --git a/test_crates/std_catch_exception/Cargo.toml b/test_crates/std_catch_exception/Cargo.toml new file mode 100644 index 0000000..5501109 --- /dev/null +++ b/test_crates/std_catch_exception/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "std_catch_exception" +version = "0.1.0" +edition = "2018" + +[dependencies] +unwinding = { path = "../../", features = ["panic"] } +libc = "0.2" diff --git a/test_crates/std_catch_exception/check.sh b/test_crates/std_catch_exception/check.sh new file mode 100755 index 0000000..408153a --- /dev/null +++ b/test_crates/std_catch_exception/check.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -o pipefail +trap "rm -f run.log" EXIT +cargo run --release $BUILD_STD 2>&1 | tee run.log +if [ $? -ne 134 ]; then + echo process is not aborted + exit 1 +fi +grep -Pz 'fatal runtime error: Rust cannot catch foreign exceptions' run.log diff --git a/test_crates/std_catch_exception/src/main.rs b/test_crates/std_catch_exception/src/main.rs new file mode 100644 index 0000000..625aede --- /dev/null +++ b/test_crates/std_catch_exception/src/main.rs @@ -0,0 +1,7 @@ +extern crate unwinding; + +fn main() { + let _ = std::panic::catch_unwind(|| { + unwinding::panic::begin_panic(Box::new("test")); + }); +} diff --git a/test_crates/throw_and_catch/Cargo.toml b/test_crates/throw_and_catch/Cargo.toml new file mode 100644 index 0000000..5e87993 --- /dev/null +++ b/test_crates/throw_and_catch/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "throw_and_catch" +version = "0.1.0" +edition = "2018" + +[dependencies] +unwinding = { path = "../..", features = ["system-alloc", "personality", "panic-handler"] } +libc = "0.2" diff --git a/test_crates/throw_and_catch/check.sh b/test_crates/throw_and_catch/check.sh new file mode 100755 index 0000000..17e13fa --- /dev/null +++ b/test_crates/throw_and_catch/check.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -o pipefail +trap "rm -f run.log" EXIT +cargo run --release $BUILD_STD 2>&1 | tee run.log +if [ $? -ne 134 ]; then + echo process is not aborted + exit 1 +fi +grep -Pz 'panicked at test_crates/throw_and_catch/src/main.rs:36:5:\npanic\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\ndropped: "string"\ncaught\npanicked at test_crates/throw_and_catch/src/main.rs:46:5:\npanic\npanicked at test_crates/throw_and_catch/src/main.rs:25:9:\npanic on drop\n( *\d+:.*\n)+thread panicked while processing panic\. aborting\.' run.log + diff --git a/example/src/main.rs b/test_crates/throw_and_catch/src/main.rs similarity index 100% rename from example/src/main.rs rename to test_crates/throw_and_catch/src/main.rs diff --git a/tests/compile_tests.rs b/tests/compile_tests.rs new file mode 100644 index 0000000..26a5697 --- /dev/null +++ b/tests/compile_tests.rs @@ -0,0 +1,20 @@ +use std::process::Command; + +#[test] +fn main() { + let dir = env!("CARGO_MANIFEST_DIR"); + + let tests = [ + "throw_and_catch", + "catch_std_exception", + "std_catch_exception", + ]; + + for test in tests { + let status = Command::new("./check.sh") + .current_dir(format!("{dir}/test_crates/{test}")) + .status() + .unwrap(); + assert!(status.success()); + } +}