refactor: abstract printer via Printer trait
All checks were successful
Test / test (push) Successful in 10s
All checks were successful
Test / test (push) Successful in 10s
This commit is contained in:
parent
5a1fedd94b
commit
5077452f20
7 changed files with 61 additions and 20 deletions
13
src/init.rs
13
src/init.rs
|
@ -1,12 +1,15 @@
|
|||
//
|
||||
use crate::model::Config;
|
||||
use crate::patterns::{issue_pattern, marker_pattern};
|
||||
use crate::printer::Printer;
|
||||
use anyhow::{Context, Result};
|
||||
use kxio::fs;
|
||||
use kxio::network::Network;
|
||||
|
||||
pub fn init_config(net: kxio::network::Network) -> Result<Config, anyhow::Error> {
|
||||
pub fn init_config(printer: &impl Printer, net: Network) -> Result<Config> {
|
||||
let config = Config::builder()
|
||||
.net(net)
|
||||
.fs(kxio::fs::new(
|
||||
.fs(fs::new(
|
||||
std::env::var("GITHUB_WORKSPACE")
|
||||
.context("GITHUB_WORKSPACE")?
|
||||
.into(),
|
||||
|
@ -18,9 +21,9 @@ pub fn init_config(net: kxio::network::Network) -> Result<Config, anyhow::Error>
|
|||
.maybe_auth_token(std::env::var("REPO_TOKEN").ok())
|
||||
.build();
|
||||
|
||||
println!("Repo: {}", config.repo());
|
||||
println!("Prefix: {}", config.prefix_pattern());
|
||||
println!("Issues: {}", config.issue_pattern());
|
||||
printer.println(format!("Repo: {}", config.repo()));
|
||||
printer.println(format!("Prefix: {}", config.prefix_pattern()));
|
||||
printer.println(format!("Issues: {}", config.issue_pattern()));
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -2,12 +2,15 @@
|
|||
use anyhow::{bail, Result};
|
||||
use init::init_config;
|
||||
use issues::fetch_open_issues;
|
||||
use kxio::network::Network;
|
||||
use printer::Printer;
|
||||
use scanner::{find_markers, DefaultFileScanner};
|
||||
|
||||
mod init;
|
||||
mod issues;
|
||||
mod model;
|
||||
mod patterns;
|
||||
mod printer;
|
||||
mod scanner;
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -16,13 +19,15 @@ mod tests;
|
|||
#[tokio::main]
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(run(kxio::network::Network::new_real()).await?)
|
||||
use printer::StandardPrinter;
|
||||
|
||||
Ok(run(&StandardPrinter, Network::new_real()).await?)
|
||||
}
|
||||
|
||||
async fn run(net: kxio::network::Network) -> Result<()> {
|
||||
println!("Forgejo TODO Checker!");
|
||||
async fn run(printer: &impl Printer, net: Network) -> Result<()> {
|
||||
printer.println("Forgejo TODO Checker!");
|
||||
|
||||
let config = init_config(net)?;
|
||||
let config = init_config(printer, net)?;
|
||||
let issues = fetch_open_issues(&config).await?;
|
||||
let markers = find_markers(&config, issues, &DefaultFileScanner)?;
|
||||
|
||||
|
@ -30,7 +35,7 @@ async fn run(net: kxio::network::Network) -> Result<()> {
|
|||
for marker in (*markers).iter() {
|
||||
match marker {
|
||||
model::Marker::Closed(_, _) | model::Marker::Invalid(_) => {
|
||||
println!("{marker}");
|
||||
printer.println("{marker}");
|
||||
errors = true;
|
||||
}
|
||||
model::Marker::Unmarked | model::Marker::Valid(_, _) => {}
|
||||
|
|
11
src/printer.rs
Normal file
11
src/printer.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
//
|
||||
pub trait Printer {
|
||||
fn println(&self, message: impl Into<String>);
|
||||
}
|
||||
|
||||
pub struct StandardPrinter;
|
||||
impl Printer for StandardPrinter {
|
||||
fn println(&self, message: impl Into<String>) {
|
||||
println!("{}", message.into());
|
||||
}
|
||||
}
|
|
@ -2,8 +2,10 @@
|
|||
use super::*;
|
||||
|
||||
use assert2::let_assert;
|
||||
use kxio::network::Network;
|
||||
use model::Config;
|
||||
use patterns::{issue_pattern, marker_pattern};
|
||||
use printer::TestPrinter;
|
||||
|
||||
#[test]
|
||||
fn init_when_all_valid() -> anyhow::Result<()> {
|
||||
|
@ -13,7 +15,8 @@ fn init_when_all_valid() -> anyhow::Result<()> {
|
|||
std::env::set_var("GITHUB_WORKSPACE", fs.base());
|
||||
std::env::set_var("GITHUB_REPOSITORY", "repo");
|
||||
std::env::set_var("GITHUB_SERVER_URL", "server");
|
||||
let net = kxio::network::Network::new_mock();
|
||||
let net = Network::new_mock();
|
||||
let printer = TestPrinter::default();
|
||||
let expected = Config::builder()
|
||||
.net(net.clone())
|
||||
.fs(kxio::fs::new(fs.base().to_path_buf()))
|
||||
|
@ -25,7 +28,7 @@ fn init_when_all_valid() -> anyhow::Result<()> {
|
|||
.build();
|
||||
|
||||
//when
|
||||
let result = init_config(net)?;
|
||||
let result = init_config(&printer, net)?;
|
||||
|
||||
//then
|
||||
assert_eq!(result.fs().base(), expected.fs().base());
|
||||
|
@ -50,9 +53,10 @@ fn init_when_no_workspace() -> anyhow::Result<()> {
|
|||
std::env::remove_var("GITHUB_WORKSPACE");
|
||||
std::env::set_var("GITHUB_REPOSITORY", "repo");
|
||||
std::env::set_var("GITHUB_SERVER_URL", "server");
|
||||
let printer = TestPrinter::default();
|
||||
|
||||
//when
|
||||
let result = init_config(kxio::network::Network::new_mock());
|
||||
let result = init_config(&printer, Network::new_mock());
|
||||
|
||||
//then
|
||||
let_assert!(Err(e) = result);
|
||||
|
@ -69,9 +73,10 @@ fn init_when_no_repository() -> anyhow::Result<()> {
|
|||
std::env::set_var("GITHUB_WORKSPACE", fs.base());
|
||||
std::env::remove_var("GITHUB_REPOSITORY");
|
||||
std::env::set_var("GITHUB_SERVER_URL", "server");
|
||||
let printer = TestPrinter::default();
|
||||
|
||||
//when
|
||||
let result = init_config(kxio::network::Network::new_mock());
|
||||
let result = init_config(&printer, Network::new_mock());
|
||||
|
||||
//then
|
||||
let_assert!(Err(e) = result);
|
||||
|
@ -88,9 +93,10 @@ fn init_when_no_server_url() -> anyhow::Result<()> {
|
|||
std::env::set_var("GITHUB_WORKSPACE", fs.base());
|
||||
std::env::set_var("GITHUB_REPOSITORY", "repo");
|
||||
std::env::remove_var("GITHUB_SERVER_URL");
|
||||
let printer = TestPrinter::default();
|
||||
|
||||
//when
|
||||
let result = init_config(kxio::network::Network::new_mock());
|
||||
let result = init_config(&printer, Network::new_mock());
|
||||
|
||||
//then
|
||||
let_assert!(Err(e) = result);
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
//
|
||||
use super::*;
|
||||
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
|
||||
use model::Config;
|
||||
use patterns::{issue_pattern, marker_pattern};
|
||||
|
||||
//
|
||||
use super::*;
|
||||
|
||||
mod init;
|
||||
mod printer;
|
||||
mod run;
|
||||
mod scanner;
|
||||
|
||||
|
|
14
src/tests/printer.rs
Normal file
14
src/tests/printer.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
//
|
||||
use std::cell::RefCell;
|
||||
|
||||
use crate::printer::Printer;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TestPrinter {
|
||||
pub messages: RefCell<Vec<String>>,
|
||||
}
|
||||
impl Printer for TestPrinter {
|
||||
fn println(&self, message: impl Into<String>) {
|
||||
self.messages.borrow_mut().push(message.into());
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ use super::*;
|
|||
use anyhow::Result;
|
||||
use kxio::network::{RequestBody, RequestMethod, SavedRequest, StatusCode};
|
||||
use pretty_assertions::assert_eq;
|
||||
use printer::TestPrinter;
|
||||
|
||||
#[tokio::test]
|
||||
async fn run_with_some_invalids() -> Result<()> {
|
||||
|
@ -29,7 +30,7 @@ async fn run_with_some_invalids() -> Result<()> {
|
|||
std::env::set_var("GITHUB_SERVER_URL", "https://git.kemitix.net");
|
||||
|
||||
//when
|
||||
let result = run(net.clone().into()).await;
|
||||
let result = run(&TestPrinter::default(), net.clone().into()).await;
|
||||
|
||||
//then
|
||||
assert!(result.is_err()); // there is an invalid file
|
||||
|
@ -66,7 +67,7 @@ async fn run_with_no_invalids() -> Result<()> {
|
|||
std::env::set_var("GITHUB_SERVER_URL", "https://git.kemitix.net");
|
||||
|
||||
//when
|
||||
let result = run(net.clone().into()).await;
|
||||
let result = run(&TestPrinter::default(), net.clone().into()).await;
|
||||
|
||||
//then
|
||||
assert!(result.is_ok()); // there is an invalid file
|
||||
|
|
Loading…
Reference in a new issue