tests: add tests for main

This commit is contained in:
Paul Campbell 2024-09-19 20:37:23 +01:00
parent b6de7831e5
commit 652b83b541
7 changed files with 75 additions and 8 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

@ -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,5 +1,10 @@
use std::sync::{LazyLock, Mutex};
// //
use super::*; use super::*;
mod init; mod init;
mod run;
mod scanner; 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(())
}

View file

@ -9,12 +9,12 @@ fn find_markers_in_dir() -> anyhow::Result<()> {
//given //given
let fs = kxio::fs::temp()?; let fs = kxio::fs::temp()?;
fs.file_write( fs.file_write(
&fs.base().join("file_1.txt"), &fs.base().join("file_with_invalids.txt"),
include_str!("data/file_1.txt"), include_str!("data/file_with_invalids.txt"),
)?; )?;
fs.file_write( fs.file_write(
&fs.base().join("file_2.txt"), &fs.base().join("file_with_valids.txt"),
include_str!("data/file_2.txt"), include_str!("data/file_with_valids.txt"),
)?; )?;
let config = Config::builder() let config = Config::builder()
@ -32,13 +32,13 @@ fn find_markers_in_dir() -> anyhow::Result<()> {
assert_eq!( assert_eq!(
markers.to_string().lines().collect::<Vec<_>>(), markers.to_string().lines().collect::<Vec<_>>(),
vec![ vec![
"- Invalid: file_1.txt#2:", "- Invalid: file_with_invalids.txt#2:",
" It contains a todo comment: // TODO: this is it", " It contains a todo comment: // TODO: this is it",
"- Invalid: file_1.txt#4:", "- Invalid: file_with_invalids.txt#4:",
" It also contains a fix-me comment: // FIXME: and this is it", " It also contains a fix-me comment: // FIXME: and this is it",
"- Valid : file_2.txt#2:", "- Valid : file_with_valids.txt#2:",
" It also has a todo comment: // TODO: (#23) and it has an issue number", " It also has a todo comment: // TODO: (#23) and it has an issue number",
"- Valid : file_2.txt#4:", "- Valid : file_with_valids.txt#4:",
" Here is a fix-me comment: // FIXME: (#43) and is also has an issue number" " Here is a fix-me comment: // FIXME: (#43) and is also has an issue number"
] ]
); );