forked from rubyatscale/codeowners-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracked_files.rs
More file actions
86 lines (71 loc) · 2.66 KB
/
tracked_files.rs
File metadata and controls
86 lines (71 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::{
collections::HashMap,
path::{Path, PathBuf},
process::Command,
};
pub(crate) fn find_tracked_files(base_path: &Path) -> Option<HashMap<PathBuf, bool>> {
let output = Command::new("git")
.args(["ls-files", "-z", "--", "."])
.current_dir(base_path)
.output()
.ok()?;
if !output.status.success() {
return None;
}
let results: HashMap<PathBuf, bool> = output
.stdout
.split(|&b| b == b'\0')
.filter(|chunk| !chunk.is_empty())
.map(|rel| std::str::from_utf8(rel).ok().map(|s| (base_path.join(s), true)))
.collect::<Option<HashMap<PathBuf, bool>>>()?;
Some(results)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_untracked_files() {
let tmp_dir = tempfile::tempdir().unwrap();
assert!(find_tracked_files(tmp_dir.path()).is_none());
std::process::Command::new("git")
.arg("init")
.current_dir(tmp_dir.path())
.output()
.expect("failed to run git init");
std::fs::write(tmp_dir.path().join("test.txt"), "test").unwrap();
let tracked = find_tracked_files(tmp_dir.path()).unwrap();
assert!(tracked.is_empty());
std::process::Command::new("git")
.arg("add")
.arg("test.txt")
.current_dir(tmp_dir.path())
.output()
.expect("failed to add test.txt");
let tracked = find_tracked_files(tmp_dir.path()).unwrap();
assert!(tracked.len() == 1);
assert!(tracked.get(&tmp_dir.path().join("test.txt")).unwrap());
}
#[test]
fn test_tracked_files_from_subdirectory() {
let tmp_dir = tempfile::tempdir().unwrap();
let backend_dir = tmp_dir.path().join("backend");
let tracked_file = backend_dir.join("app/models/foo.rb");
std::process::Command::new("git")
.arg("init")
.current_dir(tmp_dir.path())
.output()
.expect("failed to run git init");
std::fs::create_dir_all(tracked_file.parent().unwrap()).unwrap();
std::fs::write(&tracked_file, "class Foo; end").unwrap();
std::fs::write(tmp_dir.path().join("README.md"), "readme").unwrap();
std::process::Command::new("git")
.args(["add", "--all"])
.current_dir(tmp_dir.path())
.output()
.expect("failed to add tracked files");
let tracked = find_tracked_files(&backend_dir).unwrap();
assert_eq!(tracked.len(), 1);
assert!(tracked.get(&tracked_file).unwrap());
assert!(!tracked.contains_key(&backend_dir.join("backend/app/models/foo.rb")));
}
}