//! The Git backend against real repositories created in temp folders: //! an initial repository without HEAD, commits, renames, CRLF, untracked //! files, a detached HEAD, Unicode paths, a file over the cap, binary //! files, a failing hook, branches and a project inside a repository. //! Every test skips with a message when git is not installed. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] use std::path::Path; use std::process::Command; use std::time::{Duration, Instant}; use lutra_vcs::{FILE_CAP, FileState, Git, Repo, RepoWatch, Rev, VcsError}; /// The installed git, or `None` (with a message) when there is none. fn installed() -> Option { match Git::detect("") { Ok(git) => Some(git), Err(e) => { eprintln!("skipped: no usable git installed ({e})"); None } } } /// Runs git in `dir` for test setup and asserts it succeeded. fn git(dir: &Path, args: &[&str]) -> String { let out = Command::new("git") .args(args) .current_dir(dir) .env_remove("GIT_DIR") .env_remove("GIT_WORK_TREE") .env_remove("GIT_INDEX_FILE") .output() .unwrap(); assert!( out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr) ); String::from_utf8_lossy(&out.stdout).trim().to_string() } /// A new repository on branch `main` with a local identity, no signing, /// no line-ending conversion and its own hooks folder. fn init(dir: &Path) { git(dir, &["init", "-q", "-b", "main"]); for (key, value) in [ ("user.name", "Lutra Test"), ("user.email", "test@lutra.invalid"), ("commit.gpgsign", "false"), ("core.autocrlf", "false"), ("core.hooksPath", ".git/hooks"), ] { git(dir, &["config", key, value]); } } /// Writes a file, creating its folders. fn write(dir: &Path, path: &str, text: impl AsRef<[u8]>) { let full = dir.join(path); std::fs::create_dir_all(full.parent().unwrap()).unwrap(); std::fs::write(full, text).unwrap(); } /// Opens the repository at `dir`. fn open(git: &Git, dir: &Path) -> Repo { git.open(dir).unwrap().expect("a repository") } /// The states of `path` in a status, or `None` if it is not listed. fn states(repo: &Repo, path: &str) -> Option<(FileState, FileState)> { repo.status() .unwrap() .files .into_iter() .find(|f| f.path == path) .map(|f| (f.index, f.worktree)) } #[test] fn folders_outside_a_repository_have_none() { let Some(cli) = installed() else { return }; let dir = tempfile::tempdir().unwrap(); assert!(cli.open(dir.path()).unwrap().is_none()); } #[test] fn a_missing_executable_is_not_found() { let err = Git::detect("C:/no/such/git-binary-for-lutra").unwrap_err(); assert!(matches!(err, VcsError::NotFound(_)), "{err:?}"); } #[test] fn initial_repository_stages_unstages_and_commits() { let Some(cli) = installed() else { return }; let dir = tempfile::tempdir().unwrap(); init(dir.path()); write(dir.path(), "src/a.php", " = branches.local.iter().map(|b| b.name.as_str()).collect(); assert_eq!(names, vec!["feature/x", "main"]); repo.checkout("feature/x").unwrap(); assert_eq!( repo.status().unwrap().head.branch.as_deref(), Some("feature/x") ); write(dir.path(), "a.php", " = branches.remote.iter().map(|b| b.name.as_str()).collect(); assert_eq!(remote, vec!["origin/main", "origin/remote-only"]); assert_eq!(branches.local[0].upstream.as_deref(), Some("origin/main")); let status = repo.status().unwrap(); assert_eq!(status.head.upstream.as_deref(), Some("origin/main")); assert_eq!((status.head.ahead, status.head.behind), (Some(0), Some(0))); repo.checkout("origin/remote-only").unwrap(); let branches = repo.branches().unwrap(); assert_eq!(branches.current.as_deref(), Some("remote-only")); let local = branches.local.iter().find(|b| b.name == "remote-only"); assert_eq!( local.and_then(|b| b.upstream.as_deref()), Some("origin/remote-only") ); } #[test] fn remote_branches_without_a_configured_remote_check_out_untracked_and_clean() { let Some(cli) = installed() else { return }; let dir = tempfile::tempdir().unwrap(); init(dir.path()); write(dir.path(), "a.php", "&2\nexit 1\n", ); #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; let hook = dir.path().join(".git/hooks/pre-commit"); std::fs::set_permissions(hook, std::fs::Permissions::from_mode(0o755)).unwrap(); } write(dir.path(), "a.php", " = repo .status() .unwrap() .files .into_iter() .map(|f| f.path) .collect(); assert_eq!(paths, vec!["inside.php"]); repo.stage(&["inside.php".into()]).unwrap(); assert_eq!( states(&repo, "inside.php"), Some((FileState::Added, FileState::Unmodified)) ); for bad in ["../outside.php", "/etc/passwd", "", "a\nb"] { let err = repo.stage(&[bad.into()]).unwrap_err(); assert!(matches!(err, VcsError::InvalidPath(_)), "{bad}: {err:?}"); } } #[test] fn the_watch_sees_outside_changes_and_skips_rebased_ones() { let Some(cli) = installed() else { return }; let dir = tempfile::tempdir().unwrap(); init(dir.path()); write(dir.path(), "a.php", "