From 0921264bc541426e96ca00587e9134ef302e00c0 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 24 Dec 2024 10:30:52 -0500 Subject: [PATCH] test: make path arguments more generic and flexible So we don't need to `p.to_str().unwrap()` and are able to pass different types for each argument --- crates/cargo-test-support/src/lib.rs | 14 +++++++------- tests/testsuite/binary_name.rs | 2 +- tests/testsuite/dep_info.rs | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index a36e61c3b80..f282b3d9bdd 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -299,7 +299,7 @@ impl ProjectBuilder { } /// Adds a symlink to a file to the project. - pub fn symlink>(mut self, dst: T, src: T) -> Self { + pub fn symlink(mut self, dst: impl AsRef, src: impl AsRef) -> Self { self.symlinks.push(SymlinkBuilder::new( self.root.root().join(dst), self.root.root().join(src), @@ -308,7 +308,7 @@ impl ProjectBuilder { } /// Create a symlink to a directory - pub fn symlink_dir>(mut self, dst: T, src: T) -> Self { + pub fn symlink_dir(mut self, dst: impl AsRef, src: impl AsRef) -> Self { self.symlinks.push(SymlinkBuilder::new_dir( self.root.root().join(dst), self.root.root().join(src), @@ -368,7 +368,7 @@ impl ProjectBuilder { impl Project { /// Copy the test project from a fixed state - pub fn from_template(template_path: impl AsRef) -> Self { + pub fn from_template(template_path: impl AsRef) -> Self { let root = paths::root(); let project_root = root.join("case"); snapbox::dir::copy_template(template_path.as_ref(), &project_root).unwrap(); @@ -459,7 +459,7 @@ impl Project { /// # let p = cargo_test_support::project().build(); /// p.change_file("src/lib.rs", "fn new_fn() {}"); /// ``` - pub fn change_file(&self, path: &str, body: &str) { + pub fn change_file(&self, path: impl AsRef, body: &str) { FileBuilder::new(self.root().join(path), body, false).mk() } @@ -530,7 +530,7 @@ impl Project { } /// Returns the contents of a path in the project root - pub fn read_file(&self, path: &str) -> String { + pub fn read_file(&self, path: impl AsRef) -> String { let full = self.root().join(path); fs::read_to_string(&full) .unwrap_or_else(|e| panic!("could not read file {}: {}", full.display(), e)) @@ -572,12 +572,12 @@ pub fn project() -> ProjectBuilder { } /// Generates a project layout in given directory, see [`ProjectBuilder`] -pub fn project_in(dir: &str) -> ProjectBuilder { +pub fn project_in(dir: impl AsRef) -> ProjectBuilder { ProjectBuilder::new(paths::root().join(dir).join("foo")) } /// Generates a project layout inside our fake home dir, see [`ProjectBuilder`] -pub fn project_in_home(name: &str) -> ProjectBuilder { +pub fn project_in_home(name: impl AsRef) -> ProjectBuilder { ProjectBuilder::new(paths::home().join(name)) } diff --git a/tests/testsuite/binary_name.rs b/tests/testsuite/binary_name.rs index 61569168c22..a26ba9882cf 100644 --- a/tests/testsuite/binary_name.rs +++ b/tests/testsuite/binary_name.rs @@ -91,7 +91,7 @@ fn binary_name1() { let deps_path = p.bin("007bar").with_extension("d"); assert!(deps_path.is_file(), "{:?}", bar_path); - let depinfo = p.read_file(deps_path.to_str().unwrap()); + let depinfo = p.read_file(&deps_path); // Prepare what content we expect to be present in deps file. let deps_exp = format!( diff --git a/tests/testsuite/dep_info.rs b/tests/testsuite/dep_info.rs index d29ca370caf..8104f62ba86 100644 --- a/tests/testsuite/dep_info.rs +++ b/tests/testsuite/dep_info.rs @@ -25,7 +25,7 @@ fn build_dep_info() { assert!(depinfo_bin_path.is_file()); - let depinfo = p.read_file(depinfo_bin_path.to_str().unwrap()); + let depinfo = p.read_file(depinfo_bin_path); let bin_path = p.bin("foo"); let src_path = p.root().join("src").join("foo.rs"); @@ -134,7 +134,7 @@ fn dep_path_inside_target_has_correct_path() { assert!(depinfo_path.is_file(), "{:?}", depinfo_path); - let depinfo = p.read_file(depinfo_path.to_str().unwrap()); + let depinfo = p.read_file(depinfo_path); let bin_path = p.bin("a"); let target_debug_blah = Path::new("target").join("debug").join("blah");