Skip to content

Fix path canonicalization in witx use statements #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions tools/witx/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ impl WitxIo for Filesystem {
}

pub struct MockFs {
map: HashMap<String, String>,
map: HashMap<PathBuf, String>,
}

impl MockFs {
pub fn new(strings: &[(&str, &str)]) -> Self {
MockFs {
map: strings
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.map(|(k, v)| (PathBuf::from(k), v.to_string()))
.collect(),
}
}
}

impl WitxIo for MockFs {
fn fgets(&self, path: &Path) -> Result<String, WitxError> {
if let Some(entry) = self.map.get(path.to_str().unwrap()) {
if let Some(entry) = self.map.get(path) {
Ok(entry.to_string())
} else {
Err(WitxError::Io(
Expand All @@ -78,7 +78,7 @@ impl WitxIo for MockFs {
}
}
fn fget_line(&self, path: &Path, line: usize) -> Result<String, WitxError> {
if let Some(entry) = self.map.get(path.to_str().unwrap()) {
if let Some(entry) = self.map.get(path) {
entry
.lines()
.skip(line - 1)
Expand Down
32 changes: 32 additions & 0 deletions tools/witx/src/toplevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn parse_file(
.map_err(WitxError::Validation)?;
}
TopLevelSyntax::Use(u) => {
let root = path.parent().unwrap_or(root);
parse_file(u.as_ref(), io, root, validator, definitions, parsed)?;
}
}
Expand Down Expand Up @@ -138,6 +139,37 @@ mod test {
);
}

#[test]
fn multi_use_with_layered_dirs() {
let doc = parse_witx_with(
&[Path::new("/root.witx")],
&MockFs::new(&[
("/root.witx", "(use \"subdir/child.witx\")"),
(
"/subdir/child.witx",
"(use \"sibling.witx\")\n(typename $b_float f64)",
),
("/subdir/sibling.witx", "(typename $c_int u32)"),
// This definition looks just like subdir/sibling.witx but
// defines c_int differently - this test shows it does Not get
// included by subdir/child.witx's use.
("/sibling.witx", "(typename $c_int u64)"),
]),
)
.expect("parse");

let b_float = doc.typename(&Id::new("b_float")).unwrap();
assert_eq!(**b_float.type_(), Type::Builtin(BuiltinType::F64));

let c_int = doc.typename(&Id::new("c_int")).unwrap();
assert_eq!(
**c_int.type_(),
Type::Builtin(BuiltinType::U32 {
lang_ptr_size: false
})
);
}

#[test]
fn use_not_found() {
match parse_witx_with(&[Path::new("/a")], &MockFs::new(&[("/a", "(use \"b\")")]))
Expand Down