tests: add tests for scanner module
All checks were successful
Test / test (push) Successful in 8s

This commit is contained in:
Paul Campbell 2024-09-19 20:18:53 +01:00
parent efaa435717
commit c86ff97798
5 changed files with 62 additions and 1 deletions

View file

@ -3,10 +3,11 @@ use std::path::Path;
use crate::model::{Config, FoundMarkers, Line, Marker}; use crate::model::{Config, FoundMarkers, Line, Marker};
use anyhow::Result; use anyhow::Result;
use ignore::Walk;
pub fn find_markers(config: Config) -> Result<FoundMarkers, anyhow::Error> { pub fn find_markers(config: Config) -> Result<FoundMarkers, anyhow::Error> {
let mut markers = FoundMarkers::default(); 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(); let path = file.path();
if config.fs().path_is_file(path)? { if config.fs().path_is_file(path)? {
scan_file(path, &config, &mut markers)?; scan_file(path, &config, &mut markers)?;

View file

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

View file

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

View file

@ -2,3 +2,4 @@
use super::*; use super::*;
mod init; mod init;
mod scanner;

47
src/tests/scanner.rs Normal file
View file

@ -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<_>>(),
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(())
}