Skip to content

Commit

Permalink
add write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miller-time committed Jun 29, 2024
1 parent 46c2ffb commit 8e5210b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/write-tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::error::Error;

use hq_rs::{parser::Field, write};

#[test]
fn attr() -> Result<(), Box<dyn Error>> {
// filter '.version'
let fields = vec![Field::new("version")];
// hcl:
// version = "test"
let mut body = utilities::edit_hcl("version = \"test\"")?;

let value: hcl_edit::expr::Expression = "\"new_value\"".parse()?;

write(fields, &mut body, &value)?;

assert_eq!("version = \"new_value\"", body.to_string());

Ok(())
}

#[test]
fn block_attr() -> Result<(), Box<dyn Error>> {
// filter '.options.enabled'
let fields = vec![Field::new("options"), Field::new("enabled")];
// hcl:
// options { enabled = false }
let mut body = utilities::edit_hcl("options { enabled = false }")?;

let value: hcl_edit::expr::Expression = "true".parse()?;

write(fields, &mut body, &value)?;

assert_eq!("options { enabled = true }", body.to_string());

Ok(())
}

#[test]
fn labeled_block_attr() -> Result<(), Box<dyn Error>> {
// filter '.module[label="cool-module"].version'
let fields = vec![
Field::labeled("module", &["cool-module"]),
Field::new("version"),
];
// hcl:
// module "cool-module" { version = "1.0" }
let mut body = utilities::edit_hcl("module \"cool-module\" { version = \"1.0\" }")?;

let value: hcl_edit::expr::Expression = "\"2.0\"".parse()?;

write(fields, &mut body, &value)?;

assert_eq!(
"module \"cool-module\" { version = \"2.0\" }",
body.to_string()
);

Ok(())
}
1 change: 1 addition & 0 deletions utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pulish = false

[dependencies]
hcl-rs = "0.17"
hcl-edit = "0.8"
5 changes: 5 additions & 0 deletions utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ pub fn read_test_hcl() -> Result<hcl::Body, Box<dyn Error>> {
let body: hcl::Body = hcl::from_str(&contents)?;
Ok(body)
}

pub fn edit_hcl(contents: &str) -> Result<hcl_edit::structure::Body, Box<dyn Error>> {
let body: hcl_edit::structure::Body = contents.parse()?;
Ok(body)
}

0 comments on commit 8e5210b

Please sign in to comment.