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;

tests/testsuite/dep_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::Path;
66
use std::str;
77

88
use cargo_test_support::compare::assert_e2e;
9-
use cargo_test_support::paths::{self, CargoPathExt};
9+
use cargo_test_support::paths;
1010
use cargo_test_support::prelude::*;
1111
use cargo_test_support::registry::Package;
1212
use cargo_test_support::str;

tests/testsuite/doc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs;
44
use std::str;
55

66
use cargo::core::compiler::RustDocFingerprint;
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/features.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Tests for `[features]` table.
22
3-
use cargo_test_support::paths::CargoPathExt;
43
use cargo_test_support::prelude::*;
54
use cargo_test_support::registry::{Dependency, Package};
65
use cargo_test_support::str;

tests/testsuite/features2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs::File;
44

55
use cargo_test_support::cross_compile::{self, alternate};
66
use cargo_test_support::install::cargo_home;
7-
use cargo_test_support::paths::CargoPathExt;
87
use cargo_test_support::prelude::*;
98
use cargo_test_support::publish::validate_crate_contents;
109
use cargo_test_support::registry::{Dependency, Package};

tests/testsuite/fix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use cargo::core::Edition;
44
use cargo_test_support::compare::assert_e2e;
55
use cargo_test_support::git::{self, init};
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::registry::{Dependency, Package};
99
use cargo_test_support::str;

tests/testsuite/freshness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::process::Stdio;
99
use std::thread;
1010
use std::time::SystemTime;
1111

12-
use cargo_test_support::paths::{self, CargoPathExt};
12+
use cargo_test_support::paths;
1313
use cargo_test_support::prelude::*;
1414
use cargo_test_support::registry::Package;
1515
use cargo_test_support::{

tests/testsuite/git.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use std::sync::Arc;
1010
use std::thread;
1111

1212
use cargo_test_support::git::cargo_uses_gitoxide;
13-
use cargo_test_support::paths::{self, CargoPathExt};
13+
use cargo_test_support::paths;
1414
use cargo_test_support::prelude::IntoData;
1515
use cargo_test_support::prelude::*;
1616
use cargo_test_support::registry::Package;
17-
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, path2url, project};
17+
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, project};
1818
use cargo_test_support::{sleep_ms, str, t, Project};
1919

