91 lines
2 KiB
Rust
91 lines
2 KiB
Rust
|
//
|
||
|
|
||
|
// type TestResult = Result<(), Box<dyn std::error::Error>>;
|
||
|
|
||
|
use assert2::let_assert;
|
||
|
|
||
|
mod init {
|
||
|
|
||
|
use test_log::test;
|
||
|
|
||
|
use crate::{f, init::run, NAME};
|
||
|
|
||
|
use super::given;
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn when_file_does_not_exist_should_create() {
|
||
|
//given
|
||
|
let fs = given::a_filesystem();
|
||
|
let ctx = given::a_context(fs.as_real(), given::a_network().into());
|
||
|
|
||
|
//when
|
||
|
let_assert!(Ok(_) = run(&ctx));
|
||
|
|
||
|
//then
|
||
|
let path = ctx.fs.base().join(f!("{NAME}.toml"));
|
||
|
let file = ctx.fs.file(&path);
|
||
|
let contents = file.reader().expect("read file").to_string();
|
||
|
assert_eq!(contents, include_str!("../default-config.toml"));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn when_file_exists_should_err() {
|
||
|
//given
|
||
|
let fs = given::a_filesystem();
|
||
|
let path = fs.base().join(f!("{NAME}.toml"));
|
||
|
let file = fs.file(&path);
|
||
|
file.write("").expect("create file");
|
||
|
|
||
|
let ctx = given::a_context(fs.as_real(), given::a_network().into());
|
||
|
//when
|
||
|
let_assert!(Err(err) = run(&ctx));
|
||
|
|
||
|
//then
|
||
|
assert!(err
|
||
|
.to_string()
|
||
|
.contains("File already exists - not overwriting"));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mod template {
|
||
|
|
||
|
use std::collections::HashMap;
|
||
|
|
||
|
use crate::template;
|
||
|
|
||
|
#[test]
|
||
|
fn expand_should_substitute_values() {
|
||
|
//given
|
||
|
let template = "pre{param1}mid{param2}post";
|
||
|
let params = HashMap::from([("param1", "-v1-"), ("param2", "-v2-")]);
|
||
|
|
||
|
//when
|
||
|
let result = template::expand(template, params);
|
||
|
|
||
|
//then
|
||
|
assert_eq!(result, "pre-v1-mid-v2-post");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mod given {
|
||
|
use kxio::{
|
||
|
fs::{FileSystem, TempFileSystem},
|
||
|
net::{MockNet, Net},
|
||
|
};
|
||
|
|
||
|
use crate::Ctx;
|
||
|
|
||
|
pub fn a_context(fs: FileSystem, net: Net) -> Ctx {
|
||
|
Ctx { fs, net }
|
||
|
}
|
||
|
|
||
|
pub fn a_filesystem() -> TempFileSystem {
|
||
|
kxio::fs::temp().expect("temp fs")
|
||
|
}
|
||
|
|
||
|
pub fn a_network() -> MockNet {
|
||
|
kxio::net::mock()
|
||
|
}
|
||
|
}
|