rust: More usage of indoc

There's a lot more of this in the code, just decided to do a little
bit more in treefile.rs mostly.
This commit is contained in:
Colin Walters 2021-03-29 17:50:06 +00:00
parent dedf2a2653
commit 4298fd4ddd
2 changed files with 115 additions and 137 deletions

View File

@ -10,6 +10,7 @@ use anyhow::{bail, Result};
use glib::translate::*; use glib::translate::*;
use glib::GString; use glib::GString;
use glib::KeyFile; use glib::KeyFile;
use indoc::indoc;
use std::result::Result as StdResult; use std::result::Result as StdResult;
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
@ -355,15 +356,13 @@ refspec=foo:bar/x86_64/baz
o.remove_transient_state(); o.remove_transient_state();
o.set_rojig_version(Some("42")); o.set_rojig_version(Some("42"));
let mut o = Origin::new_from_str( let mut o = Origin::new_from_str(indoc! {"
r#" [origin]
[origin] baserefspec=fedora/33/x86_64/silverblue
baserefspec=fedora/33/x86_64/silverblue
[packages] [packages]
requested=virt-manager;libvirt;pcsc-lite-ccid requested=virt-manager;libvirt;pcsc-lite-ccid
"#, "})?;
)?;
assert_eq!(o.cache.refspec.kind, RefspecType::Ostree); assert_eq!(o.cache.refspec.kind, RefspecType::Ostree);
assert_eq!(o.cache.refspec.value, "fedora/33/x86_64/silverblue"); assert_eq!(o.cache.refspec.value, "fedora/33/x86_64/silverblue");
assert!(o.may_require_local_assembly()); assert!(o.may_require_local_assembly());

View File

