2021-12-16 15:57:25 +01:00
|
|
|
use std::{collections::HashSet, str::FromStr};
|
2021-12-14 19:24:13 +01:00
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
use watchexec_filterer_tagged::{Filter, Matcher, Op, Pattern, Regex, TaggedFiltererError};
|
2021-12-14 19:24:13 +01:00
|
|
|
|
|
|
|
mod helpers;
|
|
|
|
use helpers::tagged::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_filter() {
|
|
|
|
assert!(matches!(
|
|
|
|
Filter::from_str(""),
|
|
|
|
Err(TaggedFiltererError::Parse { .. })
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-12-17 11:19:36 +01:00
|
|
|
#[test]
|
|
|
|
fn only_bang() {
|
|
|
|
assert!(matches!(
|
|
|
|
Filter::from_str("!"),
|
|
|
|
Err(TaggedFiltererError::Parse { .. })
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_op() {
|
|
|
|
assert!(matches!(
|
|
|
|
Filter::from_str("foobar"),
|
|
|
|
Err(TaggedFiltererError::Parse { .. })
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-12-14 19:24:13 +01:00
|
|
|
#[test]
|
|
|
|
fn path_auto_op() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path=foo"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::Glob,
|
|
|
|
pat: Pattern::Glob("foo".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-12-16 15:57:25 +01:00
|
|
|
|
|
|
|
#[test]
|
2021-12-18 00:44:59 +01:00
|
|
|
fn fek_auto_op() {
|
2021-12-16 15:57:25 +01:00
|
|
|
assert_eq!(
|
2021-12-18 00:44:59 +01:00
|
|
|
filter("fek=foo"),
|
2021-12-16 15:57:25 +01:00
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::FileEventKind,
|
2021-12-18 00:44:59 +01:00
|
|
|
op: Op::Glob,
|
|
|
|
pat: Pattern::Glob("foo".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn other_auto_op() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("type=foo"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::FileType,
|
2021-12-16 15:57:25 +01:00
|
|
|
op: Op::InSet,
|
|
|
|
pat: Pattern::Set(HashSet::from(["foo".to_string()])),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_equal() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path==foo"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::Equal,
|
|
|
|
pat: Pattern::Exact("foo".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_not_equal() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path!=foo"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::NotEqual,
|
|
|
|
pat: Pattern::Exact("foo".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_regex() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path~=^fo+$"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::Regex,
|
|
|
|
pat: Pattern::Regex(Regex::new("^fo+$").unwrap()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_not_regex() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path~!f(o|al)+"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::NotRegex,
|
|
|
|
pat: Pattern::Regex(Regex::new("f(o|al)+").unwrap()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_glob() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path*=**/foo"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::Glob,
|
|
|
|
pat: Pattern::Glob("**/foo".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_not_glob() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path*!foo.*"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::NotGlob,
|
|
|
|
pat: Pattern::Glob("foo.*".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_in_set() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path:=foo,bar"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::InSet,
|
|
|
|
pat: Pattern::Set(HashSet::from(["foo".to_string(), "bar".to_string()])),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn op_not_in_set() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path:!baz,qux"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::NotInSet,
|
|
|
|
pat: Pattern::Set(HashSet::from(["baz".to_string(), "qux".to_string()])),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quoted_single() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path='blanche neige'"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::Glob,
|
|
|
|
pat: Pattern::Glob("blanche neige".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quoted_double() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("path=\"et les sept nains\""),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::Glob,
|
|
|
|
pat: Pattern::Glob("et les sept nains".to_string()),
|
|
|
|
negate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-12-17 11:19:36 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn negate() {
|
|
|
|
assert_eq!(
|
|
|
|
filter("!path~=^f[om]+$"),
|
|
|
|
Filter {
|
|
|
|
in_path: None,
|
|
|
|
on: Matcher::Path,
|
|
|
|
op: Op::Regex,
|
|
|
|
pat: Pattern::Regex(Regex::new("^f[om]+$").unwrap()),
|
|
|
|
negate: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|