From 09fa848b9b05dd84d0140c47f0d9ab00f13cbd68 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Tue, 9 May 2023 13:10:16 -0500 Subject: [PATCH] integration testing --- src/filetype/builder.rs | 43 +++++++++----- src/filetype/structs.rs | 2 +- src/main.rs | 6 +- src/tests/test_files/expanded | 17 ------ .../test_site/pattern/test/array.meta | 1 - .../pattern/test/sub_pat/default.meta | 1 - src/tests/test_metafile.rs | 56 +------------------ tests/files/expanded | 25 +++++++++ .../test_site/pattern/base/default.meta | 0 .../files}/test_site/pattern/pat/default.meta | 0 .../files}/test_site/pattern/pat/pattern.meta | 0 tests/files/test_site/pattern/test/array.meta | 1 + .../test_site/pattern/test/blank/default.meta | 0 .../pattern/test/default/default.meta | 1 + .../test_site/pattern/test/new/default.meta | 0 .../test_site/pattern/test/new/new.meta | 0 .../pattern/test/overwrite/default.meta | 0 .../pattern/test/overwrite/overwrite.meta | 0 .../pattern/test/parent/default.meta | 0 .../test_site/pattern/test/parent/parent.meta | 0 .../test_site/pattern/test/pattern.meta | 0 .../pattern/test/sub_pat}/default.meta | 0 .../pattern/test/sub_pat/sub_pattern.meta | 1 + .../files}/test_site/source/expand.meta | 2 +- .../test_site/source/sub_dir/sub_source.meta | 0 .../files}/test_site/source/test_source.meta | 0 tests/metafile_builder.rs | 37 ++++++++++++ 27 files changed, 104 insertions(+), 89 deletions(-) delete mode 100644 src/tests/test_files/expanded delete mode 100644 src/tests/test_files/test_site/pattern/test/array.meta delete mode 100644 src/tests/test_files/test_site/pattern/test/sub_pat/default.meta create mode 100644 tests/files/expanded rename {src/tests/test_files => tests/files}/test_site/pattern/base/default.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/pat/default.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/pat/pattern.meta (100%) create mode 100644 tests/files/test_site/pattern/test/array.meta rename {src/tests/test_files => tests/files}/test_site/pattern/test/blank/default.meta (100%) create mode 100644 tests/files/test_site/pattern/test/default/default.meta rename {src/tests/test_files => tests/files}/test_site/pattern/test/new/default.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/test/new/new.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/test/overwrite/default.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/test/overwrite/overwrite.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/test/parent/default.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/test/parent/parent.meta (100%) rename {src/tests/test_files => tests/files}/test_site/pattern/test/pattern.meta (100%) rename {src/tests/test_files/test_site/pattern/test/default => tests/files/test_site/pattern/test/sub_pat}/default.meta (100%) create mode 100644 tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta rename {src/tests/test_files => tests/files}/test_site/source/expand.meta (92%) rename {src/tests/test_files => tests/files}/test_site/source/sub_dir/sub_source.meta (100%) rename {src/tests/test_files => tests/files}/test_site/source/test_source.meta (100%) create mode 100644 tests/metafile_builder.rs diff --git a/src/filetype/builder.rs b/src/filetype/builder.rs index a3edb3e..f1f3c64 100644 --- a/src/filetype/builder.rs +++ b/src/filetype/builder.rs @@ -2,6 +2,7 @@ use crate::{parse_file, MetaFile, RootDirs, Source, Substitution}; use color_eyre::Result; use std::{ collections::HashMap, + fs, path::{Path, PathBuf}, }; @@ -18,7 +19,9 @@ pub fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>) for section in file.source.iter() { match section { // concatenate any char sequences - Source::Str(str) => output.push_str(str), + Source::Str(str) => { + output.push_str(str); + } // expand all variables and recursively expand patterns Source::Sub(sub) => { let expanded = match sub { @@ -31,25 +34,23 @@ pub fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>) // so we use them to mark keys for array substitution Substitution::Array(key) => format!("-{{{key}}}"), }; - output.push_str(&expanded); + output.push_str(&format!("\n{}\n", expanded)); } } } // deal with arrays - Ok(expand_arrays(output, file, name)?) + expand_arrays(output, file, name) } fn get_pattern(key: &str, file: &MetaFile, dirs: &RootDirs) -> Result { - let filename = match file.get_pat(key) { - Some(file) => file, - None => "default", - }; + let filename = file.get_pat(key).unwrap_or("default"); let pattern_path = key.replace('.', "/") + "/" + filename; - let mut path = dirs.pattern.join(pattern_path).canonicalize()?; - path.set_extension(".meta"); - let pattern = parse_file(path.to_str().unwrap_or_default())?; + let mut path = dirs.pattern.join(pattern_path); + path.set_extension("meta"); + let pattern = &fs::read_to_string(path.to_str().unwrap_or_default())?; + let pattern = parse_file(pattern)?; metafile_to_string(&pattern, dirs, Some(key)) } @@ -76,7 +77,13 @@ fn expand_arrays(output: String, file: &MetaFile, name: Option<&str>) -> Result< }) // make a hash map of keys in the source to previously defined arrays .map(|array| { - let key = name.unwrap_or_default().to_owned() + "." + array; + let key: String; + if let Some(name) = name { + key = name.to_owned() + "." + array; + } else { + key = array.to_string(); + } + // let key = dbg!(name.unwrap_or_default().to_owned() + "." + array); let value = file.get_arr(&key).unwrap_or_default(); (*array, value) }) @@ -84,12 +91,12 @@ fn expand_arrays(output: String, file: &MetaFile, name: Option<&str>) -> Result< let mut expanded = String::new(); // loop to duplicate the output template for each array member - for i in 0.. { + for i in 0..get_max_size(&map) { // get a fresh copy of the file let mut str = output.clone(); // replace each key in the file for (key, val) in map.iter() { - str = str.replace(&format!("-{{{key}}}"), val[i]); + str = str.replace(&format!("-{{{key}}}"), val.get(i).unwrap_or(&"")); } // concatenate to final file expanded.push_str(&str); @@ -97,3 +104,13 @@ fn expand_arrays(output: String, file: &MetaFile, name: Option<&str>) -> Result< Ok(expanded) } + +fn get_max_size(map: &HashMap<&str, &[&str]>) -> usize { + let mut max = 0; + for val in map.values() { + if max < val.len() { + max = val.len(); + } + } + max +} diff --git a/src/filetype/structs.rs b/src/filetype/structs.rs index 57b8945..a6db5fd 100644 --- a/src/filetype/structs.rs +++ b/src/filetype/structs.rs @@ -21,7 +21,7 @@ pub enum Substitution<'a> { Pattern(&'a str), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct RootDirs { pub source: PathBuf, pub build: PathBuf, diff --git a/src/main.rs b/src/main.rs index 83f53c0..bacc264 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ extern crate pest; extern crate pest_derive; -fn main() { - println!("Hello, world!"); +use color_eyre::Result; + +fn main() -> Result<()> { + Ok(()) } diff --git a/src/tests/test_files/expanded b/src/tests/test_files/expanded deleted file mode 100644 index 32b6f03..0000000 --- a/src/tests/test_files/expanded +++ /dev/null @@ -1,17 +0,0 @@ -TESTS: - -var1 [VALUE]: GOOD -var2 [BLANK]: -var3 [VAL WITH SPACES]: GOOD GOOD - -arr1 [VALUE]: GOOD -arr3 [BLANK]: - -Pattern subs: - -test [WITH ARRAY]:

GOOD

GOOD GOOD

-test.sub_pat:

SUBPATTERN

-test.default:

DEFAULT

-test.blank: - -This comment should not be rendered: diff --git a/src/tests/test_files/test_site/pattern/test/array.meta b/src/tests/test_files/test_site/pattern/test/array.meta deleted file mode 100644 index c8f73d7..0000000 --- a/src/tests/test_files/test_site/pattern/test/array.meta +++ /dev/null @@ -1 +0,0 @@ -@{array} diff --git a/src/tests/test_files/test_site/pattern/test/sub_pat/default.meta b/src/tests/test_files/test_site/pattern/test/sub_pat/default.meta deleted file mode 100644 index f49b816..0000000 --- a/src/tests/test_files/test_site/pattern/test/sub_pat/default.meta +++ /dev/null @@ -1 +0,0 @@ -GOOD diff --git a/src/tests/test_metafile.rs b/src/tests/test_metafile.rs index c8263ac..144849c 100644 --- a/src/tests/test_metafile.rs +++ b/src/tests/test_metafile.rs @@ -1,12 +1,10 @@ use crate::{metafile_to_string, parse_file, source, RootDirs, Source, Substitution}; use color_eyre::Result; use pretty_assertions::assert_eq; -use std::fs; +use std::{fs, path::PathBuf}; -static SOURCE: &str = include_str!("test_files/test_site/source/test_source.meta"); -static PATTERN: &str = include_str!("test_files/test_site/pattern/test/pattern.meta"); -static PRE_EXPAND: &str = include_str!("test_files/test_site/source/expand.meta"); -static POST_EXPAND: &str = include_str!("test_files/expanded"); +static SOURCE: &str = include_str!("../../tests/files/test_site/source/test_source.meta"); +static PATTERN: &str = include_str!("../../tests/files//test_site/pattern/test/pattern.meta"); #[test] fn test_metafile_gets() -> Result<()> { @@ -79,51 +77,3 @@ fn parse_pattern_file() -> Result<()> { Ok(()) } - -#[ignore = "Need to setup tmp dir first"] -#[test] -fn builder_tests() -> Result<()> { - // vector of tests to perform on tmp_dir - let mut tests: Vec Result<()>> = Vec::default(); - tests.push(test_metafile_to_str); - - // run tests on tmp dir - test_on_tmp_dir(tests)?; - Ok(()) -} - -// builds a tmp_dir -// runs multiple tests on it, -// sanitizes build_dir between tests -// deletes the tmpdir -// so we don't have to rebuild entire tmpdir every test -fn test_on_tmp_dir(tests: Vec Result<()>>) -> Result<()> { - let tmp_dir = std::env::temp_dir(); - - let dirs = RootDirs { - source: tmp_dir.join("source"), - build: tmp_dir.join("site"), - pattern: tmp_dir.join("pattern"), - }; - - for test in tests.iter() { - // delete and remake build dir - fs::remove_dir_all(&dirs.build)?; - fs::create_dir(&dirs.build)?; - // run test - test(&dirs)?; - } - - fs::remove_dir_all(tmp_dir)?; - Ok(()) -} - -fn test_metafile_to_str(dirs: &RootDirs) -> Result<()> { - let metafile = parse_file(PRE_EXPAND)?; - - let file = metafile_to_string(&metafile, dirs, None)?; - - assert_eq!(file, POST_EXPAND); - - Ok(()) -} diff --git a/tests/files/expanded b/tests/files/expanded new file mode 100644 index 0000000..8e3f4dd --- /dev/null +++ b/tests/files/expanded @@ -0,0 +1,25 @@ +TESTS: + +var1 [VALUE]: +GOOD +var2 [BLANK]: + +var3 [VAL WITH SPACES]: +GOOD GOOD + +arr1 [VALUE]: +GOOD +arr2 [BLANK]: + +Pattern subs: + +test [WITH ARRAY]: +

GOOD

+

GOOD GOOD

+test.sub_pat: +

SUBPATTERN

+test.default: +

DEFAULT

+test.blank: + +This comment should not be rendered: diff --git a/src/tests/test_files/test_site/pattern/base/default.meta b/tests/files/test_site/pattern/base/default.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/base/default.meta rename to tests/files/test_site/pattern/base/default.meta diff --git a/src/tests/test_files/test_site/pattern/pat/default.meta b/tests/files/test_site/pattern/pat/default.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/pat/default.meta rename to tests/files/test_site/pattern/pat/default.meta diff --git a/src/tests/test_files/test_site/pattern/pat/pattern.meta b/tests/files/test_site/pattern/pat/pattern.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/pat/pattern.meta rename to tests/files/test_site/pattern/pat/pattern.meta diff --git a/tests/files/test_site/pattern/test/array.meta b/tests/files/test_site/pattern/test/array.meta new file mode 100644 index 0000000..c530977 --- /dev/null +++ b/tests/files/test_site/pattern/test/array.meta @@ -0,0 +1 @@ +

@{arr}

diff --git a/src/tests/test_files/test_site/pattern/test/blank/default.meta b/tests/files/test_site/pattern/test/blank/default.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/blank/default.meta rename to tests/files/test_site/pattern/test/blank/default.meta diff --git a/tests/files/test_site/pattern/test/default/default.meta b/tests/files/test_site/pattern/test/default/default.meta new file mode 100644 index 0000000..ada2226 --- /dev/null +++ b/tests/files/test_site/pattern/test/default/default.meta @@ -0,0 +1 @@ +

DEFAULT

diff --git a/src/tests/test_files/test_site/pattern/test/new/default.meta b/tests/files/test_site/pattern/test/new/default.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/new/default.meta rename to tests/files/test_site/pattern/test/new/default.meta diff --git a/src/tests/test_files/test_site/pattern/test/new/new.meta b/tests/files/test_site/pattern/test/new/new.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/new/new.meta rename to tests/files/test_site/pattern/test/new/new.meta diff --git a/src/tests/test_files/test_site/pattern/test/overwrite/default.meta b/tests/files/test_site/pattern/test/overwrite/default.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/overwrite/default.meta rename to tests/files/test_site/pattern/test/overwrite/default.meta diff --git a/src/tests/test_files/test_site/pattern/test/overwrite/overwrite.meta b/tests/files/test_site/pattern/test/overwrite/overwrite.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/overwrite/overwrite.meta rename to tests/files/test_site/pattern/test/overwrite/overwrite.meta diff --git a/src/tests/test_files/test_site/pattern/test/parent/default.meta b/tests/files/test_site/pattern/test/parent/default.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/parent/default.meta rename to tests/files/test_site/pattern/test/parent/default.meta diff --git a/src/tests/test_files/test_site/pattern/test/parent/parent.meta b/tests/files/test_site/pattern/test/parent/parent.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/parent/parent.meta rename to tests/files/test_site/pattern/test/parent/parent.meta diff --git a/src/tests/test_files/test_site/pattern/test/pattern.meta b/tests/files/test_site/pattern/test/pattern.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/pattern.meta rename to tests/files/test_site/pattern/test/pattern.meta diff --git a/src/tests/test_files/test_site/pattern/test/default/default.meta b/tests/files/test_site/pattern/test/sub_pat/default.meta similarity index 100% rename from src/tests/test_files/test_site/pattern/test/default/default.meta rename to tests/files/test_site/pattern/test/sub_pat/default.meta diff --git a/tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta b/tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta new file mode 100644 index 0000000..5377fce --- /dev/null +++ b/tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta @@ -0,0 +1 @@ +

SUBPATTERN

diff --git a/src/tests/test_files/test_site/source/expand.meta b/tests/files/test_site/source/expand.meta similarity index 92% rename from src/tests/test_files/test_site/source/expand.meta rename to tests/files/test_site/source/expand.meta index 1a5f54d..3bfb4d6 100644 --- a/src/tests/test_files/test_site/source/expand.meta +++ b/tests/files/test_site/source/expand.meta @@ -7,7 +7,7 @@ ${ @{ arr1 = ['GOOD'] arr2 = [] - test.array = ["GOOD", "GOOD GOOD"] + test.arr = ["GOOD", "GOOD GOOD"] } &{ diff --git a/src/tests/test_files/test_site/source/sub_dir/sub_source.meta b/tests/files/test_site/source/sub_dir/sub_source.meta similarity index 100% rename from src/tests/test_files/test_site/source/sub_dir/sub_source.meta rename to tests/files/test_site/source/sub_dir/sub_source.meta diff --git a/src/tests/test_files/test_site/source/test_source.meta b/tests/files/test_site/source/test_source.meta similarity index 100% rename from src/tests/test_files/test_site/source/test_source.meta rename to tests/files/test_site/source/test_source.meta diff --git a/tests/metafile_builder.rs b/tests/metafile_builder.rs new file mode 100644 index 0000000..262ee9e --- /dev/null +++ b/tests/metafile_builder.rs @@ -0,0 +1,37 @@ +use color_eyre::Result; +use metaforge::*; +use pretty_assertions::assert_eq; +use std::{fs, path::PathBuf}; + +static PRE_EXPAND: &str = include_str!("./files/test_site/source/expand.meta"); +static POST_EXPAND: &str = include_str!("./files/expanded"); + +#[test] +fn test_metafile_to_str() -> Result<()> { + let metafile = parse_file(PRE_EXPAND)?; + let dirs = build_rootdir()?; + + let file = metafile_to_string(&metafile, &dirs, None)?; + + assert_eq!(file, POST_EXPAND); + + Ok(()) +} + +fn build_rootdir() -> Result { + let dir = PathBuf::from("./tests/files/test_site"); + + let dirs = RootDirs { + source: dir.join("source"), + build: dir.join("site"), + pattern: dir.join("pattern"), + }; + + if dirs.build.exists() { + fs::remove_dir(&dirs.build)?; + } + + fs::create_dir(&dirs.build)?; + + Ok(dirs) +} -- 2.45.2