Gary Guo před 1 rokem
rodič
revize
fdf70d1e18

+ 3 - 8
.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

+ 6 - 1
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"] }

+ 0 - 8
example/Cargo.toml

@@ -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"

+ 2 - 0
rust-toolchain

@@ -0,0 +1,2 @@
+[toolchain]
+channel = "nightly"

+ 8 - 0
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"

+ 9 - 0
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

+ 7 - 0
test_crates/catch_std_exception/src/main.rs

@@ -0,0 +1,7 @@
+extern crate unwinding;
+
+fn main() {
+    let _ = unwinding::panic::catch_unwind(|| {
+        panic!();
+    });
+}

+ 8 - 0
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"

+ 9 - 0
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

+ 7 - 0
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"));
+    });
+}

+ 8 - 0
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"

+ 10 - 0
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
+

+ 0 - 0
example/src/main.rs → test_crates/throw_and_catch/src/main.rs


+ 20 - 0
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());
+    }
+}