Skip to content

Commit 38ba774

Browse files
GuillaumeGomezJoshua Nelson
authored and
Joshua Nelson
committed
Extend coverage feature
1 parent 0d17c61 commit 38ba774

File tree

8 files changed

+70
-8
lines changed

8 files changed

+70
-8
lines changed

src/db/add_package.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,24 @@ pub(crate) fn add_doc_coverage(
137137
) -> Result<i32> {
138138
debug!("Adding doc coverage into database");
139139
let rows = conn.query(
140-
"INSERT INTO doc_coverage (release_id, total_items, documented_items)
141-
VALUES ($1, $2, $3)
140+
"INSERT INTO doc_coverage (
141+
release_id, total_items, documented_items,
142+
total_items_needing_examples, items_with_examples
143+
)
144+
VALUES ($1, $2, $3, $4, $5)
142145
ON CONFLICT (release_id) DO UPDATE
143146
SET
144147
total_items = $2,
145-
documented_items = $3
148+
documented_items = $3,
149+
total_items_needing_examples = $4,
150+
items_with_examples = $5
146151
RETURNING release_id",
147152
&[
148153
&release_id,
149154
&doc_coverage.total_items,
150155
&doc_coverage.documented_items,
156+
&doc_coverage.total_items_needing_examples,
157+
&doc_coverage.items_with_examples,
151158
],
152159
)?;
153160
Ok(rows[0].get(0))

src/db/migrate.rs

+19
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,25 @@ pub fn migrate(version: Option<Version>, conn: &mut Client) -> CratesfyiResult<(
438438
ALTER TABLE owners ALTER COLUMN name DROP NOT NULL;
439439
ALTER TABLE crates ALTER COLUMN github_stars DROP NOT NULL;
440440
"
441+
),
442+
migration!(
443+
context,
444+
// version
445+
18,
446+
// description
447+
"Add more information into doc coverage",
448+
// upgrade query
449+
"
450+
ALTER TABLE doc_coverage
451+
ADD COLUMN total_items_needing_examples INT,
452+
ADD COLUMN items_with_examples INT;
453+
",
454+
// downgrade query
455+
"
456+
ALTER TABLE doc_coverage
457+
DROP COLUMN total_items_needing_examples,
458+
DROP COLUMN items_with_examples;
459+
"
441460
)
442461
];
443462

src/docbuilder/rustwide_builder.rs

+11
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,15 @@ impl RustwideBuilder {
455455
struct FileCoverage {
456456
total: i32,
457457
with_docs: i32,
458+
total_examples: i32,
459+
with_examples: i32,
458460
}
459461

460462
let mut coverage = DocCoverage {
461463
total_items: 0,
462464
documented_items: 0,
465+
total_items_needing_examples: 0,
466+
items_with_examples: 0,
463467
};
464468

465469
self.prepare_command(build, target, metadata, limits, rustdoc_flags)?
@@ -472,6 +476,8 @@ impl RustwideBuilder {
472476
for file in parsed.values() {
473477
coverage.total_items += file.total;
474478
coverage.documented_items += file.with_docs;
479+
coverage.total_items_needing_examples += file.total_examples;
480+
coverage.items_with_examples += file.with_examples;
475481
}
476482
}
477483
})
@@ -670,6 +676,11 @@ pub(crate) struct DocCoverage {
670676
pub(crate) total_items: i32,
671677
/// The items of the crate that are documented, used to calculate documentation coverage.
672678
pub(crate) documented_items: i32,
679+
/// The total items that could have code examples in the current crate, used to calculate
680+
/// documentation coverage.
681+
pub(crate) total_items_needing_examples: i32,
682+
/// The items of the crate that have a code example, used to calculate documentation coverage.
683+
pub(crate) items_with_examples: i32,
673684
}
674685

