From 547ce6c3b82c5e0c04dfb902f609cb229a2bde9a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 8 May 2024 16:07:56 -0400 Subject: [PATCH] chore: cleanup some coverage tests (#23738) --- tests/integration/coverage_tests.rs | 213 +++++++++------------------- 1 file changed, 69 insertions(+), 144 deletions(-) diff --git a/tests/integration/coverage_tests.rs b/tests/integration/coverage_tests.rs index 1bc3e887a8..a8a7ca0232 100644 --- a/tests/integration/coverage_tests.rs +++ b/tests/integration/coverage_tests.rs @@ -1,11 +1,12 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. use deno_core::serde_json; -use std::fs; use test_util as util; use test_util::TempDir; +use util::assert_contains; use util::assert_starts_with; use util::env_vars_for_npm_tests; +use util::PathRef; use util::TestContext; use util::TestContextBuilder; @@ -60,9 +61,9 @@ fn error_if_invalid_cache() { let mod_test_temp_path = temp_dir_path.join("mod.test.ts"); // Write the initial mod.ts file - std::fs::copy(mod_before_path, &mod_temp_path).unwrap(); + mod_before_path.copy(&mod_temp_path); // And the test file - std::fs::copy(mod_test_path, mod_test_temp_path).unwrap(); + mod_test_path.copy(&mod_test_temp_path); // Generate coverage let output = context @@ -78,7 +79,7 @@ fn error_if_invalid_cache() { output.skip_output_check(); // Modify the file between deno test and deno coverage, thus invalidating the cache - std::fs::copy(mod_after_path, mod_temp_path).unwrap(); + mod_after_path.copy(&mod_temp_path); let output = context .new_command() @@ -90,8 +91,8 @@ fn error_if_invalid_cache() { // Expect error let error = util::strip_ansi_codes(out).to_string(); - assert!(error.contains("error: Missing transpiled source code")); - assert!(error.contains("Before generating coverage report, run `deno test --coverage` to ensure consistent state.")); + assert_contains!(error, "error: Missing transpiled source code"); + assert_contains!(error, "Before generating coverage report, run `deno test --coverage` to ensure consistent state."); } fn run_coverage_text(test_name: &str, extension: &str) { @@ -126,18 +127,9 @@ fn run_coverage_text(test_name: &str, extension: &str) { // Verify there's no "Check" being printed assert!(output.stderr().is_empty()); - let actual = util::strip_ansi_codes(output.stdout()).to_string(); - - let expected = fs::read_to_string( + output.assert_stdout_matches_file( util::testdata_path().join(format!("coverage/{test_name}_expected.out")), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); @@ -151,18 +143,9 @@ fn run_coverage_text(test_name: &str, extension: &str) { ]) .run(); - let actual = util::strip_ansi_codes(output.combined_output()).to_string(); - - let expected = fs::read_to_string( + output.assert_matches_file( util::testdata_path().join(format!("coverage/{test_name}_expected.lcov")), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); } @@ -199,18 +182,9 @@ fn multifile_coverage() { // Verify there's no "Check" being printed assert!(output.stderr().is_empty()); - let actual = util::strip_ansi_codes(output.stdout()).to_string(); - - let expected = fs::read_to_string( + output.assert_stdout_matches_file( util::testdata_path().join("coverage/multifile/expected.out"), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); let output = context @@ -223,18 +197,9 @@ fn multifile_coverage() { ]) .run(); - let actual = util::strip_ansi_codes(output.combined_output()).to_string(); - - let expected = fs::read_to_string( + output.assert_matches_file( util::testdata_path().join("coverage/multifile/expected.lcov"), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); } @@ -272,18 +237,9 @@ fn no_snaps_included(test_name: &str, extension: &str) { // Verify there's no "Check" being printed assert!(output.stderr().is_empty()); - let actual = util::strip_ansi_codes(output.stdout()).to_string(); - - let expected = fs::read_to_string( + output.assert_stdout_matches_file( util::testdata_path().join("coverage/no_snaps_included/expected.out"), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); } @@ -321,18 +277,9 @@ fn no_tests_included(test_name: &str, extension: &str) { // Verify there's no "Check" being printed assert!(output.stderr().is_empty()); - let actual = util::strip_ansi_codes(output.stdout()).to_string(); - - let expected = fs::read_to_string( + output.assert_stdout_matches_file( util::testdata_path().join("coverage/no_tests_included/expected.out"), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); } @@ -371,18 +318,9 @@ fn no_npm_cache_coverage() { // Verify there's no "Check" being printed assert!(output.stderr().is_empty()); - let actual = util::strip_ansi_codes(output.stdout()).to_string(); - - let expected = fs::read_to_string( + output.assert_stdout_matches_file( util::testdata_path().join("coverage/no_npm_coverage/expected.out"), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); } @@ -416,18 +354,9 @@ fn no_transpiled_lines() { ]) .run(); - let actual = util::strip_ansi_codes(output.combined_output()).to_string(); - - let expected = fs::read_to_string( + output.assert_matches_file( util::testdata_path().join("coverage/no_transpiled_lines/expected.out"), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } + ); output.assert_exit_code(0); @@ -441,19 +370,9 @@ fn no_transpiled_lines() { ]) .run(); - let actual = util::strip_ansi_codes(output.combined_output()).to_string(); - - let expected = fs::read_to_string( + output.assert_matches_file( util::testdata_path().join("coverage/no_transpiled_lines/expected.lcov"), - ) - .unwrap(); - - if !util::wildcard_match(&expected, &actual) { - println!("OUTPUT\n{actual}\nOUTPUT"); - println!("EXPECTED\n{expected}\nEXPECTED"); - panic!("pattern match failed"); - } - + ); output.assert_exit_code(0); } @@ -477,10 +396,10 @@ fn no_internal_code() { output.skip_output_check(); // Check that coverage files contain no internal urls - let paths = fs::read_dir(tempdir).unwrap(); + let paths = tempdir.read_dir(); for path in paths { - let unwrapped = path.unwrap().path(); - let data = fs::read_to_string(&unwrapped.clone()).unwrap(); + let unwrapped = PathRef::new(path.unwrap().path()); + let data = unwrapped.read_to_string(); let value: serde_json::Value = serde_json::from_str(&data).unwrap(); let url = value["url"].as_str().unwrap(); @@ -509,10 +428,10 @@ fn no_internal_node_code() { output.skip_output_check(); // Check that coverage files contain no internal urls - let paths = fs::read_dir(tempdir).unwrap(); + let paths = tempdir.read_dir(); for path in paths { - let unwrapped = path.unwrap().path(); - let data = fs::read_to_string(&unwrapped.clone()).unwrap(); + let unwrapped = PathRef::new(path.unwrap().path()); + let data = unwrapped.read_to_string(); let value: serde_json::Value = serde_json::from_str(&data).unwrap(); let url = value["url"].as_str().unwrap(); @@ -551,44 +470,50 @@ fn test_html_reporter() { output.assert_exit_code(0); output.assert_matches_text("HTML coverage report has been generated at [WILDCARD]/cov/html/index.html\n"); - let index_html = - fs::read_to_string(tempdir.join("html").join("index.html")).unwrap(); - assert!(index_html.contains("

Coverage report for all files

")); - assert!(index_html.contains("baz/")); - assert!(index_html.contains("href='baz/index.html'")); - assert!(index_html.contains("foo.ts")); - assert!(index_html.contains("href='foo.ts.html'")); - assert!(index_html.contains("bar.ts")); - assert!(index_html.contains("href='bar.ts.html'")); + let index_html = tempdir.join("html").join("index.html").read_to_string(); + assert_contains!(index_html, "

Coverage report for all files

"); + assert_contains!(index_html, "baz/"); + assert_contains!(index_html, "href='baz/index.html'"); + assert_contains!(index_html, "foo.ts"); + assert_contains!(index_html, "href='foo.ts.html'"); + assert_contains!(index_html, "bar.ts"); + assert_contains!(index_html, "href='bar.ts.html'"); - let foo_ts_html = - fs::read_to_string(tempdir.join("html").join("foo.ts.html")).unwrap(); - assert!(foo_ts_html.contains("

Coverage report for foo.ts

")); + let foo_ts_html = tempdir.join("html").join("foo.ts.html").read_to_string(); + assert_contains!(foo_ts_html, "

Coverage report for foo.ts

"); - let bar_ts_html = - fs::read_to_string(tempdir.join("html").join("bar.ts.html")).unwrap(); - assert!(bar_ts_html.contains("

Coverage report for bar.ts

")); + let bar_ts_html = tempdir.join("html").join("bar.ts.html").read_to_string(); + assert_contains!(bar_ts_html, "

Coverage report for bar.ts

"); // Check in source code is escaped to <T> - assert!(bar_ts_html.contains("<T>")); + assert_contains!(bar_ts_html, "<T>"); - let baz_index_html = - fs::read_to_string(tempdir.join("html").join("baz").join("index.html")) - .unwrap(); - assert!(baz_index_html.contains("

Coverage report for baz/

")); - assert!(baz_index_html.contains("qux.ts")); - assert!(baz_index_html.contains("href='qux.ts.html'")); - assert!(baz_index_html.contains("quux.ts")); - assert!(baz_index_html.contains("href='quux.ts.html'")); + let baz_index_html = tempdir + .join("html") + .join("baz") + .join("index.html") + .read_to_string(); + assert_contains!(baz_index_html, "

Coverage report for baz/

"); + assert_contains!(baz_index_html, "qux.ts"); + assert_contains!(baz_index_html, "href='qux.ts.html'"); + assert_contains!(baz_index_html, "quux.ts"); + assert_contains!(baz_index_html, "href='quux.ts.html'"); - let baz_qux_ts_html = - fs::read_to_string(tempdir.join("html").join("baz").join("qux.ts.html")) - .unwrap(); - assert!(baz_qux_ts_html.contains("

Coverage report for baz/qux.ts

")); + let baz_qux_ts_html = tempdir + .join("html") + .join("baz") + .join("qux.ts.html") + .read_to_string(); + assert_contains!(baz_qux_ts_html, "

Coverage report for baz/qux.ts

"); - let baz_quux_ts_html = - fs::read_to_string(tempdir.join("html").join("baz").join("quux.ts.html")) - .unwrap(); - assert!(baz_quux_ts_html.contains("

Coverage report for baz/quux.ts

")); + let baz_quux_ts_html = tempdir + .join("html") + .join("baz") + .join("quux.ts.html") + .read_to_string(); + assert_contains!( + baz_quux_ts_html, + "

Coverage report for baz/quux.ts

" + ); } #[test]