|
1 | | -mod ignore { |
2 | | - use std::io::Read; |
3 | | - |
4 | | - use bstr::{BStr, ByteSlice}; |
5 | | - use gix_attributes::{Ignore, Match, MatchGroup}; |
6 | | - use gix_glob::pattern::Case; |
7 | | - |
8 | | - struct Expectations<'a> { |
9 | | - lines: bstr::Lines<'a>, |
10 | | - } |
11 | | - |
12 | | - impl<'a> Iterator for Expectations<'a> { |
13 | | - type Item = (&'a BStr, Option<(&'a BStr, usize, &'a BStr)>); |
14 | | - |
15 | | - fn next(&mut self) -> Option<Self::Item> { |
16 | | - let line = self.lines.next()?; |
17 | | - let (left, value) = line.split_at(line.find_byte(b'\t').unwrap()); |
18 | | - let value = value[1..].as_bstr(); |
19 | | - |
20 | | - let source_and_line = if left == b"::" { |
21 | | - None |
22 | | - } else { |
23 | | - let mut tokens = left.split(|b| *b == b':'); |
24 | | - let source = tokens.next().unwrap().as_bstr(); |
25 | | - let line_number: usize = tokens.next().unwrap().to_str_lossy().parse().ok().unwrap(); |
26 | | - let pattern = tokens.next().unwrap().as_bstr(); |
27 | | - Some((source, line_number, pattern)) |
28 | | - }; |
29 | | - Some((value, source_and_line)) |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - #[test] |
34 | | - fn from_git_dir() -> crate::Result { |
35 | | - let dir = gix_testtools::scripted_fixture_read_only("make_global_and_external_and_dir_ignores.sh")?; |
36 | | - let repo_dir = dir.join("repo"); |
37 | | - let git_dir = repo_dir.join(".git"); |
38 | | - let baseline = std::fs::read(git_dir.parent().unwrap().join("git-check-ignore.baseline"))?; |
39 | | - let mut buf = Vec::new(); |
40 | | - let mut group = MatchGroup::from_git_dir(git_dir, Some(dir.join("user.exclude")), &mut buf)?; |
41 | | - |
42 | | - assert!( |
43 | | - !group.add_patterns_file("not-a-file", false, None, &mut buf)?, |
44 | | - "missing files are no problem and cause a negative response" |
45 | | - ); |
46 | | - assert!( |
47 | | - group.add_patterns_file(repo_dir.join(".gitignore"), true, repo_dir.as_path().into(), &mut buf)?, |
48 | | - "existing files return true" |
49 | | - ); |
50 | | - |
51 | | - buf.clear(); |
52 | | - let ignore_file = repo_dir.join("dir-with-ignore").join(".gitignore"); |
53 | | - std::fs::File::open(&ignore_file)?.read_to_end(&mut buf)?; |
54 | | - group.add_patterns_buffer(&buf, ignore_file, repo_dir.as_path().into()); |
55 | | - |
56 | | - for (path, source_and_line) in (Expectations { |
57 | | - lines: baseline.lines(), |
58 | | - }) { |
59 | | - let actual = group.pattern_matching_relative_path( |
60 | | - path, |
61 | | - repo_dir |
62 | | - .join(path.to_str_lossy().as_ref()) |
63 | | - .metadata() |
64 | | - .ok() |
65 | | - .map(|m| m.is_dir()), |
66 | | - Case::Sensitive, |
67 | | - ); |
68 | | - match (actual, source_and_line) { |
69 | | - ( |
70 | | - Some(Match { |
71 | | - sequence_number, |
72 | | - pattern: _, |
73 | | - source, |
74 | | - value: _, |
75 | | - }), |
76 | | - Some((expected_source, line, _expected_pattern)), |
77 | | - ) => { |
78 | | - assert_eq!(sequence_number, line, "our counting should match the one used in git"); |
79 | | - assert_eq!( |
80 | | - source.map(|p| p.canonicalize().unwrap()), |
81 | | - Some(repo_dir.join(expected_source.to_str_lossy().as_ref()).canonicalize()?) |
82 | | - ); |
83 | | - } |
84 | | - (None, None) => {} |
85 | | - (actual, expected) => panic!("actual {actual:?} should match {expected:?} with path '{path}'"), |
86 | | - } |
87 | | - } |
88 | | - Ok(()) |
89 | | - } |
90 | | - |
91 | | - #[test] |
92 | | - fn from_overrides() { |
93 | | - let input = ["simple", "pattern/"]; |
94 | | - let group = gix_attributes::MatchGroup::<Ignore>::from_overrides(input); |
95 | | - assert_eq!( |
96 | | - group.pattern_matching_relative_path("Simple", None, gix_glob::pattern::Case::Fold), |
97 | | - Some(pattern_to_match(&gix_glob::parse("simple").unwrap(), 0)) |
98 | | - ); |
99 | | - assert_eq!( |
100 | | - group.pattern_matching_relative_path("pattern", Some(true), gix_glob::pattern::Case::Sensitive), |
101 | | - Some(pattern_to_match(&gix_glob::parse("pattern/").unwrap(), 1)) |
102 | | - ); |
103 | | - assert_eq!(group.patterns.len(), 1); |
104 | | - assert_eq!( |
105 | | - gix_attributes::PatternList::<Ignore>::from_overrides(input), |
106 | | - group.patterns.into_iter().next().unwrap() |
107 | | - ); |
108 | | - } |
109 | | - |
110 | | - fn pattern_to_match(pattern: &gix_glob::Pattern, sequence_number: usize) -> Match<'_, ()> { |
111 | | - Match { |
112 | | - pattern, |
113 | | - value: &(), |
114 | | - source: None, |
115 | | - sequence_number, |
116 | | - } |
117 | | - } |
118 | | -} |
| 1 | +mod ignore; |
0 commit comments