From e0679943a8ad17bed20a315f1ad215f790b98d8b Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sat, 19 Nov 2022 19:29:48 +0100 Subject: [PATCH 1/3] Run tests in one thread only https://github.com/gtk-rs/gtk-test#general-setup: > When running test, you need to specify that you only want ONE thread. > Otherwise, GTK contexts might conflict into each others. --- src/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/src/meson.build b/src/meson.build index 002cc1f5..a5a4637c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -59,6 +59,7 @@ test( cargo_options, '--', '--nocapture', + '--test-threads=1', ], env: [ 'RUST_BACKTRACE=1', From 40e81dd5c45736b9f940bf5d49ceaeb70be82bac Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sat, 19 Nov 2022 18:58:13 +0100 Subject: [PATCH 2/3] Run setup_schema before all tests There are multiple tests that require schema to be compiled and GSETTINGS_SCHEMA_DIR set, not just the one in settings.rs. The other tests currently fail due to missing schema. --- Cargo.lock | 11 +++++++++++ Cargo.toml | 3 +++ src/main.rs | 32 ++++++++++++++++++++++++++++++++ src/settings.rs | 28 ---------------------------- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0153b466..36076b34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -148,6 +148,16 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "ctor" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e366bff8cd32dd8754b0991fb66b279dc48f598c3a18914852a6673deef583" +dependencies = [ + "quote", + "syn 2.0.37", +] + [[package]] name = "deluxe" version = "0.5.0" @@ -944,6 +954,7 @@ name = "kooha" version = "2.2.4" dependencies = [ "anyhow", + "ctor", "futures-channel", "futures-util", "gdk4-wayland", diff --git a/Cargo.toml b/Cargo.toml index 4c52b5f8..1afda950 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,6 @@ futures-util = { version = "0.3", default-features = false } gsettings-macro = "0.1.15" pulse = { package = "libpulse-binding", version = "2.26.0" } pulse_glib = { package = "libpulse-glib-binding", version = "2.25.1" } + +[dev-dependencies] +ctor = "0.2.5" diff --git a/src/main.rs b/src/main.rs index c05eef05..8e768beb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,6 +51,10 @@ use self::{ config::{GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE}, }; +#[cfg(test)] +#[macro_use] +extern crate ctor; + fn main() -> glib::ExitCode { tracing_subscriber::fmt::init(); @@ -70,3 +74,31 @@ fn main() -> glib::ExitCode { let app = Application::new(); app.run() } + +#[cfg(test)] +mod test { + use ctor; + use std::{env, process::Command}; + + // Run once before tests are executed. + #[ctor] + fn setup_schema() { + let schema_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/data"); + + let output = Command::new("glib-compile-schemas") + .arg(schema_dir) + .output() + .unwrap(); + + if !output.status.success() { + panic!( + "Failed to compile GSchema for tests; stdout: {}; stderr: {}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + } + + env::set_var("GSETTINGS_SCHEMA_DIR", schema_dir); + env::set_var("GSETTINGS_BACKEND", "memory"); + } +} diff --git a/src/settings.rs b/src/settings.rs index 8582a8ab..4d1c2bfc 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -150,36 +150,8 @@ impl Settings { mod tests { use super::*; - use std::{env, process::Command, sync::Once}; - - fn setup_schema() { - static INIT: Once = Once::new(); - - INIT.call_once(|| { - let schema_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/data"); - - let output = Command::new("glib-compile-schemas") - .arg(schema_dir) - .output() - .unwrap(); - - if !output.status.success() { - panic!( - "Failed to compile GSchema for tests; stdout: {}; stderr: {}", - String::from_utf8_lossy(&output.stdout), - String::from_utf8_lossy(&output.stderr) - ); - } - - env::set_var("GSETTINGS_SCHEMA_DIR", schema_dir); - env::set_var("GSETTINGS_BACKEND", "memory"); - }); - } - #[test] fn default_profile() { - setup_schema(); - assert!(Settings::default().profile().is_some()); assert!(Settings::default().profile().unwrap().supports_audio()); } From 67fb6779c2ea157df7af0bb17e958483f5b73b16 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sat, 19 Nov 2022 19:01:30 +0100 Subject: [PATCH 3/3] Use correct path in setup_schema when running via meson --- src/main.rs | 3 ++- src/meson.build | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 8e768beb..a8b47781 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,7 +83,8 @@ mod test { // Run once before tests are executed. #[ctor] fn setup_schema() { - let schema_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/data"); + let schema_dir = &env::var("GSETTINGS_SCHEMA_DIR") + .unwrap_or(concat!(env!("CARGO_MANIFEST_DIR"), "/data").into()); let output = Command::new("glib-compile-schemas") .arg(schema_dir) diff --git a/src/meson.build b/src/meson.build index a5a4637c..ae0fde16 100644 --- a/src/meson.build +++ b/src/meson.build @@ -63,6 +63,7 @@ test( ], env: [ 'RUST_BACKTRACE=1', + 'GSETTINGS_SCHEMA_DIR=@0@/data'.format(meson.project_build_root()), cargo_env ], timeout: 300, # give cargo more time