Skip to content

Commit af3a054

Browse files
committed
test: add test for metadata print
1 parent ab7163e commit af3a054

File tree

6 files changed

+153
-30
lines changed

6 files changed

+153
-30
lines changed

src/lib.rs

Lines changed: 121 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ mod tests {
654654
use sealed_test::prelude::*;
655655
use speculoos::prelude::*;
656656
use std::ffi::OsStr;
657-
use std::io::BufWriter;
658657
use std::{env, fs};
659658

660659
fn setup(dotfiles: &str) {
@@ -751,33 +750,6 @@ mod tests {
751750
Ok(())
752751
}
753752

754-
#[sealed_test(files = [ "tests/dotfiles_vars" ], before = setup("dotfiles_vars"))]
755-
fn should_print_metadata() -> Result<()> {
756-
let bombadil = Bombadil::from_settings(NoGpg)?;
757-
let mut content = vec![];
758-
let mut writer = BufWriter::new(&mut content);
759-
760-
// Act
761-
bombadil.print_metadata(MetadataType::Vars, &mut writer, false)?;
762-
let result = String::from_utf8(writer.get_ref().to_vec())?;
763-
let result = result.as_str();
764-
765-
// Assert
766-
assert_eq!(
767-
result,
768-
indoc! {
769-
r##"
770-
{
771-
"red": "#FF0000",
772-
"black": "#000000",
773-
"green": "#008000"
774-
}"##
775-
}
776-
);
777-
778-
Ok(())
779-
}
780-
781753
#[sealed_test(files = [ "tests/dotfiles_with_nested_dir" ], before = setup("dotfiles_with_nested_dir"))]
782754
fn should_get_auto_ignored_files() -> Result<()> {
783755
let bombadil = Bombadil::from_settings(NoGpg)?;
@@ -851,4 +823,125 @@ mod tests {
851823

852824
Ok(())
853825
}
826+
827+
mod metadata {
828+
use super::*;
829+
use crate::Mode::NoGpg;
830+
use indoc::indoc;
831+
use pretty_assertions::assert_eq;
832+
use std::io::BufWriter;
833+
834+
impl Bombadil {
835+
fn print_metadata_to_string(&self, data: MetadataType) -> Result<String> {
836+
let mut content = vec![];
837+
let mut writer = BufWriter::new(&mut content);
838+
self.print_metadata(data, &mut writer, false)?;
839+
let result = String::from_utf8(writer.get_ref().to_vec())?;
840+
let result = result;
841+
Ok(result)
842+
}
843+
}
844+
845+
#[sealed_test(files = [ "tests/dotfiles_full" ], before = setup("dotfiles_full"))]
846+
fn should_print_vars_metadata() -> Result<()> {
847+
let bombadil = Bombadil::from_settings(NoGpg)?;
848+
849+
// Act
850+
let result = bombadil.print_metadata_to_string(MetadataType::Vars)?;
851+
852+
// Assert
853+
assert_eq!(
854+
result,
855+
indoc! {
856+
r##"
857+
{
858+
"red": "#FF0000",
859+
"black": "#000000",
860+
"green": "#008000"
861+
}"##
862+
}
863+
);
864+
865+
Ok(())
866+
}
867+
868+
#[sealed_test(files = [ "tests/dotfiles_full" ], before = setup("dotfiles_full"))]
869+
fn should_print_vars_metadata_with_profile() -> Result<()> {
870+
let mut bombadil = Bombadil::from_settings(NoGpg)?;
871+
bombadil.enable_profiles(vec!["one"])?;
872+
873+
// Act
874+
let result = bombadil.print_metadata_to_string(MetadataType::Vars)?;
875+
876+
// Assert
877+
assert_eq!(
878+
result,
879+
indoc! {
880+
r##"
881+
{
882+
"red": "#FF0000",
883+
"black": "#000000",
884+
"green": "#008000",
885+
"yellow": "#f0f722"
886+
}"##
887+
}
888+
);
889+
890+
Ok(())
891+
}
892+
893+
#[sealed_test(files = [ "tests/dotfiles_full" ], before = setup("dotfiles_full"))]
894+
fn should_print_profiles() -> Result<()> {
895+
let bombadil = Bombadil::from_settings(NoGpg)?;
896+
897+
// Act
898+
let result = bombadil.print_metadata_to_string(MetadataType::Profiles)?;
899+
900+
// Assert
901+
assert_eq!(result, "default\none");
902+
903+
Ok(())
904+
}
905+
906+
#[sealed_test(files = [ "tests/dotfiles_full" ], before = setup("dotfiles_full"))]
907+
fn should_print_post_hooks() -> Result<()> {
908+
let bombadil = Bombadil::from_settings(NoGpg)?;
909+
910+
// Act
911+
let result = bombadil.print_metadata_to_string(MetadataType::PostHooks)?;
912+
913+
// Assert
914+
assert_eq!(result, "echo posthooks");
915+
916+
Ok(())
917+
}
918+
919+
#[sealed_test(files = [ "tests/dotfiles_full" ], before = setup("dotfiles_full"))]
920+
fn should_print_pre_hooks() -> Result<()> {
921+
let bombadil = Bombadil::from_settings(NoGpg)?;
922+
923+
// Act
924+
let result = bombadil.print_metadata_to_string(MetadataType::PreHooks)?;
925+
926+
// Assert
927+
assert_eq!(result, "echo prehooks\necho another_hook");
928+
929+
Ok(())
930+
}
931+
932+
#[sealed_test(files = [ "tests/dotfiles_full" ], before = setup("dotfiles_full"))]
933+
fn should_print_dots_with_profile() -> Result<()> {
934+
let mut bombadil = Bombadil::from_settings(NoGpg)?;
935+
bombadil.enable_profiles(vec!["one"])?;
936+
937+
// Act
938+
let result = bombadil.print_metadata_to_string(MetadataType::Dots)?;
939+
let lines: Vec<&str> = result.lines().collect();
940+
941+
// Assert
942+
assert_that!(lines).has_length(2);
943+
944+
Ok(())
945+
}
946+
}
854947
}

src/templating.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tera::{Context, Map, Tera, Value};
1515
#[serde(transparent)]
1616
pub struct Variables {
1717
/// holds the values defined in template.toml
18-
inner: Value,
18+
pub(crate) inner: Value,
1919
}
2020

2121
impl Variables {
@@ -88,7 +88,8 @@ impl Variables {
8888
}
8989

9090
/// Deserialize a toml file struct Variables
91-
pub(crate) fn from_path(path: &Path) -> Result<Self> {
91+
pub(crate) fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
92+
let path = path.as_ref();
9293
let file = File::open(path);
9394

9495
if let Err(err) = file {

tests/dotfiles_full/bombadil.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
dotfiles_dir = "dotfiles_full"
2+
3+
4+
5+
[settings]
6+
vars = [ "vars.toml" ]
7+
prehooks = [
8+
"echo prehooks",
9+
"echo another_hook"
10+
]
11+
posthooks = [ "echo posthooks" ]
12+
13+
[settings.dots]
14+
css = { source = "template.css", target = ".config/template.css" }
15+
16+
[profiles.one]
17+
vars = [ "profile_vars.toml" ]
18+
19+
[profiles.one.dots]
20+
css = { target = ".config/template_with_yellow.css" }
21+
another = { source = "template.css", target = ".config/template_again.css" }
22+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yellow = "#f0f722"

tests/dotfiles_full/template.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.class {
2+
color: {{red}}
3+
}

tests/dotfiles_full/vars.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
red = "#FF0000"
2+
black = "#000000"
3+
green = "#008000"

0 commit comments

Comments
 (0)