Added unit and integration tests

This commit is contained in:
2025-09-10 22:34:36 -06:00
parent 083245b447
commit 0f5c28a040
17 changed files with 244 additions and 47 deletions
+27 -26
View File
@@ -1,5 +1,7 @@
use crate::command::preview_command;
use anyhow::{Context, Result, anyhow};
use gman::config::{Config, RunConfig};
use gman::providers::SecretProvider;
use heck::ToSnakeCase;
use log::{debug, error};
use regex::Regex;
@@ -8,8 +10,6 @@ use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use gman::config::{Config, RunConfig};
use gman::providers::SecretProvider;
const ARG_FORMAT_PLACEHOLDER_KEY: &str = "{{key}}";
const ARG_FORMAT_PLACEHOLDER_VALUE: &str = "{{value}}";
@@ -246,32 +246,33 @@ pub fn parse_args(
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use gman::config::RunConfig;
use crate::cli::generate_files_secret_injections;
use crate::cli::generate_files_secret_injections;
use gman::config::RunConfig;
use pretty_assertions::{assert_eq, assert_str_eq};
use std::collections::HashMap;
#[test]
fn test_generate_files_secret_injections() {
let mut secrets = HashMap::new();
secrets.insert("SECRET1".to_string(), "value1".to_string());
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("test.txt");
std::fs::write(&file_path, "{{secret1}}").unwrap();
#[test]
fn test_generate_files_secret_injections() {
let mut secrets = HashMap::new();
secrets.insert("SECRET1".to_string(), "value1".to_string());
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("test.txt");
std::fs::write(&file_path, "{{secret1}}").unwrap();
let run_config = RunConfig {
name: Some("test".to_string()),
secrets: Some(vec!["secret1".to_string()]),
files: Some(vec![file_path.clone()]),
flag: None,
flag_position: None,
arg_format: None,
};
let run_config = RunConfig {
name: Some("test".to_string()),
secrets: Some(vec!["secret1".to_string()]),
files: Some(vec![file_path.clone()]),
flag: None,
flag_position: None,
arg_format: None,
};
let result = generate_files_secret_injections(secrets, &run_config).unwrap();
let result = generate_files_secret_injections(secrets, &run_config).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].0, &file_path);
assert_eq!(result[0].1, "{{secret1}}");
assert_eq!(result[0].2, "value1");
}
assert_eq!(result.len(), 1);
assert_eq!(result[0].0, &file_path);
assert_str_eq!(result[0].1, "{{secret1}}");
assert_str_eq!(result[0].2, "value1");
}
}
+20
View File
@@ -102,3 +102,23 @@ fn ps_quote(s: &OsStr) -> String {
s.into_owned()
}
}
#[cfg(test)]
mod tests {
use crate::command::preview_command;
use pretty_assertions::assert_str_eq;
use std::process::Command;
#[test]
fn test_preview_command() {
let mut cmd = Command::new("echo");
cmd.arg("hello world");
cmd.env("MY_VAR", "my_value");
let preview = preview_command(&cmd);
if cfg!(unix) {
assert_str_eq!(preview, "MY_VAR=my_value echo 'hello world'");
} else if cfg!(windows) {
assert_str_eq!(preview, "set MY_VAR=my_value && \"echo\" \"hello world\"");
}
}
}
+3 -3
View File
@@ -1,15 +1,15 @@
use clap::{
crate_authors, crate_description, crate_name, crate_version, CommandFactory, Parser, ValueEnum,
CommandFactory, Parser, ValueEnum, crate_authors, crate_description, crate_name, crate_version,
};
use std::ffi::OsString;
use anyhow::{Context, Result};
use clap::Subcommand;
use crossterm::execute;
use crossterm::terminal::{disable_raw_mode, LeaveAlternateScreen};
use crossterm::terminal::{LeaveAlternateScreen, disable_raw_mode};
use gman::config::Config;
use gman::providers::local::LocalProvider;
use gman::providers::SupportedProvider;
use gman::providers::local::LocalProvider;
use heck::ToSnakeCase;
use std::io::{self, IsTerminal, Read, Write};
use std::panic::PanicHookInfo;
+17
View File
@@ -41,3 +41,20 @@ pub fn get_log_path() -> PathBuf {
log_path.push("gman.log");
log_path
}
#[cfg(test)]
mod tests {
use crate::utils::get_log_path;
#[test]
fn test_get_log_path() {
let log_path = get_log_path();
if cfg!(target_os = "linux") {
assert!(log_path.ends_with(".cache/gman/gman.log"));
} else if cfg!(target_os = "macos") {
assert!(log_path.ends_with("Library/Logs/gman/gman.log"));
} else if cfg!(target_os = "windows") {
assert!(log_path.ends_with("Logs\\gman\\gman.log"));
}
}
}
+1
View File
@@ -157,6 +157,7 @@ pub fn decrypt_string(password: impl Into<SecretString>, envelope: &str) -> Resu
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn round_trip() {
+24
View File
@@ -330,3 +330,27 @@ fn get_password(config: &Config) -> Result<SecretString> {
Ok(SecretString::new(password.into()))
}
}
#[cfg(test)]
mod tests {
use crate::derive_key;
use crate::providers::local::derive_key_with_params;
use pretty_assertions::assert_eq;
use secrecy::SecretString;
#[test]
fn test_derive_key() {
let password = SecretString::new("test_password".to_string().into());
let salt = [0u8; 16];
let key = derive_key(&password, &salt).unwrap();
assert_eq!(key.as_slice().len(), 32);
}
#[test]
fn test_derive_key_with_params() {
let password = SecretString::new("test_password".to_string().into());
let salt = [0u8; 16];
let key = derive_key_with_params(&password, &salt, 10, 1, 1).unwrap();
assert_eq!(key.as_slice().len(), 32);
}
}