test(cli): add tests
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-05-14 07:58:28 +01:00
parent a4694d48f6
commit a7e7d12928
3 changed files with 63 additions and 19 deletions

View file

@ -1,27 +1,28 @@
use std::path::PathBuf;
use kxio::fs::FileSystem; use kxio::fs::FileSystem;
pub fn run(fs: FileSystem) { pub fn run(fs: FileSystem) {
let file_name = ".git-next.toml"; let file_name = ".git-next.toml";
let pathbuf = PathBuf::from(file_name); let pathbuf = fs.base().join(file_name);
let Ok(exists) = fs.path_exists(&pathbuf) else { match fs.path_exists(&pathbuf) {
eprintln!("Could not check if file exist: {}", file_name); Ok(exists) => {
return; if exists {
}; eprintln!(
if exists { "The configuration file already exists at {} - not overwritting it.",
eprintln!( file_name
"The configuration file already exists at {} - not overwritting it.", );
file_name } else {
); match fs.file_write(&pathbuf, include_str!("../../../default.toml")) {
} else { Ok(_) => {
match fs.file_write(&pathbuf, include_str!("../../../default.toml")) { println!("Created a default configuration file at {}", file_name);
Ok(_) => { }
println!("Created a default configuration file at {}", file_name); Err(e) => {
} eprintln!("Failed to write to the configuration file: {}", e)
Err(e) => { }
eprintln!("Failed to write to the configuration file: {}", e) }
} }
} }
Err(err) => {
eprintln!("Could not check if file exist: {} - {err:?}", file_name);
}
} }
} }

View file

@ -1,5 +1,8 @@
mod init; mod init;
#[cfg(test)]
mod tests;
use std::path::PathBuf; use std::path::PathBuf;
use clap::Parser; use clap::Parser;

40
crates/cli/src/tests.rs Normal file
View file

@ -0,0 +1,40 @@
type TestResult = Result<(), Box<dyn std::error::Error>>;
mod init {
use super::*;
#[test]
fn should_not_update_file_if_it_exists() -> TestResult {
let fs = kxio::fs::temp()?;
let file = fs.base().join(".git-next.toml");
fs.file_write(&file, "contents")?;
crate::init::run(fs.clone());
assert_eq!(
fs.file_read_to_string(&file)?,
"contents",
"The file has been changed"
);
Ok(())
}
#[test]
fn should_create_default_file_if_not_exists() -> TestResult {
let fs = kxio::fs::temp()?;
crate::init::run(fs.clone());
let file = fs.base().join(".git-next.toml");
assert!(fs.path_exists(&file)?, "The file has not been created");
assert_eq!(
fs.file_read_to_string(&file)?,
include_str!("../../../default.toml"),
"The file does not match the default template"
);
Ok(())
}
}