From 5077452f2012f239580b62959e3277fcd3f66f3f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 21 Sep 2024 18:25:42 +0100 Subject: [PATCH] refactor: abstract printer via Printer trait --- src/init.rs | 13 ++++++++----- src/main.rs | 15 ++++++++++----- src/printer.rs | 11 +++++++++++ src/tests/init.rs | 16 +++++++++++----- src/tests/mod.rs | 7 ++++--- src/tests/printer.rs | 14 ++++++++++++++ src/tests/run.rs | 5 +++-- 7 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 src/printer.rs create mode 100644 src/tests/printer.rs diff --git a/src/init.rs b/src/init.rs index e7bc50f..3e7e027 100644 --- a/src/init.rs +++ b/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 { +pub fn init_config(printer: &impl Printer, net: Network) -> Result { 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 .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) } diff --git a/src/main.rs b/src/main.rs index bd0757c..fc3587c 100644 --- a/src/main.rs +++ b/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> { - 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(_, _) => {} diff --git a/src/printer.rs b/src/printer.rs new file mode 100644 index 0000000..fce09b1 --- /dev/null +++ b/src/printer.rs @@ -0,0 +1,11 @@ +// +pub trait Printer { + fn println(&self, message: impl Into); +} + +pub struct StandardPrinter; +impl Printer for StandardPrinter { + fn println(&self, message: impl Into) { + println!("{}", message.into()); + } +} diff --git a/src/tests/init.rs b/src/tests/init.rs index 9e61a70..487c9ab 100644 --- a/src/tests/init.rs +++ b/src/tests/init.rs @@ -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); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c477d4f..35ea1f9 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -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; diff --git a/src/tests/printer.rs b/src/tests/printer.rs new file mode 100644 index 0000000..21db2d3 --- /dev/null +++ b/src/tests/printer.rs @@ -0,0 +1,14 @@ +// +use std::cell::RefCell; + +use crate::printer::Printer; + +#[derive(Default)] +pub struct TestPrinter { + pub messages: RefCell>, +} +impl Printer for TestPrinter { + fn println(&self, message: impl Into) { + self.messages.borrow_mut().push(message.into()); + } +} diff --git a/src/tests/run.rs b/src/tests/run.rs index e97a108..295d143 100644 --- a/src/tests/run.rs +++ b/src/tests/run.rs @@ -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