Skip to content

Commit 68656b7

Browse files
authored
Rollup merge of #147422 - jyn514:license-diff, r=Kobzol
collect-license-metadata: Print a diff of the expected output Previously, `x test collect-license-metadata` gave the following message on errors: ``` gathering license information from REUSE (this might take a minute...) finished gathering the license information from REUSE in 78.69s loading existing license information The existing /home/runner/work/ferrocene/ferrocene/license-metadata.json file is out of date. Run ./x run collect-license-metadata to update it. Error: The existing /home/runner/work/ferrocene/ferrocene/license-metadata.json file doesn't match what REUSE reports. Bootstrap failed while executing `test collect-license-metadata` ``` Notable, this doesn't actually say what went wrong. Print a diff in addition so it's more clear what broke: ``` ... "license": { "copyright": [ + "2010 The Rust Project Developers", "2016, 2017, 2018, 2019, 2020, 2021 AXE Consultants. All Rights", + "License. Subject to the terms and conditions of this", "Notice", - "The Ferrocene Developers" + "The Ferrocene Developers", + "[yyyy] [name of copyright owner]" ], ... ``` Currently, this prints the entire text of the JSON file as context. That's not ideal, but it's rare for this to fail, so I think it's ok for now. I considered using `assert_json_diff` instead of `similar`, but its errors are a lot harder to read IMO, even though they are better at omitting unnecessary context: ``` Diff: json atoms at path ".files.children[0].children[10].license.copyright[0]" are not equal: lhs: "2016 The Fuchsia Authors" rhs: "2019 The Crossbeam Project Developers" json atoms at path ".files.children[0].children[10].license.spdx" are not equal: lhs: "BSD-2-Clause AND (Apache-2.0 OR MIT)" rhs: "Apache-2.0 OR MIT" json atom at path ".files.children[0].children[10].children" is missing from lhs json atoms at path ".files.children[0].children[10].name" are not equal: lhs: "library/std/src/sys/sync/mutex/fuchsia.rs" rhs: "library/std/src/sync/mpmc" ... ```
2 parents a8cc1a2 + bc930cd commit 68656b7

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ dependencies = [
686686
"anyhow",
687687
"serde",
688688
"serde_json",
689+
"similar",
689690
"spdx-rs",
690691
]
691692

src/tools/collect-license-metadata/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ license = "MIT OR Apache-2.0"
99
anyhow = "1.0.65"
1010
serde = { version = "1.0.147", features = ["derive"] }
1111
serde_json = "1.0.85"
12+
similar = "2.7.0"
1213
spdx-rs = "0.5.1"

src/tools/collect-license-metadata/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@ mod reuse;
55
use std::path::PathBuf;
66

77
use anyhow::{Context, Error};
8+
use similar::{ChangeTag, TextDiff};
89

910
use crate::licenses::LicensesInterner;
1011

12+
fn diff_text(expected: &str, actual: &str) {
13+
for change in TextDiff::from_lines(expected, actual).iter_all_changes() {
14+
let sign = match change.tag() {
15+
ChangeTag::Delete => "-",
16+
ChangeTag::Insert => "+",
17+
ChangeTag::Equal => " ",
18+
};
19+
print!("{}{}", sign, change);
20+
}
21+
}
22+
1123
/// The entry point to the binary.
1224
///
1325
/// You should probably let `bootstrap` execute this program instead of running it directly.
@@ -41,6 +53,8 @@ fn main() -> Result<(), Error> {
4153
if existing_json != output {
4254
eprintln!("The existing {} file is out of date.", dest.display());
4355
eprintln!("Run ./x run collect-license-metadata to update it.");
56+
eprintln!("Diff:");
57+
diff_text(&existing, &serde_json::to_string_pretty(&output).unwrap());
4458
anyhow::bail!("The existing {} file doesn't match what REUSE reports.", dest.display());
4559
}
4660
println!("license information matches");

0 commit comments

Comments
 (0)