Skip to content

Commit

Permalink
fix: avoid symbolic links and transforma paths to canonical usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ramos committed Jul 19, 2024
1 parent 5fd30a2 commit 069bb3f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
5 changes: 4 additions & 1 deletion src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ pub fn get_project_root_path(root: Option<PathBuf>) -> Option<String> {
}
};

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.
Expand Down
39 changes: 19 additions & 20 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,23 +37,6 @@ pub struct PackageScopeMetadata {
pub path: Option<String>,
}

#[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<PackageScopeMetadata> {
let regex = Regex::new("^((?:@[^/@]+/)?[^/@]+)(?:@([^/]+))?(/.*)?$").unwrap();
Expand Down Expand Up @@ -77,8 +70,11 @@ pub(crate) fn strip_trailing_newline(input: &String) -> String {
pub(crate) fn create_test_monorepo(
package_manager: &PackageManager,
) -> Result<std::path::PathBuf, std::io::Error> {
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));
Expand Down Expand Up @@ -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))
}

0 comments on commit 069bb3f

Please sign in to comment.