Skip to content

Commit eb8d3e2

Browse files
committed
simplify join_paths error message and add test
1 parent f1fed1b commit eb8d3e2

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@ use tempfile::Builder as TempFileBuilder;
1919
/// error message.
2020
pub fn join_paths<T: AsRef<OsStr>>(paths: &[T], env: &str) -> Result<OsString> {
2121
env::join_paths(paths.iter()).with_context(|| {
22-
let paths = paths.iter()
23-
.map(|s| s.as_ref().to_string_lossy())
24-
.map(|s| format!(" \"{s}\""))
25-
.collect::<Vec<String>>()
26-
.join(",\n");
27-
28-
format!(
29-
"failed to join paths from `${env}` together\n\
30-
If you set `${env}` manually, check if it contains an unterminated quote character \
31-
or path separators (usually `:` or `;`). Please avoid using them. \
32-
Otherwise, check if any of paths listed below contain one of those characters:\n{paths}"
33-
)
22+
let mut message = format!(
23+
"failed to join paths from `${env}` together\n\n\
24+
Check if any of path segments listed below contain an \
25+
unterminated quote character or path separator:"
26+
);
27+
for path in paths {
28+
use std::fmt::Write;
29+
write!(&mut message, "\n {:?}", Path::new(path)).unwrap();
30+
}
31+
32+
message
3433
})
3534
}
3635

@@ -745,3 +744,44 @@ fn exclude_from_time_machine(path: &Path) {
745744
// Errors are ignored, since it's an optional feature and failure
746745
// doesn't prevent Cargo from working
747746
}
747+
748+
#[cfg(test)]
749+
mod tests {
750+
use super::join_paths;
751+
752+
#[test]
753+
fn join_paths_lists_paths_on_error() {
754+
let valid_paths = vec!["/testing/one", "/testing/two"];
755+
// does not fail on valid input
756+
let _joined = join_paths(&valid_paths, "TESTING1").unwrap();
757+
758+
#[cfg(unix)]
759+
{
760+
let invalid_paths = vec!["/testing/one", "/testing/t:wo/three"];
761+
let err = join_paths(&invalid_paths, "TESTING2").unwrap_err();
762+
assert_eq!(
763+
err.to_string(),
764+
"failed to join paths from `$TESTING2` together\n\n\
765+
Check if any of path segments listed below contain an \
766+
unterminated quote character or path separator:\
767+
\n \"/testing/one\"\
768+
\n \"/testing/t:wo/three\"\
769+
"
770+
);
771+
}
772+
#[cfg(windows)]
773+
{
774+
let invalid_paths = vec!["/testing/one", "/testing/t\"wo/three"];
775+
let err = join_paths(&invalid_paths, "TESTING2").unwrap_err();
776+
assert_eq!(
777+
err.to_string(),
778+
"failed to join paths from `$TESTING2` together\n\n\
779+
Check if any of path segments listed below contain an \
780+
unterminated quote character or path separator:\
781+
\n \"/testing/one\"\
782+
\n \"/testing/t\\\"wo/three\"\
783+
"
784+
);
785+
}
786+
}
787+
}

0 commit comments

Comments
 (0)