@@ -468,17 +468,15 @@ struct User {
468
468
mod test {
469
469
use std:: { env, io:: Read , path:: PathBuf } ;
470
470
471
+ use regex:: Regex ;
471
472
use tempfile:: { tempdir, NamedTempFile } ;
472
473
473
474
use super :: { GithubApiClient , USER_AGENT } ;
474
475
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,
479
477
cli:: LinesChangedOnly ,
480
478
common_fs:: FileObj ,
481
- rest_api:: RestApiClient ,
479
+ rest_api:: { FeedbackInput , RestApiClient , USER_OUTREACH } ,
482
480
} ;
483
481
484
482
// ************************** tests for GithubApiClient::make_headers()
@@ -513,94 +511,72 @@ mod test {
513
511
assert_header ( true , None ) ;
514
512
}
515
513
516
- // ************************** tests for GithubApiClient::set_exit_code()
514
+ // ************************* tests for step-summary and output variables
517
515
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 ) {
524
517
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={}\n format-checks-failed={}\n tidy-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
+ }
556
522
let mut files = vec ! [ FileObj :: new( PathBuf :: from( "tests/demo/demo.cpp" ) ) ] ;
557
523
capture_clang_tools_output (
558
524
& mut files,
559
525
env:: var ( "CLANG-VERSION" ) . unwrap_or ( "" . to_string ( ) ) . as_str ( ) ,
560
- "readability-*" ,
561
- "file" ,
526
+ tidy_checks ,
527
+ style ,
562
528
& LinesChangedOnly :: Off ,
563
529
None ,
564
530
None ,
565
531
) ;
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:\n Some 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
+ }
577
570
}
578
571
579
572
#[ test]
580
573
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:\n No problems need attention." ) ) ;
577
+ assert_eq ! (
578
+ & gh_out,
579
+ "checks-failed=0\n format-checks-failed=0\n tidy-checks-failed=0\n "
593
580
) ;
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) ;
605
581
}
606
582
}
0 commit comments