Skip to content

Commit fdc008c

Browse files
authored
Update spdx (#791)
1 parent 835aea7 commit fdc008c

File tree

8 files changed

+165
-46
lines changed

8 files changed

+165
-46
lines changed

Cargo.lock

Lines changed: 25 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ serde_json = "1.0"
103103
# Avoid some heap allocations when we likely won't need them
104104
smallvec = "1.14"
105105
# Used for parsing and checking SPDX license expressions
106-
spdx = "0.11"
106+
spdx = "0.12"
107107
# Lazy
108108
strum = { version = "0.27", features = ["derive"] }
109109
# Index retrieval and querying

src/licenses.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -178,33 +178,30 @@ fn evaluate_expression(
178178
notes.push(" - No additional metadata available for license".into());
179179
}
180180
} else {
181-
// This would only happen if askalono used a newer license list than spdx, but we update
182-
// both simultaneously
181+
// This would only happen if askalono used a newer license list
182+
// than spdx, but we update both simultaneously
183183
notes.push(format!("{} is not an SPDX license", failed_req.req));
184184
}
185185
}
186186

187-
let (id, offset) =
188-
//if let Some(((file_id, range), original)) = original_loc.clone().zip(original) {
189-
//let mut err_span = failed_req.span.start as usize..failed_req.span.end as usize;
190-
//Gatherer::correct_span(original, &mut err_span);
191-
if let Some((file_id, range)) = &original_loc {
192-
(
193-
*file_id,
194-
range.start,
195-
)
196-
} else {
197-
(
198-
nfo.file_id,
199-
nfo.offset,
200-
)
201-
};
187+
let (id, offset) = if let Some((file_id, range)) = &original_loc {
188+
(*file_id, range.start)
189+
} else {
190+
(nfo.file_id, nfo.offset)
191+
};
202192

203193
let start = offset + failed_req.span.start as usize;
204194

205195
// TODO: fix this in spdx, but we only get the span for the license, not the exception
206-
let end = if let Some(exc) = &failed_req.req.exception {
207-
failed_req.span.end as usize + 6 /*" WITH "*/ + exc.name.len()
196+
let end = if let Some(ai) = &failed_req.req.addition {
197+
failed_req.span.end as usize + 6 /*" WITH "*/ + match ai {
198+
spdx::AdditionItem::Spdx(exc) => exc.name.len(),
199+
spdx::AdditionItem::Other { doc_ref, add_ref } => {
200+
/*AdditionRef-*/ 12 + add_ref.len() + doc_ref.as_deref().map_or(0, |dr| {
201+
/*DocumentRef-:*/ 13 + dr.len()
202+
})
203+
}
204+
}
208205
} else {
209206
failed_req.span.end as usize
210207
};
@@ -254,9 +251,9 @@ pub fn check(
254251
for krate_lic_nfo in summary.nfos {
255252
let mut pack = Pack::with_kid(Check::Licenses, krate_lic_nfo.krate.id.clone());
256253

257-
// If the user has set this, check if it's a private workspace
258-
// crate or a crate from a private registry and just print out
259-
// a help message that we skipped it
254+
// If the user has set this, check if it's a private workspace crate or
255+
// a crate from a private registry and just print out a help message
256+
// that we skipped it
260257
if ctx.cfg.private.ignore
261258
&& (krate_lic_nfo.krate.is_private(&private_registries)
262259
|| ctx

tests/licenses.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,30 @@ fn forces_apache_over_pixar() {
332332

333333
insta::assert_json_snapshot!(diags);
334334
}
335+
336+
#[test]
337+
fn insane_licenses() {
338+
let cfg = tu::Config::new("allow = ['MIT']");
339+
340+
let mut cmd = krates::Cmd::new();
341+
cmd.manifest_path("tests/test_data/insane-licenses/Cargo.toml");
342+
343+
let krates: Krates = krates::Builder::new()
344+
.build(cmd, krates::NoneFilter)
345+
.unwrap();
346+
347+
let (ctx, summary) = setup(&krates, func_name!(), cfg);
348+
349+
let diags = tu::run_gather(ctx, |ctx, tx| {
350+
crate::licenses::check(
351+
ctx,
352+
summary,
353+
diag::ErrorSink {
354+
overrides: None,
355+
channel: tx,
356+
},
357+
);
358+
});
359+
360+
insta::assert_json_snapshot!(diags);
361+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
source: tests/licenses.rs
3+
expression: diags
4+
---
5+
[
6+
{
7+
"fields": {
8+
"code": "rejected",
9+
"graphs": [
10+
{
11+
"Krate": {
12+
"name": "insane-licenses",
13+
"version": "0.1.0"
14+
}
15+
}
16+
],
17+
"labels": [
18+
{
19+
"column": 12,
20+
"line": 5,
21+
"message": "",
22+
"span": "(MIT or Apache-2.0 with LLVM-exception) and BSD-2-Clause with AdditionRef-boop or BSD-3-Clause with DocumentRef-bippity:AdditionRef-boppity"
23+
},
24+
{
25+
"column": 13,
26+
"line": 5,
27+
"message": "accepted: license is explicitly allowed",
28+
"span": "MIT"
29+
},
30+
{
31+
"column": 20,
32+
"line": 5,
33+
"message": "rejected: license is not explicitly allowed",
34+
"span": "Apache-2.0 with LLVM-exception"
35+
},
36+
{
37+
"column": 56,
38+
"line": 5,
39+
"message": "rejected: license is not explicitly allowed",
40+
"span": "BSD-2-Clause with AdditionRef-boop"
41+
},
42+
{
43+
"column": 94,
44+
"line": 5,
45+
"message": "rejected: license is not explicitly allowed",
46+
"span": "BSD-3-Clause with DocumentRef-bippity:AdditionRef-boppity"
47+
}
48+
],
49+
"message": "failed to satisfy license requirements",
50+
"notes": [
51+
"Apache-2.0 - Apache License 2.0:",
52+
" - OSI approved",
53+
" - FSF Free/Libre",
54+
"BSD-2-Clause - BSD 2-Clause \"Simplified\" License:",
55+
" - OSI approved",
56+
" - FSF Free/Libre",
57+
"BSD-3-Clause - BSD 3-Clause \"New\" or \"Revised\" License:",
58+
" - OSI approved",
59+
" - FSF Free/Libre"
60+
],
61+
"severity": "error"
62+
},
63+
"type": "diagnostic"
64+
}
65+
]

tests/test_data/insane-licenses/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "insane-licenses"
3+
version = "0.1.0"
4+
edition = "2024"
5+
license = "(MIT or Apache-2.0 with LLVM-exception) and BSD-2-Clause with AdditionRef-boop or BSD-3-Clause with DocumentRef-bippity:AdditionRef-boppity"
6+
7+
[workspace]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: u64, right: u64) -> u64 {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}

0 commit comments

Comments
 (0)