675686
pub(crate) struct BuildResult {

src/test/fakes.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,18 @@ impl<'a> FakeRelease<'a> {
183183
self
184184
}
185185

186-
pub(crate) fn coverage(mut self, documented_items: i32, total_items: i32) -> Self {
186+
pub(crate) fn coverage(
187+
mut self,
188+
documented_items: i32,
189+
total_items: i32,
190+
total_items_needing_examples: i32,
191+
items_with_examples: i32,
192+
) -> Self {
187193
self.build_result.doc_coverage = Some(DocCoverage {
188194
total_items,
189195
documented_items,
196+
total_items_needing_examples,
197+
items_with_examples,
190198
});
191199
self
192200
}

src/web/crate_details.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub struct CrateDetails {
4545
documentation_url: Option<String>,
4646
total_items: Option<f32>,
4747
documented_items: Option<f32>,
48+
total_items_needing_examples: Option<f32>,
49+
items_with_examples: Option<f32>,
4850
}
4951

5052
fn optional_markdown<S>(markdown: &Option<String>, serializer: S) -> Result<S::Ok, S::Error>
@@ -100,7 +102,9 @@ impl CrateDetails {
100102
releases.documentation_url,
101103
releases.default_target,
102104
doc_coverage.total_items,
103-
doc_coverage.documented_items
105+
doc_coverage.documented_items,
106+
doc_coverage.total_items_needing_examples,
107+
doc_coverage.items_with_examples
104108
FROM releases
105109
INNER JOIN crates ON releases.crate_id = crates.id
106110
LEFT JOIN doc_coverage ON doc_coverage.release_id = releases.id
@@ -156,6 +160,8 @@ impl CrateDetails {
156160

157161
let documented_items: Option<i32> = krate.get("documented_items");
158162
let total_items: Option<i32> = krate.get("total_items");
163+
let total_items_needing_examples: Option<i32> = krate.get("total_items_needing_examples");
164+
let items_with_examples: Option<i32> = krate.get("items_with_examples");
159165

160166
let mut crate_details = CrateDetails {
161167
name: krate.get("name"),
@@ -189,6 +195,8 @@ impl CrateDetails {
189195
documentation_url: krate.get("documentation_url"),
190196
documented_items: documented_items.map(|v| v as f32),
191197
total_items: total_items.map(|v| v as f32),
198+
total_items_needing_examples: total_items_needing_examples.map(|v| v as f32),
199+
items_with_examples: items_with_examples.map(|v| v as f32),
192200
};
193201

194202
if let Some(repository_url) = crate_details.repository_url.clone() {

src/web/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,12 @@ mod test {
644644
.name("foo")
645645
.version("0.0.1")
646646
.source_file("test.rs", &[])
647-
.coverage(6, 10)
647+
.coverage(6, 10, 2, 1)
648648
.create()?;
649649
let web = env.frontend();
650650

651651
let foo_crate = kuchiki::parse_html().one(web.get("/crate/foo/0.0.1").send()?.text()?);
652-
for value in &["60%", "6", "10"] {
652+
for value in &["60%", "6", "10", "2", "1"] {
653653
assert!(foo_crate
654654
.select(".pure-menu-item b")
655655
.unwrap()

templates/crate/details.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
{% set percent = details.documented_items * 100 / details.total_items %}
2121
<li class="pure-menu-heading">Coverage</li>
2222
<li class="pure-menu-item" style="text-align:center;"><b>{{ percent | round(precision=2) }}%</b><br>
23-
<span style="font-size: 13px;"><b>{{ details.documented_items }}</b> out of <b>{{ details.total_items }}</b> items documented</span>
23+
<span class="documented-info"><b>{{ details.documented_items }}</b> out of <b>{{ details.total_items }}</b> items documented</span>
24+
{%- if details.total_items_needing_examples and details.items_with_examples -%}
25+
<span style="font-size: 13px;"><b>{{ details.items_with_examples }}</b> out of <b>{{ details.total_items_needing_examples }}</b> items with examples</span>
26+
{%- endif -%}
2427
</li>
2528
{%- endif -%}
2629
{# List the release author's names and a link to their docs.rs profile #}

templates/style/base.scss

+6
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ div.package-page-container {
311311
margin: 20px 5px 15px 5px;
312312
}
313313

314+
li.pure-menu-item > .documented-info {
315+
font-size: 13px;
316+
display: block;
317+
width: 100%;
318+
}
319+
314320
li.pure-menu-heading:first-child {
315321
margin-top: 0;
316322
}

0 commit comments

Comments
 (0)