-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
--show-coverage json #66472
--show-coverage json #66472
Changes from all commits
8858d71
10492c3
15babed
f1070b1
1c01646
b646627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
use crate::clean; | ||
use crate::config::OutputFormat; | ||
use crate::core::DocContext; | ||
use crate::fold::{self, DocFolder}; | ||
use crate::passes::Pass; | ||
|
||
use rustc_ast::attr; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::FileName; | ||
use serde::Serialize; | ||
use serde_json; | ||
|
||
use std::collections::BTreeMap; | ||
use std::ops; | ||
|
@@ -16,16 +19,16 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass { | |
description: "counts the number of items with and without documentation", | ||
}; | ||
|
||
fn calculate_doc_coverage(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate { | ||
let mut calc = CoverageCalculator::default(); | ||
fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::Crate { | ||
let mut calc = CoverageCalculator::new(); | ||
let krate = calc.fold_crate(krate); | ||
|
||
calc.print_results(); | ||
calc.print_results(ctx.renderinfo.borrow().output_format); | ||
|
||
krate | ||
} | ||
|
||
#[derive(Default, Copy, Clone)] | ||
#[derive(Default, Copy, Clone, Serialize)] | ||
struct ItemCount { | ||
total: u64, | ||
with_docs: u64, | ||
|
@@ -64,13 +67,41 @@ impl ops::AddAssign for ItemCount { | |
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct CoverageCalculator { | ||
items: BTreeMap<FileName, ItemCount>, | ||
} | ||
|
||
fn limit_filename_len(filename: String) -> String { | ||
let nb_chars = filename.chars().count(); | ||
if nb_chars > 35 { | ||
"...".to_string() | ||
+ &filename[filename.char_indices().nth(nb_chars - 32).map(|x| x.0).unwrap_or(0)..] | ||
} else { | ||
filename | ||
} | ||
} | ||
|
||
impl CoverageCalculator { | ||
fn print_results(&self) { | ||
fn new() -> CoverageCalculator { | ||
CoverageCalculator { items: Default::default() } | ||
} | ||
|
||
fn to_json(&self) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be cleaner to implement |
||
serde_json::to_string( | ||
&self | ||
.items | ||
.iter() | ||
.map(|(k, v)| (k.to_string(), v)) | ||
.collect::<BTreeMap<String, &ItemCount>>(), | ||
) | ||
.expect("failed to convert JSON data to string") | ||
} | ||
|
||
fn print_results(&self, output_format: Option<OutputFormat>) { | ||
if output_format.map(|o| o.is_json()).unwrap_or_else(|| false) { | ||
println!("{}", self.to_json()); | ||
return; | ||
} | ||
let mut total = ItemCount::default(); | ||
|
||
fn print_table_line() { | ||
|
@@ -93,15 +124,7 @@ impl CoverageCalculator { | |
|
||
for (file, &count) in &self.items { | ||
if let Some(percentage) = count.percentage() { | ||
let mut name = file.to_string(); | ||
// if a filename is too long, shorten it so we don't blow out the table | ||
// FIXME(misdreavus): this needs to count graphemes, and probably also track | ||
// double-wide characters... | ||
if name.len() > 35 { | ||
name = "...".to_string() + &name[name.len() - 32..]; | ||
} | ||
|
||
print_table_record(&name, count, percentage); | ||
print_table_record(&limit_filename_len(file.to_string()), count, percentage); | ||
|
||
total += count; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// compile-flags:-Z unstable-options --output-format html --show-coverage | ||
|
||
/// Foo | ||
pub struct Xo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
error: html output format isn't supported for the --show-coverage option | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// build-pass | ||
// compile-flags:-Z unstable-options --output-format json --show-coverage | ||
|
||
pub mod foo { | ||
/// Hello! | ||
pub struct Foo; | ||
/// Bar | ||
pub enum Bar { A } | ||
} | ||
|
||
/// X | ||
pub struct X; | ||
|
||
/// Bar | ||
pub mod bar { | ||
/// bar | ||
pub struct Bar; | ||
/// X | ||
pub enum X { Y } | ||
} | ||
|
||
/// yolo | ||
pub enum Yolo { X } | ||
|
||
pub struct Xo<T: Clone> { | ||
x: T, | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1 @@ | ||||||||||||||||
{"$DIR/json.rs":{"total":13,"with_docs":7}} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't yet pass on Windows because of how rust/src/tools/compiletest/src/runtest.rs Lines 3213 to 3215 in abc3073
cflags.contains("--output-format json") || cflags.contains("--output-format=json") to rust/src/tools/compiletest/src/runtest.rs Lines 3204 to 3207 in abc3073
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch, thanks a lot! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
output_format
field would fit better onDocContext
thanRenderInfo
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's impacting the rendering so from my point of view, it fits better here. :)