rust-lang / rust-lang/git2-rs

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

Open
#550 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.1k
Forks
450
Avg merge
11m
Merged PRs (30d)
1

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/config.rs with get_user_config_type and the tests test_get_user_config_type_xdg and test_get_user_config_type_global. Run cargo test get_user_config_type -- --test-threads=1 --show-output, then inspect how configuration and environment state are handled between tests. Done means both tests pass together as well as individually.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.