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.
This commit is contained in:
Ed Page 2024-07-18 15:41:26 -05:00
parent 3a615ca9c8
commit d17322dccb
5 changed files with 27 additions and 20 deletions

View File

@ -38,7 +38,7 @@ use some of the helper functions in this file to interact with the repository.
*/
use crate::{path2url, project, Project, ProjectBuilder, SymlinkBuilder};
use crate::{paths::CargoPathExt, project, Project, ProjectBuilder, SymlinkBuilder};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Once;
@ -118,7 +118,7 @@ impl Repository {
}
pub fn url(&self) -> Url {
path2url(self.0.workdir().unwrap().to_path_buf())
self.0.workdir().unwrap().to_url()
}
pub fn revparse_head(&self) -> String {

View File

@ -340,7 +340,8 @@ impl Project {
/// File url for root, ex: `file:///path/to/cargo/target/cit/t0/foo`
pub fn url(&self) -> Url {
path2url(self.root())
use paths::CargoPathExt;
self.root().to_url()
}
/// Path to an example built as a library.
@ -1185,10 +1186,6 @@ pub fn basic_lib_manifest(name: &str) -> String {
)
}
pub fn path2url<P: AsRef<Path>>(p: P) -> Url {
Url::from_file_path(p).ok().unwrap()
}
struct RustcInfo {
verbose_version: String,
host: String,

View File

@ -111,6 +111,8 @@ pub fn home() -> PathBuf {
}
pub trait CargoPathExt {
fn to_url(&self) -> url::Url;
fn rm_rf(&self);
fn mkdir_p(&self);
@ -132,6 +134,10 @@ pub trait CargoPathExt {
}
impl CargoPathExt for Path {
fn to_url(&self) -> url::Url {
url::Url::from_file_path(self).ok().unwrap()
}
fn rm_rf(&self) {
let meta = match self.symlink_metadata() {
Ok(meta) => meta,
@ -212,6 +218,10 @@ impl CargoPathExt for Path {
}
impl CargoPathExt for PathBuf {
fn to_url(&self) -> url::Url {
self.as_path().to_url()
}
fn rm_rf(&self) {
self.as_path().rm_rf()
}

View File

@ -14,7 +14,7 @@ use cargo_test_support::paths;
use cargo_test_support::prelude::IntoData;
use cargo_test_support::prelude::*;
use cargo_test_support::registry::Package;
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, path2url, project};
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, project};
use cargo_test_support::{sleep_ms, str, t, Project};
#[cargo_test]
@ -878,7 +878,7 @@ fn dep_with_submodule() {
let git_project2 = git::new("dep2", |project| project.file("lib.rs", "pub fn dep() {}"));
let repo = git2::Repository::open(&git_project.root()).unwrap();
let url = path2url(git_project2.root()).to_string();
let url = git_project2.root().to_url().to_string();
git::add_submodule(&repo, &url, Path::new("src"));
git::commit(&repo);
@ -1000,7 +1000,7 @@ fn dep_with_bad_submodule() {
let git_project2 = git::new("dep2", |project| project.file("lib.rs", "pub fn dep() {}"));
let repo = git2::Repository::open(&git_project.root()).unwrap();
let url = path2url(git_project2.root()).to_string();
let url = git_project2.root().to_url().to_string();
git::add_submodule(&repo, &url, Path::new("src"));
git::commit(&repo);
@ -2441,12 +2441,12 @@ fn dont_require_submodules_are_checked_out() {
let git2 = git::new("dep2", |p| p);
let repo = git2::Repository::open(&git1.root()).unwrap();
let url = path2url(git2.root()).to_string();
let url = git2.root().to_url().to_string();
git::add_submodule(&repo, &url, Path::new("a/submodule"));
git::commit(&repo);
git2::Repository::init(&p.root()).unwrap();
let url = path2url(git1.root()).to_string();
let url = git1.root().to_url().to_string();
let dst = paths::home().join("foo");
git2::Repository::clone(&url, &dst).unwrap();
@ -2863,7 +2863,7 @@ fn failed_submodule_checkout() {
drop((repo, url));
let repo = git2::Repository::open(&git_project.root()).unwrap();
let url = path2url(git_project2.root()).to_string();
let url = git_project2.root().to_url().to_string();
git::add_submodule(&repo, &url, Path::new("src"));
git::commit(&repo);
drop(repo);
@ -3152,7 +3152,7 @@ fn dirty_submodule() {
project.no_manifest().file("lib.rs", "pub fn f() {}")
});
let url = path2url(git_project2.root()).to_string();
let url = git_project2.root().to_url().to_string();
git::add_submodule(&repo, &url, Path::new("src"));
// Submodule added, but not committed.
@ -3200,7 +3200,7 @@ to proceed despite this and include the uncommitted changes, pass the `--allow-d
// Try with a nested submodule.
let git_project3 = git::new("bar", |project| project.no_manifest().file("mod.rs", ""));
let url = path2url(git_project3.root()).to_string();
let url = git_project3.root().to_url().to_string();
git::add_submodule(&sub_repo, &url, Path::new("bar"));
git_project
.cargo("package --no-verify")
@ -4085,7 +4085,7 @@ fn git_worktree_with_bare_original_repo() {
.bare(true)
.clone_local(git2::build::CloneLocal::Local)
.clone(
path2url(git_project.root()).as_str(),
git_project.root().to_url().as_str(),
&paths::root().join("foo-bare"),
)
.unwrap()

View File

@ -7,8 +7,8 @@ use cargo_test_support::prelude::*;
use cargo_test_support::publish::validate_crate_contents;
use cargo_test_support::registry::{self, Package};
use cargo_test_support::{
basic_manifest, cargo_process, git, path2url, paths, project, rustc_host, str,
symlink_supported, t, ProjectBuilder,
basic_manifest, cargo_process, git, paths, project, rustc_host, str, symlink_supported, t,
ProjectBuilder,
};
use flate2::read::GzDecoder;
use tar::Archive;
@ -607,7 +607,7 @@ fn package_git_submodule() {
});
let repository = git2::Repository::open(&project.root()).unwrap();
let url = path2url(library.root()).to_string();
let url = library.root().to_url().to_string();
git::add_submodule(&repository, &url, Path::new("bar"));
git::commit(&repository);
@ -655,7 +655,7 @@ fn package_symlink_to_submodule() {
});
let repository = git2::Repository::open(&project.root()).unwrap();
let url = path2url(library.root()).to_string();
let url = library.root().to_url().to_string();
git::add_submodule(&repository, &url, Path::new("submodule"));
t!(symlink(
&project.root().join("submodule"),