refactor: extract inline module tests::init
Some checks failed
Test / build (map[name:nightly]) (push) Successful in 2m12s
Test / build (map[name:stable]) (push) Successful in 2m0s
Release Please / Release-plz (push) Failing after 19s

This commit is contained in:
Paul Campbell 2024-12-14 19:50:21 +00:00
parent 3ae594c567
commit 9db75b81c0
2 changed files with 38 additions and 40 deletions

37
src/tests/init.rs Normal file
View file

@ -0,0 +1,37 @@
use super::*;
use test_log::test;
#[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(), given::a_printer());
//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(), given::a_printer());
//when
let_assert!(Err(err) = run(&ctx));
//then
assert!(err
.to_string()
.contains("File already exists - not overwriting"));
}

View file

@ -10,46 +10,7 @@ use crate::{
}; };
mod config; mod config;
mod init;
mod init {
use super::*;
use test_log::test;
#[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(), given::a_printer());
//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(), given::a_printer());
//when
let_assert!(Err(err) = run(&ctx));
//then
assert!(err
.to_string()
.contains("File already exists - not overwriting"));
}
}
mod template { mod template {
use super::*; use super::*;