Skip to content

Commit 4945803

Browse files
authored
test: make path arguments more generic and flexible (#14979)
### What does this PR try to resolve? So we don't need to `p.to_str().unwrap()` and are able to pass different types for each argument ### How should we test and review this PR? No response.
2 parents 3447b11 + 0921264 commit 4945803

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl ProjectBuilder {
299299
}
300300

301301
/// Adds a symlink to a file to the project.
302-
pub fn symlink<T: AsRef<Path>>(mut self, dst: T, src: T) -> Self {
302+
pub fn symlink(mut self, dst: impl AsRef<Path>, src: impl AsRef<Path>) -> Self {
303303
self.symlinks.push(SymlinkBuilder::new(
304304
self.root.root().join(dst),
305305
self.root.root().join(src),
@@ -308,7 +308,7 @@ impl ProjectBuilder {
308308
}
309309

310310
/// Create a symlink to a directory
311-
pub fn symlink_dir<T: AsRef<Path>>(mut self, dst: T, src: T) -> Self {
311+
pub fn symlink_dir(mut self, dst: impl AsRef<Path>, src: impl AsRef<Path>) -> Self {
312312
self.symlinks.push(SymlinkBuilder::new_dir(
313313
self.root.root().join(dst),
314314
self.root.root().join(src),
@@ -368,7 +368,7 @@ impl ProjectBuilder {
368368

369369
impl Project {
370370
/// Copy the test project from a fixed state
371-
pub fn from_template(template_path: impl AsRef<std::path::Path>) -> Self {
371+
pub fn from_template(template_path: impl AsRef<Path>) -> Self {
372372
let root = paths::root();
373373
let project_root = root.join("case");
374374
snapbox::dir::copy_template(template_path.as_ref(), &project_root).unwrap();
@@ -459,7 +459,7 @@ impl Project {
459459
/// # let p = cargo_test_support::project().build();
460460
/// p.change_file("src/lib.rs", "fn new_fn() {}");
461461
/// ```
462-
pub fn change_file(&self, path: &str, body: &str) {
462+
pub fn change_file(&self, path: impl AsRef<Path>, body: &str) {
463463
FileBuilder::new(self.root().join(path), body, false).mk()
464464
}
465465

@@ -530,7 +530,7 @@ impl Project {
530530
}
531531

532532
/// Returns the contents of a path in the project root
533-
pub fn read_file(&self, path: &str) -> String {
533+
pub fn read_file(&self, path: impl AsRef<Path>) -> String {
534534
let full = self.root().join(path);
535535
fs::read_to_string(&full)
536536
.unwrap_or_else(|e| panic!("could not read file {}: {}", full.display(), e))
@@ -572,12 +572,12 @@ pub fn project() -> ProjectBuilder {
572572
}
573573

574574
/// Generates a project layout in given directory, see [`ProjectBuilder`]
575-
pub fn project_in(dir: &str) -> ProjectBuilder {
575+
pub fn project_in(dir: impl AsRef<Path>) -> ProjectBuilder {
576576
ProjectBuilder::new(paths::root().join(dir).join("foo"))
577577
}
578578

579579
/// Generates a project layout inside our fake home dir, see [`ProjectBuilder`]
580-
pub fn project_in_home(name: &str) -> ProjectBuilder {
580+
pub fn project_in_home(name: impl AsRef<Path>) -> ProjectBuilder {
581581
ProjectBuilder::new(paths::home().join(name))
582582
}
583583

tests/testsuite/binary_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn binary_name1() {
9191
let deps_path = p.bin("007bar").with_extension("d");
9292
assert!(deps_path.is_file(), "{:?}", bar_path);
9393

94-
let depinfo = p.read_file(deps_path.to_str().unwrap());
94+
let depinfo = p.read_file(&deps_path);
9595

9696
// Prepare what content we expect to be present in deps file.
9797
let deps_exp = format!(

tests/testsuite/dep_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn build_dep_info() {
2525

2626
assert!(depinfo_bin_path.is_file());
2727

28-
let depinfo = p.read_file(depinfo_bin_path.to_str().unwrap());
28+
let depinfo = p.read_file(depinfo_bin_path);
2929

3030
let bin_path = p.bin("foo");
3131
let src_path = p.root().join("src").join("foo.rs");
@@ -134,7 +134,7 @@ fn dep_path_inside_target_has_correct_path() {
134134

135135
assert!(depinfo_path.is_file(), "{:?}", depinfo_path);
136136

137-
let depinfo = p.read_file(depinfo_path.to_str().unwrap());
137+
let depinfo = p.read_file(depinfo_path);
138138

139139
let bin_path = p.bin("a");
140140
let target_debug_blah = Path::new("target").join("debug").join("blah");

0 commit comments

Comments
 (0)