1
1
use std:: fmt;
2
2
use std:: future:: Future ;
3
- use std:: path:: Path ;
3
+ use std:: path:: { Path , PathBuf } ;
4
4
5
- use anyhow:: { anyhow, Context , Error , Result } ;
5
+ use anyhow:: { anyhow, Context , Result } ;
6
6
use console:: style;
7
- use libtest_mimic:: { run_tests , Arguments , Outcome , Test } ;
7
+ use libtest_mimic:: { Arguments , Failed , Trial } ;
8
8
use similar:: { ChangeTag , TextDiff } ;
9
9
use tokio:: runtime:: Runtime ;
10
10
@@ -29,7 +29,7 @@ impl fmt::Display for Line {
29
29
/// Test runner based on libtest-mimic.
30
30
pub fn planner_test_runner < F , Ft , R > ( path : impl AsRef < Path > , runner_fn : F ) -> Result < ( ) >
31
31
where
32
- F : Fn ( ) -> Ft + Send + Sync + ' static ,
32
+ F : Fn ( ) -> Ft + Send + Sync + ' static + Clone ,
33
33
Ft : Future < Output = Result < R > > + Send ,
34
34
R : PlannerTestRunner ,
35
35
{
@@ -42,81 +42,81 @@ where
42
42
let path = entry. context ( "failed to read glob entry" ) ?;
43
43
let filename = path. file_name ( ) . context ( "unable to extract filename" ) ?;
44
44
let testname = filename. to_str ( ) . context ( "unable to convert to string" ) ?;
45
- tests. push ( Test {
46
- name : testname
45
+
46
+ let nocapture = args. nocapture ;
47
+ let runner_fn = runner_fn. clone ( ) ;
48
+
49
+ tests. push ( Trial :: test (
50
+ testname
47
51
. strip_suffix ( TEST_SUFFIX )
48
52
. unwrap ( )
49
53
. replace ( '/' , "_" ) ,
50
- kind : "" . into ( ) ,
51
- is_ignored : false ,
52
- is_bench : false ,
53
- data : path. clone ( ) ,
54
- } ) ;
54
+ move || run ( path, nocapture, runner_fn) ,
55
+ ) ) ;
55
56
}
56
57
57
58
if tests. is_empty ( ) {
58
59
return Err ( anyhow ! ( "no test discovered" ) ) ;
59
60
}
60
61
62
+ libtest_mimic:: run ( & args, tests) . exit ( ) ;
63
+ }
64
+
65
+ fn run < F , Ft , R > ( path : PathBuf , nocapture : bool , runner_fn : F ) -> Result < ( ) , Failed >
66
+ where
67
+ F : Fn ( ) -> Ft + Send + Sync + ' static + Clone ,
68
+ Ft : Future < Output = Result < R > > + Send ,
69
+ R : PlannerTestRunner ,
70
+ {
61
71
fn build_runtime ( ) -> Runtime {
62
72
tokio:: runtime:: Builder :: new_current_thread ( )
63
73
. enable_all ( )
64
74
. build ( )
65
75
. unwrap ( )
66
76
}
67
77
68
- run_tests ( & args, tests, move |case| {
69
- let path = case. data . clone ( ) ;
70
- let runner_fn = & runner_fn;
71
- match build_runtime ( ) . block_on ( async move {
72
- let mut runner = runner_fn ( ) . await ?;
73
- let testcases = tokio:: fs:: read ( & path) . await ?;
74
- let testcases: Vec < TestCase > = serde_yaml:: from_slice ( & testcases) ?;
75
- let testcases = parse_test_cases ( testcases) ?;
76
- let mut generated_result = String :: new ( ) ;
77
- for testcase in testcases {
78
- let runner_result = runner. run ( & testcase) . await ;
79
- generate_result ( & testcase, & runner_result, & mut generated_result) ?;
80
- }
81
- let path = {
82
- let mut path = path;
83
- path. set_extension ( RESULT_SUFFIX ) ;
84
- path
78
+ build_runtime ( ) . block_on ( async move {
79
+ let mut runner = runner_fn ( ) . await ?;
80
+ let testcases = tokio:: fs:: read ( & path) . await ?;
81
+ let testcases: Vec < TestCase > = serde_yaml:: from_slice ( & testcases) ?;
82
+ let testcases = parse_test_cases ( testcases) ?;
83
+ let mut generated_result = String :: new ( ) ;
84
+ for testcase in testcases {
85
+ let runner_result = runner. run ( & testcase) . await ;
86
+ generate_result ( & testcase, & runner_result, & mut generated_result) ?;
87
+ }
88
+ let path = {
89
+ let mut path = path;
90
+ path. set_extension ( RESULT_SUFFIX ) ;
91
+ path
92
+ } ;
93
+ let expected_result = tokio:: fs:: read_to_string ( & path) . await ?;
94
+
95
+ let diff = TextDiff :: from_lines ( & generated_result, & expected_result) ;
96
+
97
+ for change in diff. iter_all_changes ( ) {
98
+ use console:: Style ;
99
+ let ( sign, sty) = match change. tag ( ) {
100
+ ChangeTag :: Delete => ( "-" , Style :: new ( ) . red ( ) ) ,
101
+ ChangeTag :: Insert => ( "+" , Style :: new ( ) . green ( ) ) ,
102
+ ChangeTag :: Equal => ( " " , Style :: new ( ) ) ,
85
103
} ;
86
- let expected_result = tokio:: fs:: read_to_string ( & path) . await ?;
87
-
88
- let diff = TextDiff :: from_lines ( & generated_result, & expected_result) ;
89
-
90
- for change in diff. iter_all_changes ( ) {
91
- use console:: Style ;
92
- let ( sign, sty) = match change. tag ( ) {
93
- ChangeTag :: Delete => ( "-" , Style :: new ( ) . red ( ) ) ,
94
- ChangeTag :: Insert => ( "+" , Style :: new ( ) . green ( ) ) ,
95
- ChangeTag :: Equal => ( " " , Style :: new ( ) ) ,
96
- } ;
97
-
98
- if args. nocapture {
99
- print ! (
100
- "{}{} {}{}" ,
101
- style( Line ( change. old_index( ) ) ) . dim( ) ,
102
- style( Line ( change. new_index( ) ) ) . dim( ) ,
103
- sty. apply_to( sign) . bold( ) ,
104
- sty. apply_to( change)
105
- ) ;
106
- }
107
- }
108
104
109
- if generated_result != expected_result {
110
- Err ( anyhow ! ( "test failed" ) )
111
- } else {
112
- Ok :: < _ , Error > ( ( ) )
105
+ if nocapture {
106
+ print ! (
107
+ "{}{} {}{}" ,
108
+ style( Line ( change. old_index( ) ) ) . dim( ) ,
109
+ style( Line ( change. new_index( ) ) ) . dim( ) ,
110
+ sty. apply_to( sign) . bold( ) ,
111
+ sty. apply_to( change)
112
+ ) ;
113
113
}
114
- } ) {
115
- Ok ( _) => Outcome :: Passed ,
116
- Err ( err) => Outcome :: Failed {
117
- msg : Some ( format ! ( "{:#}" , err) ) ,
118
- } ,
114
+ }
115
+
116
+ if generated_result != expected_result {
117
+ Err ( Failed :: without_message ( ) )
118
+ } else {
119
+ Ok ( ( ) )
119
120
}
120
121
} )
121
- . exit ( ) ;
122
122
}
0 commit comments