run_make_support: move path-related helpers into own module

This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-07-15 10:50:28 +00:00
parent 483328d401
commit dc9531533c
3 changed files with 75 additions and 34 deletions

View File

@ -1,5 +1,10 @@
//! This module contains external tool dependencies that we assume are available in the environment,
//! such as `cc` or `python`.
//!
//! # Notes
//!
//! - This is not the *only* place where external dependencies are assumed or referenced. For
//! example, see [`cygpath_windows`][crate::path_helpers::cygpath_windows].
pub mod cc;
pub mod clang;

View File

@ -12,6 +12,7 @@
pub mod env_checked;
pub mod external_deps;
pub mod fs_wrapper;
pub mod path_helpers;
pub mod run;
pub mod targets;
@ -67,19 +68,11 @@
bin_name, dynamic_lib_extension, dynamic_lib_name, rust_lib_name, static_lib_name,
};
/// Path-related helpers.
pub use path_helpers::{cwd, cygpath_windows, path, source_root};
use command::{Command, CompletedProcess};
/// Returns the path for a local test file.
pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
cwd().join(p.as_ref())
}
/// Path to the root rust-lang/rust source checkout.
#[must_use]
pub fn source_root() -> PathBuf {
env_var("SOURCE_ROOT").into()
}
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
#[cfg(target_family = "windows")]
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
@ -108,12 +101,6 @@ pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
));
}
/// Return the current working directory.
#[must_use]
pub fn cwd() -> PathBuf {
std::env::current_dir().unwrap()
}
// FIXME(Oneirical): This will no longer be required after compiletest receives the ability
// to manipulate read-only files. See https://github.com/rust-lang/rust/issues/126334
/// Ensure that the path P is read-only while the test runs, and restore original permissions
@ -227,23 +214,6 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
count
}
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
/// available on the platform!
#[track_caller]
#[must_use]
pub fn cygpath_windows<P: AsRef<Path>>(path: P) -> String {
let caller = panic::Location::caller();
let mut cygpath = Command::new("cygpath");
cygpath.arg("-w");
cygpath.arg(path.as_ref());
let output = cygpath.run();
if !output.status().success() {
handle_failed_output(&cygpath, output, caller.line());
}
// cygpath -w can attach a newline
output.stdout_utf8().trim().to_string()
}
pub(crate) fn handle_failed_output(
cmd: &Command,
output: CompletedProcess,

View File

@ -0,0 +1,66 @@
//! Collection of path-related helpers.
use std::panic;
use std::path::{Path, PathBuf};
use crate::command::Command;
use crate::env_checked::env_var;
use crate::handle_failed_output;
/// Return the current working directory.
///
/// This forwards to [`std::env::current_dir`], please see its docs regarding platform-specific
/// behavior.
#[must_use]
pub fn cwd() -> PathBuf {
std::env::current_dir().unwrap()
}
/// Construct a `PathBuf` relative to the current working directory by joining `cwd()` with the
/// relative path. This is mostly a convenience helper so the test writer does not need to write
/// `PathBuf::from(path_like_string)`.
///
/// # Example
///
/// ```rust
/// let p = path("support_file.txt");
/// ```
pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
cwd().join(p.as_ref())
}
/// Path to the root `rust-lang/rust` source checkout.
#[must_use]
pub fn source_root() -> PathBuf {
env_var("SOURCE_ROOT").into()
}
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
/// available on the platform!
///
/// # FIXME
///
/// FIXME(jieyouxu): we should consider not depending on `cygpath`.
///
/// > The cygpath program is a utility that converts Windows native filenames to Cygwin POSIX-style
/// > pathnames and vice versa.
/// >
/// > [irrelevant entries omitted...]
/// >
/// > `-w, --windows print Windows form of NAMEs (C:\WINNT)`
/// >
/// > -- *from [cygpath documentation](https://cygwin.com/cygwin-ug-net/cygpath.html)*.
#[track_caller]
#[must_use]
pub fn cygpath_windows<P: AsRef<Path>>(path: P) -> String {
let caller = panic::Location::caller();
let mut cygpath = Command::new("cygpath");
cygpath.arg("-w");
cygpath.arg(path.as_ref());
let output = cygpath.run();
if !output.status().success() {
handle_failed_output(&cygpath, output, caller.line());
}
// cygpath -w can attach a newline
output.stdout_utf8().trim().to_string()
}