Add tests for filtering based on tags

This commit is contained in:
Martin Heuschober 2023-10-16 19:34:33 +01:00
parent 53fe4bf26a
commit 131378c1c7
24 changed files with 80 additions and 146 deletions

View File

@ -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

View File

@ -45,13 +45,6 @@ struct Opts {
#[options(no_short, help = "Export only files with this tag")]
only_tags: Vec<String>,
#[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<Vec<String>> {
// 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<FrontmatterStrategy> {
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);

View File

@ -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,
}
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -0,0 +1,7 @@
---
tags:
- export
- me
---
A public note

View File

@ -0,0 +1,6 @@
---
tags:
- export
---
A public note

View File

@ -1,5 +1,5 @@
---
private: false
title: foo
---
A public note.

View File

@ -1,5 +0,0 @@
---
no-expört: false
---
A note with negated special ignore keyword

View File

@ -1,5 +0,0 @@
---
no-expört: true
---
A note with a special ignore keyword

View File

@ -1,5 +0,0 @@
---
no-expört: false
---
A note with negated special ignore keyword

View File

@ -0,0 +1,5 @@
---
tags: [export, me]
---
A public note

View File

@ -0,0 +1,5 @@
---
tags: [export, no-export, private]
---
A private note

View File

@ -0,0 +1,5 @@
---
tags: [export]
---
A public note

View File

@ -0,0 +1,5 @@
---
tags: [no, no-export]
---
A private note

View File

@ -1,5 +1,5 @@
---
private: false
title: foo
---
A public note.

View File

@ -1,5 +1,5 @@
---
private: true
tags: [private]
---
A private note.

View File

@ -1,4 +0,0 @@
---
no-expört: false
---
A note with negated special ignore keyword

View File

@ -1,4 +0,0 @@
---
no-expört: true
---
A note with a special ignore keyword

View File

@ -1 +0,0 @@
A note without frontmatter should be exported.

View File

@ -1,4 +0,0 @@
---
private: true
---
A private note.

View File

@ -1,4 +0,0 @@
---
private: false
---
A public note.