From 131378c1c7f34983c8eccd6254f47e557c2e0aa8 Mon Sep 17 00:00:00 2001 From: Martin Heuschober Date: Mon, 16 Oct 2023 19:34:33 +0100 Subject: [PATCH] Add tests for filtering based on tags --- src/lib.rs | 6 -- src/main.rs | 18 ----- src/postprocessors.rs | 11 --- tests/export_test.rs | 75 ------------------- tests/postprocessors_test.rs | 45 ++++++++++- .../expected/filter-by-tags/export-me.md | 7 ++ .../expected/filter-by-tags/export.md | 6 ++ .../no-frontmatter.md | 0 .../public.md => filter-by-tags/no-tags.md} | 2 +- .../ignore-default-keyword/do-expört.md | 5 -- .../ignore-default-keyword/no-expört.md | 5 -- .../ignore-specific-keyword/do-expört.md | 5 -- .../input/filter-by-tags/export-me.md | 5 ++ .../input/filter-by-tags/export-no-export.md | 5 ++ tests/testdata/input/filter-by-tags/export.md | 5 ++ .../filter-by-tags}/no-frontmatter.md | 0 .../input/filter-by-tags/no-no-export.md | 5 ++ .../filter-by-tags/no-tags.md} | 2 +- .../filter-by-tags}/private.md | 2 +- .../input/ignore-keyword/do-expört.md | 4 - .../input/ignore-keyword/no-expört.md | 4 - .../input/ignore-keyword/no-frontmatter.md | 1 - .../testdata/input/ignore-keyword/private.md | 4 - tests/testdata/input/ignore-keyword/public.md | 4 - 24 files changed, 80 insertions(+), 146 deletions(-) create mode 100644 tests/testdata/expected/filter-by-tags/export-me.md create mode 100644 tests/testdata/expected/filter-by-tags/export.md rename tests/testdata/expected/{ignore-default-keyword => filter-by-tags}/no-frontmatter.md (100%) rename tests/testdata/expected/{ignore-default-keyword/public.md => filter-by-tags/no-tags.md} (61%) delete mode 100644 tests/testdata/expected/ignore-default-keyword/do-expört.md delete mode 100644 tests/testdata/expected/ignore-default-keyword/no-expört.md delete mode 100644 tests/testdata/expected/ignore-specific-keyword/do-expört.md create mode 100644 tests/testdata/input/filter-by-tags/export-me.md create mode 100644 tests/testdata/input/filter-by-tags/export-no-export.md create mode 100644 tests/testdata/input/filter-by-tags/export.md rename tests/testdata/{expected/ignore-specific-keyword => input/filter-by-tags}/no-frontmatter.md (100%) create mode 100644 tests/testdata/input/filter-by-tags/no-no-export.md rename tests/testdata/{expected/ignore-specific-keyword/public.md => input/filter-by-tags/no-tags.md} (61%) rename tests/testdata/{expected/ignore-specific-keyword => input/filter-by-tags}/private.md (60%) delete mode 100644 tests/testdata/input/ignore-keyword/do-expört.md delete mode 100644 tests/testdata/input/ignore-keyword/no-expört.md delete mode 100644 tests/testdata/input/ignore-keyword/no-frontmatter.md delete mode 100644 tests/testdata/input/ignore-keyword/private.md delete mode 100644 tests/testdata/input/ignore-keyword/public.md diff --git a/src/lib.rs b/src/lib.rs index 568ec90..5280190 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -306,12 +306,6 @@ impl<'a> Exporter<'a> { self } - /// Set the frontmatter keyword that excludes files from being exported. - pub fn ignore_frontmatter_keyword(&mut self, keyword: &'a str) -> &mut Exporter<'a> { - self.ignore_frontmatter_keyword = keyword; - self - } - /// Set the behavior when recursive embeds are encountered. /// /// When `recursive` is true (the default), emdeds are always processed recursively. This may diff --git a/src/main.rs b/src/main.rs index d1728b7..1798d1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,13 +45,6 @@ struct Opts { #[options(no_short, help = "Export only files with this tag")] only_tags: Vec, - #[options( - no_short, - help = "Exclude files with this flag in the frontmatter from the export", - default = "private" - )] - ignore_frontmatter_flag: String, - #[options(no_short, help = "Export hidden files", default = "false")] hidden: bool, @@ -69,14 +62,6 @@ struct Opts { hard_linebreaks: bool, } -// fn comma_separated(input: &str) -> Result> { -// Ok(if input.is_empty() { -// Vec::new() -// } else { -// input.split(",").map(|s| s.trim().to_string()).collect() -// }) -// } - fn frontmatter_strategy_from_str(input: &str) -> Result { match input { "auto" => Ok(FrontmatterStrategy::Auto), @@ -115,9 +100,6 @@ fn main() { exporter.add_postprocessor(&softbreaks_to_hardbreaks); } - let frontmatter_flag_postprocessor = filter_by_frontmatter_flag(args.ignore_frontmatter_flag); - exporter.add_postprocessor(&frontmatter_flag_postprocessor); - let tags_postprocessor = filter_by_tags(args.skip_tags, args.only_tags); exporter.add_postprocessor(&tags_postprocessor); diff --git a/src/postprocessors.rs b/src/postprocessors.rs index 53fc795..5537b00 100644 --- a/src/postprocessors.rs +++ b/src/postprocessors.rs @@ -46,14 +46,3 @@ pub fn filter_by_tags( } } } - -pub fn filter_by_frontmatter_flag( - flag: String, -) -> impl Fn(&mut Context, &mut MarkdownEvents) -> PostprocessorResult { - move |context: &mut Context, _events: &mut MarkdownEvents| -> PostprocessorResult { - match context.frontmatter.get(flag.as_str()) { - Some(Value::Bool(true)) => PostprocessorResult::StopAndSkipNote, - _ => PostprocessorResult::Continue, - } - } -} diff --git a/tests/export_test.rs b/tests/export_test.rs index cb094d3..237bbd3 100644 --- a/tests/export_test.rs +++ b/tests/export_test.rs @@ -419,78 +419,3 @@ fn test_same_filename_different_directories() { let actual = read_to_string(tmp_dir.path().join(PathBuf::from("Note.md"))).unwrap(); assert_eq!(expected, actual); } - -#[test] -fn test_ignore_frontmatter_default_keyword() { - let tmp_dir = TempDir::new().expect("failed to make tempdir"); - let mut exporter = Exporter::new( - PathBuf::from("tests/testdata/input/ignore-keyword/"), - tmp_dir.path().to_path_buf(), - ); - exporter.run().expect("exporter returned error"); - - let walker = WalkDir::new("tests/testdata/expected/ignore-default-keyword/") - // Without sorting here, different test runs may trigger the first assertion failure in - // unpredictable order. - .sort_by(|a, b| a.file_name().cmp(b.file_name())) - .into_iter(); - for entry in walker { - let entry = entry.unwrap(); - if entry.metadata().unwrap().is_dir() { - continue; - }; - let filename = entry.file_name().to_string_lossy().into_owned(); - let expected = read_to_string(entry.path()).unwrap_or_else(|_| { - panic!( - "failed to read {} from testdata/expected/ignore-default-keyword/", - entry.path().display() - ) - }); - let actual = read_to_string(tmp_dir.path().join(PathBuf::from(&filename))) - .unwrap_or_else(|_| panic!("failed to read {} from temporary exportdir", filename)); - - assert_eq!( - expected, actual, - "{} does not have expected content", - filename - ); - } -} - -#[test] -fn test_ignore_frontmatter_specific_keyword() { - let tmp_dir = TempDir::new().expect("failed to make tempdir"); - let mut exporter = Exporter::new( - PathBuf::from("tests/testdata/input/ignore-keyword/"), - tmp_dir.path().to_path_buf(), - ); - exporter.ignore_frontmatter_keyword("no-expört"); - exporter.run().expect("exporter returned error"); - - let walker = WalkDir::new("tests/testdata/expected/ignore-specific-keyword/") - // Without sorting here, different test runs may trigger the first assertion failure in - // unpredictable order. - .sort_by(|a, b| a.file_name().cmp(b.file_name())) - .into_iter(); - for entry in walker { - let entry = entry.unwrap(); - if entry.metadata().unwrap().is_dir() { - continue; - }; - let filename = entry.file_name().to_string_lossy().into_owned(); - let expected = read_to_string(entry.path()).unwrap_or_else(|_| { - panic!( - "failed to read {} from testdata/expected/ignore-specific-keyword/", - entry.path().display() - ) - }); - let actual = read_to_string(tmp_dir.path().join(PathBuf::from(&filename))) - .unwrap_or_else(|_| panic!("failed to read {} from temporary exportdir", filename)); - - assert_eq!( - expected, actual, - "{} does not have expected content", - filename - ); - } -} diff --git a/tests/postprocessors_test.rs b/tests/postprocessors_test.rs index 5a4c282..c04c7c5 100644 --- a/tests/postprocessors_test.rs +++ b/tests/postprocessors_test.rs @@ -1,4 +1,4 @@ -use obsidian_export::postprocessors::softbreaks_to_hardbreaks; +use obsidian_export::postprocessors::{filter_by_tags, softbreaks_to_hardbreaks}; use obsidian_export::{Context, Exporter, MarkdownEvents, PostprocessorResult}; use pretty_assertions::assert_eq; use pulldown_cmark::{CowStr, Event}; @@ -8,6 +8,7 @@ use std::fs::{read_to_string, remove_file}; use std::path::PathBuf; use std::sync::Mutex; use tempfile::TempDir; +use walkdir::WalkDir; /// This postprocessor replaces any instance of "foo" with "bar" in the note body. fn foo_to_bar(_ctx: &mut Context, events: &mut MarkdownEvents) -> PostprocessorResult { @@ -247,3 +248,45 @@ fn test_softbreaks_to_hardbreaks() { let actual = read_to_string(tmp_dir.path().join(PathBuf::from("hard_linebreaks.md"))).unwrap(); assert_eq!(expected, actual); } + +#[test] +fn test_filter_by_tags() { + let tmp_dir = TempDir::new().expect("failed to make tempdir"); + let mut exporter = Exporter::new( + PathBuf::from("tests/testdata/input/filter-by-tags"), + tmp_dir.path().to_path_buf(), + ); + let filter_by_tags = filter_by_tags( + vec!["private".to_string(), "no-export".to_string()], + vec!["export".to_string()], + ); + exporter.add_postprocessor(&filter_by_tags); + exporter.run().unwrap(); + + let walker = WalkDir::new("tests/testdata/expected/filter-by-tags/") + // Without sorting here, different test runs may trigger the first assertion failure in + // unpredictable order. + .sort_by(|a, b| a.file_name().cmp(b.file_name())) + .into_iter(); + for entry in walker { + let entry = entry.unwrap(); + if entry.metadata().unwrap().is_dir() { + continue; + }; + let filename = entry.file_name().to_string_lossy().into_owned(); + let expected = read_to_string(entry.path()).unwrap_or_else(|_| { + panic!( + "failed to read {} from testdata/expected/filter-by-tags", + entry.path().display() + ) + }); + let actual = read_to_string(tmp_dir.path().join(PathBuf::from(&filename))) + .unwrap_or_else(|_| panic!("failed to read {} from temporary exportdir", filename)); + + assert_eq!( + expected, actual, + "{} does not have expected content", + filename + ); + } +} diff --git a/tests/testdata/expected/filter-by-tags/export-me.md b/tests/testdata/expected/filter-by-tags/export-me.md new file mode 100644 index 0000000..2d4d4f0 --- /dev/null +++ b/tests/testdata/expected/filter-by-tags/export-me.md @@ -0,0 +1,7 @@ +--- +tags: +- export +- me +--- + +A public note diff --git a/tests/testdata/expected/filter-by-tags/export.md b/tests/testdata/expected/filter-by-tags/export.md new file mode 100644 index 0000000..c4e074c --- /dev/null +++ b/tests/testdata/expected/filter-by-tags/export.md @@ -0,0 +1,6 @@ +--- +tags: +- export +--- + +A public note diff --git a/tests/testdata/expected/ignore-default-keyword/no-frontmatter.md b/tests/testdata/expected/filter-by-tags/no-frontmatter.md similarity index 100% rename from tests/testdata/expected/ignore-default-keyword/no-frontmatter.md rename to tests/testdata/expected/filter-by-tags/no-frontmatter.md diff --git a/tests/testdata/expected/ignore-default-keyword/public.md b/tests/testdata/expected/filter-by-tags/no-tags.md similarity index 61% rename from tests/testdata/expected/ignore-default-keyword/public.md rename to tests/testdata/expected/filter-by-tags/no-tags.md index 6c8e990..fdc5f81 100644 --- a/tests/testdata/expected/ignore-default-keyword/public.md +++ b/tests/testdata/expected/filter-by-tags/no-tags.md @@ -1,5 +1,5 @@ --- -private: false +title: foo --- A public note. diff --git a/tests/testdata/expected/ignore-default-keyword/do-expört.md b/tests/testdata/expected/ignore-default-keyword/do-expört.md deleted file mode 100644 index 41b1696..0000000 --- a/tests/testdata/expected/ignore-default-keyword/do-expört.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -no-expört: false ---- - -A note with negated special ignore keyword diff --git a/tests/testdata/expected/ignore-default-keyword/no-expört.md b/tests/testdata/expected/ignore-default-keyword/no-expört.md deleted file mode 100644 index 06d8818..0000000 --- a/tests/testdata/expected/ignore-default-keyword/no-expört.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -no-expört: true ---- - -A note with a special ignore keyword diff --git a/tests/testdata/expected/ignore-specific-keyword/do-expört.md b/tests/testdata/expected/ignore-specific-keyword/do-expört.md deleted file mode 100644 index 41b1696..0000000 --- a/tests/testdata/expected/ignore-specific-keyword/do-expört.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -no-expört: false ---- - -A note with negated special ignore keyword diff --git a/tests/testdata/input/filter-by-tags/export-me.md b/tests/testdata/input/filter-by-tags/export-me.md new file mode 100644 index 0000000..5b36253 --- /dev/null +++ b/tests/testdata/input/filter-by-tags/export-me.md @@ -0,0 +1,5 @@ +--- +tags: [export, me] +--- + +A public note diff --git a/tests/testdata/input/filter-by-tags/export-no-export.md b/tests/testdata/input/filter-by-tags/export-no-export.md new file mode 100644 index 0000000..7d604f5 --- /dev/null +++ b/tests/testdata/input/filter-by-tags/export-no-export.md @@ -0,0 +1,5 @@ +--- +tags: [export, no-export, private] +--- + +A private note diff --git a/tests/testdata/input/filter-by-tags/export.md b/tests/testdata/input/filter-by-tags/export.md new file mode 100644 index 0000000..acdf457 --- /dev/null +++ b/tests/testdata/input/filter-by-tags/export.md @@ -0,0 +1,5 @@ +--- +tags: [export] +--- + +A public note diff --git a/tests/testdata/expected/ignore-specific-keyword/no-frontmatter.md b/tests/testdata/input/filter-by-tags/no-frontmatter.md similarity index 100% rename from tests/testdata/expected/ignore-specific-keyword/no-frontmatter.md rename to tests/testdata/input/filter-by-tags/no-frontmatter.md diff --git a/tests/testdata/input/filter-by-tags/no-no-export.md b/tests/testdata/input/filter-by-tags/no-no-export.md new file mode 100644 index 0000000..cc21504 --- /dev/null +++ b/tests/testdata/input/filter-by-tags/no-no-export.md @@ -0,0 +1,5 @@ +--- +tags: [no, no-export] +--- + +A private note diff --git a/tests/testdata/expected/ignore-specific-keyword/public.md b/tests/testdata/input/filter-by-tags/no-tags.md similarity index 61% rename from tests/testdata/expected/ignore-specific-keyword/public.md rename to tests/testdata/input/filter-by-tags/no-tags.md index 6c8e990..fdc5f81 100644 --- a/tests/testdata/expected/ignore-specific-keyword/public.md +++ b/tests/testdata/input/filter-by-tags/no-tags.md @@ -1,5 +1,5 @@ --- -private: false +title: foo --- A public note. diff --git a/tests/testdata/expected/ignore-specific-keyword/private.md b/tests/testdata/input/filter-by-tags/private.md similarity index 60% rename from tests/testdata/expected/ignore-specific-keyword/private.md rename to tests/testdata/input/filter-by-tags/private.md index 7a28b41..88b76e9 100644 --- a/tests/testdata/expected/ignore-specific-keyword/private.md +++ b/tests/testdata/input/filter-by-tags/private.md @@ -1,5 +1,5 @@ --- -private: true +tags: [private] --- A private note. diff --git a/tests/testdata/input/ignore-keyword/do-expört.md b/tests/testdata/input/ignore-keyword/do-expört.md deleted file mode 100644 index f2e5631..0000000 --- a/tests/testdata/input/ignore-keyword/do-expört.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -no-expört: false ---- -A note with negated special ignore keyword diff --git a/tests/testdata/input/ignore-keyword/no-expört.md b/tests/testdata/input/ignore-keyword/no-expört.md deleted file mode 100644 index 4dae97d..0000000 --- a/tests/testdata/input/ignore-keyword/no-expört.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -no-expört: true ---- -A note with a special ignore keyword diff --git a/tests/testdata/input/ignore-keyword/no-frontmatter.md b/tests/testdata/input/ignore-keyword/no-frontmatter.md deleted file mode 100644 index 427b0ea..0000000 --- a/tests/testdata/input/ignore-keyword/no-frontmatter.md +++ /dev/null @@ -1 +0,0 @@ -A note without frontmatter should be exported. diff --git a/tests/testdata/input/ignore-keyword/private.md b/tests/testdata/input/ignore-keyword/private.md deleted file mode 100644 index 5e9a51e..0000000 --- a/tests/testdata/input/ignore-keyword/private.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -private: true ---- -A private note. diff --git a/tests/testdata/input/ignore-keyword/public.md b/tests/testdata/input/ignore-keyword/public.md deleted file mode 100644 index abf8e35..0000000 --- a/tests/testdata/input/ignore-keyword/public.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -private: false ---- -A public note.