Skip to content

Commit

Permalink
feat(coverage): add summary reporter (denoland#21535)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Dec 12, 2023
1 parent a4f45f7 commit 5ddf873
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 58 deletions.
13 changes: 11 additions & 2 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct CompletionsFlags {

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CoverageType {
Summary,
Pretty,
Lcov,
Html,
Expand Down Expand Up @@ -1413,6 +1414,12 @@ Generate html reports from lcov:
)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("pretty")
.long("pretty")
.help("Output coverage report in pretty format in the terminal.")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("files")
.num_args(1..)
Expand Down Expand Up @@ -3320,8 +3327,10 @@ fn coverage_parse(flags: &mut Flags, matches: &mut ArgMatches) {
CoverageType::Lcov
} else if matches.get_flag("html") {
CoverageType::Html
} else {
} else if matches.get_flag("pretty") {
CoverageType::Pretty
} else {
CoverageType::Summary
};
let output = matches.remove_one::<PathBuf>("output");
flags.subcommand = DenoSubcommand::Coverage(CoverageFlags {
Expand Down Expand Up @@ -7908,7 +7917,7 @@ mod tests {
output: None,
include: vec![r"^file:".to_string()],
exclude: vec![r"test\.(js|mjs|ts|jsx|tsx)$".to_string()],
r#type: CoverageType::Pretty
r#type: CoverageType::Summary
}),
..Flags::default()
}
Expand Down
61 changes: 58 additions & 3 deletions cli/tests/integration/coverage_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ fn run_coverage_text(test_name: &str, extension: &str) {

let output = context
.new_command()
.args_vec(vec!["coverage".to_string(), format!("{}/", tempdir)])
.args_vec(vec![
"coverage".to_string(),
"--pretty".to_string(),
format!("{}/", tempdir),
])
.split_output()
.run();

Expand Down Expand Up @@ -184,7 +188,11 @@ fn multifile_coverage() {

let output = context
.new_command()
.args_vec(vec!["coverage".to_string(), format!("{}/", tempdir)])
.args_vec(vec![
"coverage".to_string(),
"--pretty".to_string(),
format!("{}/", tempdir),
])
.split_output()
.run();

Expand Down Expand Up @@ -255,6 +263,7 @@ fn no_snaps_included(test_name: &str, extension: &str) {
.args_vec(vec![
"coverage".to_string(),
"--include=no_snaps_included.ts".to_string(),
"--pretty".to_string(),
format!("{}/", tempdir),
])
.split_output()
Expand Down Expand Up @@ -303,6 +312,7 @@ fn no_tests_included(test_name: &str, extension: &str) {
.args_vec(vec![
"coverage".to_string(),
format!("--exclude={}", util::std_path().canonicalize()),
"--pretty".to_string(),
format!("{}/", tempdir),
])
.split_output()
Expand Down Expand Up @@ -350,7 +360,11 @@ fn no_npm_cache_coverage() {

let output = context
.new_command()
.args_vec(vec!["coverage".to_string(), format!("{}/", tempdir)])
.args_vec(vec![
"coverage".to_string(),
"--pretty".to_string(),
format!("{}/", tempdir),
])
.split_output()
.run();

Expand Down Expand Up @@ -397,6 +411,7 @@ fn no_transpiled_lines() {
.args_vec(vec![
"coverage".to_string(),
"--include=no_transpiled_lines/index.ts".to_string(),
"--pretty".to_string(),
format!("{}/", tempdir),
])
.run();
Expand Down Expand Up @@ -575,3 +590,43 @@ fn test_html_reporter() {
.unwrap();
assert!(baz_quux_ts_html.contains("<h1>Coverage report for baz/quux.ts</h1>"));
}

#[test]
fn test_summary_reporter() {
let context = TestContext::default();
let tempdir = context.temp_dir();
let tempdir = tempdir.path().join("cov");

let output = context
.new_command()
.args_vec(vec![
"test".to_string(),
"--quiet".to_string(),
format!("--coverage={}", tempdir),
"coverage/multisource".to_string(),
])
.run();

output.assert_exit_code(0);
output.skip_output_check();

let output = context
.new_command()
.args_vec(vec!["coverage".to_string(), format!("{}/", tempdir)])
.run();

output.assert_exit_code(0);
output.assert_matches_text(
"----------------------------------
File | Branch % | Line % |
----------------------------------
bar.ts | 0.0 | 57.1 |
baz/quux.ts | 0.0 | 28.6 |
baz/qux.ts | 100.0 | 100.0 |
foo.ts | 50.0 | 76.9 |
----------------------------------
All files | 40.0 | 61.0 |
----------------------------------
",
);
}
9 changes: 8 additions & 1 deletion cli/tests/testdata/coverage/multisource/baz/quux.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export function quux(cond: boolean) {
if (cond) {
return 1;
const a = 1;
const b = a;
const c = b;
const d = c;
const e = d;
const f = e;
const g = f;
return g;
} else {
return 2;
}
Expand Down
7 changes: 7 additions & 0 deletions cli/tests/testdata/coverage/multisource/foo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export function foo(cond: boolean) {
let a = 0;
if (cond) {
a = 1;
} else {
a = 2;
}

if (a == 4) {
return 1;
} else {
return 2;
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/testdata/coverage/multisource/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { quux } from "./baz/quux.ts";

Deno.test("foo", () => {
foo(true);
foo(false);
});

Deno.test("bar", () => {
Expand All @@ -13,6 +14,7 @@ Deno.test("bar", () => {

Deno.test("qux", () => {
qux(true);
qux(false);
});

Deno.test("quux", () => {
Expand Down
Loading

0 comments on commit 5ddf873

Please sign in to comment.