forked from kemitix/git-next
test(cli): add tests
This commit is contained in:
parent
a4694d48f6
commit
a7e7d12928
3 changed files with 63 additions and 19 deletions
|
@ -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.",
|
||||
|
@ -24,4 +20,9 @@ pub fn run(fs: FileSystem) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Could not check if file exist: {} - {err:?}", file_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
40
crates/cli/src/tests.rs
Normal 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(())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue