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

This commit is contained in:
Paul Campbell 2024-09-19 19:32:15 +01:00
parent 5357bbe4c5
commit 76a047b9ca
5 changed files with 105 additions and 1 deletions

View file

@ -10,3 +10,6 @@ ureq = "2.10"
kxio = "1.2" kxio = "1.2"
ignore = "0.4" ignore = "0.4"
bon = "2.3" bon = "2.3"
[dev-dependencies]
assert2 = "0.3"

View file

@ -8,6 +8,9 @@ mod model;
mod patterns; mod patterns;
mod scanner; mod scanner;
#[cfg(test)]
mod tests;
fn main() -> Result<()> { fn main() -> Result<()> {
println!("Forgejo TODO Checker!"); println!("Forgejo TODO Checker!");

View file

@ -4,7 +4,7 @@
use bon::Builder; use bon::Builder;
use regex::Regex; use regex::Regex;
#[derive(Builder)] #[derive(Debug, Builder)]
pub struct Config { pub struct Config {
fs: kxio::fs::FileSystem, fs: kxio::fs::FileSystem,
repo: String, repo: String,

94
src/tests/init.rs Normal file
View file

@ -0,0 +1,94 @@
//
use super::*;
use assert2::let_assert;
use model::Config;
use patterns::{issue_pattern, marker_pattern};
#[test]
fn init_when_all_valid() -> anyhow::Result<()> {
//given
let fs = kxio::fs::temp()?;
std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::set_var("GITHUB_REPOSITORY", "repo");
std::env::set_var("GITHUB_SERVER_URL", "server");
let expected = Config::builder()
.fs(kxio::fs::new(fs.base().to_path_buf()))
.repo("repo".to_string())
.server("server".to_string())
.prefix_pattern(marker_pattern()?)
.issue_pattern(issue_pattern()?)
.maybe_auth_token(Some("auth".to_string()))
.build();
//when
let result = init_config()?;
//then
assert_eq!(result.fs().base(), expected.fs().base());
assert_eq!(result.repo(), expected.repo());
// assert_eq!(result.server(), expected.server());
assert_eq!(
result.prefix_pattern().to_string(),
expected.prefix_pattern().to_string()
);
assert_eq!(
result.issue_pattern().to_string(),
expected.issue_pattern().to_string()
);
Ok(())
}
#[test]
fn init_when_no_workspace() -> anyhow::Result<()> {
//given
std::env::remove_var("GITHUB_WORKSPACE");
std::env::set_var("GITHUB_REPOSITORY", "repo");
std::env::set_var("GITHUB_SERVER_URL", "server");
//when
let result = init_config();
//then
let_assert!(Err(e) = result);
assert_eq!(e.to_string(), "GITHUB_WORKSPACE");
Ok(())
}
#[test]
fn init_when_no_repository() -> anyhow::Result<()> {
//given
let fs = kxio::fs::temp()?;
std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::remove_var("GITHUB_REPOSITORY");
std::env::set_var("GITHUB_SERVER_URL", "server");
//when
let result = init_config();
//then
let_assert!(Err(e) = result);
assert_eq!(e.to_string(), "GITHUB_REPOSITORY");
Ok(())
}
#[test]
fn init_when_no_server_url() -> anyhow::Result<()> {
//given
let fs = kxio::fs::temp()?;
std::env::set_var("GITHUB_WORKSPACE", fs.base());
std::env::set_var("GITHUB_REPOSITORY", "repo");
std::env::remove_var("GITHUB_SERVER_URL");
//when
let result = init_config();
//then
let_assert!(Err(e) = result);
assert_eq!(e.to_string(), "GITHUB_SERVER_URL");
Ok(())
}

4
src/tests/mod.rs Normal file
View file

@ -0,0 +1,4 @@
//
use super::*;
mod init;