apache_avro_test_helper/lib.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#[cfg(not(target_arch = "wasm32"))]
19use ctor::{ctor, dtor};
20use std::cell::RefCell;
21
22thread_local! {
23 // The unit tests run in parallel
24 // We need to keep the log messages in a thread-local variable
25 // and clear them after assertion
26 pub(crate) static LOG_MESSAGES: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
27}
28
29pub mod data;
30pub mod logger;
31
32#[cfg(not(target_arch = "wasm32"))]
33#[ctor]
34fn before_all() {
35 // better stacktraces in tests
36 better_panic::Settings::new()
37 .most_recent_first(true)
38 .lineno_suffix(false)
39 .backtrace_first(true)
40 .install();
41
42 // enable logging in tests
43 logger::install();
44}
45
46#[cfg(not(target_arch = "wasm32"))]
47#[dtor]
48fn after_all() {
49 logger::clear_log_messages();
50}
51
52/// A custom error type for tests.
53#[derive(Debug)]
54pub enum TestError {}
55
56/// A converter of any error into [TestError].
57/// It is used to print better error messages in the tests.
58/// Borrowed from <https://bluxte.net/musings/2023/01/08/improving_failure_messages_rust_tests/>
59impl<Err: std::fmt::Display> From<Err> for TestError {
60 #[track_caller]
61 fn from(err: Err) -> Self {
62 panic!("{}: {}", std::any::type_name::<Err>(), err);
63 }
64}
65
66pub type TestResult = anyhow::Result<(), TestError>;
67
68/// Does nothing. Just loads the crate.
69/// Should be used in the integration tests, because they do not use [dev-dependencies]
70/// and do not auto-load this crate.
71pub fn init() {}