Fix whitelisting in pathed globs

This commit is contained in:
Félix Saparelli 2021-12-15 04:57:17 +13:00
parent a4c863baf8
commit 015447e433
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
4 changed files with 51 additions and 2 deletions

View File

@ -286,7 +286,6 @@ impl TaggedFilterer {
// /foo/bar /foo/bar/baz.txt /baz.* pass
// /foo/bar /foo/bar/baz.txt /blah fail
// /foo/quz /foo/bar/baz.txt /baz.* skip
// TODO: lots of tests
// Ok(Some(bool)) => the match was applied, bool is the result
// Ok(None) => for some precondition, the match was not done (mismatched tag, out of context, …)
@ -422,7 +421,11 @@ impl TaggedFilterer {
let mut builder = GitignoreBuilder::new(&self.origin);
for filter in globs {
if let Pattern::Glob(glob) = filter.pat {
if let Pattern::Glob(mut glob) = filter.pat {
if filter.negate {
glob.insert(0, '!');
}
trace!(?op_filter, in_path=?filter.in_path, ?glob, "adding new glob line");
builder
.add_line(filter.in_path, &glob)

View File

@ -277,3 +277,33 @@ async fn globs() {
filterer.file_doesnt_pass("song/bird/bananas");
filterer.dir_doesnt_pass("song/bird/bananas");
}
#[tokio::test]
async fn negate() {
let filterer = filt("", &[file("negate")]).await;
filterer.file_does_pass("yeah");
filterer.file_doesnt_pass("nah");
filterer.file_does_pass("nah.yeah");
}
#[tokio::test]
async fn allowlist() {
let filterer = filt("", &[file("allowlist")]).await;
filterer.file_does_pass("mod.go");
filterer.file_does_pass("foo.go");
filterer.file_does_pass("go.sum");
filterer.file_does_pass("go.mod");
filterer.file_does_pass("README.md");
filterer.file_does_pass("LICENSE");
filterer.file_does_pass(".gitignore");
filterer.file_doesnt_pass("evil.sum");
filterer.file_doesnt_pass("evil.mod");
filterer.file_doesnt_pass("gofile.gone");
filterer.file_doesnt_pass("go.js");
filterer.file_doesnt_pass("README.asciidoc");
filterer.file_doesnt_pass("LICENSE.txt");
filterer.file_doesnt_pass("foo/.gitignore");
}

View File

@ -0,0 +1,14 @@
# from https://github.com/github/gitignore
*
!/.gitignore
!*.go
!go.sum
!go.mod
!README.md
!LICENSE
!*/

2
lib/tests/ignores/negate Normal file
View File

@ -0,0 +1,2 @@
nah
!nah.yeah