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,14 +1,10 @@
use std::path::PathBuf;
use kxio::fs::FileSystem;
pub fn run(fs: FileSystem) {
let file_name = ".git-next.toml";
let pathbuf = PathBuf::from(file_name);
let Ok(exists) = fs.path_exists(&pathbuf) else {
eprintln!("Could not check if file exist: {}", file_name);
return;
};
let pathbuf = fs.base().join(file_name);
match fs.path_exists(&pathbuf) {
Ok(exists) => {
if exists {
eprintln!(
"The configuration file already exists at {} - not overwritting it.",
@ -25,3 +21,8 @@ pub fn run(fs: FileSystem) {
}
}
}
Err(err) => {
eprintln!("Could not check if file exist: {} - {err:?}", file_name);
}
}
}

View file

@ -1,5 +1,8 @@
mod init;
#[cfg(test)]
mod tests;
use std::path::PathBuf;
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(())
}
}