Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/cargo_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ impl CargoConfig {
#[cfg(test)]
mod tests {
use super::*;
use std::path::{Path, PathBuf};

struct CwdGuard(PathBuf);

impl CwdGuard {
fn set_to(path: &Path) -> Self {
let previous = std::env::current_dir().expect("Failed to get current directory");
std::env::set_current_dir(path).expect("Failed to change directory");
Self(previous)
}
}

impl Drop for CwdGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.0);
}
}

const TEST_CARGO_TOML: &str = r#"
[package]
Expand Down Expand Up @@ -144,7 +161,7 @@ version = "1.0.0"
#[test]
fn test_from_current_dir() {
let tree = setup_test_dir(None);
std::env::set_current_dir(&tree.root).expect("Failed to change directory");
let _cwd_guard = CwdGuard::set_to(&tree.root);

let config = CargoConfig::from_current_dir().expect("Failed to read from current dir");
assert_eq!(config.toml["package"]["name"].as_str(), Some("test-app"));
Expand All @@ -153,7 +170,7 @@ version = "1.0.0"
#[test]
fn test_lock_from_current_dir() {
let tree = setup_test_dir_with_lock(None, None);
std::env::set_current_dir(&tree.root).expect("Failed to change directory");
let _cwd_guard = CwdGuard::set_to(&tree.root);

let config = CargoConfig::lock_from_current_dir().expect("Failed to read Cargo.lock");
let packages = config
Expand Down
27 changes: 13 additions & 14 deletions src/tests_cfg/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
use crate::bgworker;
use std::path::PathBuf;

#[cfg(any(feature = "bg_pg", feature = "bg_sqlt"))]
fn queue_jobs_fixture_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
.join("queue")
.join("jobs.yaml")
}

#[cfg(feature = "bg_pg")]
/// # Panics
///
/// This function will panic if it fails to prepare or insert the seed data, causing the tests to fail quickly
/// and preventing further test execution with incomplete setup.
pub async fn postgres_seed_data(pool: &sqlx::PgPool) {
let yaml_tasks = std::fs::read_to_string(
PathBuf::from("tests")
.join("fixtures")
.join("queue")
.join("jobs.yaml"),
)
.expect("Failed to read YAML file");
let yaml_tasks =
std::fs::read_to_string(queue_jobs_fixture_path()).expect("Failed to read YAML file");

let tasks: Vec<bgworker::pg::Job> =
serde_yaml::from_str(&yaml_tasks).expect("Failed to parse YAML");
Expand Down Expand Up @@ -43,13 +47,8 @@ pub async fn postgres_seed_data(pool: &sqlx::PgPool) {
/// This function will panic if it fails to prepare or insert the seed data, causing the tests to fail quickly
/// and preventing further test execution with incomplete setup.
pub async fn sqlite_seed_data(pool: &sqlx::Pool<sqlx::Sqlite>) {
let yaml_tasks = std::fs::read_to_string(
PathBuf::from("tests")
.join("fixtures")
.join("queue")
.join("jobs.yaml"),
)
.expect("Failed to read YAML file");
let yaml_tasks =
std::fs::read_to_string(queue_jobs_fixture_path()).expect("Failed to read YAML file");

let tasks: Vec<bgworker::sqlt::Job> =
serde_yaml::from_str(&yaml_tasks).expect("Failed to parse YAML");
Expand Down
Loading