2022-06-15 05:25:05 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-12-14 18:32:30 +01:00
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
use ignore_files::{IgnoreFile, IgnoreFilter};
|
|
|
|
use project_origins::ProjectType;
|
2021-12-14 18:32:30 +01:00
|
|
|
use watchexec::{
|
|
|
|
error::RuntimeError,
|
2022-06-11 08:43:11 +02:00
|
|
|
event::{filekind::FileEventKind, Event, FileType, Priority, ProcessEnd, Source, Tag},
|
2022-06-15 05:25:05 +02:00
|
|
|
filter::Filterer,
|
2021-12-14 18:32:30 +01:00
|
|
|
};
|
2022-06-15 05:25:05 +02:00
|
|
|
use watchexec_filterer_ignore::IgnoreFilterer;
|
2023-03-18 09:32:24 +01:00
|
|
|
use watchexec_signals::Signal;
|
2022-06-15 05:25:05 +02:00
|
|
|
|
2022-01-15 03:15:40 +01:00
|
|
|
pub mod ignore {
|
|
|
|
pub use super::ig_file as file;
|
|
|
|
pub use super::ignore_filt as filt;
|
|
|
|
pub use super::Applies;
|
|
|
|
pub use super::PathHarness;
|
2022-06-11 08:43:11 +02:00
|
|
|
pub use watchexec::event::Priority;
|
2022-01-15 03:15:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PathHarness: Filterer {
|
2021-12-14 18:32:30 +01:00
|
|
|
fn check_path(
|
|
|
|
&self,
|
|
|
|
path: PathBuf,
|
|
|
|
file_type: Option<FileType>,
|
2022-01-15 03:15:40 +01:00
|
|
|
) -> std::result::Result<bool, RuntimeError> {
|
|
|
|
let event = Event {
|
|
|
|
tags: vec![Tag::Path { path, file_type }],
|
|
|
|
metadata: Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-06-11 08:43:11 +02:00
|
|
|
self.check_event(&event, Priority::Normal)
|
2022-01-15 03:15:40 +01:00
|
|
|
}
|
2021-12-14 18:32:30 +01:00
|
|
|
|
|
|
|
fn path_pass(&self, path: &str, file_type: Option<FileType>, pass: bool) {
|
2023-01-06 14:53:49 +01:00
|
|
|
let origin = std::fs::canonicalize(".").unwrap();
|
2021-12-14 18:32:30 +01:00
|
|
|
let full_path = if let Some(suf) = path.strip_prefix("/test/") {
|
|
|
|
origin.join(suf)
|
|
|
|
} else if Path::new(path).has_root() {
|
|
|
|
path.into()
|
|
|
|
} else {
|
|
|
|
origin.join(path)
|
|
|
|
};
|
|
|
|
|
|
|
|
tracing::info!(?path, ?file_type, ?pass, "check");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
self.check_path(full_path, file_type).unwrap(),
|
|
|
|
pass,
|
|
|
|
"{} {:?} (expected {})",
|
|
|
|
match file_type {
|
|
|
|
Some(FileType::File) => "file",
|
|
|
|
Some(FileType::Dir) => "dir",
|
|
|
|
Some(FileType::Symlink) => "symlink",
|
|
|
|
Some(FileType::Other) => "other",
|
|
|
|
None => "path",
|
|
|
|
},
|
|
|
|
path,
|
|
|
|
if pass { "pass" } else { "fail" }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_does_pass(&self, path: &str) {
|
|
|
|
self.path_pass(path, Some(FileType::File), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_doesnt_pass(&self, path: &str) {
|
|
|
|
self.path_pass(path, Some(FileType::File), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dir_does_pass(&self, path: &str) {
|
|
|
|
self.path_pass(path, Some(FileType::Dir), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dir_doesnt_pass(&self, path: &str) {
|
|
|
|
self.path_pass(path, Some(FileType::Dir), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unk_does_pass(&self, path: &str) {
|
|
|
|
self.path_pass(path, None, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unk_doesnt_pass(&self, path: &str) {
|
|
|
|
self.path_pass(path, None, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 03:15:40 +01:00
|
|
|
impl PathHarness for IgnoreFilterer {}
|
2021-12-14 18:32:30 +01:00
|
|
|
|
2021-12-14 19:24:13 +01:00
|
|
|
pub trait TaggedHarness {
|
2022-06-11 08:43:11 +02:00
|
|
|
fn check_tag(&self, tag: Tag, priority: Priority) -> std::result::Result<bool, RuntimeError>;
|
|
|
|
|
|
|
|
fn priority_pass(&self, priority: Priority, pass: bool) {
|
|
|
|
tracing::info!(?priority, ?pass, "check");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
self.check_tag(Tag::Source(Source::Filesystem), priority)
|
|
|
|
.unwrap(),
|
|
|
|
pass,
|
|
|
|
"{priority:?} (expected {})",
|
|
|
|
if pass { "pass" } else { "fail" }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn priority_does_pass(&self, priority: Priority) {
|
|
|
|
self.priority_pass(priority, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn priority_doesnt_pass(&self, priority: Priority) {
|
|
|
|
self.priority_pass(priority, false);
|
|
|
|
}
|
2021-12-14 19:24:13 +01:00
|
|
|
|
|
|
|
fn tag_pass(&self, tag: Tag, pass: bool) {
|
|
|
|
tracing::info!(?tag, ?pass, "check");
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-06-11 08:43:11 +02:00
|
|
|
self.check_tag(tag.clone(), Priority::Normal).unwrap(),
|
2021-12-14 19:24:13 +01:00
|
|
|
pass,
|
2022-06-11 08:43:11 +02:00
|
|
|
"{tag:?} (expected {})",
|
2021-12-14 19:24:13 +01:00
|
|
|
if pass { "pass" } else { "fail" }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fek_does_pass(&self, fek: FileEventKind) {
|
|
|
|
self.tag_pass(Tag::FileEventKind(fek), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fek_doesnt_pass(&self, fek: FileEventKind) {
|
|
|
|
self.tag_pass(Tag::FileEventKind(fek), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source_does_pass(&self, source: Source) {
|
|
|
|
self.tag_pass(Tag::Source(source), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source_doesnt_pass(&self, source: Source) {
|
|
|
|
self.tag_pass(Tag::Source(source), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pid_does_pass(&self, pid: u32) {
|
|
|
|
self.tag_pass(Tag::Process(pid), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pid_doesnt_pass(&self, pid: u32) {
|
|
|
|
self.tag_pass(Tag::Process(pid), false);
|
|
|
|
}
|
|
|
|
|
2023-03-18 09:32:24 +01:00
|
|
|
fn signal_does_pass(&self, sig: Signal) {
|
2021-12-14 19:24:13 +01:00
|
|
|
self.tag_pass(Tag::Signal(sig), true);
|
|
|
|
}
|
|
|
|
|
2023-03-18 09:32:24 +01:00
|
|
|
fn signal_doesnt_pass(&self, sig: Signal) {
|
2021-12-14 19:24:13 +01:00
|
|
|
self.tag_pass(Tag::Signal(sig), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn complete_does_pass(&self, exit: Option<ProcessEnd>) {
|
|
|
|
self.tag_pass(Tag::ProcessCompletion(exit), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn complete_doesnt_pass(&self, exit: Option<ProcessEnd>) {
|
|
|
|
self.tag_pass(Tag::ProcessCompletion(exit), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 03:15:40 +01:00
|
|
|
fn tracing_init() {
|
|
|
|
use tracing_subscriber::{
|
|
|
|
fmt::{format::FmtSpan, Subscriber},
|
|
|
|
util::SubscriberInitExt,
|
|
|
|
EnvFilter,
|
|
|
|
};
|
|
|
|
Subscriber::builder()
|
|
|
|
.pretty()
|
2022-01-15 04:40:01 +01:00
|
|
|
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
|
2022-01-15 03:15:40 +01:00
|
|
|
.with_env_filter(EnvFilter::from_default_env())
|
|
|
|
.finish()
|
|
|
|
.try_init()
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn ignore_filt(origin: &str, ignore_files: &[IgnoreFile]) -> IgnoreFilterer {
|
|
|
|
tracing_init();
|
2023-01-06 14:53:49 +01:00
|
|
|
let origin = tokio::fs::canonicalize(".").await.unwrap().join(origin);
|
2022-06-15 05:25:05 +02:00
|
|
|
IgnoreFilterer(
|
|
|
|
IgnoreFilter::new(origin, ignore_files)
|
2021-12-14 18:32:30 +01:00
|
|
|
.await
|
2022-06-15 05:25:05 +02:00
|
|
|
.expect("making filterer"),
|
|
|
|
)
|
2021-12-23 14:21:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ig_file(name: &str) -> IgnoreFile {
|
2023-01-06 14:53:49 +01:00
|
|
|
let path = std::fs::canonicalize(".")
|
2021-12-14 18:32:30 +01:00
|
|
|
.unwrap()
|
|
|
|
.join("tests")
|
|
|
|
.join("ignores")
|
|
|
|
.join(name);
|
|
|
|
IgnoreFile {
|
|
|
|
path,
|
|
|
|
applies_in: None,
|
|
|
|
applies_to: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Applies {
|
|
|
|
fn applies_in(self, origin: &str) -> Self;
|
|
|
|
fn applies_to(self, project_type: ProjectType) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Applies for IgnoreFile {
|
|
|
|
fn applies_in(mut self, origin: &str) -> Self {
|
2023-01-06 14:53:49 +01:00
|
|
|
let origin = std::fs::canonicalize(".").unwrap().join(origin);
|
2021-12-14 18:32:30 +01:00
|
|
|
self.applies_in = Some(origin);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn applies_to(mut self, project_type: ProjectType) -> Self {
|
|
|
|
self.applies_to = Some(project_type);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|