Skip to content

Commit

Permalink
Extract nonexistent directory logic to a test helper struct
Browse files Browse the repository at this point in the history
  • Loading branch information
EliahKagan committed Aug 28, 2024
1 parent 56dab13 commit e60540f
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions gix-path/src/env/git/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,41 @@ mod locations {
}
}

use std::path::Path;
use std::path::{Path, PathBuf};

use gix_testtools::tempfile;
use serial_test::serial;

/// Wrapper for a path to a location kept from accidentally existing, until drop.
#[derive(Debug)]
struct NonexistentLocation {
_empty: tempfile::TempDir,
nonexistent: PathBuf,
}

impl NonexistentLocation {
fn new() -> Self {
let empty = tempfile::tempdir().expect("can create new temporary subdirectory");

let nonexistent = empty
.path()
.canonicalize()
.expect("path to the new directory works")
.join("nonexistent");

assert!(!nonexistent.exists(), "Test bug: Need nonexistent directory");

Self {
_empty: empty,
nonexistent,
}
}

fn path(&self) -> &Path {
&self.nonexistent
}
}

fn set_temp_env_vars<'a>(path: &Path) -> gix_testtools::Env<'a> {
let path_str = path.to_str().expect("valid Unicode");

Expand Down Expand Up @@ -400,15 +431,8 @@ fn exe_info() {
#[test]
#[serial]
fn exe_info_tolerates_broken_tmp() {
let empty = gix_testtools::tempfile::tempdir().expect("can create new temporary subdirectory");
let nonexistent = empty
.path()
.canonicalize()
.expect("path to the new directory works")
.join("nonexistent");
assert!(!nonexistent.exists(), "Test bug: Need nonexistent directory");

let _env = set_temp_env_vars(&nonexistent);
let non = NonexistentLocation::new();
let _env = set_temp_env_vars(non.path());
check_exe_info();
}

Expand Down

0 comments on commit e60540f

Please sign in to comment.