Skip to content

Commit

Permalink
✅ Add a few more test cases for <include>
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Nov 17, 2024
1 parent 44bb379 commit f204c7d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ impl Config {
}

pub fn read_file(file_path: impl AsRef<Path>) -> Result<Self> {
let doc = Document::read_file(file_path)?;
// TODO: error message should contain file path to missing `<include>`
let doc = Document::read_file(&file_path)?;
Self::try_from(doc)
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/config/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl FromStr for Document {
impl Document {
pub fn read_file(file_path: impl AsRef<Path>) -> Result<Document> {
let text = read_to_string(file_path.as_ref())?;

let mut doc = Document::from_str(&text)?;
doc.file_path = Some(file_path.as_ref().to_path_buf());
doc.resolve_includes()
Expand Down Expand Up @@ -263,6 +264,12 @@ fn resolve_include_path(
return p.canonicalize().map_err(Error::msg);
}

error!(
"resolve_include_path: {} {}",
&base_path.as_ref().display(),
&include_path.as_ref().display()
);

base_path
.as_ref()
.join(p)
Expand Down
18 changes: 16 additions & 2 deletions tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use busd::config::{Access, Config, Name, Operation, OwnOperation, Policy};

#[test]
fn config_read_file_with_includes_ok() {
let got = Config::read_file("./tests/fixture.conf")
.expect("should read and parse ./tests/fixture.conf");
let got =
Config::read_file("./tests/fixtures/valid.conf").expect("should read and parse XML input");

assert_eq!(
got,
Expand Down Expand Up @@ -49,3 +49,17 @@ fn config_read_file_with_includes_ok() {
}
);
}

#[should_panic]
#[test]
fn config_read_file_with_missing_include_err() {
Config::read_file("./tests/fixtures/missing_include.conf")
.expect("should read and parse XML input");
}

#[should_panic]
#[test]
fn config_read_file_with_transitive_missing_include_err() {
Config::read_file("./tests/fixtures/transitive_missing_include.conf")
.expect("should read and parse XML input");
}
5 changes: 5 additions & 0 deletions tests/fixtures/missing_include.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<include>./missing.conf</include>
</busconfig>
5 changes: 5 additions & 0 deletions tests/fixtures/transitive_missing_include.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<include>./missing_include.conf</include>
</busconfig>
4 changes: 2 additions & 2 deletions tests/fixture.conf → tests/fixtures/valid.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<allow own="*"/>
<deny own="*"/>
</policy>
<include>./fixture_included.conf</include>
<include ignore_missing="yes">./fixture_missing.conf</include>
<include>./valid_included.conf</include>
<include ignore_missing="yes">./valid_missing.conf</include>
</busconfig>
File renamed without changes.

0 comments on commit f204c7d

Please sign in to comment.