Skip to main content

Source tests/metadata.rs

1#[allow(dead_code)]2mod support;3use scraper::{Html, Selector};4use serde_json::Value;5use std::path::Path;6use support::*;78fn read(path: &Path) -> Value {9    serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap()10}11#[test]12fn new_report_case_alias_metadata_is_internal_and_repeated_runs_succeed() {13    let f = Fixture::new("clean");14    let probe = f.root.join("case-probe");15    std::fs::write(&probe, "probe").unwrap();16    if !f.root.join("CASE-PROBE").exists() {17        return;18    }19    for (report, metadata) in [20        ("Report", "report/navigation.json"),21        ("Report", "Report/navigation.json"),22        ("Report", "REPORT/navigation.json"),23        ("Nested/Report", "nested/REPORT/navigation.json"),24        ("Étage", "étage/navigation.json"),25    ] {26        assert_success(&f.run(&["--output", report, "--metadata", metadata]));27        assert!(f.root.join(report).join("index.html").is_file());28        assert_eq!(read(&f.root.join(metadata))["schema_version"], 1);29    }30}31#[test]32fn distinct_case_sensitive_output_paths_remain_external() {33    let f = Fixture::new("clean");34    std::fs::write(f.root.join("case-probe"), "probe").unwrap();35    if f.root.join("CASE-PROBE").exists() {36        return;37    }38    assert_success(&f.run(&["--output", "Report", "--metadata", "report/navigation.json"]));39    assert!(f.root.join("Report/index.html").is_file());40    assert!(!f.root.join("report/index.html").exists());41    assert_eq!(42        read(&f.root.join("report/navigation.json"))["schema_version"],43        144    );45}46fn assert_url(root: &Path, href: &str) {47    assert!(!href.starts_with('/') && !href.contains('\\') && !href.contains("://"));48    let base = url::Url::from_directory_path(root).unwrap();49    let target = base.join(href).unwrap();50    let file = target.to_file_path().unwrap().canonicalize().unwrap();51    assert!(file.starts_with(root.canonicalize().unwrap()));52    let html = Html::parse_document(&std::fs::read_to_string(file).unwrap());53    if let Some(fragment) = target.fragment() {54        assert!(55            html.select(&Selector::parse("[id]").unwrap())56                .any(|e| e.value().id() == Some(fragment)),57            "{href}"58        );59    }60}61#[test]62fn metadata_is_optional_and_links_to_real_report_destinations() {63    let f = Fixture::new("warning");64    assert_success(&f.run(&[]));65    assert!(!f.report().join("metadata.json").exists());66    assert_success(&f.run(&["--metadata", "target/clippy-html/metadata.json"]));67    let json = read(&f.report().join("metadata.json"));68    assert_eq!(json["schema_version"], 1);69    assert_eq!(json["generator"]["name"], "cargo-clippy-html");70    assert_eq!(json["generator"]["version"], env!("CARGO_PKG_VERSION"));71    assert_eq!(json["execution"]["clippy_exit_code"], 0);72    assert_eq!(json["execution"]["collection_complete"], true);73    assert!(json.get("report_url").is_none() && json.get("toolchain").is_none());74    let pages = json["pages"].as_array().unwrap();75    for kind in ["overview", "lint", "category", "source"] {76        assert!(pages.iter().any(|p| p["kind"] == kind));77    }78    for page in pages {79        assert_url(&f.report(), page["url"].as_str().unwrap());80    }81    let findings = json["findings"].as_array().unwrap();82    assert!(!findings.is_empty()); warning: clippy::assert_is_empty83    for finding in findings {84        assert_eq!(finding["code"], "clippy::clone_on_copy");85        assert_eq!(finding["level"], "warning");86        assert_eq!(finding["category"], "complexity");87        assert!(finding["message"].as_str().unwrap().contains("clone"));88        assert!(89            json["packages"]90                .as_array()91                .unwrap()92                .iter()93                .any(|p| p["id"] == finding["package_id"])94        );95        assert!(finding.get("suggestions").is_none() && finding.get("children").is_none());96        assert_url(&f.report(), finding["url"].as_str().unwrap());97        assert_eq!(finding["location"]["path"], "src/lib.rs");98        assert_eq!(finding["location"]["line"], 3);99        assert_url(&f.report(), finding["location"]["url"].as_str().unwrap());100    }101}102#[test]103fn external_metadata_replaces_the_selected_file_without_using_its_folder_as_url_base() {104    let f = Fixture::new("workspace");105    let path = f.root.join("수집 결과/nested/navigation.json");106    std::fs::create_dir_all(path.parent().unwrap()).unwrap();107    std::fs::write(&path, "old result").unwrap();108    assert_success(&f.run(&[109        "--metadata",110        "수집 결과/nested/navigation.json",111        "--",112        "--all-targets",113    ]));114    let json = read(&path);115    assert!(json["packages"].as_array().unwrap().len() >= 2);116    let ids: std::collections::BTreeSet<_> = json["findings"]117        .as_array()118        .unwrap()119        .iter()120        .map(|f| f["id"].as_str().unwrap())121        .collect();122    assert_eq!(ids.len(), json["findings"].as_array().unwrap().len());123    for page in json["pages"].as_array().unwrap() {124        assert_url(&f.report(), page["url"].as_str().unwrap());125    }126    for finding in json["findings"].as_array().unwrap() {127        assert_url(&f.report(), finding["url"].as_str().unwrap());128        if let Some(loc) = finding.get("location") {129            assert_url(&f.report(), loc["url"].as_str().unwrap());130        }131    }132    assert_success(&f.run(&[133        "--metadata",134        "수집 결과/nested/navigation.json",135        "--",136        "--all-targets",137    ]));138    assert_eq!(139        json,140        read(&path),141        "cached builds retain the same navigation index"142    );143}144#[test]145fn metadata_preserves_failed_and_clean_inspection_states() {146    for (fixture, success) in [("clean", true), ("compile-error", false)] {147        let f = Fixture::new(fixture);148        let output = f.run(&["--metadata", "navigation.json"]);149        assert_eq!(output.status.success(), success);150        let json = read(&f.root.join("navigation.json"));151        assert_eq!(152            json["execution"]["clippy_exit_code"],153            if success { 0 } else { 101 }154        );155        if success {156            assert!(json["findings"].as_array().unwrap().is_empty()); warning: clippy::assert_is_empty157        }158        for finding in json["findings"].as_array().unwrap() {159            assert_url(&f.report(), finding["url"].as_str().unwrap());160        }161    }162}163164#[test]165fn metadata_conflicts_and_invalid_external_paths_keep_the_previous_report() {166    let f = Fixture::new("warning");167    assert_success(&f.run(&["--metadata", "target/clippy-html/navigation.json"]));168    let original = std::fs::read(f.report().join("index.html")).unwrap();169    let old_metadata = std::fs::read(f.report().join("navigation.json")).unwrap();170    let external = f.root.join("external.json");171    std::fs::write(&external, "old external").unwrap();172    for path in [173        "target/clippy-html/index.html",174        "target/clippy-html/static.files/search-index.js",175        "target/clippy-html/static.files",176        "target/clippy-html/.clippy-html-owned",177        "target/.clippy-html-publish.lock",178        "target/clippy-html",179        "external.json/child.json",180    ] {181        let result = f.run(&["--metadata", path]);182        assert!(!result.status.success(), "accepted {path}");183        assert_eq!(184            original,185            std::fs::read(f.report().join("index.html")).unwrap()186        );187        assert_eq!(188            old_metadata,189            std::fs::read(f.report().join("navigation.json")).unwrap()190        );191        assert_eq!(std::fs::read(&external).unwrap(), b"old external");192    }193    let result = f.run(&["--output", "external.json", "--metadata", "external.json"]);194    assert!(!result.status.success());195    assert_eq!(std::fs::read(external).unwrap(), b"old external");196}197198#[test]199fn report_local_metadata_replaces_with_the_report_and_is_absent_without_opt_in() {200    let f = Fixture::new("clean");201    for _ in 0..2 {202        assert_success(&f.run(&["--metadata", "target/clippy-html/索引 space/nested.json"]));203        assert_eq!(204            read(&f.report().join("索引 space/nested.json"))["schema_version"],205            1206        );207    }208    assert_success(&f.run(&[]));209    assert!(!f.report().join("索引 space/nested.json").exists());210}211

