Debian Codegraph @

helix-vcs/src/git/test.rs

raw symbols SCIP
1 use std::{fs::File, io::Write, path::Path, process::Command};
2
3 use tempfile::TempDir;
4
5 use crate::git;
6
7 fn exec_git_cmd(args: &str, git_dir: &Path) {
8 let res = Command::new("git")
9 .arg("-C")
10 .arg(git_dir) // execute the git command in this directory
11 .args(args.split_whitespace())
12 .env_remove("GIT_DIR")
13 .env_remove("GIT_ASKPASS")
14 .env_remove("SSH_ASKPASS")
15 .env("GIT_TERMINAL_PROMPT", "false")
16 .env("GIT_AUTHOR_DATE", "2000-01-01 00:00:00 +0000")
17 .env("GIT_AUTHOR_EMAIL", "[email protected]")
18 .env("GIT_AUTHOR_NAME", "author")
19 .env("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000")
20 .env("GIT_COMMITTER_EMAIL", "[email protected]")
21 .env("GIT_COMMITTER_NAME", "committer")
22 .env("GIT_CONFIG_COUNT", "2")
23 .env("GIT_CONFIG_KEY_0", "commit.gpgsign")
24 .env("GIT_CONFIG_VALUE_0", "false")
25 .env("GIT_CONFIG_KEY_1", "init.defaultBranch")
26 .env("GIT_CONFIG_VALUE_1", "main")
27 .output()
28 .unwrap_or_else(|_| panic!("`git {args}` failed"));
29 if !res.status.success() {
30 println!("{}", String::from_utf8_lossy(&res.stdout));
31 eprintln!("{}", String::from_utf8_lossy(&res.stderr));
32 panic!("`git {args}` failed (see output above)")
33 }
34 }
35
36 fn create_commit(repo: &Path, add_modified: bool) {
37 if add_modified {
38 exec_git_cmd("add -A", repo);
39 }
40 exec_git_cmd("commit -m message", repo);
41 }
42
43 fn empty_git_repo() -> TempDir {
44 let tmp = tempfile::tempdir().expect("create temp dir for git testing");
45 exec_git_cmd("init", tmp.path());
46 exec_git_cmd("config user.email [email protected]", tmp.path());
47 exec_git_cmd("config user.name helix-test", tmp.path());
48 tmp
49 }
50
51 #[test]
52 fn missing_file() {
53 let temp_git = empty_git_repo();
54 let file = temp_git.path().join("file.txt");
55 File::create(&file).unwrap().write_all(b"foo").unwrap();
56
57 assert!(git::get_diff_base(&file, true).is_err());
58 }
59
60 #[test]
61 fn unmodified_file() {
62 let temp_git = empty_git_repo();
63 let file = temp_git.path().join("file.txt");
64 let contents = b"foo".as_slice();
65 File::create(&file).unwrap().write_all(contents).unwrap();
66 create_commit(temp_git.path(), true);
67 assert_eq!(
68 git::get_diff_base(&file, true).unwrap(),
69 Vec::from(contents)
70 );
71 }
72
73 #[test]
74 fn modified_file() {
75 let temp_git = empty_git_repo();
76 let file = temp_git.path().join("file.txt");
77 let contents = b"foo".as_slice();
78 File::create(&file).unwrap().write_all(contents).unwrap();
79 create_commit(temp_git.path(), true);
80 File::create(&file).unwrap().write_all(b"bar").unwrap();
81
82 assert_eq!(
83 git::get_diff_base(&file, true).unwrap(),
84 Vec::from(contents)
85 );
86 }
87
88 /// Test that `get_file_head` does not return content for a directory.
89 /// This is important to correctly cover cases where a directory is removed and replaced by a file.
90 /// If the contents of the directory object were returned a diff between a path and the directory children would be produced.
91 #[test]
92 fn directory() {
93 let temp_git = empty_git_repo();
94 let dir = temp_git.path().join("file.txt");
95 std::fs::create_dir(&dir).expect("");
96 let file = dir.join("file.txt");
97 let contents = b"foo".as_slice();
98 File::create(file).unwrap().write_all(contents).unwrap();
99
100 create_commit(temp_git.path(), true);
101
102 std::fs::remove_dir_all(&dir).unwrap();
103 File::create(&dir).unwrap().write_all(b"bar").unwrap();
104 assert!(git::get_diff_base(&dir, true).is_err());
105 }
106
107 /// Test that `get_diff_base` resolves symlinks so that the same diff base is
108 /// used as the target file.
109 ///
110 /// This is important to correctly cover cases where a symlink is removed and
111 /// replaced by a file. If the contents of the symlink object were returned
112 /// a diff between a literal file path and the actual file content would be
113 /// produced (bad ui).
114 #[cfg(any(unix, windows))]
115 #[test]
116 fn symlink() {
117 #[cfg(unix)]
118 use std::os::unix::fs::symlink;
119 #[cfg(not(unix))]
120 use std::os::windows::fs::symlink_file as symlink;
121
122 let temp_git = empty_git_repo();
123 let file = temp_git.path().join("file.txt");
124 let contents = Vec::from(b"foo");
125 File::create(&file).unwrap().write_all(&contents).unwrap();
126 let file_link = temp_git.path().join("file_link.txt");
127
128 symlink("file.txt", &file_link).unwrap();
129 create_commit(temp_git.path(), true);
130
131 assert_eq!(git::get_diff_base(&file_link, true).unwrap(), contents);
132 assert_eq!(git::get_diff_base(&file, true).unwrap(), contents);
133 }
134
135 /// Test that `get_diff_base` returns content when the file is a symlink to
136 /// another file that is in a git repo, but the symlink itself is not.
137 #[cfg(any(unix, windows))]
138 #[test]
139 fn symlink_to_git_repo() {
140 #[cfg(unix)]
141 use std::os::unix::fs::symlink;
142 #[cfg(not(unix))]
143 use std::os::windows::fs::symlink_file as symlink;
144
145 let temp_dir = tempfile::tempdir().expect("create temp dir");
146 let temp_git = empty_git_repo();
147
148 let file = temp_git.path().join("file.txt");
149 let contents = Vec::from(b"foo");
150 File::create(&file).unwrap().write_all(&contents).unwrap();
151 create_commit(temp_git.path(), true);
152
153 let file_link = temp_dir.path().join("file_link.txt");
154 symlink(&file, &file_link).unwrap();
155
156 assert_eq!(git::get_diff_base(&file_link, true).unwrap(), contents);
157 assert_eq!(git::get_diff_base(&file, true).unwrap(), contents);
158 }