From 5e26707e42c7e94f6d9bff1059ee3a206f809ffd Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 29 Jul 2023 19:23:05 +0200 Subject: [PATCH] feat: add tests for git setup Signed-off-by: kjuulh --- crates/cuddle-please/src/command.rs | 26 +++++++++++++++++++++--- crates/cuddle-please/src/git_client.rs | 22 ++++++++++++++++++++ crates/cuddle-please/src/lib.rs | 1 + crates/cuddle-please/src/main.rs | 2 ++ crates/cuddle-please/tests/common/mod.rs | 15 ++++++++++++++ crates/cuddle-please/tests/config.rs | 18 ++-------------- crates/cuddle-please/tests/vcs.rs | 21 +++++++++++++++++++ 7 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 crates/cuddle-please/src/git_client.rs create mode 100644 crates/cuddle-please/tests/vcs.rs diff --git a/crates/cuddle-please/src/command.rs b/crates/cuddle-please/src/command.rs index a54ae46..a91558a 100644 --- a/crates/cuddle-please/src/command.rs +++ b/crates/cuddle-please/src/command.rs @@ -9,7 +9,10 @@ use anyhow::Context; use clap::{Args, Parser, Subcommand}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; -use crate::ui::{ConsoleUi, DynUi}; +use crate::{ + git_client::VcsClient, + ui::{ConsoleUi, DynUi}, +}; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -35,7 +38,6 @@ struct GlobalArgs { long, long_help = "token is the personal access token from gitea. It requires at least repository write access, it isn't required by default, but for most usecases the flow will fail without it", global = true, - group = "global", help_heading = "Global" )] token: Option, @@ -48,11 +50,19 @@ struct GlobalArgs { #[arg(long, global = true, group = "global", help_heading = "Global")] source: Option, + /// no version control system, forces please to allow no .git/ or friends + #[arg( + long, + global = true, + help_heading = "Global", + long_help = "no version control system. This forces cuddle-please to accept that it won't be running in git. All fields will have to be fed through values in the given commands." + )] + no_vcs: bool, + /// Inject configuration from stdin #[arg( long, global = true, - group = "global", help_heading = "Global", long_help = "inject via stdin cat < anyhow::Result { + if self.global.no_vcs { + Ok(VcsClient::new_noop()) + } else { + VcsClient::new_git(current_dir) + } + } } #[derive(Debug, Clone, Subcommand)] diff --git a/crates/cuddle-please/src/git_client.rs b/crates/cuddle-please/src/git_client.rs new file mode 100644 index 0000000..ec3f456 --- /dev/null +++ b/crates/cuddle-please/src/git_client.rs @@ -0,0 +1,22 @@ +use std::path::{Path, PathBuf}; + +#[derive(Clone, Debug, PartialEq)] +pub enum VcsClient { + Noop {}, + Git { source: PathBuf }, +} + +impl VcsClient { + pub fn new_noop() -> VcsClient { + Self::Noop {} + } + pub fn new_git(path: &Path) -> anyhow::Result { + if !path.to_path_buf().join(".git").exists() { + anyhow::bail!("git directory not found in: {}", path.display().to_string()) + } + + Ok(Self::Git { + source: path.to_path_buf(), + }) + } +} diff --git a/crates/cuddle-please/src/lib.rs b/crates/cuddle-please/src/lib.rs index 14e82c0..92b85fb 100644 --- a/crates/cuddle-please/src/lib.rs +++ b/crates/cuddle-please/src/lib.rs @@ -1,2 +1,3 @@ pub mod command; +pub mod git_client; pub mod ui; diff --git a/crates/cuddle-please/src/main.rs b/crates/cuddle-please/src/main.rs index 6a4e57c..ce14512 100644 --- a/crates/cuddle-please/src/main.rs +++ b/crates/cuddle-please/src/main.rs @@ -1,7 +1,9 @@ pub mod command; +pub mod git_client; pub mod ui; use command::Command; +use ui::ConsoleUi; #[tokio::main] async fn main() -> anyhow::Result<()> { diff --git a/crates/cuddle-please/tests/common/mod.rs b/crates/cuddle-please/tests/common/mod.rs index 3c801bd..2765fff 100644 --- a/crates/cuddle-please/tests/common/mod.rs +++ b/crates/cuddle-please/tests/common/mod.rs @@ -2,6 +2,7 @@ use cuddle_please::ui::{DynUi, Ui}; use std::{ io::Write, + path::PathBuf, sync::{Arc, Mutex}, }; @@ -111,3 +112,17 @@ impl From<&BufferUi> for DynUi { value.clone().into() } } + +pub fn assert_output(ui: &BufferUi, expected_stdout: &str, expected_stderr: &str) { + let (stdout, stderr) = ui.get_output(); + + assert_eq!(expected_stdout, &stdout); + assert_eq!(expected_stderr, &stderr); +} + +pub fn get_test_data_path(item: &str) -> PathBuf { + std::env::current_dir() + .ok() + .map(|p| p.join("testdata").join(item)) + .unwrap() +} diff --git a/crates/cuddle-please/tests/config.rs b/crates/cuddle-please/tests/config.rs index 8acff95..ad9f8a6 100644 --- a/crates/cuddle-please/tests/config.rs +++ b/crates/cuddle-please/tests/config.rs @@ -1,29 +1,15 @@ pub mod common; -use std::path::PathBuf; - use common::BufferUi; use cuddle_please::command::Command; use tracing_test::traced_test; +use crate::common::{assert_output, get_test_data_path}; + fn get_base_args<'a>() -> Vec<&'a str> { vec!["cuddle-please", "config", "list"] } -fn assert_output(ui: &BufferUi, expected_stdout: &str, expected_stderr: &str) { - let (stdout, stderr) = ui.get_output(); - - assert_eq!(expected_stdout, &stdout); - assert_eq!(expected_stderr, &stderr); -} - -fn get_test_data_path(item: &str) -> PathBuf { - std::env::current_dir() - .ok() - .map(|p| p.join("testdata").join(item)) - .unwrap() -} - #[test] #[traced_test] fn test_config_from_current_dir() { diff --git a/crates/cuddle-please/tests/vcs.rs b/crates/cuddle-please/tests/vcs.rs new file mode 100644 index 0000000..ca099a9 --- /dev/null +++ b/crates/cuddle-please/tests/vcs.rs @@ -0,0 +1,21 @@ +pub mod common; + +use cuddle_please::git_client::VcsClient; +use tracing_test::traced_test; + +use crate::common::get_test_data_path; + +#[test] +#[traced_test] +fn test_vcs_get_noop() { + let noop = VcsClient::new_noop(); + assert!(matches!(noop, VcsClient::Noop {})); +} + +#[test] +#[traced_test] +fn test_vcs_get_git_found() { + let testdata = get_test_data_path("git-found"); + let git = VcsClient::new_git(&testdata).unwrap(); + assert_eq!(git, VcsClient::Git { source: testdata }) +}