Skip to content

Commit 8a05e13

Browse files
committed
remove duplicated test code
1 parent f0bed47 commit 8a05e13

File tree

2 files changed

+57
-79
lines changed

2 files changed

+57
-79
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
*.md text eol=lf
99
*.rs text eol=lf
1010
*.yaml text eol=lf
11+
*.toml text eol=lf
1112
*.yml text eol=lf
1213
*.sh text eol=lf
1314
*.cpp text eol=lf
1415
*.hpp text eol=lf
1516
*.patch text eol=lf
1617
.gitattributes text eol=lf
1718
*.txt text eol=lf
19+
*.code-workspace text eol=lf

cpp-linter-lib/src/rest_api/github_api.rs

Lines changed: 55 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,15 @@ struct User {
468468
mod test {
469469
use std::{env, io::Read, path::PathBuf};
470470

471+
use regex::Regex;
471472
use tempfile::{tempdir, NamedTempFile};
472473

473474
use super::{GithubApiClient, USER_AGENT};
474475
use crate::{
475-
clang_tools::{
476-
capture_clang_tools_output, clang_format::tally_format_advice,
477-
clang_tidy::tally_tidy_advice,
478-
},
476+
clang_tools::capture_clang_tools_output,
479477
cli::LinesChangedOnly,
480478
common_fs::FileObj,
481-
rest_api::RestApiClient,
479+
rest_api::{FeedbackInput, RestApiClient, USER_OUTREACH},
482480
};
483481

484482
// ************************** tests for GithubApiClient::make_headers()
@@ -513,94 +511,72 @@ mod test {
513511
assert_header(true, None);
514512
}
515513

516-
// ************************** tests for GithubApiClient::set_exit_code()
514+
// ************************* tests for step-summary and output variables
517515

518-
#[test]
519-
fn set_exit_code() {
520-
let rest_api_client = GithubApiClient::new();
521-
let checks_failed = 3;
522-
let format_checks_failed = 2;
523-
let tidy_checks_failed = 1;
516+
fn create_comment(tidy_checks: &str, style: &str) -> (String, String) {
524517
let tmp_dir = tempdir().unwrap();
525-
let mut tmp_file = NamedTempFile::new_in(tmp_dir.path()).unwrap();
526-
env::set_var("GITHUB_OUTPUT", tmp_file.path());
527-
assert_eq!(
528-
checks_failed,
529-
rest_api_client.set_exit_code(
530-
checks_failed,
531-
Some(format_checks_failed),
532-
Some(tidy_checks_failed)
533-
)
534-
);
535-
let mut output_file_content = String::new();
536-
tmp_file.read_to_string(&mut output_file_content).unwrap();
537-
assert!(output_file_content.contains(
538-
format!(
539-
"checks-failed={}\nformat-checks-failed={}\ntidy-checks-failed={}\n",
540-
3, 2, 1
541-
)
542-
.as_str()
543-
));
544-
println!("temp file used: {:?}", tmp_file.path());
545-
drop(tmp_file);
546-
drop(tmp_dir);
547-
}
548-
549-
// ************************* tests for comment output
550-
551-
#[test]
552-
fn check_comment_concerns() {
553-
let tmp_dir = tempdir().unwrap();
554-
let mut tmp_file = NamedTempFile::new_in(tmp_dir.path()).unwrap();
555-
let rest_api_client = GithubApiClient::new();
518+
let rest_api_client = GithubApiClient::default();
519+
if env::var("ACTIONS_STEP_DEBUG").is_ok_and(|var| var == "true") {
520+
assert!(rest_api_client.debug_enabled);
521+
}
556522
let mut files = vec![FileObj::new(PathBuf::from("tests/demo/demo.cpp"))];
557523
capture_clang_tools_output(
558524
&mut files,
559525
env::var("CLANG-VERSION").unwrap_or("".to_string()).as_str(),
560-
"readability-*",
561-
"file",
526+
tidy_checks,
527+
style,
562528
&LinesChangedOnly::Off,
563529
None,
564530
None,
565531
);
566-
let format_checks_failed = tally_format_advice(&files);
567-
let tidy_checks_failed = tally_tidy_advice(&files);
568-
let comment =
569-
rest_api_client.make_comment(&files, format_checks_failed, tidy_checks_failed, None);
570-
assert!(format_checks_failed > 0);
571-
assert!(tidy_checks_failed > 0);
572-
env::set_var("GITHUB_STEP_SUMMARY", tmp_file.path());
573-
rest_api_client.post_step_summary(&comment);
574-
let mut output_file_content = String::new();
575-
tmp_file.read_to_string(&mut output_file_content).unwrap();
576-
assert_eq!(format!("\n{comment}\n\n"), output_file_content);
532+
let feedback_inputs = FeedbackInput {
533+
style: style.to_string(),
534+
step_summary: true,
535+
..Default::default()
536+
};
537+
let mut step_summary_path = NamedTempFile::new_in(tmp_dir.path()).unwrap();
538+
env::set_var("GITHUB_STEP_SUMMARY", step_summary_path.path());
539+
let mut gh_out_path = NamedTempFile::new_in(tmp_dir.path()).unwrap();
540+
env::set_var("GITHUB_OUTPUT", gh_out_path.path());
541+
rest_api_client.post_feedback(&files, feedback_inputs);
542+
let mut step_summary_content = String::new();
543+
step_summary_path
544+
.read_to_string(&mut step_summary_content)
545+
.unwrap();
546+
assert!(&step_summary_content.contains(USER_OUTREACH));
547+
let mut gh_out_content = String::new();
548+
gh_out_path.read_to_string(&mut gh_out_content).unwrap();
549+
assert!(gh_out_content.starts_with("checks-failed="));
550+
(step_summary_content, gh_out_content)
551+
}
552+
553+
#[test]
554+
fn check_comment_concerns() {
555+
let (comment, gh_out) = create_comment("readability-*", "file");
556+
assert!(&comment.contains(":warning:\nSome files did not pass the configured checks!\n"));
557+
let fmt_pattern = Regex::new(r"format-checks-failed=(\d+)\n").unwrap();
558+
let tidy_pattern = Regex::new(r"tidy-checks-failed=(\d+)\n").unwrap();
559+
for pattern in [fmt_pattern, tidy_pattern] {
560+
let number = pattern
561+
.captures(&gh_out)
562+
.expect("found no number of checks-failed")
563+
.get(1)
564+
.unwrap()
565+
.as_str()
566+
.parse::<u64>()
567+
.unwrap();
568+
assert!(number > 0);
569+
}
577570
}
578571

579572
#[test]
580573
fn check_comment_lgtm() {
581-
let tmp_dir = tempdir().unwrap();
582-
let mut tmp_file = NamedTempFile::new_in(tmp_dir.path()).unwrap();
583-
let rest_api_client = GithubApiClient::new();
584-
let mut files = vec![FileObj::new(PathBuf::from("tests/demo/demo.cpp"))];
585-
capture_clang_tools_output(
586-
&mut files,
587-
env::var("CLANG-VERSION").unwrap_or("".to_string()).as_str(),
588-
"-*",
589-
"",
590-
&LinesChangedOnly::Off,
591-
None,
592-
None,
574+
env::set_var("ACTIONS_STEP_DEBUG", "true");
575+
let (comment, gh_out) = create_comment("-*", "");
576+
assert!(&comment.contains(":heavy_check_mark:\nNo problems need attention."));
577+
assert_eq!(
578+
&gh_out,
579+
"checks-failed=0\nformat-checks-failed=0\ntidy-checks-failed=0\n"
593580
);
594-
let format_checks_failed = tally_format_advice(&files);
595-
let tidy_checks_failed = tally_tidy_advice(&files);
596-
let comment =
597-
rest_api_client.make_comment(&files, format_checks_failed, tidy_checks_failed, None);
598-
assert_eq!(format_checks_failed, 0);
599-
assert_eq!(tidy_checks_failed, 0);
600-
env::set_var("GITHUB_STEP_SUMMARY", tmp_file.path());
601-
rest_api_client.post_step_summary(&comment);
602-
let mut output_file_content = String::new();
603-
tmp_file.read_to_string(&mut output_file_content).unwrap();
604-
assert_eq!(format!("\n{comment}\n\n"), output_file_content);
605581
}
606582
}

0 commit comments

Comments
 (0)