Skip to content

Commit 5ac5f74

Browse files
committed
improve structure of exe_info tests
1 parent 5200184 commit 5ac5f74

File tree

1 file changed

+165
-160
lines changed

1 file changed

+165
-160
lines changed

gix-path/src/env/git/tests.rs

Lines changed: 165 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::Path;
2+
13
#[cfg(windows)]
24
mod locations {
35
use std::ffi::{OsStr, OsString};
@@ -355,201 +357,204 @@ mod locations {
355357
}
356358
}
357359

358-
use std::path::{Path, PathBuf};
360+
mod exe_info {
361+
use std::path::{Path, PathBuf};
359362

360-
use gix_testtools::tempfile;
361-
use serial_test::serial;
363+
use crate::env::git::{exe_info, NULL_DEVICE};
364+
use gix_testtools::tempfile;
365+
use serial_test::serial;
362366

363-
/// Wrapper for a valid path to a plausible location, kept from accidentally existing (until drop).
364-
#[derive(Debug)]
365-
struct NonexistentLocation {
366-
_empty: tempfile::TempDir,
367-
nonexistent: PathBuf,
368-
}
367+
/// Wrapper for a valid path to a plausible location, kept from accidentally existing (until drop).
368+
#[derive(Debug)]
369+
struct NonexistentLocation {
370+
_empty: tempfile::TempDir,
371+
nonexistent: PathBuf,
372+
}
369373

370-
impl NonexistentLocation {
371-
fn new() -> Self {
372-
let empty = tempfile::tempdir().expect("can create new temporary subdirectory");
374+
impl NonexistentLocation {
375+
fn new() -> Self {
376+
let empty = tempfile::tempdir().expect("can create new temporary subdirectory");
373377

374-
let nonexistent = empty
375-
.path()
376-
.canonicalize()
377-
.expect("path to the new directory works")
378-
.join("nonexistent");
378+
let nonexistent = empty
379+
.path()
380+
.canonicalize()
381+
.expect("path to the new directory works")
382+
.join("nonexistent");
379383

380-
assert!(!nonexistent.exists(), "Test bug: Need nonexistent directory");
384+
assert!(!nonexistent.exists(), "Test bug: Need nonexistent directory");
381385

382-
Self {
383-
_empty: empty,
384-
nonexistent,
386+
Self {
387+
_empty: empty,
388+
nonexistent,
389+
}
390+
}
391+
392+
fn path(&self) -> &Path {
393+
&self.nonexistent
385394
}
386395
}
387396

388-
fn path(&self) -> &Path {
389-
&self.nonexistent
397+
fn set_temp_env_vars<'a>(path: &Path) -> gix_testtools::Env<'a> {
398+
let path_str = path.to_str().expect("valid Unicode");
399+
400+
let env = gix_testtools::Env::new()
401+
.set("TMPDIR", path_str) // Mainly for Unix.
402+
.set("TMP", path_str) // Mainly for Windows.
403+
.set("TEMP", path_str); // Mainly for Windows, too.
404+
405+
assert_eq!(
406+
std::env::temp_dir(),
407+
path,
408+
"Possible test bug: Temp dir path may not have been customized successfully"
409+
);
410+
411+
env
390412
}
391-
}
392413

393-
fn set_temp_env_vars<'a>(path: &Path) -> gix_testtools::Env<'a> {
394-
let path_str = path.to_str().expect("valid Unicode");
414+
fn unset_windows_directory_vars<'a>() -> gix_testtools::Env<'a> {
415+
gix_testtools::Env::new().unset("windir").unset("SystemRoot")
416+
}
395417

396-
let env = gix_testtools::Env::new()
397-
.set("TMPDIR", path_str) // Mainly for Unix.
398-
.set("TMP", path_str) // Mainly for Windows.
399-
.set("TEMP", path_str); // Mainly for Windows, too.
418+
fn check_exe_info() {
419+
let path = exe_info()
420+
.map(crate::from_bstring)
421+
.expect("It is present in the test environment (nonempty config)");
400422

401-
assert_eq!(
402-
std::env::temp_dir(),
403-
path,
404-
"Possible test bug: Temp dir path may not have been customized successfully"
405-
);
423+
assert!(
424+
path.is_absolute(),
425+
"It is absolute (unless overridden such as with GIT_CONFIG_SYSTEM)"
426+
);
427+
assert!(
428+
path.exists(),
429+
"It should exist on disk, since `git config` just found an entry there"
430+
);
431+
}
406432

407-
env
408-
}
433+
#[test]
434+
#[serial]
435+
fn with_unmodified_environment() {
436+
check_exe_info();
437+
}
409438

410-
fn unset_windows_directory_vars<'a>() -> gix_testtools::Env<'a> {
411-
gix_testtools::Env::new().unset("windir").unset("SystemRoot")
412-
}
439+
#[test]
440+
#[serial]
441+
fn tolerates_broken_temp() {
442+
let non = NonexistentLocation::new();
443+
let _env = set_temp_env_vars(non.path());
444+
check_exe_info();
445+
}
413446

414-
fn check_exe_info() {
415-
let path = super::exe_info()
416-
.map(crate::from_bstring)
417-
.expect("It is present in the test environment (nonempty config)");
447+
#[test]
448+
#[serial]
449+
fn tolerates_oversanitized_env() {
450+
// This test runs on all systems, but it is only checking for a Windows regression. Also, on
451+
// Windows, having both a broken temp dir and an over-sanitized environment is not supported.
452+
let _env = unset_windows_directory_vars();
453+
check_exe_info();
454+
}
418455

419-
assert!(
420-
path.is_absolute(),
421-
"It is absolute (unless overridden such as with GIT_CONFIG_SYSTEM)"
422-
);
423-
assert!(
424-
path.exists(),
425-
"It should exist on disk, since `git config` just found an entry there"
426-
);
427-
}
456+
#[test]
457+
#[serial]
458+
fn same_result_with_broken_temp() {
459+
let with_unmodified_temp = exe_info();
428460

429-
#[test]
430-
#[serial]
431-
fn exe_info() {
432-
check_exe_info();
433-
}
461+
let with_nonexistent_temp = {
462+
let non = NonexistentLocation::new();
463+
let _env = set_temp_env_vars(non.path());
464+
exe_info()
465+
};
434466

435-
#[test]
436-
#[serial]
437-
fn exe_info_tolerates_broken_temp() {
438-
let non = NonexistentLocation::new();
439-
let _env = set_temp_env_vars(non.path());
440-
check_exe_info();
441-
}
467+
assert_eq!(with_unmodified_temp, with_nonexistent_temp);
468+
}
442469

443-
#[test]
444-
#[serial]
445-
fn exe_info_tolerates_oversanitized_env() {
446-
// This test runs on all systems, but it is only checking for a Windows regression. Also, on
447-
// Windows, having both a broken temp dir and an over-sanitized environment is not supported.
448-
let _env = unset_windows_directory_vars();
449-
check_exe_info();
450-
}
470+
#[test]
471+
#[serial]
472+
fn same_result_with_oversanitized_env() {
473+
let with_unmodified_env = exe_info();
451474

452-
#[test]
453-
#[serial]
454-
fn exe_info_same_result_with_broken_temp() {
455-
let with_unmodified_temp = super::exe_info();
475+
let with_oversanitized_env = {
476+
let _env = unset_windows_directory_vars();
477+
exe_info()
478+
};
456479

457-
let with_nonexistent_temp = {
458-
let non = NonexistentLocation::new();
459-
let _env = set_temp_env_vars(non.path());
460-
super::exe_info()
461-
};
480+
assert_eq!(with_unmodified_env, with_oversanitized_env);
481+
}
462482

463-
assert_eq!(with_unmodified_temp, with_nonexistent_temp);
464-
}
483+
#[test]
484+
#[serial]
485+
#[cfg(not(target_os = "macos"))] // Assumes no higher "unknown" scope. The `nosystem` case works.
486+
fn never_from_local_scope() {
487+
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh").expect("script succeeds");
465488

466-
#[test]
467-
#[serial]
468-
fn exe_info_same_result_with_oversanitized_env() {
469-
let with_unmodified_env = super::exe_info();
489+
let _cwd = gix_testtools::set_current_dir(repo).expect("can change to repo dir");
490+
let _env = gix_testtools::Env::new()
491+
.set("GIT_CONFIG_SYSTEM", NULL_DEVICE)
492+
.set("GIT_CONFIG_GLOBAL", NULL_DEVICE);
470493

471-
let with_oversanitized_env = {
472-
let _env = unset_windows_directory_vars();
473-
super::exe_info()
474-
};
494+
let maybe_path = exe_info();
495+
assert_eq!(
496+
maybe_path, None,
497+
"Should find no config path if the config would be local (empty system config)"
498+
);
499+
}
475500

476-
assert_eq!(with_unmodified_env, with_oversanitized_env);
477-
}
501+
#[test]
502+
#[serial]
503+
fn never_from_local_scope_nosystem() {
504+
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh").expect("script succeeds");
478505

479-
#[test]
480-
#[serial]
481-
#[cfg(not(target_os = "macos"))] // Assumes no higher "unknown" scope. The `nosystem` case works.
482-
fn exe_info_never_from_local_scope() {
483-
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh").expect("script succeeds");
484-
485-
let _cwd = gix_testtools::set_current_dir(repo).expect("can change to repo dir");
486-
let _env = gix_testtools::Env::new()
487-
.set("GIT_CONFIG_SYSTEM", super::NULL_DEVICE)
488-
.set("GIT_CONFIG_GLOBAL", super::NULL_DEVICE);
489-
490-
let maybe_path = super::exe_info();
491-
assert_eq!(
492-
maybe_path, None,
493-
"Should find no config path if the config would be local (empty system config)"
494-
);
495-
}
506+
let _cwd = gix_testtools::set_current_dir(repo).expect("can change to repo dir");
507+
let _env = gix_testtools::Env::new()
508+
.set("GIT_CONFIG_NOSYSTEM", "1")
509+
.set("GIT_CONFIG_GLOBAL", NULL_DEVICE);
496510

497-
#[test]
498-
#[serial]
499-
fn exe_info_never_from_local_scope_nosystem() {
500-
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh").expect("script succeeds");
511+
let maybe_path = exe_info();
512+
assert_eq!(
513+
maybe_path, None,
514+
"Should find no config path if the config would be local (suppressed system config)"
515+
);
516+
}
501517

502-
let _cwd = gix_testtools::set_current_dir(repo).expect("can change to repo dir");
503-
let _env = gix_testtools::Env::new()
504-
.set("GIT_CONFIG_NOSYSTEM", "1")
505-
.set("GIT_CONFIG_GLOBAL", super::NULL_DEVICE);
518+
#[test]
519+
#[serial]
520+
#[cfg(not(target_os = "macos"))] // Assumes no higher "unknown" scope. The `nosystem` case works.
521+
fn never_from_local_scope_even_if_temp_is_here() {
522+
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh")
523+
.expect("script succeeds")
524+
.canonicalize()
525+
.expect("repo path is valid and exists");
506526

507-
let maybe_path = super::exe_info();
508-
assert_eq!(
509-
maybe_path, None,
510-
"Should find no config path if the config would be local (suppressed system config)"
511-
);
512-
}
527+
let _cwd = gix_testtools::set_current_dir(&repo).expect("can change to repo dir");
528+
let _env = set_temp_env_vars(&repo)
529+
.set("GIT_CONFIG_SYSTEM", NULL_DEVICE)
530+
.set("GIT_CONFIG_GLOBAL", NULL_DEVICE);
513531

514-
#[test]
515-
#[serial]
516-
#[cfg(not(target_os = "macos"))] // Assumes no higher "unknown" scope. The `nosystem` case works.
517-
fn exe_info_never_from_local_scope_even_if_temp_is_here() {
518-
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh")
519-
.expect("script succeeds")
520-
.canonicalize()
521-
.expect("repo path is valid and exists");
522-
523-
let _cwd = gix_testtools::set_current_dir(&repo).expect("can change to repo dir");
524-
let _env = set_temp_env_vars(&repo)
525-
.set("GIT_CONFIG_SYSTEM", super::NULL_DEVICE)
526-
.set("GIT_CONFIG_GLOBAL", super::NULL_DEVICE);
527-
528-
let maybe_path = super::exe_info();
529-
assert_eq!(
530-
maybe_path, None,
531-
"Should find no config path if the config would be local even in a `/tmp`-like dir (empty system config)"
532-
);
533-
}
532+
let maybe_path = exe_info();
533+
assert_eq!(
534+
maybe_path, None,
535+
"Should find no config path if the config would be local even in a `/tmp`-like dir (empty system config)"
536+
);
537+
}
534538

535-
#[test]
536-
#[serial]
537-
fn exe_info_never_from_local_scope_even_if_temp_is_here_nosystem() {
538-
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh")
539-
.expect("script succeeds")
540-
.canonicalize()
541-
.expect("repo path is valid and exists");
542-
543-
let _cwd = gix_testtools::set_current_dir(&repo).expect("can change to repo dir");
544-
let _env = set_temp_env_vars(&repo)
545-
.set("GIT_CONFIG_NOSYSTEM", "1")
546-
.set("GIT_CONFIG_GLOBAL", super::NULL_DEVICE);
547-
548-
let maybe_path = super::exe_info();
549-
assert_eq!(
539+
#[test]
540+
#[serial]
541+
fn never_from_local_scope_even_if_temp_is_here_nosystem() {
542+
let repo = gix_testtools::scripted_fixture_read_only("local_config.sh")
543+
.expect("script succeeds")
544+
.canonicalize()
545+
.expect("repo path is valid and exists");
546+
547+
let _cwd = gix_testtools::set_current_dir(&repo).expect("can change to repo dir");
548+
let _env = set_temp_env_vars(&repo)
549+
.set("GIT_CONFIG_NOSYSTEM", "1")
550+
.set("GIT_CONFIG_GLOBAL", NULL_DEVICE);
551+
552+
let maybe_path = exe_info();
553+
assert_eq!(
550554
maybe_path, None,
551555
"Should find no config path if the config would be local even in a `/tmp`-like dir (suppressed system config)"
552556
);
557+
}
553558
}
554559

555560
#[test]

0 commit comments

Comments
 (0)