11use crate :: commands:: command_util;
2- use crate :: commands:: command_util:: ask_exercise_interactive;
2+ use crate :: commands:: command_util:: { ask_exercise_interactive, find_course_config_for_exercise } ;
33use crate :: io_module:: { Io , PrintColor } ;
4- use std:: env;
5- use std:: path:: { Path , PathBuf } ;
4+ use std:: path:: Path ;
65use tmc_langs:: RunResult ;
76
8- /// Executes tmc tests for exercise(s)
7+ /// Executes tmc tests for one exercise. If path not given, check if current folder is an exercise.
8+ /// If not, asks exercise with an interactive menu.
99pub fn test ( io : & mut dyn Io , exercise_folder : Option < & str > ) {
10- let status = match env:: current_dir ( ) {
11- Ok ( mut pathbuf) => {
12- if let Some ( exercise) = exercise_folder {
13- pathbuf. push ( exercise) ;
14- }
10+ let mut exercise_name = "" . to_string ( ) ;
11+ let mut course_config = None ;
12+ let mut exercise_dir = std:: path:: PathBuf :: new ( ) ;
1513
16- match tmc_langs:: find_exercise_directories ( pathbuf. as_path ( ) ) {
17- Ok ( exercises) => match exercises. len ( ) {
18- 0 => ask_interactively_and_test ( io) ,
19- 1 => test_exercise_path ( io, exercises[ 0 ] . as_path ( ) ) ,
20- _ => test_exercises ( io, exercises) ,
21- } ,
22- Err ( _) => ask_interactively_and_test ( io) ,
23- }
24- }
25- Err ( error) => Err ( format ! (
26- "Invalid directory / Insufficient permissions: {}" ,
27- error
28- ) ) ,
29- } ;
14+ let path = std:: env:: current_dir ( ) . unwrap ( ) ;
15+ let mut path = path. to_str ( ) . unwrap ( ) ;
16+ if let Some ( folder) = exercise_folder {
17+ path = folder;
18+ }
3019
31- if let Err ( err) = status {
32- io. println ( & err, PrintColor :: Failed ) ;
20+ if let Err ( error) = find_course_config_for_exercise (
21+ & mut exercise_name,
22+ & mut course_config,
23+ & mut exercise_dir,
24+ path,
25+ ) {
26+ if exercise_folder. is_some ( ) {
27+ io. println ( & error, PrintColor :: Failed ) ;
28+ return ;
29+ }
3330 }
34- }
3531
36- /// Asks with interactive menu what exercise should be tested and runs tests
37- fn ask_interactively_and_test ( io : & mut dyn Io ) -> Result < ( ) , String > {
38- // No exercises found, ask with interactive menu
39- let mut exercise_name = String :: from ( "" ) ;
40- let mut exercise_dir: PathBuf = PathBuf :: new ( ) ;
41- let mut course_config = None ;
42- match ask_exercise_interactive ( & mut exercise_name, & mut exercise_dir, & mut course_config) {
43- Ok ( ( ) ) => ( ) ,
44- Err ( msg) => {
45- return Err ( msg) ;
32+ if course_config. is_none ( ) {
33+ // Did not find course config, use interactive selection if possible
34+ match ask_exercise_interactive ( & mut exercise_name, & mut exercise_dir, & mut course_config) {
35+ Ok ( ( ) ) => ( ) ,
36+ Err ( msg) => {
37+ io. println ( & msg, PrintColor :: Failed ) ;
38+ return ;
39+ }
4640 }
4741 }
48- test_exercise_path ( io, & exercise_dir)
42+
43+ if let Err ( err) = test_exercise_path ( io, & exercise_dir) {
44+ io. println ( & err, PrintColor :: Failed ) ;
45+ }
4946}
5047
5148/// Wrapper around test_exercise funtion to get uniform Result type
@@ -56,25 +53,6 @@ fn test_exercise_path(io: &mut dyn Io, path: &Path) -> Result<(), String> {
5653 Ok ( ( ) )
5754 }
5855}
59- /// Executes tmc tests for multiple exercises
60- fn test_exercises ( io : & mut dyn Io , paths : Vec < PathBuf > ) -> Result < ( ) , String > {
61- let mut exercises_completed = 0_usize ;
62- let mut exercises_total = 0_usize ;
63- for exercise_path in paths {
64- match test_exercise ( io, exercise_path. as_path ( ) , false ) {
65- Ok ( passed) => {
66- exercises_total += 1 ;
67- if passed {
68- exercises_completed += 1 ;
69- }
70- }
71- Err ( err) => return Err ( err) , // Stops iteration on first error
72- }
73- }
74-
75- print_result_tests ( io, exercises_completed, exercises_total) ;
76- Ok ( ( ) )
77- }
7856
7957/// Executes tests for a single exercise, returns true if all tests passed (false if not).
8058fn test_exercise ( io : & mut dyn Io , path : & Path , print_progress : bool ) -> Result < bool , String > {
@@ -97,22 +75,6 @@ fn test_exercise(io: &mut dyn Io, path: &Path, print_progress: bool) -> Result<b
9775 }
9876}
9977
100- /// Prints the end result of running multiple tests
101- fn print_result_tests ( io : & mut dyn Io , exercises_completed : usize , exercises_total : usize ) {
102- io. println ( "" , PrintColor :: Normal ) ;
103- io. println (
104- & format ! (
105- "Total results: {}/{} exercises passed" ,
106- exercises_completed, exercises_total
107- ) ,
108- PrintColor :: Normal ,
109- ) ;
110- io. println (
111- & command_util:: get_progress_string ( exercises_completed, exercises_total, 64 ) ,
112- PrintColor :: Normal ,
113- ) ;
114- }
115-
11678/// Prints the result of running tests for a single exercise
11779fn print_result_test (
11880 io : & mut dyn Io ,
@@ -485,71 +447,5 @@ mod tests {
485447 "print_result_test returned true, expected false"
486448 ) ;
487449 }
488-
489- #[ test]
490- fn print_multiple_completed_tests_results_test ( ) {
491- let mut v: Vec < String > = Vec :: new ( ) ;
492- let input = vec ! [ ] ;
493- let mut input = input. iter ( ) ;
494-
495- let mut io = IoTest {
496- list : & mut v,
497- input : & mut input,
498- } ;
499-
500- print_result_tests ( & mut io, 10 , 10 ) ;
501-
502- assert_eq ! ( io. list[ 0 ] , "" ) ;
503-
504- assert ! (
505- io. list[ 1 ] . contains( "Total results" ) ,
506- "line does not contain 'Total results'"
507- ) ;
508- assert ! (
509- io. list[ 1 ] . contains( "10/10" ) ,
510- "line does not contain total completed exercises '10/10'"
511- ) ;
512- assert ! (
513- io. list[ 2 ] . contains( "█" ) ,
514- "line does not contain progress char '█'"
515- ) ;
516- assert ! (
517- !io. list[ 2 ] . contains( "░" ) ,
518- "line contains progress char '░', which should not appear at 100% completed"
519- ) ;
520- }
521-
522- #[ test]
523- fn print_multiple_failed_tests_results_test ( ) {
524- let mut v: Vec < String > = Vec :: new ( ) ;
525- let input = vec ! [ ] ;
526- let mut input = input. iter ( ) ;
527-
528- let mut io = IoTest {
529- list : & mut v,
530- input : & mut input,
531- } ;
532-
533- print_result_tests ( & mut io, 5 , 10 ) ;
534-
535- assert_eq ! ( io. list[ 0 ] , "" ) ;
536-
537- assert ! (
538- io. list[ 1 ] . contains( "Total results" ) ,
539- "line does not contain 'Total results'"
540- ) ;
541- assert ! (
542- io. list[ 1 ] . contains( "5/10" ) ,
543- "line does not contain total completed exercises '10/10'"
544- ) ;
545- assert ! (
546- io. list[ 2 ] . contains( "█" ) ,
547- "line does not contain progress char '█'"
548- ) ;
549- assert ! (
550- io. list[ 2 ] . contains( "░" ) ,
551- "line does not contain progress char '░'"
552- ) ;
553- }
554450 }
555451}
0 commit comments