Skip to content

Config get_xdg() and and get_global() weird state behaviour... #550

Open
@bradwood

Description

@bradwood

Hi all,

I have this function:

fn get_user_config_type() -> Option<UserGitConfigLevel> {

    if GitConfig::find_xdg().is_ok() {
        Some(UserGitConfigLevel::XDG)
    } else {
        Some(UserGitConfigLevel::Global)
    }
}

And I have these 2 tests:

    #[test]
    fn test_get_user_config_type_xdg() {
        // set up XDG
        let home = assert_fs::TempDir::new().unwrap().into_persistent();
        env::set_var("HOME", home.path()); //fake home directory
        env::set_var("XDG_CONFIG_HOME", home.child(".config").path()); //fake XDG config directory
        std::fs::create_dir_all(home.child(".config/git").path()).unwrap(); // create the actual XDG config dir
        std::fs::write(home.child(".config/git/config").path(),"").unwrap(); // touch the xdg config file

        let config_type = get_user_config_type();

        assert_eq!(config_type.unwrap(), UserGitConfigLevel::XDG);

        home.close().unwrap()
    }

    #[test]
    fn test_get_user_config_type_global() {
        // set up home dir
        let home = assert_fs::TempDir::new().unwrap().into_persistent();
        env::set_var("HOME", home.path()); //fake home directory
        std::fs::write(home.child(".gitconfig").path(),"").unwrap(); // touch the xdg config file

        let config_type = get_user_config_type();

        assert_eq!(config_type.unwrap(), UserGitConfigLevel::Global);

        home.close().unwrap()
    }

When I call each test individually, like this, they both succeed:

git-lab-rust! ❯ cargo test get_user_config_type_xdg -- --test-threads=1 --show-output 
    Finished test [unoptimized + debuginfo] target(s) in 0.06s
     Running target/debug/deps/git_lab-088586bafc7dced7

running 1 test
test config::config_unit_tests::test_get_user_config_type_xdg ... ok

successes:

successes:
    config::config_unit_tests::test_get_user_config_type_xdg

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 35 filtered out

     Running target/debug/deps/cli_invocation_tests-a5abebe7e09b566f

running 0 tests

successes:

successes:

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out

git-lab-rust ❯ cargo test get_user_config_type_global -- --test-threads=1 --show-output 
    Finished test [unoptimized + debuginfo] target(s) in 0.06s
     Running target/debug/deps/git_lab-088586bafc7dced7

running 1 test
test config::config_unit_tests::test_get_user_config_type_global ... ok

successes:

successes:
    config::config_unit_tests::test_get_user_config_type_global

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 35 filtered out

     Running target/debug/deps/cli_invocation_tests-a5abebe7e09b566f

running 0 tests

successes:

successes:

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out

git-lab-rust ❯

However, when I run them both in one cargo test invocation, I always get one failure:

git-lab-rust! ❯ cargo test get_user_config_type -- --test-threads=1 --show-output 
    Finished test [unoptimized + debuginfo] target(s) in 0.03s
     Running target/debug/deps/git_lab-088586bafc7dced7

running 2 tests
test config::config_unit_tests::test_get_user_config_type_global ... ok
test config::config_unit_tests::test_get_user_config_type_xdg ... FAILED

successes:

successes:
    config::config_unit_tests::test_get_user_config_type_global

failures:

---- config::config_unit_tests::test_get_user_config_type_xdg stdout ----
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Global`,
 right: `XDG`', src/config.rs:493:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    config::config_unit_tests::test_get_user_config_type_xdg

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 34 filtered out

error: test failed, to rerun pass '--bin git-lab'
git-lab-rust!

Can anyone explain why this is? There seems to be some state which is persisting when both tests are run in one invocation which seems completely weird and maybe is a bug...

Any thoughts or suggestions to make these tests work together would be much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions