diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 08665f069ad6..510535417ee4 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -12,7 +12,9 @@ use std::thread; use cargo_test_support::git::cargo_uses_gitoxide; use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::registry::Package; -use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, path2url, project}; +use cargo_test_support::{ + basic_lib_manifest, basic_manifest, git, git_process, main_file, path2url, project, +}; use cargo_test_support::{sleep_ms, t, Project}; #[cargo_test] @@ -3760,3 +3762,133 @@ fn different_user_relative_submodules() { assert!(project.bin("foo").is_file()); } + +#[cargo_test] +fn git_worktree_with_original_repo_renamed() { + let project = project().build(); + let git_project = git::new("foo", |project| { + project + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + authors = [] + license = "MIR OR Apache-2.0" + description = "A test!" + homepage = "https://example.org" + documentation = "" + repository = "https://example.org" + readme = "./README.md" + "#, + ) + .file("src/lib.rs", "") + .file("README.md", "") + }); + + let repo = git2::Repository::open(&git_project.root()).unwrap(); + let repo_root = repo.workdir().unwrap().parent().unwrap(); + let opts = git2::WorktreeAddOptions::new(); + let _ = repo + .worktree("bar", &repo_root.join("bar"), Some(&opts)) + .unwrap(); + + // Rename the original repository + let new = repo_root.join("foo2"); + fs::rename(&git_project.root(), &new).unwrap(); + + project + .cargo("package --list") + .cwd(&new) + .with_stdout( + "\ +.cargo_vcs_info.json +Cargo.toml +Cargo.toml.orig +README.md +src/lib.rs +", + ) + .run(); + + project + .cargo("check") + .cwd(&new) + .with_stderr(&format!( + "[CHECKING] foo v0.5.0 ([CWD])\n\ + [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) \ + in [..]\n" + )) + .run(); +} + +#[cargo_test(requires_git)] +fn git_worktree_with_bare_original_repo() { + let project = project().build(); + let git_project = git::new("foo", |project| { + project + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + authors = [] + license = "MIR OR Apache-2.0" + description = "A test!" + homepage = "https://example.org" + documentation = "" + repository = "https://example.org" + readme = "./README.md" + "#, + ) + .file("src/lib.rs", "") + .file("README.md", "") + }); + + // Create a "bare" Git repository. + // Keep the `.git` folder and delete the others. + let root = git_project.root(); + fs::remove_file(&root.join("Cargo.toml")).unwrap(); + fs::remove_file(&root.join("README.md")).unwrap(); + fs::remove_dir_all(&root.join("src")).unwrap(); + git_process("config --bool core.bare true") + .cwd(&root) + .exec() + .unwrap(); + + let repo = git2::Repository::open(&root).unwrap(); + assert!(repo.is_bare()); + let repo_root = root.parent().unwrap(); + let opts = git2::WorktreeAddOptions::new(); + let wt = repo + .worktree("bar", &repo_root.join("bar"), Some(&opts)) + .unwrap(); + + project + .cargo("package --list") + .cwd(wt.path()) + .with_stdout( + "\ +.cargo_vcs_info.json +Cargo.toml +Cargo.toml.orig +README.md +src/lib.rs +", + ) + .run(); + + project + .cargo("check") + .cwd(wt.path()) + .with_stderr(&format!( + "[CHECKING] foo v0.5.0 ([CWD])\n\ + [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) \ + in [..]\n" + )) + .run(); +}