Skip to content

Commit d17322d

Browse files
committed
fix(test): Move path2url to CargoPathExt::to_url
I was considering moving this into `paths` and noticed `CargoPathExt`. I figured if we had any extension traits for `Path`, then this is a reasonable one to add.
1 parent 3a615ca commit d17322d

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ impl Project {
340340

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

346347
/// Path to an example built as a library.
@@ -1185,10 +1186,6 @@ pub fn basic_lib_manifest(name: &str) -> String {
11851186
)
11861187
}
11871188

1188-
pub fn path2url<P: AsRef<Path>>(p: P) -> Url {
1189-
Url::from_file_path(p).ok().unwrap()
1190-
}
1191-
11921189
struct RustcInfo {
11931190
verbose_version: String,
11941191
host: String,

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

Lines changed: 10 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,
@@ -212,6 +218,10 @@ impl CargoPathExt for Path {
212218
}
213219

214220
impl CargoPathExt for PathBuf {
221+
fn to_url(&self) -> url::Url {
222+
self.as_path().to_url()
223+
}
224+
215225
fn rm_rf(&self) {
216226
self.as_path().rm_rf()
217227
}

tests/testsuite/git.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ 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/package.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use cargo_test_support::prelude::*;
77
use cargo_test_support::publish::validate_crate_contents;
88
use cargo_test_support::registry::{self, Package};
99
use cargo_test_support::{
10-
basic_manifest, cargo_process, git, path2url, paths, project, rustc_host, str,
11-
symlink_supported, t, ProjectBuilder,
10+
basic_manifest, cargo_process, git, paths, project, rustc_host, str, symlink_supported, t,
11+
ProjectBuilder,
1212
};
1313
use flate2::read::GzDecoder;
1414
use tar::Archive;
@@ -607,7 +607,7 @@ fn package_git_submodule() {
607607
});
608608

609609
let repository = git2::Repository::open(&project.root()).unwrap();
610-
let url = path2url(library.root()).to_string();
610+
let url = library.root().to_url().to_string();
611611
git::add_submodule(&repository, &url, Path::new("bar"));
612612
git::commit(&repository);
613613

@@ -655,7 +655,7 @@ fn package_symlink_to_submodule() {
655655
});
656656

657657
let repository = git2::Repository::open(&project.root()).unwrap();
658-
let url = path2url(library.root()).to_string();
658+
let url = library.root().to_url().to_string();
659659
git::add_submodule(&repository, &url, Path::new("submodule"));
660660
t!(symlink(
661661
&project.root().join("submodule"),

0 commit comments

Comments
 (0)