From cfc6c92adb9c063078305139076ce558113313a9 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Tue, 9 May 2023 20:46:50 -0500 Subject: [PATCH] test site passes --- bacon.toml | 4 +-- src/filetype/builder.rs | 35 +++++++++++++++---- src/parser/parse.rs | 12 ++++--- src/tests/test_metafile.rs | 19 +++++----- .../pattern/base/default.meta | 0 .../pattern/pat/default.meta | 0 .../pattern/pat/pattern.meta | 0 .../pattern/test/array.meta | 0 .../pattern/test/blank/default.meta | 0 .../pattern/test/default/default.meta | 0 .../pattern/test/new/default.meta | 0 .../pattern/test/new/new.meta | 0 .../pattern/test/overwrite/default.meta | 0 .../pattern/test/overwrite/overwrite.meta | 0 .../pattern/test/parent/default.meta | 0 .../pattern/test/parent/parent.meta | 0 .../pattern/test/pattern.meta | 0 .../pattern/test/sub_pat/default.meta | 0 .../pattern/test/sub_pat/sub_pattern.meta | 0 .../source/expand.meta | 0 .../source/sub_dir/sub_source.meta | 0 .../source/test_source.meta | 0 tests/files/expanded | 14 ++++---- tests/metafile_builder.rs | 7 ++-- 24 files changed, 60 insertions(+), 31 deletions(-) rename {tests/files/test_site => test_site}/pattern/base/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/pat/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/pat/pattern.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/array.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/blank/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/default/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/new/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/new/new.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/overwrite/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/overwrite/overwrite.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/parent/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/parent/parent.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/pattern.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/sub_pat/default.meta (100%) rename {tests/files/test_site => test_site}/pattern/test/sub_pat/sub_pattern.meta (100%) rename {tests/files/test_site => test_site}/source/expand.meta (100%) rename {tests/files/test_site => test_site}/source/sub_dir/sub_source.meta (100%) rename {tests/files/test_site => test_site}/source/test_source.meta (100%) diff --git a/bacon.toml b/bacon.toml index a4c3c37..ae7b891 100644 --- a/bacon.toml +++ b/bacon.toml @@ -24,8 +24,8 @@ command = [ need_stdout = false [jobs.test] -command = [ "cargo", "test", "--color", "always" ] -need_stdout = true +command = [ "cargo", "test", "--color", "always", "--", "--color", "always" ] +need_stdout = false [jobs.doc] command = ["cargo", "doc", "--color", "always", "--no-deps"] diff --git a/src/filetype/builder.rs b/src/filetype/builder.rs index f1f3c64..f3320d3 100644 --- a/src/filetype/builder.rs +++ b/src/filetype/builder.rs @@ -14,7 +14,8 @@ pub fn build_metafile(file: &MetaFile, dirs: &RootDirs, path: &Path) -> Result<( } pub fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>) -> Result { - let mut output = String::default(); + let mut output = String::new(); + let mut arrays = false; for section in file.source.iter() { match section { @@ -27,30 +28,53 @@ pub fn metafile_to_string(file: &MetaFile, dirs: &RootDirs, name: Option<&str>) let expanded = match sub { Substitution::Variable(key) => file .get_var(key) + .filter(|val| *val != "BLANK") .map(|val| val.to_string()) .unwrap_or_default(), Substitution::Pattern(key) => get_pattern(key, file, dirs)?, // comments have already been removed at this point, // so we use them to mark keys for array substitution - Substitution::Array(key) => format!("-{{{key}}}"), + Substitution::Array(key) => { + arrays = true; + format!("-{{{key}}}") + } }; output.push_str(&format!("\n{}\n", expanded)); } } } + println!("{}", output); + // deal with arrays - expand_arrays(output, file, name) + if arrays { + expand_arrays(output, file, name) + } else { + Ok(output) + } } fn get_pattern(key: &str, file: &MetaFile, dirs: &RootDirs) -> Result { - let filename = file.get_pat(key).unwrap_or("default"); + let mut filename = file.get_pat(key).unwrap_or("default"); + if filename == "BLANK" { + return Ok(String::new()); + }; + + if filename == "DEFAULT" { + filename = "default"; + } let pattern_path = key.replace('.', "/") + "/" + filename; let mut path = dirs.pattern.join(pattern_path); path.set_extension("meta"); + eprintln!("{:?}", path); let pattern = &fs::read_to_string(path.to_str().unwrap_or_default())?; - let pattern = parse_file(pattern)?; + let mut pattern = parse_file(pattern)?; + + pattern.variables = file.variables.clone(); + pattern.arrays = file.arrays.clone(); + pattern.patterns = file.patterns.clone(); + metafile_to_string(&pattern, dirs, Some(key)) } @@ -83,7 +107,6 @@ fn expand_arrays(output: String, file: &MetaFile, name: Option<&str>) -> Result< } 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) }) diff --git a/src/parser/parse.rs b/src/parser/parse.rs index c509760..37df17e 100644 --- a/src/parser/parse.rs +++ b/src/parser/parse.rs @@ -104,11 +104,15 @@ fn parse_assign(pair: Pair) -> (&'_ str, &'_ str) { } if Rule::value == pair.as_rule() { let tmp = pair.as_str(); - // blank and default shoud be handled by whoever is getting the value - // set it to empty strings to remove it from the HashMap - if tmp == "BLANK" || tmp == "DEFAULT" { - return ("", ""); + + if tmp == "BLANK" { + return (key, "BLANK"); + } + + if tmp == "DEFAULT" { + return (key, "DEFAULT"); } + // remove surrounding quotes from values by returning // everything except first and last characters // a string is defined as " ... " or ' ... ' diff --git a/src/tests/test_metafile.rs b/src/tests/test_metafile.rs index 144849c..04cc4ea 100644 --- a/src/tests/test_metafile.rs +++ b/src/tests/test_metafile.rs @@ -1,10 +1,9 @@ -use crate::{metafile_to_string, parse_file, source, RootDirs, Source, Substitution}; +use crate::{parse_file, source, Source, Substitution}; use color_eyre::Result; use pretty_assertions::assert_eq; -use std::{fs, path::PathBuf}; -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"); +static SOURCE: &str = include_str!("../../test_site/source/test_source.meta"); +static PATTERN: &str = include_str!("../../test_site/pattern/test/pattern.meta"); #[test] fn test_metafile_gets() -> Result<()> { @@ -12,7 +11,7 @@ fn test_metafile_gets() -> Result<()> { assert_eq!(source.get_var("var").unwrap(), "GOOD"); assert_eq!(source.get_var("single_quotes").unwrap(), "GOOD"); - assert_eq!(source.get_var("blank"), None); + assert_eq!(source.get_var("blank").unwrap(), "BLANK"); assert_eq!(source.get_var("not_defined"), None); assert_eq!(source.get_arr("sub.array").unwrap(), ["GOOD", "GOOD"]); @@ -24,8 +23,8 @@ fn test_metafile_gets() -> Result<()> { assert_eq!(source.get_arr("not_defined"), None); assert_eq!(source.get_pat("test").unwrap(), "pattern"); - assert_eq!(source.get_pat("test.sub_pat"), None); - assert_eq!(source.get_pat("blank_pat"), None); + assert_eq!(source.get_pat("test.sub_pat").unwrap(), "DEFAULT"); + assert_eq!(source.get_pat("blank_pat").unwrap(), "BLANK"); assert_eq!(source.get_pat("not_defined"), None); Ok(()) @@ -36,7 +35,7 @@ fn parse_meta_file() -> Result<()> { let source = parse_file(SOURCE)?; assert_eq!(source.variables.get("var").unwrap(), &"GOOD"); - assert_eq!(source.variables.get("blank"), None); + assert_eq!(source.variables.get("blank").unwrap(), &"BLANK"); assert_eq!(source.variables.get("not_here"), None); assert_eq!( @@ -54,8 +53,8 @@ fn parse_meta_file() -> Result<()> { assert_eq!(source.arrays.get("not_defined"), None); assert_eq!(source.patterns.get("test").unwrap(), &"pattern"); - assert_eq!(source.patterns.get("test.sub_pat"), None); - assert_eq!(source.patterns.get("blank_pat"), None); + assert_eq!(source.patterns.get("test.sub_pat").unwrap(), &"DEFAULT"); + assert_eq!(source.patterns.get("blank_pat").unwrap(), &"BLANK"); assert_eq!(source.patterns.get("not_defined"), None); Ok(()) diff --git a/tests/files/test_site/pattern/base/default.meta b/test_site/pattern/base/default.meta similarity index 100% rename from tests/files/test_site/pattern/base/default.meta rename to test_site/pattern/base/default.meta diff --git a/tests/files/test_site/pattern/pat/default.meta b/test_site/pattern/pat/default.meta similarity index 100% rename from tests/files/test_site/pattern/pat/default.meta rename to test_site/pattern/pat/default.meta diff --git a/tests/files/test_site/pattern/pat/pattern.meta b/test_site/pattern/pat/pattern.meta similarity index 100% rename from tests/files/test_site/pattern/pat/pattern.meta rename to test_site/pattern/pat/pattern.meta diff --git a/tests/files/test_site/pattern/test/array.meta b/test_site/pattern/test/array.meta similarity index 100% rename from tests/files/test_site/pattern/test/array.meta rename to test_site/pattern/test/array.meta diff --git a/tests/files/test_site/pattern/test/blank/default.meta b/test_site/pattern/test/blank/default.meta similarity index 100% rename from tests/files/test_site/pattern/test/blank/default.meta rename to test_site/pattern/test/blank/default.meta diff --git a/tests/files/test_site/pattern/test/default/default.meta b/test_site/pattern/test/default/default.meta similarity index 100% rename from tests/files/test_site/pattern/test/default/default.meta rename to test_site/pattern/test/default/default.meta diff --git a/tests/files/test_site/pattern/test/new/default.meta b/test_site/pattern/test/new/default.meta similarity index 100% rename from tests/files/test_site/pattern/test/new/default.meta rename to test_site/pattern/test/new/default.meta diff --git a/tests/files/test_site/pattern/test/new/new.meta b/test_site/pattern/test/new/new.meta similarity index 100% rename from tests/files/test_site/pattern/test/new/new.meta rename to test_site/pattern/test/new/new.meta diff --git a/tests/files/test_site/pattern/test/overwrite/default.meta b/test_site/pattern/test/overwrite/default.meta similarity index 100% rename from tests/files/test_site/pattern/test/overwrite/default.meta rename to test_site/pattern/test/overwrite/default.meta diff --git a/tests/files/test_site/pattern/test/overwrite/overwrite.meta b/test_site/pattern/test/overwrite/overwrite.meta similarity index 100% rename from tests/files/test_site/pattern/test/overwrite/overwrite.meta rename to test_site/pattern/test/overwrite/overwrite.meta diff --git a/tests/files/test_site/pattern/test/parent/default.meta b/test_site/pattern/test/parent/default.meta similarity index 100% rename from tests/files/test_site/pattern/test/parent/default.meta rename to test_site/pattern/test/parent/default.meta diff --git a/tests/files/test_site/pattern/test/parent/parent.meta b/test_site/pattern/test/parent/parent.meta similarity index 100% rename from tests/files/test_site/pattern/test/parent/parent.meta rename to test_site/pattern/test/parent/parent.meta diff --git a/tests/files/test_site/pattern/test/pattern.meta b/test_site/pattern/test/pattern.meta similarity index 100% rename from tests/files/test_site/pattern/test/pattern.meta rename to test_site/pattern/test/pattern.meta diff --git a/tests/files/test_site/pattern/test/sub_pat/default.meta b/test_site/pattern/test/sub_pat/default.meta similarity index 100% rename from tests/files/test_site/pattern/test/sub_pat/default.meta rename to test_site/pattern/test/sub_pat/default.meta diff --git a/tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta b/test_site/pattern/test/sub_pat/sub_pattern.meta similarity index 100% rename from tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta rename to test_site/pattern/test/sub_pat/sub_pattern.meta diff --git a/tests/files/test_site/source/expand.meta b/test_site/source/expand.meta similarity index 100% rename from tests/files/test_site/source/expand.meta rename to test_site/source/expand.meta diff --git a/tests/files/test_site/source/sub_dir/sub_source.meta b/test_site/source/sub_dir/sub_source.meta similarity index 100% rename from tests/files/test_site/source/sub_dir/sub_source.meta rename to test_site/source/sub_dir/sub_source.meta diff --git a/tests/files/test_site/source/test_source.meta b/test_site/source/test_source.meta similarity index 100% rename from tests/files/test_site/source/test_source.meta rename to test_site/source/test_source.meta diff --git a/tests/files/expanded b/tests/files/expanded index 8e3f4dd..bc2fff8 100644 --- a/tests/files/expanded +++ b/tests/files/expanded @@ -6,20 +6,22 @@ var2 [BLANK]: var3 [VAL WITH SPACES]: GOOD GOOD - arr1 [VALUE]: GOOD arr2 [BLANK]: Pattern subs: -test [WITH ARRAY]: -

GOOD

-

GOOD GOOD

+test [WITH ARRAY]: +

+GOOD +

+GOOD GOOD +

test.sub_pat:

SUBPATTERN

test.default:

DEFAULT

-test.blank: +test.blank: -This comment should not be rendered: +This comment should not be rendered: diff --git a/tests/metafile_builder.rs b/tests/metafile_builder.rs index 262ee9e..80f64e5 100644 --- a/tests/metafile_builder.rs +++ b/tests/metafile_builder.rs @@ -3,7 +3,7 @@ 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 PRE_EXPAND: &str = include_str!("../test_site/source/expand.meta"); static POST_EXPAND: &str = include_str!("./files/expanded"); #[test] @@ -13,13 +13,14 @@ fn test_metafile_to_str() -> Result<()> { let file = metafile_to_string(&metafile, &dirs, None)?; - assert_eq!(file, POST_EXPAND); + eprintln!("{}", file); + assert_eq!(file + "\n", POST_EXPAND); Ok(()) } fn build_rootdir() -> Result { - let dir = PathBuf::from("./tests/files/test_site"); + let dir = PathBuf::from("./test_site"); let dirs = RootDirs { source: dir.join("source"), -- 2.45.2