Skip to content

Commit

Permalink
Run setup_schema before all tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jirutka committed Nov 19, 2022
1 parent 87eecc3 commit 7baf3ad
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ futures-util = { version = "0.3", default-features = false }
gsettings-macro = "0.1.11"
pulse = { package = "libpulse-binding", version = "2.26.0" }
pulse_glib = { package = "libpulse-glib-binding", version = "2.25.1" }

[dev-dependencies]
ctor = "0.1.26"
32 changes: 32 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ use self::{
config::{GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE},
};

#[cfg(test)]
#[macro_use]
extern crate ctor;

static THREAD_POOL: Lazy<glib::ThreadPool> = Lazy::new(|| glib::ThreadPool::shared(None).unwrap());

fn main() {
Expand All @@ -72,3 +76,31 @@ fn main() {
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");
}
}
28 changes: 0 additions & 28 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,36 +207,8 @@ fn is_accessible(path: &Path) -> bool {
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());
}
Expand Down

0 comments on commit 7baf3ad

Please sign in to comment.