]> git.huck.website - metaforge.git/commitdiff
integration testing
authorHuck Boles <huck@huck.website>
Tue, 9 May 2023 18:10:16 +0000 (13:10 -0500)
committerHuck Boles <huck@huck.website>
Tue, 9 May 2023 18:10:16 +0000 (13:10 -0500)
27 files changed:
src/filetype/builder.rs
src/filetype/structs.rs
src/main.rs
src/tests/test_files/expanded [deleted file]
src/tests/test_files/test_site/pattern/test/array.meta [deleted file]
src/tests/test_files/test_site/pattern/test/sub_pat/default.meta [deleted file]
src/tests/test_metafile.rs
tests/files/expanded [new file with mode: 0644]
tests/files/test_site/pattern/base/default.meta [moved from src/tests/test_files/test_site/pattern/base/default.meta with 100% similarity]
tests/files/test_site/pattern/pat/default.meta [moved from src/tests/test_files/test_site/pattern/pat/default.meta with 100% similarity]
tests/files/test_site/pattern/pat/pattern.meta [moved from src/tests/test_files/test_site/pattern/pat/pattern.meta with 100% similarity]
tests/files/test_site/pattern/test/array.meta [new file with mode: 0644]
tests/files/test_site/pattern/test/blank/default.meta [moved from src/tests/test_files/test_site/pattern/test/blank/default.meta with 100% similarity]
tests/files/test_site/pattern/test/default/default.meta [new file with mode: 0644]
tests/files/test_site/pattern/test/new/default.meta [moved from src/tests/test_files/test_site/pattern/test/new/default.meta with 100% similarity]
tests/files/test_site/pattern/test/new/new.meta [moved from src/tests/test_files/test_site/pattern/test/new/new.meta with 100% similarity]
tests/files/test_site/pattern/test/overwrite/default.meta [moved from src/tests/test_files/test_site/pattern/test/overwrite/default.meta with 100% similarity]
tests/files/test_site/pattern/test/overwrite/overwrite.meta [moved from src/tests/test_files/test_site/pattern/test/overwrite/overwrite.meta with 100% similarity]
tests/files/test_site/pattern/test/parent/default.meta [moved from src/tests/test_files/test_site/pattern/test/parent/default.meta with 100% similarity]
tests/files/test_site/pattern/test/parent/parent.meta [moved from src/tests/test_files/test_site/pattern/test/parent/parent.meta with 100% similarity]
tests/files/test_site/pattern/test/pattern.meta [moved from src/tests/test_files/test_site/pattern/test/pattern.meta with 100% similarity]
tests/files/test_site/pattern/test/sub_pat/default.meta [moved from src/tests/test_files/test_site/pattern/test/default/default.meta with 100% similarity]
tests/files/test_site/pattern/test/sub_pat/sub_pattern.meta [new file with mode: 0644]
tests/files/test_site/source/expand.meta [moved from src/tests/test_files/test_site/source/expand.meta with 92% similarity]
tests/files/test_site/source/sub_dir/sub_source.meta [moved from src/tests/test_files/test_site/source/sub_dir/sub_source.meta with 100% similarity]
tests/files/test_site/source/test_source.meta [moved from src/tests/test_files/test_site/source/test_source.meta with 100% similarity]
tests/metafile_builder.rs [new file with mode: 0644]

index a3edb3e8112cf1f6a4fc8048437b998b2df9c404..f1f3c646ca2b50652125c282e2c64afe362d7a0e 100644 (file)
@@ -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<String> {
-    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
+}
index 57b89453ced53b21103c8ab2f0775316b31e6fc8..a6db5fdd3b7d6b744098880679dc8e70bd69c9eb 100644 (file)
@@ -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,
index 83f53c0be370a95d6c06b5ff894941019d1998e5..bacc2643e23d2c1e3f2758b19da13e3ee1239035 100644 (file)
@@ -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 (file)
index 32b6f03..0000000
+++ /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]: <p>GOOD</p><p>GOOD GOOD</p>
-test.sub_pat: <h1>SUBPATTERN</h1>
-test.default: <h1>DEFAULT</h1>
-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 (file)
index c8f73d7..0000000
+++ /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 (file)
index f49b816..0000000
+++ /dev/null
@@ -1 +0,0 @@
-GOOD
index c8263ac55d00ec7c7f073f7c6a9ccddbd3ffd1ae..144849c9ba5a742cd71a2bc98c32fda6787de0b0 100644 (file)
@@ -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<fn(dirs: &RootDirs) -> 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<fn(dirs: &RootDirs) -> 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 (file)
index 0000000..8e3f4dd
--- /dev/null
@@ -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]: 
+<p>GOOD</p>
+<p>GOOD GOOD</p>
+test.sub_pat:
+<h1>SUBPATTERN</h1>
+test.default:
+<h1>DEFAULT</h1>
+test.blank: 
+
+This comment should not be rendered: 
diff --git a/tests/files/test_site/pattern/test/array.meta b/tests/files/test_site/pattern/test/array.meta
new file mode 100644 (file)
index 0000000..c530977
--- /dev/null
@@ -0,0 +1 @@
+<p>@{arr}</p>
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 (file)
index 0000000..ada2226
--- /dev/null
@@ -0,0 +1 @@
+<h1>DEFAULT</h1>
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 (file)
index 0000000..5377fce
--- /dev/null
@@ -0,0 +1 @@
+<h1>SUBPATTERN</h1>
similarity index 92%
rename from src/tests/test_files/test_site/source/expand.meta
rename to tests/files/test_site/source/expand.meta
index 1a5f54db8623b8cfdbf9b6f8d98e8f92bbb941ed..3bfb4d606c4c6952a2b280924789b5d90bcce134 100644 (file)
@@ -7,7 +7,7 @@ ${
 @{
     arr1 = ['GOOD']
     arr2 = []
-    test.array = ["GOOD", "GOOD GOOD"]
+    test.arr = ["GOOD", "GOOD GOOD"]
 }
 
 &{
diff --git a/tests/metafile_builder.rs b/tests/metafile_builder.rs
new file mode 100644 (file)
index 0000000..262ee9e
--- /dev/null
@@ -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<RootDirs> {
+    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)
+}