Findings in this file§

warning clippy::assert_is_empty §

used `assert!` to check that a value is not empty

tests/metadata.rs:82:5

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • metadata (test)

Primary: tests/metadata.rs:82:5

    assert!(!findings.is_empty());

Primary: tests/metadata.rs:82:11

    assert!(!findings.is_empty());

Primary: tests/metadata.rs:82:13

    assert!(!findings.is_empty());

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#assert_is_empty

note: `-W clippy::assert-is-empty` implied by `-W clippy::pedantic`

help: to override `-W clippy::pedantic` add `#[allow(clippy::assert_is_empty)]`

help: use `assert_ne!` to show the value on failure

Suggestion: use `assert_ne!` to show the value on failure

MachineApplicable

--- "a/tests/metadata.rs"
+++ "b/tests/metadata.rs"
@@ -79,7 +79,7 @@
         assert_url(&f.report(), page["url"].as_str().unwrap());
     }
     let findings = json["findings"].as_array().unwrap();
-    assert!(!findings.is_empty());
+    assert_ne!(findings.as_slice(), []);
     for finding in findings {
         assert_eq!(finding["code"], "clippy::clone_on_copy");
         assert_eq!(finding["level"], "warning");
Rendered diagnostic (text)
warning: used `assert!` to check that a value is not empty
  --> tests/metadata.rs:82:5
   |
82 |     assert!(!findings.is_empty());
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#assert_is_empty
   = note: `-W clippy::assert-is-empty` implied by `-W clippy::pedantic`
   = help: to override `-W clippy::pedantic` add `#[allow(clippy::assert_is_empty)]`
help: use `assert_ne!` to show the value on failure
   |
82 -     assert!(!findings.is_empty());
82 +     assert_ne!(findings.as_slice(), []);
   |

warning clippy::assert_is_empty §

used `assert!` to check that a value is empty

tests/metadata.rs:156:13

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • metadata (test)

Primary: tests/metadata.rs:156:13

            assert!(json["findings"].as_array().unwrap().is_empty());

Primary: tests/metadata.rs:156:19

            assert!(json["findings"].as_array().unwrap().is_empty());

Primary: tests/metadata.rs:156:21

            assert!(json["findings"].as_array().unwrap().is_empty());

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#assert_is_empty

help: use `assert_eq!` to show the value on failure

Suggestion: use `assert_eq!` to show the value on failure

MachineApplicable

--- "a/tests/metadata.rs"
+++ "b/tests/metadata.rs"
@@ -153,7 +153,7 @@
             if success { 0 } else { 101 }
         );
         if success {
-            assert!(json["findings"].as_array().unwrap().is_empty());
+            assert_eq!(json["findings"].as_array().unwrap().as_slice(), []);
         }
         for finding in json["findings"].as_array().unwrap() {
             assert_url(&f.report(), finding["url"].as_str().unwrap());
Rendered diagnostic (text)
warning: used `assert!` to check that a value is empty
   --> tests/metadata.rs:156:13
    |
156 |             assert!(json["findings"].as_array().unwrap().is_empty());
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#assert_is_empty
help: use `assert_eq!` to show the value on failure
    |
156 -             assert!(json["findings"].as_array().unwrap().is_empty());
156 +             assert_eq!(json["findings"].as_array().unwrap().as_slice(), []);
    |