From b0a001c6a4c8e170ee291ab7effd027f69e959e1 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Wed, 8 Nov 2023 13:35:35 +0800 Subject: [PATCH] create relative path through ignore root and path --- src/cmd/watch.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index d112a75f..9a6168bf 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -5,7 +5,6 @@ use mdbook::errors::Result; use mdbook::utils; use mdbook::MDBook; use pathdiff::diff_paths; -use std::env; use std::path::{Path, PathBuf}; use std::sync::mpsc::channel; use std::thread::sleep; @@ -89,13 +88,16 @@ fn find_gitignore(book_root: &Path) -> Option { } fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec { - let current_dir = env::current_dir().expect("Unable to determine the current directory"); + let ignore_root = ignore + .path() + .canonicalize() + .expect("ignore root canonicalize error"); paths .iter() .filter(|path| { let relative_path = - diff_paths(¤t_dir, &path).expect("One of the paths should be an absolute"); + diff_paths(&path, &ignore_root).expect("One of the paths should be an absolute"); !ignore .matched_path_or_any_parents(&relative_path, relative_path.is_dir()) .is_ignore() @@ -185,3 +187,50 @@ where } } } + +#[cfg(test)] +mod tests { + use super::*; + use ignore::gitignore::GitignoreBuilder; + use std::env; + + fn path_to_buf(root: &str, file_name: &str) -> PathBuf { + let mut path = PathBuf::from(root); + path.push(file_name); + path + } + + #[test] + fn test_filter_ignored_files() { + let current_dir = env::current_dir().unwrap(); + + let ignore = GitignoreBuilder::new(¤t_dir) + .add_line(None, "*.html") + .unwrap() + .build() + .unwrap(); + let should_remain = path_to_buf(current_dir.to_str().unwrap(), "record.text"); + let should_filter = path_to_buf(current_dir.to_str().unwrap(), "index.html"); + + let remain = filter_ignored_files(ignore, &[should_remain.clone(), should_filter]); + assert_eq!(remain, vec![should_remain]) + } + + #[test] + fn filter_ignored_files_should_handle_parent_dir() { + let current_dir = env::current_dir().unwrap(); + + let ignore = GitignoreBuilder::new(¤t_dir) + .add_line(None, "*.html") + .unwrap() + .build() + .unwrap(); + + let parent_dir = format!("{}/../", current_dir.to_str().unwrap()); + let should_remain = path_to_buf(&parent_dir, "record.text"); + let should_filter = path_to_buf(&parent_dir, "index.html"); + + let remain = filter_ignored_files(ignore, &[should_remain.clone(), should_filter]); + assert_eq!(remain, vec![should_remain]) + } +}