Compare commits

...

2 commits

Author SHA1 Message Date
652b83b541 tests: add tests for main 2024-09-19 22:27:12 +01:00
b6de7831e5 tests: add tests for scanner module
All checks were successful
Test / test (push) Successful in 11s
2024-09-19 20:37:07 +01:00
8 changed files with 129 additions and 1 deletions

View file

@ -12,6 +12,10 @@ mod scanner;
mod tests; mod tests;
fn main() -> Result<()> { fn main() -> Result<()> {
run()
}
fn run() -> std::result::Result<(), anyhow::Error> {
println!("Forgejo TODO Checker!"); println!("Forgejo TODO Checker!");
let config = init_config()?; let config = init_config()?;

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

@ -8,6 +8,7 @@ use patterns::{issue_pattern, marker_pattern};
#[test] #[test]
fn init_when_all_valid() -> anyhow::Result<()> { fn init_when_all_valid() -> anyhow::Result<()> {
//given //given
let _env = THE_ENVIRONMENT.lock();
let fs = kxio::fs::temp()?; let fs = kxio::fs::temp()?;
std::env::set_var("GITHUB_WORKSPACE", fs.base()); std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::set_var("GITHUB_REPOSITORY", "repo"); std::env::set_var("GITHUB_REPOSITORY", "repo");
@ -43,6 +44,7 @@ fn init_when_all_valid() -> anyhow::Result<()> {
#[test] #[test]
fn init_when_no_workspace() -> anyhow::Result<()> { fn init_when_no_workspace() -> anyhow::Result<()> {
//given //given
let _env = THE_ENVIRONMENT.lock();
std::env::remove_var("GITHUB_WORKSPACE"); std::env::remove_var("GITHUB_WORKSPACE");
std::env::set_var("GITHUB_REPOSITORY", "repo"); std::env::set_var("GITHUB_REPOSITORY", "repo");
std::env::set_var("GITHUB_SERVER_URL", "server"); std::env::set_var("GITHUB_SERVER_URL", "server");
@ -60,6 +62,7 @@ fn init_when_no_workspace() -> anyhow::Result<()> {
#[test] #[test]
fn init_when_no_repository() -> anyhow::Result<()> { fn init_when_no_repository() -> anyhow::Result<()> {
//given //given
let _env = THE_ENVIRONMENT.lock();
let fs = kxio::fs::temp()?; let fs = kxio::fs::temp()?;
std::env::set_var("GITHUB_WORKSPACE", fs.base()); std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::remove_var("GITHUB_REPOSITORY"); std::env::remove_var("GITHUB_REPOSITORY");
@ -78,6 +81,7 @@ fn init_when_no_repository() -> anyhow::Result<()> {
#[test] #[test]
fn init_when_no_server_url() -> anyhow::Result<()> { fn init_when_no_server_url() -> anyhow::Result<()> {
//given //given
let _env = THE_ENVIRONMENT.lock();
let fs = kxio::fs::temp()?; let fs = kxio::fs::temp()?;
std::env::set_var("GITHUB_WORKSPACE", fs.base()); std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::set_var("GITHUB_REPOSITORY", "repo"); std::env::set_var("GITHUB_REPOSITORY", "repo");

View file

@ -1,4 +1,10 @@
use std::sync::{LazyLock, Mutex};
// //
use super::*; use super::*;
mod init; mod init;
mod run;
mod scanner;
pub static THE_ENVIRONMENT: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

54
src/tests/run.rs Normal file
View file

@ -0,0 +1,54 @@
//
use super::*;
use anyhow::Result;
#[test]
fn run_with_some_invalids() -> Result<()> {
//given
let _env = THE_ENVIRONMENT.lock();
let fs = kxio::fs::temp()?;
fs.file_write(
&fs.base().join("file_with_invalids.txt"),
include_str!("data/file_with_invalids.txt"),
)?;
fs.file_write(
&fs.base().join("file_with_valids.txt"),
include_str!("data/file_with_valids.txt"),
)?;
std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::set_var("GITHUB_REPOSITORY", "kemitix/test");
std::env::set_var("GITHUB_SERVER_URL", "https://git.kemitix.net");
//when
run()?;
//then
// TODO: add check that run fails because file_1.txt is invalid
// TODO: add check that network requests were made to get issues
Ok(())
}
#[test]
fn run_with_no_invalids() -> Result<()> {
//given
let _env = THE_ENVIRONMENT.lock();
let fs = kxio::fs::temp()?;
fs.file_write(
&fs.base().join("file_with_valids.txt"),
include_str!("data/file_with_valids.txt"),
)?;
std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::set_var("GITHUB_REPOSITORY", "kemitix/test");
std::env::set_var("GITHUB_SERVER_URL", "https://git.kemitix.net");
//when
run()?;
//then
// TODO: add check that run fails because file_1.txt is invalid
// TODO: add check that network requests were made to get issues
Ok(())
}

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_with_invalids.txt"),
include_str!("data/file_with_invalids.txt"),
)?;
fs.file_write(
&fs.base().join("file_with_valids.txt"),
include_str!("data/file_with_valids.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_with_invalids.txt#2:",
" It contains a todo comment: // TODO: this is it",
"- Invalid: file_with_invalids.txt#4:",
" It also contains a fix-me comment: // FIXME: and this is it",
"- Valid : file_with_valids.txt#2:",
" It also has a todo comment: // TODO: (#23) and it has an issue number",
"- Valid : file_with_valids.txt#4:",
" Here is a fix-me comment: // FIXME: (#43) and is also has an issue number"
]
);
Ok(())
}