From 3ea3c3885bff114eed94f80638bade3e12a50a96 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 16 Dec 2023 20:16:53 +0300 Subject: [PATCH 1/2] create helper fn `hex_encode` and remove `hex` dependency Signed-off-by: onur-ozkan --- src/bootstrap/Cargo.lock | 7 ------- src/bootstrap/Cargo.toml | 1 - src/bootstrap/src/core/build_steps/setup.rs | 3 ++- src/bootstrap/src/core/download.rs | 4 ++-- src/bootstrap/src/lib.rs | 3 ++- src/bootstrap/src/tests/setup.rs | 3 ++- src/bootstrap/src/utils/helpers.rs | 8 ++++++++ 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/bootstrap/Cargo.lock b/src/bootstrap/Cargo.lock index f452b944e75be..f8e6d629ba347 100644 --- a/src/bootstrap/Cargo.lock +++ b/src/bootstrap/Cargo.lock @@ -55,7 +55,6 @@ dependencies = [ "cmake", "fd-lock", "filetime", - "hex", "home", "ignore", "junction", @@ -313,12 +312,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" - [[package]] name = "home" version = "0.5.4" diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index 24951cf20fa08..077d1954b7b9e 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -39,7 +39,6 @@ clap = { version = "4.4.7", default-features = false, features = ["std", "usage" clap_complete = "4.4.3" cmake = "0.1.38" filetime = "0.2" -hex = "0.4" home = "0.5.4" ignore = "0.4.10" libc = "0.2.150" diff --git a/src/bootstrap/src/core/build_steps/setup.rs b/src/bootstrap/src/core/build_steps/setup.rs index fe84b95f90c8b..9c897ae1bb784 100644 --- a/src/bootstrap/src/core/build_steps/setup.rs +++ b/src/bootstrap/src/core/build_steps/setup.rs @@ -1,6 +1,7 @@ use crate::core::builder::{Builder, RunConfig, ShouldRun, Step}; use crate::t; use crate::utils::change_tracker::CONFIG_CHANGE_HISTORY; +use crate::utils::helpers::hex_encode; use crate::Config; use sha2::Digest; use std::env::consts::EXE_SUFFIX; @@ -566,7 +567,7 @@ fn create_vscode_settings_maybe(config: &Config) -> io::Result { if let Ok(current) = fs::read_to_string(&vscode_settings) { let mut hasher = sha2::Sha256::new(); hasher.update(¤t); - let hash = hex::encode(hasher.finalize().as_slice()); + let hash = hex_encode(hasher.finalize().as_slice()); if hash == *current_hash { return Ok(true); } else if historical_hashes.contains(&hash.as_str()) { diff --git a/src/bootstrap/src/core/download.rs b/src/bootstrap/src/core/download.rs index 0a5844a6859f8..072fa42b1c072 100644 --- a/src/bootstrap/src/core/download.rs +++ b/src/bootstrap/src/core/download.rs @@ -11,9 +11,9 @@ use std::{ use build_helper::ci::CiEnv; use xz2::bufread::XzDecoder; -use crate::core::build_steps::llvm::detect_llvm_sha; use crate::core::config::RustfmtMetadata; use crate::utils::helpers::{check_run, exe, program_out_of_date}; +use crate::{core::build_steps::llvm::detect_llvm_sha, utils::helpers::hex_encode}; use crate::{t, Config}; static SHOULD_FIX_BINS_AND_DYLIBS: OnceLock = OnceLock::new(); @@ -342,7 +342,7 @@ impl Config { reader.consume(l); } - let checksum = hex::encode(hasher.finalize().as_slice()); + let checksum = hex_encode(hasher.finalize().as_slice()); let verified = checksum == expected; if !verified { diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 9efb4990cab08..871318de5955e 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -34,6 +34,7 @@ use filetime::FileTime; use sha2::digest::Digest; use termcolor::{ColorChoice, StandardStream, WriteColor}; use utils::channel::GitInfo; +use utils::helpers::hex_encode; use crate::core::builder; use crate::core::builder::Kind; @@ -1871,7 +1872,7 @@ pub fn generate_smart_stamp_hash(dir: &Path, additional_input: &str) -> String { hasher.update(status); hasher.update(additional_input); - hex::encode(hasher.finalize().as_slice()) + hex_encode(hasher.finalize().as_slice()) } /// Ensures that the behavior dump directory is properly initialized. diff --git a/src/bootstrap/src/tests/setup.rs b/src/bootstrap/src/tests/setup.rs index 0fe6e4a464463..3e4d66c74546f 100644 --- a/src/bootstrap/src/tests/setup.rs +++ b/src/bootstrap/src/tests/setup.rs @@ -1,11 +1,12 @@ use super::{RUST_ANALYZER_SETTINGS, SETTINGS_HASHES}; +use crate::utils::helpers::hex_encode; use sha2::Digest; #[test] fn check_matching_settings_hash() { let mut hasher = sha2::Sha256::new(); hasher.update(&RUST_ANALYZER_SETTINGS); - let hash = hex::encode(hasher.finalize().as_slice()); + let hash = hex_encode(hasher.finalize().as_slice()); assert_eq!( &hash, SETTINGS_HASHES.last().unwrap(), diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index d96608db22c0e..f1584052691d0 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -548,3 +548,11 @@ pub fn add_rustdoc_cargo_linker_args( cmd.env("RUSTDOCFLAGS", flags); } } + +/// Converts `T` into a hexadecimal `String`. +pub fn hex_encode(input: T) -> String +where + T: AsRef<[u8]>, +{ + input.as_ref().iter().map(|x| format!("{:02x}", x)).collect() +} From 87b5cbcb305755e6f7033670b68840076da75e79 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 16 Dec 2023 20:59:08 +0300 Subject: [PATCH 2/2] expand helpers tests with new test module `tests/helpers` Signed-off-by: onur-ozkan --- src/bootstrap/src/tests/builder.rs | 16 --------- src/bootstrap/src/tests/helpers.rs | 56 ++++++++++++++++++++++++++++++ src/bootstrap/src/utils/helpers.rs | 4 +++ 3 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 src/bootstrap/src/tests/helpers.rs diff --git a/src/bootstrap/src/tests/builder.rs b/src/bootstrap/src/tests/builder.rs index 744015e8e8204..700ebcf5e3741 100644 --- a/src/bootstrap/src/tests/builder.rs +++ b/src/bootstrap/src/tests/builder.rs @@ -156,22 +156,6 @@ fn alias_and_path_for_library() { assert_eq!(first(cache.all::()), &[doc_std!(A => A, stage = 0)]); } -#[test] -fn test_beta_rev_parsing() { - use crate::utils::helpers::extract_beta_rev; - - // single digit revision - assert_eq!(extract_beta_rev("1.99.9-beta.7 (xxxxxx)"), Some("7".to_string())); - // multiple digits - assert_eq!(extract_beta_rev("1.99.9-beta.777 (xxxxxx)"), Some("777".to_string())); - // nightly channel (no beta revision) - assert_eq!(extract_beta_rev("1.99.9-nightly (xxxxxx)"), None); - // stable channel (no beta revision) - assert_eq!(extract_beta_rev("1.99.9 (xxxxxxx)"), None); - // invalid string - assert_eq!(extract_beta_rev("invalid"), None); -} - mod defaults { use super::{configure, first, run_build}; use crate::core::builder::*; diff --git a/src/bootstrap/src/tests/helpers.rs b/src/bootstrap/src/tests/helpers.rs new file mode 100644 index 0000000000000..d334d9bae60cb --- /dev/null +++ b/src/bootstrap/src/tests/helpers.rs @@ -0,0 +1,56 @@ +use crate::utils::helpers::{extract_beta_rev, hex_encode, make}; +use std::{env, path::PathBuf}; + +#[test] +fn test_make() { + for (host, make_path) in vec![ + ("dragonfly", PathBuf::from("gmake")), + ("netbsd", PathBuf::from("gmake")), + ("freebsd", PathBuf::from("gmake")), + ("openbsd", PathBuf::from("gmake")), + ("linux", PathBuf::from("make")), + // for checking the default + ("_", PathBuf::from("make")), + ] { + assert_eq!(make(host), make_path); + } +} + +#[cfg(unix)] +#[test] +fn test_absolute_unix() { + use crate::utils::helpers::absolute_unix; + + // Test an absolute path + let path = PathBuf::from("/home/user/file.txt"); + assert_eq!(absolute_unix(&path).unwrap(), PathBuf::from("/home/user/file.txt")); + + // Test an absolute path with double leading slashes + let path = PathBuf::from("//root//file.txt"); + assert_eq!(absolute_unix(&path).unwrap(), PathBuf::from("//root/file.txt")); + + // Test a relative path + let path = PathBuf::from("relative/path"); + assert_eq!(absolute_unix(&path).unwrap(), env::current_dir().unwrap().join("relative/path")); +} + +#[test] +fn test_beta_rev_parsing() { + // single digit revision + assert_eq!(extract_beta_rev("1.99.9-beta.7 (xxxxxx)"), Some("7".to_string())); + // multiple digits + assert_eq!(extract_beta_rev("1.99.9-beta.777 (xxxxxx)"), Some("777".to_string())); + // nightly channel (no beta revision) + assert_eq!(extract_beta_rev("1.99.9-nightly (xxxxxx)"), None); + // stable channel (no beta revision) + assert_eq!(extract_beta_rev("1.99.9 (xxxxxxx)"), None); + // invalid string + assert_eq!(extract_beta_rev("invalid"), None); +} + +#[test] +fn test_string_to_hex_encode() { + let input_string = "Hello, World!"; + let hex_string = hex_encode(input_string); + assert_eq!(hex_string, "48656c6c6f2c20576f726c6421"); +} diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index f1584052691d0..1b56a72c0adc7 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -20,6 +20,10 @@ use crate::LldMode; pub use crate::utils::dylib::{dylib_path, dylib_path_var}; +#[cfg(test)] +#[path = "../tests/helpers.rs"] +mod tests; + /// A helper macro to `unwrap` a result except also print out details like: /// /// * The file/line of the panic