Skip to content

Commit 3a2aa58

Browse files
committed
Auto merge of #119020 - onur-ozkan:remove-hex, r=albertlarsan68
remove `hex` dependency in bootstrap First commit removes the `hex` dependency, as we can achieve the same output with the added function, which is very small and simple. Second commit creates a test module for the helpers util and adds unit tests for multiple helper functions, including `hex_encode`.
2 parents e95a69d + 81b98a0 commit 3a2aa58

File tree

9 files changed

+79
-29
lines changed

9 files changed

+79
-29
lines changed

src/bootstrap/Cargo.lock

-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ dependencies = [
5555
"cmake",
5656
"fd-lock",
5757
"filetime",
58-
"hex",
5958
"home",
6059
"ignore",
6160
"junction",
@@ -313,12 +312,6 @@ version = "0.4.1"
313312
source = "registry+https://github.com/rust-lang/crates.io-index"
314313
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
315314

316-
[[package]]
317-
name = "hex"
318-
version = "0.4.3"
319-
source = "registry+https://github.com/rust-lang/crates.io-index"
320-
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
321-
322315
[[package]]
323316
name = "home"
324317
version = "0.5.4"

src/bootstrap/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ clap = { version = "4.4.7", default-features = false, features = ["std", "usage"
3939
clap_complete = "4.4.3"
4040
cmake = "0.1.38"
4141
filetime = "0.2"
42-
hex = "0.4"
4342
home = "0.5.4"
4443
ignore = "0.4.10"
4544
libc = "0.2.150"

src/bootstrap/src/core/build_steps/setup.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
22
use crate::t;
33
use crate::utils::change_tracker::CONFIG_CHANGE_HISTORY;
4+
use crate::utils::helpers::hex_encode;
45
use crate::Config;
56
use sha2::Digest;
67
use std::env::consts::EXE_SUFFIX;
@@ -566,7 +567,7 @@ fn create_vscode_settings_maybe(config: &Config) -> io::Result<bool> {
566567
if let Ok(current) = fs::read_to_string(&vscode_settings) {
567568
let mut hasher = sha2::Sha256::new();
568569
hasher.update(&current);
569-
let hash = hex::encode(hasher.finalize().as_slice());
570+
let hash = hex_encode(hasher.finalize().as_slice());
570571
if hash == *current_hash {
571572
return Ok(true);
572573
} else if historical_hashes.contains(&hash.as_str()) {

src/bootstrap/src/core/download.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use std::{
1111
use build_helper::ci::CiEnv;
1212
use xz2::bufread::XzDecoder;
1313

14-
use crate::core::build_steps::llvm::detect_llvm_sha;
1514
use crate::core::config::RustfmtMetadata;
1615
use crate::utils::helpers::{check_run, exe, program_out_of_date};
16+
use crate::{core::build_steps::llvm::detect_llvm_sha, utils::helpers::hex_encode};
1717
use crate::{t, Config};
1818

1919
static SHOULD_FIX_BINS_AND_DYLIBS: OnceLock<bool> = OnceLock::new();
@@ -345,7 +345,7 @@ impl Config {
345345
reader.consume(l);
346346
}
347347

348-
let checksum = hex::encode(hasher.finalize().as_slice());
348+
let checksum = hex_encode(hasher.finalize().as_slice());
349349
let verified = checksum == expected;
350350

351351
if !verified {

src/bootstrap/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use filetime::FileTime;
3434
use sha2::digest::Digest;
3535
use termcolor::{ColorChoice, StandardStream, WriteColor};
3636
use utils::channel::GitInfo;
37+
use utils::helpers::hex_encode;
3738

3839
use crate::core::builder;
3940
use crate::core::builder::Kind;
@@ -1871,7 +1872,7 @@ pub fn generate_smart_stamp_hash(dir: &Path, additional_input: &str) -> String {
18711872
hasher.update(status);
18721873
hasher.update(additional_input);
18731874

1874-
hex::encode(hasher.finalize().as_slice())
1875+
hex_encode(hasher.finalize().as_slice())
18751876
}
18761877

18771878
/// Ensures that the behavior dump directory is properly initialized.

src/bootstrap/src/tests/builder.rs

-16
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,6 @@ fn alias_and_path_for_library() {
156156
assert_eq!(first(cache.all::<doc::Std>()), &[doc_std!(A => A, stage = 0)]);
157157
}
158158

159-
#[test]
160-
fn test_beta_rev_parsing() {
161-
use crate::utils::helpers::extract_beta_rev;
162-
163-
// single digit revision
164-
assert_eq!(extract_beta_rev("1.99.9-beta.7 (xxxxxx)"), Some("7".to_string()));
165-
// multiple digits
166-
assert_eq!(extract_beta_rev("1.99.9-beta.777 (xxxxxx)"), Some("777".to_string()));
167-
// nightly channel (no beta revision)
168-
assert_eq!(extract_beta_rev("1.99.9-nightly (xxxxxx)"), None);
169-
// stable channel (no beta revision)
170-
assert_eq!(extract_beta_rev("1.99.9 (xxxxxxx)"), None);
171-
// invalid string
172-
assert_eq!(extract_beta_rev("invalid"), None);
173-
}
174-
175159
mod defaults {
176160
use super::{configure, first, run_build};
177161
use crate::core::builder::*;

src/bootstrap/src/tests/helpers.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::utils::helpers::{extract_beta_rev, hex_encode, make};
2+
use std::path::PathBuf;
3+
4+
#[test]
5+
fn test_make() {
6+
for (host, make_path) in vec![
7+
("dragonfly", PathBuf::from("gmake")),
8+
("netbsd", PathBuf::from("gmake")),
9+
("freebsd", PathBuf::from("gmake")),
10+
("openbsd", PathBuf::from("gmake")),
11+
("linux", PathBuf::from("make")),
12+
// for checking the default
13+
("_", PathBuf::from("make")),
14+
] {
15+
assert_eq!(make(host), make_path);
16+
}
17+
}
18+
19+
#[cfg(unix)]
20+
#[test]
21+
fn test_absolute_unix() {
22+
use crate::utils::helpers::absolute_unix;
23+
24+
// Test an absolute path
25+
let path = PathBuf::from("/home/user/file.txt");
26+
assert_eq!(absolute_unix(&path).unwrap(), PathBuf::from("/home/user/file.txt"));
27+
28+
// Test an absolute path with double leading slashes
29+
let path = PathBuf::from("//root//file.txt");
30+
assert_eq!(absolute_unix(&path).unwrap(), PathBuf::from("//root/file.txt"));
31+
32+
// Test a relative path
33+
let path = PathBuf::from("relative/path");
34+
assert_eq!(
35+
absolute_unix(&path).unwrap(),
36+
std::env::current_dir().unwrap().join("relative/path")
37+
);
38+
}
39+
40+
#[test]
41+
fn test_beta_rev_parsing() {
42+
// single digit revision
43+
assert_eq!(extract_beta_rev("1.99.9-beta.7 (xxxxxx)"), Some("7".to_string()));
44+
// multiple digits
45+
assert_eq!(extract_beta_rev("1.99.9-beta.777 (xxxxxx)"), Some("777".to_string()));
46+
// nightly channel (no beta revision)
47+
assert_eq!(extract_beta_rev("1.99.9-nightly (xxxxxx)"), None);
48+
// stable channel (no beta revision)
49+
assert_eq!(extract_beta_rev("1.99.9 (xxxxxxx)"), None);
50+
// invalid string
51+
assert_eq!(extract_beta_rev("invalid"), None);
52+
}
53+
54+
#[test]
55+
fn test_string_to_hex_encode() {
56+
let input_string = "Hello, World!";
57+
let hex_string = hex_encode(input_string);
58+
assert_eq!(hex_string, "48656c6c6f2c20576f726c6421");
59+
}

src/bootstrap/src/tests/setup.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::{RUST_ANALYZER_SETTINGS, SETTINGS_HASHES};
2+
use crate::utils::helpers::hex_encode;
23
use sha2::Digest;
34

45
#[test]
56
fn check_matching_settings_hash() {
67
let mut hasher = sha2::Sha256::new();
78
hasher.update(&RUST_ANALYZER_SETTINGS);
8-
let hash = hex::encode(hasher.finalize().as_slice());
9+
let hash = hex_encode(hasher.finalize().as_slice());
910
assert_eq!(
1011
&hash,
1112
SETTINGS_HASHES.last().unwrap(),

src/bootstrap/src/utils/helpers.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use crate::LldMode;
2020

2121
pub use crate::utils::dylib::{dylib_path, dylib_path_var};
2222

23+
#[cfg(test)]
24+
#[path = "../tests/helpers.rs"]
25+
mod tests;
26+
2327
/// A helper macro to `unwrap` a result except also print out details like:
2428
///
2529
/// * The file/line of the panic
@@ -540,3 +544,11 @@ pub fn add_rustdoc_cargo_linker_args(
540544
cmd.env("RUSTDOCFLAGS", flags);
541545
}
542546
}
547+
548+
/// Converts `T` into a hexadecimal `String`.
549+
pub fn hex_encode<T>(input: T) -> String
550+
where
551+
T: AsRef<[u8]>,
552+
{
553+
input.as_ref().iter().map(|x| format!("{:02x}", x)).collect()
554+
}

0 commit comments

Comments
 (0)