From 652b83b54116a6f608816347b25d306d8aef69fd Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Sep 2024 20:37:23 +0100 Subject: [PATCH] tests: add tests for main --- src/main.rs | 4 ++ .../{file_1.txt => file_with_invalids.txt} | 0 .../data/{file_2.txt => file_with_valids.txt} | 0 src/tests/init.rs | 4 ++ src/tests/mod.rs | 5 ++ src/tests/run.rs | 54 +++++++++++++++++++ src/tests/scanner.rs | 16 +++--- 7 files changed, 75 insertions(+), 8 deletions(-) rename src/tests/data/{file_1.txt => file_with_invalids.txt} (100%) rename src/tests/data/{file_2.txt => file_with_valids.txt} (100%) create mode 100644 src/tests/run.rs diff --git a/src/main.rs b/src/main.rs index 2a9a380..b5ae892 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,10 @@ mod scanner; mod tests; fn main() -> Result<()> { + run() +} + +fn run() -> std::result::Result<(), anyhow::Error> { println!("Forgejo TODO Checker!"); let config = init_config()?; diff --git a/src/tests/data/file_1.txt b/src/tests/data/file_with_invalids.txt similarity index 100% rename from src/tests/data/file_1.txt rename to src/tests/data/file_with_invalids.txt diff --git a/src/tests/data/file_2.txt b/src/tests/data/file_with_valids.txt similarity index 100% rename from src/tests/data/file_2.txt rename to src/tests/data/file_with_valids.txt diff --git a/src/tests/init.rs b/src/tests/init.rs index 6cb0140..96764ab 100644 --- a/src/tests/init.rs +++ b/src/tests/init.rs @@ -8,6 +8,7 @@ use patterns::{issue_pattern, marker_pattern}; #[test] fn init_when_all_valid() -> anyhow::Result<()> { //given + let _env = THE_ENVIRONMENT.lock(); let fs = kxio::fs::temp()?; std::env::set_var("GITHUB_WORKSPACE", fs.base()); std::env::set_var("GITHUB_REPOSITORY", "repo"); @@ -43,6 +44,7 @@ fn init_when_all_valid() -> anyhow::Result<()> { #[test] fn init_when_no_workspace() -> anyhow::Result<()> { //given + let _env = THE_ENVIRONMENT.lock(); std::env::remove_var("GITHUB_WORKSPACE"); std::env::set_var("GITHUB_REPOSITORY", "repo"); std::env::set_var("GITHUB_SERVER_URL", "server"); @@ -60,6 +62,7 @@ fn init_when_no_workspace() -> anyhow::Result<()> { #[test] fn init_when_no_repository() -> anyhow::Result<()> { //given + let _env = THE_ENVIRONMENT.lock(); let fs = kxio::fs::temp()?; std::env::set_var("GITHUB_WORKSPACE", fs.base()); std::env::remove_var("GITHUB_REPOSITORY"); @@ -78,6 +81,7 @@ fn init_when_no_repository() -> anyhow::Result<()> { #[test] fn init_when_no_server_url() -> anyhow::Result<()> { //given + let _env = THE_ENVIRONMENT.lock(); let fs = kxio::fs::temp()?; std::env::set_var("GITHUB_WORKSPACE", fs.base()); std::env::set_var("GITHUB_REPOSITORY", "repo"); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 33776f6..b2b2058 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,5 +1,10 @@ +use std::sync::{LazyLock, Mutex}; + // use super::*; mod init; +mod run; mod scanner; + +pub static THE_ENVIRONMENT: LazyLock> = LazyLock::new(|| Mutex::new(())); diff --git a/src/tests/run.rs b/src/tests/run.rs new file mode 100644 index 0000000..f681928 --- /dev/null +++ b/src/tests/run.rs @@ -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(()) +} diff --git a/src/tests/scanner.rs b/src/tests/scanner.rs index 18117af..9e586a6 100644 --- a/src/tests/scanner.rs +++ b/src/tests/scanner.rs @@ -9,12 +9,12 @@ 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.base().join("file_with_invalids.txt"), + include_str!("data/file_with_invalids.txt"), )?; fs.file_write( - &fs.base().join("file_2.txt"), - include_str!("data/file_2.txt"), + &fs.base().join("file_with_valids.txt"), + include_str!("data/file_with_valids.txt"), )?; let config = Config::builder() @@ -32,13 +32,13 @@ fn find_markers_in_dir() -> anyhow::Result<()> { assert_eq!( markers.to_string().lines().collect::>(), vec![ - "- Invalid: file_1.txt#2:", + "- Invalid: file_with_invalids.txt#2:", " 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", - "- 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", - "- 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" ] );