From 9db75b81c05f80b9bb0b0195db3a92c45c13776e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 14 Dec 2024 19:50:21 +0000 Subject: [PATCH] refactor: extract inline module tests::init --- src/tests/init.rs | 37 +++++++++++++++++++++++++++++++++++++ src/tests/mod.rs | 41 +---------------------------------------- 2 files changed, 38 insertions(+), 40 deletions(-) create mode 100644 src/tests/init.rs diff --git a/src/tests/init.rs b/src/tests/init.rs new file mode 100644 index 0000000..ac1ff98 --- /dev/null +++ b/src/tests/init.rs @@ -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")); +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 1ed14da..e0557e4 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -10,46 +10,7 @@ use crate::{ }; mod config; - -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 init; mod template { use super::*;