@ -1215,28 +1215,28 @@ mod tests {
static ARCH_X86_64: &str = "x86_64"; static ARCH_X86_64: &str = "x86_64";
static VALID_PRELUDE: &str = indoc! {r#" static VALID_PRELUDE: &str = indoc! {r#"
ref: "exampleos/x86_64/blah" ref: "exampleos/x86_64/blah"
packages: packages:
- foo bar - foo bar
- baz - baz
- corge 'quuz >= 1.0' - corge 'quuz >= 1.0'
packages-x86_64: packages-x86_64:
- grub2 grub2-tools - grub2 grub2-tools
packages-s390x: packages-s390x:
- zipl - zipl
"#}; "#};
// This one has "comments" (hence unknown keys) // This one has "comments" (hence unknown keys)
static VALID_PRELUDE_JS: &str = r###" static VALID_PRELUDE_JS: &str = indoc! {r#"
{ {
"ref": "exampleos/${basearch}/blah", "ref": "exampleos/${basearch}/blah",
"comment-packages": "We want baz to enable frobnication", "comment-packages": "We want baz to enable frobnication",
"packages": ["foo", "bar", "baz"], "packages": ["foo", "bar", "baz"],
"packages-x86_64": ["grub2", "grub2-tools"], "packages-x86_64": ["grub2", "grub2-tools"],
"comment-packages-s390x": "Note that s390x uses its own bootloader", "comment-packages-s390x": "Note that s390x uses its own bootloader",
"packages-s390x": ["zipl"] "packages-s390x": ["zipl"]
} }
"###; "#};
#[test] #[test]
fn basic_valid() { fn basic_valid() {
@ -1251,18 +1251,16 @@ mod tests {
#[test] #[test]
fn basic_valid_add_remove_files() { fn basic_valid_add_remove_files() {
let mut buf = VALID_PRELUDE.to_string(); let mut buf = VALID_PRELUDE.to_string();
buf.push_str( buf.push_str(indoc! {r#"
r###" add-files:
add-files: - - foo
- - foo - /usr/bin/foo
- /usr/bin/foo - - baz
- - baz - /usr/bin/blah
- /usr/bin/blah remove-files:
remove-files: - foo
- foo - bar
- bar "#});
"###,
);
let buf = buf.as_bytes(); let buf = buf.as_bytes();
let mut input = io::BufReader::new(buf); let mut input = io::BufReader::new(buf);
let treefile = let treefile =
@ -1313,12 +1311,12 @@ remove-files:
#[test] #[test]
fn basic_valid_releasever() { fn basic_valid_releasever() {
let buf = r###" let buf = indoc! {r#"
ref: "exampleos/${basearch}/${releasever}" ref: "exampleos/${basearch}/${releasever}"
releasever: 30 releasever: 30
automatic-version-prefix: ${releasever} automatic-version-prefix: ${releasever}
mutate-os-release: ${releasever} mutate-os-release: ${releasever}
"###; "#};
let mut input = io::BufReader::new(buf.as_bytes()); let mut input = io::BufReader::new(buf.as_bytes());
let mut treefile = let mut treefile =
treefile_parse_stream(utils::InputFormat::YAML, &mut input, Some(ARCH_X86_64)).unwrap(); treefile_parse_stream(utils::InputFormat::YAML, &mut input, Some(ARCH_X86_64)).unwrap();
@ -1339,15 +1337,13 @@ mutate-os-release: ${releasever}
#[test] #[test]
fn basic_valid_legacy() { fn basic_valid_legacy() {
let treefile = append_and_parse( let treefile = append_and_parse(indoc! {"
" gpg_key: foo
gpg_key: foo boot_location: new
boot_location: new default_target: bar
default_target: bar automatic_version_prefix: baz
automatic_version_prefix: baz rpmdb: sqlite
rpmdb: sqlite "});
",
);
assert!(treefile.gpg_key.unwrap() == "foo"); assert!(treefile.gpg_key.unwrap() == "foo");
assert!(treefile.boot_location.unwrap() == BootLocation::New); assert!(treefile.boot_location.unwrap() == BootLocation::New);
assert!(treefile.default_target.unwrap() == "bar"); assert!(treefile.default_target.unwrap() == "bar");
@ -1357,14 +1353,12 @@ rpmdb: sqlite
#[test] #[test]
fn basic_valid_legacy_new() { fn basic_valid_legacy_new() {
let treefile = append_and_parse( let treefile = append_and_parse(indoc! {"
" gpg-key: foo
gpg-key: foo boot-location: new
boot-location: new default-target: bar
default-target: bar automatic-version-prefix: baz
automatic-version-prefix: baz "});
",
);
assert!(treefile.gpg_key.unwrap() == "foo"); assert!(treefile.gpg_key.unwrap() == "foo");
assert!(treefile.boot_location.unwrap() == BootLocation::New); assert!(treefile.boot_location.unwrap() == BootLocation::New);
assert!(treefile.default_target.unwrap() == "bar"); assert!(treefile.default_target.unwrap() == "bar");
@ -1373,58 +1367,47 @@ automatic-version-prefix: baz
#[test] #[test]
fn basic_invalid_legacy_both() { fn basic_invalid_legacy_both() {
test_invalid( test_invalid(indoc! {"
" gpg-key: foo
gpg-key: foo gpg_key: bar
gpg_key: bar "});
", test_invalid(indoc! {"
); boot-location: new
test_invalid( boot_location: both
" "});
boot-location: new test_invalid(indoc! {"
boot_location: both default-target: foo
", default_target: bar
); "});
test_invalid( test_invalid(indoc! {"
" automatic-version-prefix: foo
default-target: foo automatic_version_prefix: bar
default_target: bar "});
",
);
test_invalid(
"
automatic-version-prefix: foo
automatic_version_prefix: bar
",
);
} }
#[test] #[test]
fn test_invalid_install_langs() { fn test_invalid_install_langs() {
test_invalid( test_invalid(indoc! {r#"
r###"install_langs: install_langs:
- "klingon" - "klingon"
- "esperanto" - "esperanto"
"###, "#});
);
} }
#[test] #[test]
fn test_invalid_arch_packages_type() { fn test_invalid_arch_packages_type() {
test_invalid( test_invalid(indoc! {"
r###"packages-hal9000: true packages-hal9000: true
"###, "});
);
} }
#[test] #[test]
fn test_invalid_arch_packages_array_type() { fn test_invalid_arch_packages_array_type() {
test_invalid( test_invalid(indoc! {"
r###"packages-hal9000: packages-hal9000:
- 12 - 12
- 34 - 34
"###, "});
);
} }
fn new_test_treefile<'a, 'b>( fn new_test_treefile<'a, 'b>(
@ -1450,12 +1433,12 @@ automatic_version_prefix: bar
assert!(tf.parsed.machineid_compat.is_none()); assert!(tf.parsed.machineid_compat.is_none());
} }
const ROJIG_YAML: &'static str = r###" const ROJIG_YAML: &'static str = indoc! {r#"
rojig: rojig:
name: "exampleos" name: "exampleos"
license: "MIT" license: "MIT"
summary: "ExampleOS rojig base image" summary: "ExampleOS rojig base image"
"###; "#};
#[test] #[test]
fn test_treefile_new_rojig() { fn test_treefile_new_rojig() {
@ -1477,17 +1460,15 @@ rojig:
workdir_d.write_file_contents( workdir_d.write_file_contents(
"foo.yaml", "foo.yaml",
0o644, 0o644,
r#" indoc! {"
packages: packages:
- fooinclude - fooinclude
"#, "},
)?; )?;
let mut buf = VALID_PRELUDE.to_string(); let mut buf = VALID_PRELUDE.to_string();
buf.push_str( buf.push_str(indoc! {"
r#" include: foo.yaml
include: foo.yaml "});
"#,
);
let tf = new_test_treefile(workdir.path(), buf.as_str(), None)?; let tf = new_test_treefile(workdir.path(), buf.as_str(), None)?;
assert!(tf.parsed.packages.unwrap().len() == 6); assert!(tf.parsed.packages.unwrap().len() == 6);
Ok(()) Ok(())
@ -1528,27 +1509,25 @@ arch-include:
#[test] #[test]
fn test_treefile_merge() { fn test_treefile_merge() {
let basearch = Some(ARCH_X86_64); let basearch = Some(ARCH_X86_64);
let mut base = append_and_parse( let mut base = append_and_parse(indoc! {r#"
r###" add-commit-metadata:
add-commit-metadata: my-first-key: "please don't override me"
my-first-key: "please don't override me" my-second-key: "override me"
my-second-key: "override me" etc-group-members:
etc-group-members: - sudo
- sudo "#});
"###,
);
let mut mid_input = io::BufReader::new( let mut mid_input = io::BufReader::new(
r###" indoc! {r#"
packages: packages:
- some layered packages - some layered packages
add-commit-metadata: add-commit-metadata:
my-second-key: "something better" my-second-key: "something better"
my-third-key: 1000 my-third-key: 1000
my-fourth-key: my-fourth-key:
nested: table nested: table
etc-group-members: etc-group-members:
- docker - docker
"### "#}
.as_bytes(), .as_bytes(),
); );
let mut mid = let mut mid =