Skip to content

Commit

Permalink
Normalize $CARGO_HOME in license file path (sstadick#12)
Browse files Browse the repository at this point in the history
* Normalize $CARGO_HOME in license file path

* update THIRDPARTY.yaml

* fix one clippy warning abs() -> unsigned_abs()

* ignore clippy lint regarding PartialEq derive without Eq
  • Loading branch information
Skgland authored Oct 7, 2022
1 parent 7b4c1cf commit 8146004
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ binary-dependencies = [ "anyhow", "env_logger", "structopt" ]
anyhow = {version = "1.0.43", optional = true}
cargo_metadata = "0.14.0"
env_logger = {version = "0.9.0", optional = true}
# https://doc.rust-lang.org/cargo/guide/cargo-home.html points to this for finding cargo home
home = "0.5.3"
itertools = "0.10.1"
log = "0.4.14"
regex = "1.5.4"
Expand Down
8 changes: 8 additions & 0 deletions THIRDPARTY.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn compare(mut text_freq: HashMap<String, u32>, template_freq: &HashMap<String,

for (word, &count) in template_freq {
let text_count = text_freq.remove(word).unwrap_or(0);
let diff = ((text_count as i32) - (count as i32)).abs() as u32;
let diff = ((text_count as i32) - (count as i32)).unsigned_abs() as u32;
errors += diff;
}

Expand Down
1 change: 1 addition & 0 deletions src/finalized_license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::license::License;

pub static LICENSE_NOT_FOUNT_TEXT: &str = "NOT FOUND";

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct LicenseAndText {
/// The license itself in SPDX format
Expand Down
18 changes: 15 additions & 3 deletions src/found_license.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A FoundLicense represents the raw form of the found parts of a license for a given package.
use cargo_metadata::Package;
use cargo_metadata::{camino::Utf8PathBuf, Package};
use thiserror::Error;

use crate::{
Expand Down Expand Up @@ -258,8 +258,20 @@ impl Licensed for Package {
.as_ref()
.and_then(|license| license.parse::<License>().ok())
.or_else(|| {
self.license_file()
.map(|p| License::File(p.into_std_path_buf()))
self.license_file().map(|license_file| {
// If the license file starts with the cargo_home path, strip it and replace with the ENV var $CARGO_HOME.
// This makes licenses comparable across machines.
let license_file = if let Some(stripped_license_file) = home::cargo_home()
.ok()
.and_then(|cargo_home| license_file.strip_prefix(cargo_home).ok())
{
Utf8PathBuf::from("$CARGO_HOME").join(stripped_license_file)
} else {
// license file is not under $CARGO_HOME keep it as is
license_file
};
License::File(license_file.into_std_path_buf())
})
})
.unwrap_or_default()
}
Expand Down

0 comments on commit 8146004

Please sign in to comment.