2020
#[cargo_test]
@@ -878,7 +878,7 @@ fn dep_with_submodule() {
878878
let git_project2 = git::new("dep2", |project| project.file("lib.rs", "pub fn dep() {}"));
879879

880880
let repo = git2::Repository::open(&git_project.root()).unwrap();
881-
let url = path2url(git_project2.root()).to_string();
881+
let url = git_project2.root().to_url().to_string();
882882
git::add_submodule(&repo, &url, Path::new("src"));
883883
git::commit(&repo);
884884

@@ -1000,7 +1000,7 @@ fn dep_with_bad_submodule() {
10001000
let git_project2 = git::new("dep2", |project| project.file("lib.rs", "pub fn dep() {}"));
10011001

10021002
let repo = git2::Repository::open(&git_project.root()).unwrap();
1003-
let url = path2url(git_project2.root()).to_string();
1003+
let url = git_project2.root().to_url().to_string();
10041004
git::add_submodule(&repo, &url, Path::new("src"));
10051005
git::commit(&repo);
10061006

@@ -2441,12 +2441,12 @@ fn dont_require_submodules_are_checked_out() {
24412441
let git2 = git::new("dep2", |p| p);
24422442

24432443
let repo = git2::Repository::open(&git1.root()).unwrap();
2444-
let url = path2url(git2.root()).to_string();
2444+
let url = git2.root().to_url().to_string();
24452445
git::add_submodule(&repo, &url, Path::new("a/submodule"));
24462446
git::commit(&repo);
24472447

24482448
git2::Repository::init(&p.root()).unwrap();
2449-
let url = path2url(git1.root()).to_string();
2449+
let url = git1.root().to_url().to_string();
24502450
let dst = paths::home().join("foo");
24512451
git2::Repository::clone(&url, &dst).unwrap();
24522452

@@ -2863,7 +2863,7 @@ fn failed_submodule_checkout() {
28632863
drop((repo, url));
28642864

28652865
let repo = git2::Repository::open(&git_project.root()).unwrap();
2866-
let url = path2url(git_project2.root()).to_string();
2866+
let url = git_project2.root().to_url().to_string();
28672867
git::add_submodule(&repo, &url, Path::new("src"));
28682868
git::commit(&repo);
28692869
drop(repo);
@@ -3152,7 +3152,7 @@ fn dirty_submodule() {
31523152
project.no_manifest().file("lib.rs", "pub fn f() {}")
31533153
});
31543154

3155-
let url = path2url(git_project2.root()).to_string();
3155+
let url = git_project2.root().to_url().to_string();
31563156
git::add_submodule(&repo, &url, Path::new("src"));
31573157

31583158
// Submodule added, but not committed.
@@ -3200,7 +3200,7 @@ to proceed despite this and include the uncommitted changes, pass the `--allow-d
32003200

32013201
// Try with a nested submodule.
32023202
let git_project3 = git::new("bar", |project| project.no_manifest().file("mod.rs", ""));
3203-
let url = path2url(git_project3.root()).to_string();
3203+
let url = git_project3.root().to_url().to_string();
32043204
git::add_submodule(&sub_repo, &url, Path::new("bar"));
32053205
git_project
32063206
.cargo("package --no-verify")
@@ -4085,7 +4085,7 @@ fn git_worktree_with_bare_original_repo() {
40854085
.bare(true)
40864086
.clone_local(git2::build::CloneLocal::Local)
40874087
.clone(
4088-
path2url(git_project.root()).as_str(),
4088+
git_project.root().to_url().as_str(),
40894089
&paths::root().join("foo-bare"),
40904090
)
40914091
.unwrap()

tests/testsuite/global_cache_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use cargo::core::global_cache_tracker::{self, DeferredGlobalLastUse, GlobalCache
1818
use cargo::util::cache_lock::CacheLockMode;
1919
use cargo::util::interning::InternedString;
2020
use cargo::GlobalContext;
21-
use cargo_test_support::paths::{self, CargoPathExt};
21+
use cargo_test_support::paths;
2222
use cargo_test_support::prelude::*;
2323
use cargo_test_support::registry::{Package, RegistryBuilder};
2424
use cargo_test_support::{

tests/testsuite/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use cargo_util::{ProcessBuilder, ProcessError};
2121
use cargo_test_support::install::{
2222
assert_has_installed_exe, assert_has_not_installed_exe, cargo_home, exe,
2323
};
24-
use cargo_test_support::paths::{self, CargoPathExt};
24+
use cargo_test_support::paths;
2525

2626
fn pkg(name: &str, vers: &str) {
2727
Package::new(name, vers)

tests/testsuite/install_upgrade.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::sync::atomic::{AtomicUsize, Ordering};
88

99
use cargo::core::PackageId;
1010
use cargo_test_support::install::{cargo_home, exe};
11-
use cargo_test_support::paths::CargoPathExt;
1211
use cargo_test_support::prelude::*;
1312
use cargo_test_support::registry::{self, Package};
1413
use cargo_test_support::{

tests/testsuite/local_registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fs;
44

5-
use cargo_test_support::paths::{self, CargoPathExt};
5+
use cargo_test_support::paths;
66
use cargo_test_support::prelude::*;
77
use cargo_test_support::registry::{registry_path, Package};
88
use cargo_test_support::{basic_manifest, project, str, t};

tests/testsuite/login.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fs;
44
use std::path::PathBuf;
55

66
use cargo_test_support::cargo_process;
7-
use cargo_test_support::paths::{self, CargoPathExt};
7+
use cargo_test_support::paths;
88
use cargo_test_support::prelude::*;
99
use cargo_test_support::registry::{self, RegistryBuilder};
1010
use cargo_test_support::str;

tests/testsuite/logout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Tests for the `cargo logout` command.
22
33
use super::login::check_token;
4-
use cargo_test_support::paths::{self, CargoPathExt};
4+
use cargo_test_support::paths;
55
use cargo_test_support::prelude::*;
66
use cargo_test_support::registry::TestRegistry;
77
use cargo_test_support::{cargo_process, registry, str};

tests/testsuite/old_cargos.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use std::fs;
1616

1717
use cargo::CargoResult;
18-
use cargo_test_support::paths::CargoPathExt;
1918
use cargo_test_support::prelude::*;
2019
use cargo_test_support::registry::{self, Dependency, Package};
2120
use cargo_test_support::{cargo_exe, execs, paths, process, project, rustc_host};

tests/testsuite/owner.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::fs;
44

5-
use cargo_test_support::paths::CargoPathExt;
65
use cargo_test_support::prelude::*;
76
use cargo_test_support::project;
87
use cargo_test_support::registry::{self, api_path};

tests/testsuite/package.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
use std::fs::{self, read_to_string, File};
44
use std::path::Path;
55

6-
use cargo_test_support::paths::CargoPathExt;
76
use cargo_test_support::prelude::*;
87
use cargo_test_support::publish::validate_crate_contents;
98
use cargo_test_support::registry::{self, Package};
109
use cargo_test_support::{
11-
basic_manifest, cargo_process, git, path2url, paths, project, rustc_host, str,
12-
symlink_supported, t, ProjectBuilder,
10+
basic_manifest, cargo_process, git, paths, project, rustc_host, str, symlink_supported, t,
11+
ProjectBuilder,
1312
};
1413
use flate2::read::GzDecoder;
1514
use tar::Archive;
@@ -608,7 +607,7 @@ fn package_git_submodule() {
608607
});
609608

610609
let repository = git2::Repository::open(&project.root()).unwrap();
611-
let url = path2url(library.root()).to_string();
610+
let url = library.root().to_url().to_string();
612611
git::add_submodule(&repository, &url, Path::new("bar"));
613612
git::commit(&repository);
614613

@@ -656,7 +655,7 @@ fn package_symlink_to_submodule() {
656655
});
657656

658657
let repository = git2::Repository::open(&project.root()).unwrap();
659-
let url = path2url(library.root()).to_string();
658+
let url = library.root().to_url().to_string();
660659
git::add_submodule(&repository, &url, Path::new("submodule"));
661660
t!(symlink(
662661
&project.root().join("submodule"),

tests/testsuite/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fs;
44

5-
use cargo_test_support::paths::{self, CargoPathExt};
5+
use cargo_test_support::paths;
66
use cargo_test_support::prelude::*;
77
use cargo_test_support::registry::Package;
88
use cargo_test_support::str;

0 commit comments

Comments
 (0)