Skip to content

Commit

Permalink
print notification when a file from license-files list doesnt exist
Browse files Browse the repository at this point in the history
  • Loading branch information
khodzha committed Aug 18, 2020
1 parent 1fde105 commit 8aa388c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/cargo-deny/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::stats::{AllStats, Stats};
use anyhow::{Context, Error};
use cargo_deny::{
advisories, bans,
diag::{CargoSpans, Diagnostic, Files, Severity},
diag::{CargoSpans, Diagnostic, FileId, Files, Severity},
licenses, sources, CheckCtx,
};
use clap::arg_enum;
Expand Down Expand Up @@ -80,7 +80,7 @@ impl ValidConfig {
cfg_path: Option<PathBuf>,
files: &mut Files,
log_ctx: crate::common::LogContext,
) -> Result<Self, Error> {
) -> Result<(Self, FileId), Error> {
use cargo_deny::UnvalidatedConfig;

let (cfg_contents, cfg_path) = match cfg_path {
Expand Down Expand Up @@ -153,7 +153,7 @@ impl ValidConfig {
match validate() {
Ok((diags, vc)) => {
print(diags);
Ok(vc)
Ok((vc, id))
}
Err(diags) => {
print(diags);
Expand All @@ -173,7 +173,7 @@ pub(crate) fn cmd(
krate_ctx: crate::common::KrateContext,
) -> Result<AllStats, Error> {
let mut files = Files::new();
let mut cfg = ValidConfig::load(
let (mut cfg, cfg_file_id) = ValidConfig::load(
krate_ctx.get_config_path(args.config.clone()),
&mut files,
log_ctx,
Expand Down Expand Up @@ -282,7 +282,7 @@ pub(crate) fn cmd(
.with_store(std::sync::Arc::new(store))
.with_confidence_threshold(cfg.licenses.confidence_threshold);

Some(gatherer.gather(&krates, &mut files, Some(&cfg.licenses)))
Some(gatherer.gather(&krates, &mut files, Some(&cfg.licenses), Some(&cfg_file_id)))
} else {
None
};
Expand Down
2 changes: 1 addition & 1 deletion src/cargo-deny/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub fn cmd(

let mut files = Files::new();

let summary = gatherer.gather(&krates, &mut files, None);
let summary = gatherer.gather(&krates, &mut files, None, None);

#[derive(Serialize)]
struct Crate {
Expand Down
6 changes: 4 additions & 2 deletions src/licenses/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub struct Private {
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct FileSource {
/// The crate relative path of the LICENSE file
pub path: PathBuf,
/// Spanned so we can report typos on it in case it never matches anything.
pub path: Spanned<PathBuf>,
/// The hash of the LICENSE text. If the `path`'s hash
/// differs from the contents of the path, the file is
/// parsed to determine if the license(s) contained in
Expand Down Expand Up @@ -394,14 +395,15 @@ mod test {
version: semver::VersionReq::parse("0.1.1").unwrap(),
}]
);
let p: PathBuf = "LICENSE".into();
assert_eq!(
validated.clarifications,
vec![ValidClarification {
name: "ring".to_owned(),
version: semver::VersionReq::parse("*").unwrap(),
expression: spdx::Expression::parse("MIT AND ISC AND OpenSSL").unwrap(),
license_files: vec![FileSource {
path: "LICENSE".into(),
path: p.fake(),
hash: 0xbd0e_ed23,
}],
expr_offset: 415,
Expand Down
31 changes: 30 additions & 1 deletion src/licenses/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl LicensePack {
}

for (expected, actual) in self.license_files.iter().zip(hashes.iter()) {
if !expected.path.ends_with(&actual.path) {
if !expected.path.ends_with(&actual.path.as_ref()) {
return false;
}

Expand Down Expand Up @@ -517,6 +517,7 @@ impl Gatherer {
krates: &'k crate::Krates,
files: &mut Files,
cfg: Option<&ValidConfig>,
cfg_file_id: Option<&FileId>,
) -> Summary<'k> {
let mut summary = Summary::new(self.store);

Expand Down Expand Up @@ -602,6 +603,7 @@ impl Gatherer {

// 1
if let Some(ref cfg) = cfg {
let mut skipped_clarifs = vec![];
for clarification in iter_clarifications(&cfg.clarifications, krate) {
let lp = match license_pack {
Some(ref lp) => lp,
Expand Down Expand Up @@ -631,6 +633,33 @@ impl Gatherer {
},
labels,
};
} else {
skipped_clarifs.push(clarification);
}
}

// if we reached here it means no clarification matched LicensePack
// maybe there was a typo in clarification license-files path so we check for that
if let Some(cfg_file_id) = cfg_file_id {
for clarification in skipped_clarifs {
let root_path = krate.manifest_path.parent().unwrap();
for file in &clarification.license_files {
let path = root_path.join(&file.path.as_ref());
if !path.exists() {
labels.push(
Label::secondary(
cfg_file_id.to_owned(),
file.path.span().clone(),
)
.with_message(
format!(
"Couldn't find this file in crate '{}', maybe a typo?",
krate.name
),
),
);
}
}
}
}
}
Expand Down

0 comments on commit 8aa388c

Please sign in to comment.