Skip to content

Commit a861aa2

Browse files
committed
difftest: better io error reporting in config
1 parent 904b74f commit a861aa2

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

tests/difftests/types/src/config.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use serde::{Deserialize, Serialize};
23
use std::fs::File;
34
use std::io::Write;
@@ -11,8 +12,18 @@ pub struct Config {
1112

1213
impl Config {
1314
pub fn write_result<A: bytemuck::NoUninit>(&self, output: &[A]) -> anyhow::Result<()> {
14-
let mut f = File::create(&self.output_path)?;
15-
f.write_all(bytemuck::cast_slice(output))?;
15+
let mut f = File::create(&self.output_path).with_context(|| {
16+
format!(
17+
"failed to create output file '{}'",
18+
self.output_path.display()
19+
)
20+
})?;
21+
f.write_all(bytemuck::cast_slice(output)).with_context(|| {
22+
format!(
23+
"failed to write to output file '{}'",
24+
self.output_path.display()
25+
)
26+
})?;
1627
Ok(())
1728
}
1829
}
@@ -106,15 +117,28 @@ impl TestMetadata {
106117

107118
impl Config {
108119
pub fn from_path<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
109-
let content = fs::read_to_string(path)?;
110-
let config = serde_json::from_str(&content)?;
120+
let path = path.as_ref();
121+
let content = fs::read_to_string(path)
122+
.with_context(|| format!("Could not read file '{}'", path.display()))?;
123+
let config = serde_json::from_str(&content).with_context(|| {
124+
format!(
125+
"Could not parse json in file '{}':\n{content}",
126+
path.display()
127+
)
128+
})?;
111129
Ok(config)
112130
}
113131

114132
/// Write test metadata to the configured metadata path
115133
pub fn write_metadata(&self, metadata: &TestMetadata) -> anyhow::Result<()> {
116-
let metadata_json = serde_json::to_string(metadata)?;
117-
fs::write(&self.metadata_path, metadata_json)?;
134+
let metadata_json =
135+
serde_json::to_string(metadata).context("Could not serialize TestMetadata")?;
136+
fs::write(&self.metadata_path, metadata_json).with_context(|| {
137+
format!(
138+
"Could not write TestMetadata to file at '{}'",
139+
self.metadata_path.display()
140+
)
141+
})?;
118142
Ok(())
119143
}
120144
}

0 commit comments

Comments
 (0)