Skip to content

Commit

Permalink
Fix duplicate license spans (#601)
Browse files Browse the repository at this point in the history
Resolves: #600
  • Loading branch information
Jake-Shadle committed Feb 6, 2024
1 parent e1d90f1 commit 4a250ab
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,22 @@ pub(crate) mod test {
pub(crate) id: FileId,
}

pub(crate) fn load<T: serde::de::DeserializeOwned>(path: impl Into<PathBuf>) -> ConfigData<T> {
let path = path.into();
let contents = std::fs::read_to_string(&path).unwrap();

pub(crate) fn load_str<T: serde::de::DeserializeOwned>(
name: impl Into<std::ffi::OsString>,
contents: impl Into<String>,
) -> ConfigData<T> {
let contents = contents.into();
let config = toml::from_str(&contents).unwrap();
let mut files = Files::new();
let id = files.add(&path, contents);
let id = files.add(name, contents);

ConfigData { config, files, id }
}

pub(crate) fn load<T: serde::de::DeserializeOwned>(path: impl Into<PathBuf>) -> ConfigData<T> {
let path = path.into();
let contents = std::fs::read_to_string(&path).unwrap();

load_str(path, contents)
}
}
50 changes: 42 additions & 8 deletions src/licenses/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ impl crate::cfg::UnvalidatedConfig for Config {
Diagnostic::error()
.with_message("a license id was specified in both `allow` and `deny`")
.with_labels(vec![
Label::secondary(cfg_file, self.deny[di].span.clone())
Label::secondary(cfg_file, denied[di].span.clone())
.with_message("deny"),
Label::secondary(cfg_file, self.allow[ai].span.clone())
Label::secondary(cfg_file, allowed[ai].span.clone())
.with_message("allow"),
]),
);
Expand Down Expand Up @@ -448,14 +448,14 @@ mod test {
use super::*;
use crate::cfg::{test::*, *};

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Licenses {
licenses: Config,
}

#[test]
fn deserializes_licenses_cfg() {
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Licenses {
licenses: Config,
}

let mut cd: ConfigData<Licenses> = load("tests/cfg/licenses.toml");

let mut diags = Vec::new();
Expand Down Expand Up @@ -511,4 +511,38 @@ mod test {
}]
);
}

#[test]
fn correct_duplicate_license_spans() {
let cfg = r#"[licenses]
allow = [
"MIT",
"Apache-2.0",
"BSD-3-Clause",
"ISC",
"CC0-1.0",
"Unicode-DFS-2016",
]
deny = [
"MIT",
"GPL-1.0",
"GPL-2.0",
"GPL-3.0",
"AGPL-3.0",
]"#;

let mut cd: ConfigData<Licenses> = load_str("license-in-allow-and-deny", cfg);
let mut diags = Vec::new();
let _validated = cd
.config
.licenses
.validate(cd.id, &mut cd.files, &mut diags);

let diags: Vec<_> = diags
.into_iter()
.map(|d| crate::diag::diag_to_json(d.into(), &cd.files, None))
.collect();

insta::assert_json_snapshot!(diags);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: src/licenses/cfg.rs
expression: diags
---
[
{
"fields": {
"labels": [
{
"column": 4,
"line": 11,
"message": "deny",
"span": "MIT"
},
{
"column": 5,
"line": 3,
"message": "allow",
"span": "MIT"
}
],
"message": "a license id was specified in both `allow` and `deny`",
"severity": "error"
},
"type": "diagnostic"
}
]

0 comments on commit 4a250ab

Please sign in to comment.