From c86ff97798d7a8e6299378ac5e64d927e524a5a5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Sep 2024 20:18:53 +0100 Subject: [PATCH] tests: add tests for scanner module --- src/scanner.rs | 3 ++- src/tests/data/file_1.txt | 7 ++++++ src/tests/data/file_2.txt | 5 +++++ src/tests/mod.rs | 1 + src/tests/scanner.rs | 47 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/tests/data/file_1.txt create mode 100644 src/tests/data/file_2.txt create mode 100644 src/tests/scanner.rs diff --git a/src/scanner.rs b/src/scanner.rs index a385ee0..49cd060 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -3,10 +3,11 @@ use std::path::Path; use crate::model::{Config, FoundMarkers, Line, Marker}; use anyhow::Result; +use ignore::Walk; pub fn find_markers(config: Config) -> Result { let mut markers = FoundMarkers::default(); - for file in ignore::Walk::new(config.fs().base()).flatten() { + for file in Walk::new(config.fs().base()).flatten() { let path = file.path(); if config.fs().path_is_file(path)? { scan_file(path, &config, &mut markers)?; diff --git a/src/tests/data/file_1.txt b/src/tests/data/file_1.txt new file mode 100644 index 0000000..4d43b2d --- /dev/null +++ b/src/tests/data/file_1.txt @@ -0,0 +1,7 @@ +This is a text file. + +It contains a todo comment: // TODO: this is it + +It also contains a fix-me comment: // FIXME: and this is it + +Both of these are missing an issue identifier. diff --git a/src/tests/data/file_2.txt b/src/tests/data/file_2.txt new file mode 100644 index 0000000..7bc4c1a --- /dev/null +++ b/src/tests/data/file_2.txt @@ -0,0 +1,5 @@ +This is another text file. + +It also has a todo comment: // TODO: (#23) and it has an issue number + +Here is a fix-me comment: // FIXME: (#43) and is also has an issue number diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 2af0c17..33776f6 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -2,3 +2,4 @@ use super::*; mod init; +mod scanner; diff --git a/src/tests/scanner.rs b/src/tests/scanner.rs new file mode 100644 index 0000000..18117af --- /dev/null +++ b/src/tests/scanner.rs @@ -0,0 +1,47 @@ +use model::Config; +use patterns::{issue_pattern, marker_pattern}; + +// +use super::*; + +#[test] +fn find_markers_in_dir() -> anyhow::Result<()> { + //given + let fs = kxio::fs::temp()?; + fs.file_write( + &fs.base().join("file_1.txt"), + include_str!("data/file_1.txt"), + )?; + fs.file_write( + &fs.base().join("file_2.txt"), + include_str!("data/file_2.txt"), + )?; + + let config = Config::builder() + .fs(fs.clone()) + .server("".to_string()) + .repo("".to_string()) + .prefix_pattern(marker_pattern()?) + .issue_pattern(issue_pattern()?) + .build(); + + //when + let markers = find_markers(config)?; + + //then + assert_eq!( + markers.to_string().lines().collect::>(), + vec![ + "- Invalid: file_1.txt#2:", + " It contains a todo comment: // TODO: this is it", + "- Invalid: file_1.txt#4:", + " It also contains a fix-me comment: // FIXME: and this is it", + "- Valid : file_2.txt#2:", + " It also has a todo comment: // TODO: (#23) and it has an issue number", + "- Valid : file_2.txt#4:", + " Here is a fix-me comment: // FIXME: (#43) and is also has an issue number" + ] + ); + + Ok(()) +}