From 6d92f97ed37b1b850bb3370483733c98acec5cae Mon Sep 17 00:00:00 2001 From: Cormac Relf Date: Sun, 13 Oct 2024 18:16:02 +1100 Subject: [PATCH] git: add test to diagnose worktree gitfiles on windows We're getting paths like this, I think. But I don't know what git does, and I don't have access to a windows machine. gitdir: \\?\C:\Users\runneradmin\AppData\Local\Temp\jj-test-fy26a2\second --- cli/tests/test_git_colocated.rs | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/cli/tests/test_git_colocated.rs b/cli/tests/test_git_colocated.rs index 8f737478ae..50cf264596 100644 --- a/cli/tests/test_git_colocated.rs +++ b/cli/tests/test_git_colocated.rs @@ -22,6 +22,7 @@ use jj_lib::object_id::ObjectId; use jj_lib::op_store::RefTarget; use jj_lib::op_store::ViewId; +use crate::common::get_stdout_string; use crate::common::TestEnvironment; #[test] @@ -1325,3 +1326,67 @@ fn test_colocated_view_proto_deprecated() { ~ "#); } + +#[test] +fn test_worktrees_match_git_behaviour() { + // TODO: Better way to disable the test if git command couldn't be executed + if Command::new("git").arg("--version").status().is_err() { + eprintln!("Skipping because git command might fail to run"); + return; + } + + let test_env = TestEnvironment::default(); + let repo_path = test_env.env_root().join("repo"); + let abc_path = test_env.env_root().join("abc"); + + let git_cmd_success = |test_env: &TestEnvironment, path: &Path, args: &[&str]| -> String { + let cmd = Command::new("git") + .args(args) + .current_dir(path) + .env("GIT_AUTHOR_NAME", "JJ Test Suite") + .env("GIT_AUTHOR_EMAIL", "jj@example.com") + .env("GIT_AUTHOR_DATE", "2005-04-07T22:13:13Z") + .env("GIT_COMMITTER_NAME", "JJ Test Suite") + .env("GIT_COMMITTER_EMAIL", "jj@example.com") + .env("GIT_COMMITTER_DATE", "2005-04-07T22:13:13Z") + .assert() + .success(); + let stdout = get_stdout_string(&cmd); + test_env.normalize_output(&stdout) + }; + + let read_worktree_setup = || { + let gitfile = std::fs::read_to_string(abc_path.join(".git")).unwrap(); + let worktree_path = repo_path.join(".git").join("worktrees").join("abc"); + let gitdir = std::fs::read_to_string(worktree_path.join("gitdir")).unwrap(); + let commondir = std::fs::read_to_string(worktree_path.join("commondir")).unwrap(); + eprintln!(" abc/.git -> {gitfile}"); + eprintln!(" repo/.git/worktrees/abc/gitdir: {gitdir}"); + eprintln!(" repo/.git/worktrees/abc/commondir: {commondir}"); + (gitfile, gitdir, commondir) + }; + + git_cmd_success(&test_env, test_env.env_root(), &["init", "repo"]); + std::fs::write(repo_path.join("file.txt"), "contents").unwrap(); + git_cmd_success(&test_env, &repo_path, &["add", "file.txt"]); + git_cmd_success(&test_env, &repo_path, &["commit", "-m", "initial commit"]); + git_cmd_success(&test_env, &repo_path, &["worktree", "add", "../abc"]); + + eprintln!("git:"); + let (git_gitfile, git_gitdir, git_commondir) = read_worktree_setup(); + + std::fs::remove_dir_all(&repo_path).unwrap(); + std::fs::remove_dir_all(&abc_path).unwrap(); + + test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "--colocate", "repo"]); + std::fs::write(repo_path.join("file.txt"), "contents").unwrap(); + test_env.jj_cmd_ok(&repo_path, &["commit", "-m", "initial commit"]); + test_env.jj_cmd_ok(&repo_path, &["workspace", "add", "--colocate", "../abc"]); + + eprintln!("jj:"); + let (jj_gitfile, jj_gitdir, jj_commondir) = read_worktree_setup(); + + assert_eq!(jj_gitfile, git_gitfile); + assert_eq!(jj_gitdir, git_gitdir); + assert_eq!(jj_commondir, git_commondir); +}