From e9601ef13854c408c5d6014ca9a5558470a0d76f Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 30 Nov 2023 14:26:13 +0000 Subject: [PATCH] test: make Result-returning tests produce a line number Without this change when the test returns a `Result`, `cargo test` does not show the line number. To make the tests as easy to debug as the panicking tests, enable `backtrace` feature on `anyhow` and add debug information to add source line numbers to backtraces. Now running `RUST_BACKTRACE=1 cargo test` produces backtraces with the line numbers. For example: Error: near ",": syntax error in SELECT COUNT(,,*) FROM msgs_status_updates; at offset 13 Caused by: Error code 1: SQL error or missing database Stack backtrace: 0: as core::ops::try_trait::FromResidual>>::from_residual at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1962:27 1: deltachat::sql::Sql::query_row::{{closure}}::{{closure}} at ./src/sql.rs:466:23 2: deltachat::sql::Sql::call::{{closure}}::{{closure}} at ./src/sql.rs:379:55 3: tokio::runtime::context::runtime_mt::exit_runtime at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/context/runtime_mt.rs:35:5 4: tokio::runtime::scheduler::multi_thread::worker::block_in_place at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/scheduler/multi_thread/worker.rs:438:9 5: tokio::runtime::scheduler::block_in_place::block_in_place at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/scheduler/block_in_place.rs:20:5 6: tokio::task::blocking::block_in_place at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/task/blocking.rs:78:9 7: deltachat::sql::Sql::call::{{closure}} at ./src/sql.rs:379:19 8: deltachat::sql::Sql::query_row::{{closure}} at ./src/sql.rs:469:10 9: deltachat::sql::Sql::count::{{closure}} at ./src/sql.rs:443:76 10: deltachat::webxdc::tests::change_logging_webxdc::{{closure}} at ./src/webxdc.rs:2644:18 11: as core::future::future::Future>::poll at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/future/future.rs:125:9 12: tokio::runtime::park::CachedParkThread::block_on::{{closure}} at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/park.rs:282:63 13: tokio::runtime::coop::with_budget at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/coop.rs:107:5 tokio::runtime::coop::budget at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/coop.rs:73:5 tokio::runtime::park::CachedParkThread::block_on at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/park.rs:282:31 14: tokio::runtime::context::blocking::BlockingRegionGuard::block_on at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/runtime/context/blocking.rs:66:9 ... Line 10 of the backtrace contains the line number in the test (2644). --- CONTRIBUTING.md | 6 ++++++ Cargo.toml | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fe3e6df52..f288b0d9a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -91,6 +91,12 @@ All errors should be handled in one of these ways: - With `.log_err().ok()`. - Bubbled up with `?`. +`backtrace` feature is enabled for `anyhow` crate +and `debug = 1` option is set in the test profile. +This allows to run `RUST_BACKTRACE=1 cargo test` +and get a backtrace with line numbers in resultified tests +which return `anyhow::Result`. + ### Logging For logging, use `info!`, `warn!` and `error!` macros. diff --git a/Cargo.toml b/Cargo.toml index c22119833..c0a312c32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,10 @@ panic = 'abort' opt-level = 1 [profile.test] +# Make anyhow `backtrace` feature useful. +# With `debug = 0` there are no line numbers in the backtrace +# produced with RUST_BACKTRACE=1. +debug = 1 opt-level = 0 # Always optimize dependencies. @@ -99,6 +103,7 @@ uuid = { version = "1", features = ["serde", "v4"] } [dev-dependencies] ansi_term = "0.12.0" +anyhow = { version = "1", features = ["backtrace"] } # Enable `backtrace` feature in tests. criterion = { version = "0.5.1", features = ["async_tokio"] } futures-lite = "2.0.0" log = "0.4"