From 069bb3fd09c3a9f62a1bbcc3da90f51d0d45e167 Mon Sep 17 00:00:00 2001 From: Miguel Ramos Date: Fri, 19 Jul 2024 10:34:48 +0100 Subject: [PATCH] fix: avoid symbolic links and transforma paths to canonical usage --- Cargo.lock | 1 + Cargo.toml | 1 + src/paths.rs | 5 ++++- src/utils.rs | 39 +++++++++++++++++++-------------------- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f297d9c..a54a83c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2585,6 +2585,7 @@ dependencies = [ "napi", "napi-derive", "package_json_schema", + "rand", "regex", "semver", "serde", diff --git a/Cargo.toml b/Cargo.toml index 1fa8846c..e57f9037 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ version-compare = "0.2" git-cliff-core = "2.4.0" chrono = "0.4.38" semver = "1.0.23" +rand = "0.8.5" [build-dependencies] vergen = { version = "8.3.2", features = [ diff --git a/src/paths.rs b/src/paths.rs index 9bd9cc2f..7e2a593b 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -34,7 +34,10 @@ pub fn get_project_root_path(root: Option) -> Option { } }; - Some(project_root) + let canonic_path = &std::fs::canonicalize(Path::new(&project_root)).unwrap(); + let root = canonic_path.as_path().display().to_string(); + + Some(root) } /// Get the git root directory. diff --git a/src/utils.rs b/src/utils.rs index 22555f51..62215dab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,11 +3,21 @@ use regex::Regex; +#[cfg(test)] +use std::path::Path; +#[cfg(test)] +use std::path::PathBuf; + #[cfg(test)] use std::fs::{create_dir, File}; #[cfg(test)] use std::io::Write; +#[cfg(test)] +use rand::distributions::Alphanumeric; +#[cfg(test)] +use rand::{thread_rng, Rng}; + #[cfg(test)] #[cfg(not(windows))] use std::os::unix::fs::PermissionsExt; @@ -27,23 +37,6 @@ pub struct PackageScopeMetadata { pub path: Option, } -#[cfg(test)] -struct Rng(u64); - -#[cfg(test)] -impl Rng { - const A: u64 = 6364136223846793005; - - fn rand(&mut self) -> u64 { - self.0 = self.0.wrapping_mul(Self::A).wrapping_add(1); - self.0 - } - - fn from_seed(seed: u64) -> Self { - Self(seed ^ 3141592653589793238) - } -} - /// Extracts the package scope name and version from a package name. pub(crate) fn package_scope_name_version(pkg_name: &str) -> Option { let regex = Regex::new("^((?:@[^/@]+/)?[^/@]+)(?:@([^/]+))?(/.*)?$").unwrap(); @@ -77,8 +70,11 @@ pub(crate) fn strip_trailing_newline(input: &String) -> String { pub(crate) fn create_test_monorepo( package_manager: &PackageManager, ) -> Result { - let mut rng = Rng::from_seed(0); - let rand_string = format!("{:016x}", rng.rand()); + let rand_string: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(30) + .map(char::from) + .collect(); let temp_dir = std::env::temp_dir(); let monorepo_temp_dir = temp_dir.join(format!("monorepo-{}", rand_string)); @@ -271,5 +267,8 @@ pub(crate) fn create_test_monorepo( tag_b.wait_with_output()?; - Ok(monorepo_temp_dir) + let canonic_path = &std::fs::canonicalize(Path::new(&monorepo_temp_dir)).unwrap(); + let root = canonic_path.as_path().display().to_string(); + + Ok(PathBuf::from(root)) }