Skip to content

Commit f10c069

Browse files
committed
Auto merge of #14266 - epage:path2url, r=weihanglo
fix(test): Move path2url to CargoPathExt::to_url ### What does this PR try to resolve? This is a small step, like #14243, to improve the clarity of `cargo-test-support`s API. Overall, I'm trying to make it more obvious on https://docs.rs/cargo-test-support/latest/cargo_test_support/ which items to reach for when. I figured this is one that could be demoted to `paths` When doing so, I noticed `CargoPathExt`. I figured if we had any extension traits for `Path`, then this is a reasonable one to add. ### How should we test and review this PR? ### Additional information
2 parents 61424d6 + d17322d commit f10c069

37 files changed

+64
-54
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use some of the helper functions in this file to interact with the repository.
3838
3939
*/
4040

41-
use crate::{path2url, project, Project, ProjectBuilder, SymlinkBuilder};
41+
use crate::{paths::CargoPathExt, project, Project, ProjectBuilder, SymlinkBuilder};
4242
use std::fs;
4343
use std::path::{Path, PathBuf};
4444
use std::sync::Once;
@@ -118,7 +118,7 @@ impl Repository {
118118
}
119119

120120
pub fn url(&self) -> Url {
121-
path2url(self.0.workdir().unwrap().to_path_buf())
121+
self.0.workdir().unwrap().to_url()
122122
}
123123

124124
pub fn revparse_head(&self) -> String {

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub mod tools;
7474

7575
pub mod prelude {
7676
pub use crate::cargo_test;
77+
pub use crate::paths::CargoPathExt;
7778
pub use crate::ArgLine;
7879
pub use crate::CargoCommand;
7980
pub use crate::ChannelChanger;
@@ -339,7 +340,8 @@ impl Project {
339340

340341
/// File url for root, ex: `file:///path/to/cargo/target/cit/t0/foo`
341342
pub fn url(&self) -> Url {
342-
path2url(self.root())
343+
use paths::CargoPathExt;
344+
self.root().to_url()
343345
}
344346

345347
/// Path to an example built as a library.
@@ -1184,10 +1186,6 @@ pub fn basic_lib_manifest(name: &str) -> String {
11841186
)
11851187
}
11861188

1187-
pub fn path2url<P: AsRef<Path>>(p: P) -> Url {
1188-
Url::from_file_path(p).ok().unwrap()
1189-
}
1190-
11911189
struct RustcInfo {
11921190
verbose_version: String,
11931191
host: String,

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub fn home() -> PathBuf {
111111
}
112112

113113
pub trait CargoPathExt {
114+
fn to_url(&self) -> url::Url;
115+
114116
fn rm_rf(&self);
115117
fn mkdir_p(&self);
116118

@@ -132,6 +134,10 @@ pub trait CargoPathExt {
132134
}
133135

134136
impl CargoPathExt for Path {
137+
fn to_url(&self) -> url::Url {
138+
url::Url::from_file_path(self).ok().unwrap()
139+
}
140+
135141
fn rm_rf(&self) {
136142
let meta = match self.symlink_metadata() {
137143
Ok(meta) => meta,
@@ -211,6 +217,30 @@ impl CargoPathExt for Path {
211217
}
212218
}
213219

220+
impl CargoPathExt for PathBuf {
221+
fn to_url(&self) -> url::Url {
222+
self.as_path().to_url()
223+
}
224+
225+
fn rm_rf(&self) {
226+
self.as_path().rm_rf()
227+
}
228+
fn mkdir_p(&self) {
229+
self.as_path().mkdir_p()
230+
}
231+
232+
fn ls_r(&self) -> Vec<PathBuf> {
233+
self.as_path().ls_r()
234+
}
235+
236+
fn move_in_time<F>(&self, travel_amount: F)
237+
where
238+
F: Fn(i64, u32) -> (i64, u32),
239+
{
240+
self.as_path().move_in_time(travel_amount)
241+
}
242+
}
243+
214244
fn do_op<F>(path: &Path, desc: &str, mut f: F)
215245
where
216246
F: FnMut(&Path) -> io::Result<()>,

tests/testsuite/bench.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Tests for the `cargo bench` command.
22
3-
use cargo_test_support::paths::CargoPathExt;
43
use cargo_test_support::prelude::*;
54
use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project, str};
65

tests/testsuite/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use cargo::{
1212
GlobalContext,
1313
};
1414
use cargo_test_support::compare::assert_e2e;
15-
use cargo_test_support::paths::{root, CargoPathExt};
15+
use cargo_test_support::paths::root;
1616
use cargo_test_support::prelude::*;
1717
use cargo_test_support::registry::Package;
1818
use cargo_test_support::str;

tests/testsuite/build_script.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::thread;
77

88
use cargo_test_support::compare::assert_e2e;
99
use cargo_test_support::install::cargo_home;
10-
use cargo_test_support::paths::CargoPathExt;
1110
use cargo_test_support::prelude::*;
1211
use cargo_test_support::registry::Package;
1312
use cargo_test_support::str;

tests/testsuite/cache_lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::thread::JoinHandle;
44

55
use cargo::util::cache_lock::{CacheLockMode, CacheLocker};
6-
use cargo_test_support::paths::{self, CargoPathExt};
6+
use cargo_test_support::paths;
77
use cargo_test_support::prelude::*;
88
use cargo_test_support::{retry, thread_wait_timeout, threaded_timeout};
99

tests/testsuite/cargo_command.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::process::Stdio;
88
use std::str;
99

1010
use cargo_test_support::basic_manifest;
11-
use cargo_test_support::paths::CargoPathExt;
1211
use cargo_test_support::prelude::*;
1312
use cargo_test_support::registry::Package;
1413
use cargo_test_support::str;

tests/testsuite/check.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fmt::{self, Write};
44

55
use cargo_test_support::compare::assert_e2e;
66
use cargo_test_support::install::exe;
7-
use cargo_test_support::paths::CargoPathExt;
87
use cargo_test_support::prelude::*;
98
use cargo_test_support::registry::Package;
109
use cargo_test_support::str;

tests/testsuite/clean.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Tests for the `cargo clean` command.
22
3-
use cargo_test_support::paths::CargoPathExt;
43
use cargo_test_support::prelude::*;
54
use cargo_test_support::registry::Package;
65
use cargo_test_support::str;

0 commit comments

Comments
 (0)