Skip to content

Commit c7d365b

Browse files
committed
add error contexts to Go::compile
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 01fc8d1 commit c7d365b

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

crates/test/src/go.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Compile, LanguageMethods, Runner, Verify};
2-
use anyhow::Result;
2+
use anyhow::{Context as _, Result};
33
use std::env;
44
use std::fs;
55
use std::path::{Path, PathBuf};
@@ -63,14 +63,15 @@ impl LanguageMethods for Go {
6363
// convention. For example, if the filename is `test.go`, then we'll
6464
// also include `${prefix}+test.go` for any value of `${prefix}`.
6565
for path in all_paths(&compile.component.path)? {
66-
let test = fs::read_to_string(&path)?;
66+
let test = fs::read_to_string(&path)
67+
.with_context(|| format!("unable to read `{}`", path.display()))?;
6768
let package_name = package_name(&test);
6869
let package_dir = compile.bindings_dir.join(package_name);
69-
fs::create_dir_all(&package_dir)?;
70-
fs::write(
71-
&package_dir.join(path.file_name().unwrap()),
72-
test.as_bytes(),
73-
)?;
70+
fs::create_dir_all(&package_dir)
71+
.with_context(|| format!("unable to create `{}`", package_dir.display()))?;
72+
let output = &package_dir.join(path.file_name().unwrap());
73+
fs::write(output, test.as_bytes())
74+
.with_context(|| format!("unable to write `{}`", output.display()))?;
7475
}
7576

7677
runner.run_command(
@@ -124,7 +125,11 @@ fn all_paths(path: &Path) -> Result<Vec<PathBuf>> {
124125
.and_then(|name| name.strip_suffix(suffix))
125126
{
126127
let suffix = &format!("+{name}{suffix}");
127-
for entry in path.parent().unwrap().read_dir()? {
128+
let parent = path.parent().unwrap();
129+
for entry in parent
130+
.read_dir()
131+
.with_context(|| format!("unable to read dir `{}`", parent.display()))?
132+
{
128133
let entry = entry?;
129134
if entry
130135
.file_name()

0 commit comments

Comments
 (0)