tests: add tests for main
All checks were successful
Test / test (push) Successful in 9s

This commit is contained in:
Paul Campbell 2024-09-19 20:37:23 +01:00
parent c86ff97798
commit a2f7601eab
7 changed files with 75 additions and 8 deletions

View file

@ -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()?;

View file

@ -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");

View file

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

View file

@ -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<_>>(),
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"
]
);