2024-11-29 14:31:40 +00:00
|
|
|
//
|
2024-12-05 20:07:29 +00:00
|
|
|
use std::collections::HashMap;
|
2024-11-29 14:31:40 +00:00
|
|
|
|
|
|
|
// type TestResult = Result<(), Box<dyn std::error::Error>>;
|
|
|
|
|
|
|
|
use assert2::let_assert;
|
2024-12-05 20:07:29 +00:00
|
|
|
use kxio::{
|
|
|
|
fs::{FileSystem, TempFileSystem},
|
|
|
|
net::{MockNet, Net},
|
|
|
|
print::Printer,
|
|
|
|
};
|
2024-11-29 14:31:40 +00:00
|
|
|
|
2024-12-05 20:07:29 +00:00
|
|
|
use crate::{config::AppConfig, f, init::run, Ctx, NAME};
|
2024-11-29 19:19:36 +00:00
|
|
|
|
|
|
|
mod config {
|
|
|
|
use super::*;
|
|
|
|
|
2024-11-30 11:30:36 +00:00
|
|
|
use crate::config::{NextcloudConfig, TrelloConfig};
|
2024-11-29 19:19:36 +00:00
|
|
|
use crate::s;
|
|
|
|
|
2024-11-29 19:19:36 +00:00
|
|
|
#[test]
|
|
|
|
fn load_config() {
|
|
|
|
//given
|
|
|
|
let fs = given::a_filesystem();
|
|
|
|
let file = fs.base().join(f!("{}.toml", NAME));
|
2024-11-29 19:19:36 +00:00
|
|
|
fs.file(&file)
|
|
|
|
.write(
|
|
|
|
[
|
|
|
|
"[trello]",
|
|
|
|
"api_key = \"trello-api-key\"",
|
|
|
|
"api_secret = \"trello-api-secret\"",
|
|
|
|
"board_name = \"trello-board-name\"",
|
2024-11-30 11:30:36 +00:00
|
|
|
"",
|
|
|
|
"[nextcloud]",
|
|
|
|
"hostname = \"nextcloud-hostname\"",
|
|
|
|
"username = \"nextcloud-username\"",
|
|
|
|
"password = \"nextcloud-password\"",
|
|
|
|
"board_id = 22",
|
2024-11-29 19:19:36 +00:00
|
|
|
]
|
|
|
|
.join("\n"),
|
|
|
|
)
|
|
|
|
.expect("write file");
|
2024-12-07 10:05:37 +00:00
|
|
|
let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer());
|
2024-11-29 19:19:36 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
let_assert!(Ok(config) = AppConfig::load(&ctx));
|
|
|
|
|
|
|
|
//then
|
2024-11-29 19:19:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
config,
|
|
|
|
AppConfig {
|
|
|
|
trello: TrelloConfig {
|
2024-12-04 20:31:36 +00:00
|
|
|
api_key: s!("trello-api-key").into(),
|
|
|
|
api_secret: s!("trello-api-secret").into(),
|
|
|
|
board_name: s!("trello-board-name").into(),
|
2024-11-30 11:30:36 +00:00
|
|
|
},
|
|
|
|
nextcloud: NextcloudConfig {
|
|
|
|
hostname: s!("nextcloud-hostname").into(),
|
|
|
|
username: s!("nextcloud-username").into(),
|
|
|
|
password: s!("nextcloud-password").into(),
|
|
|
|
board_id: 22.into()
|
2024-11-29 19:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2024-11-29 19:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 14:31:40 +00:00
|
|
|
mod init {
|
|
|
|
use super::*;
|
2024-12-05 20:07:29 +00:00
|
|
|
use test_log::test;
|
2024-11-29 14:31:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn when_file_does_not_exist_should_create() {
|
|
|
|
//given
|
|
|
|
let fs = given::a_filesystem();
|
2024-12-07 10:05:37 +00:00
|
|
|
let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer());
|
2024-11-29 14:31:40 +00:00
|
|
|
|
|
|
|
//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");
|
|
|
|
|
2024-12-07 10:05:37 +00:00
|
|
|
let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer());
|
2024-11-29 14:31:40 +00:00
|
|
|
//when
|
|
|
|
let_assert!(Err(err) = run(&ctx));
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert!(err
|
|
|
|
.to_string()
|
|
|
|
.contains("File already exists - not overwriting"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod template {
|
2024-12-05 20:07:29 +00:00
|
|
|
use super::*;
|
2024-11-29 14:31:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expand_should_substitute_values() {
|
|
|
|
//given
|
|
|
|
let template = "pre{param1}mid{param2}post";
|
|
|
|
let params = HashMap::from([("param1", "-v1-"), ("param2", "-v2-")]);
|
|
|
|
|
|
|
|
//when
|
2024-12-05 20:07:29 +00:00
|
|
|
let result = crate::template::expand(template, params);
|
2024-11-29 14:31:40 +00:00
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(result, "pre-v1-mid-v2-post");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod given {
|
2024-12-05 20:07:29 +00:00
|
|
|
use super::*;
|
2024-11-29 14:31:40 +00:00
|
|
|
|
2024-12-07 10:05:37 +00:00
|
|
|
pub fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx {
|
|
|
|
Ctx { fs, net, prt }
|
2024-11-29 14:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn a_filesystem() -> TempFileSystem {
|
|
|
|
kxio::fs::temp().expect("temp fs")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn a_network() -> MockNet {
|
|
|
|
kxio::net::mock()
|
|
|
|
}
|
2024-12-07 10:05:37 +00:00
|
|
|
|
|
|
|
pub fn a_printer() -> Printer {
|
|
|
|
kxio::print::test()
|
|
|
|
}
|
2024-12-05 20:07:29 +00:00
|
|
|
|
|
|
|
// pub fn a_config() -> AppConfig {
|
|
|
|
// AppConfig {
|
|
|
|
// trello: a_trello_config(),
|
|
|
|
// nextcloud: a_nextcloud_config(),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// pub fn a_trello_config() -> TrelloConfig {
|
|
|
|
// TrelloConfig {
|
|
|
|
// api_key: s!("trello-api-key").into(),
|
|
|
|
// api_secret: s!("trello-api-secret").into(),
|
|
|
|
// board_name: s!("Trello Platform Changes").into(),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// pub fn a_nextcloud_config() -> NextcloudConfig {
|
|
|
|
// let hostname = s!("nextcloud.example.org").into();
|
|
|
|
// let username = s!("username").into();
|
|
|
|
// let password = s!("password").into();
|
|
|
|
// let board_id = NextcloudBoardId::new(2);
|
|
|
|
// NextcloudConfig {
|
|
|
|
// hostname,
|
|
|
|
// username,
|
|
|
|
// password,
|
|
|
|
// board_id,
|
|
|
|
// }
|
|
|
|
// }
|
2024-11-29 14:31:40 +00:00
|
|
|
}
|