Skip to content

Commit c8a8636

Browse files
Allow to capture output of Suite.run_with_filter (#14787)
1 parent b1d42a1 commit c8a8636

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

distribution/lib/Standard/Test/0.0.0-dev/docs/api/Suite.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
- type Suite
44
- group_names self -> Standard.Base.Any.Any
55
- print_all self -> Standard.Base.Any.Any
6-
- run_with_filter self filter:(Standard.Base.Data.Text.Text|Standard.Base.Nothing.Nothing)= should_exit:Standard.Base.Data.Boolean.Boolean= -> (Standard.Base.Data.Boolean.Boolean|Standard.Base.Nothing.Nothing)
6+
- run_with_filter self filter:(Standard.Base.Data.Text.Text|Standard.Base.Nothing.Nothing)= should_exit:Standard.Base.Data.Boolean.Boolean= output:(Standard.Base.Data.Text.Text -> Standard.Base.Nothing.Nothing)= -> (Standard.Base.Data.Boolean.Boolean|Standard.Base.Nothing.Nothing)
77
- type Suite_Builder
88
- group self name:Standard.Base.Data.Text.Text fn:(Standard.Test.Group.Group_Builder -> Standard.Base.Any.Any) ~pending:(Standard.Base.Data.Text.Text|Standard.Base.Nothing.Nothing)= -> Standard.Base.Any.Any

distribution/lib/Standard/Test/0.0.0-dev/src/Suite.enso

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ type Suite
5555
- `should_exit`: If true, executes `System.exit` at the end, so the
5656
method does not return. If false, return boolean from the method
5757
indicating whether some tests failed.
58+
- `output`: A function to print out textual info about progress of execution.
59+
By default it uses `IO.println` output
5860

5961
## Returns
6062
Boolean if `should_exit` is False, otherwise exits the process.
61-
run_with_filter self (filter : (Text | Nothing) = Nothing) (should_exit : Boolean = True) -> (Boolean | Nothing) =
63+
run_with_filter self (filter : (Text | Nothing) = Nothing) (should_exit : Boolean = True) (output:Text->Nothing=IO.println) -> (Boolean | Nothing) =
6264
config = Suite_Config.from_environment
65+
self.run_impl output config filter should_exit
6366

67+
private run_impl self println config:Suite_Config (filter : (Text | Nothing) = Nothing) (should_exit : Boolean = True) -> (Boolean | Nothing) =
6468
# List of pairs of groups and their specs that match the filter
6569
matching_specs = self.groups.flat_map group->
6670
group_matches = Helpers.name_matches group.name filter
@@ -93,10 +97,10 @@ type Suite
9397
case group.is_pending of
9498
False ->
9599
results = Helpers.run_specs_from_group specs group progress_reporter
96-
Test_Reporter.print_report results config junit_sb_builder
100+
Test_Reporter.print_report println results config junit_sb_builder
97101
all_results_bldr.append_vector_range results
98102
True ->
99-
Test_Reporter.print_pending_group group config junit_sb_builder
103+
Test_Reporter.print_pending_group println group config junit_sb_builder
100104

101105
all_results = all_results_bldr.to_vector
102106
succ_tests = all_results.filter (r-> r.is_success) . length
@@ -112,16 +116,16 @@ type Suite
112116
pending_groups = matching_specs.filter (p-> p.first.is_pending) . length
113117
case should_exit of
114118
True ->
115-
IO.println ""
116-
IO.println <| succ_tests.to_text + " tests succeeded."
117-
IO.println <| failed_tests_number.to_text + " tests failed."
118-
IO.println <| skipped_tests.to_text + " tests skipped."
119-
IO.println <| pending_groups.to_text + " groups skipped."
120-
IO.println ""
119+
println ""
120+
println <| succ_tests.to_text + " tests succeeded."
121+
println <| failed_tests_number.to_text + " tests failed."
122+
println <| skipped_tests.to_text + " tests skipped."
123+
println <| pending_groups.to_text + " groups skipped."
124+
println ""
121125
if failed_tests_number > 0 then
122-
IO.println <| "Failed tests: '" + failed_tests_names + "'"
126+
println <| "Failed tests: '" + failed_tests_names + "'"
123127
if failed_tests_number > 10 then IO.println "(Displaying only first 10 failed tests)"
124-
IO.println ""
128+
println ""
125129
exit_code = if failed_tests_number > 0 then 1 else 0
126130
System.exit exit_code
127131
False ->

distribution/lib/Standard/Test/0.0.0-dev/src/Test_Reporter.enso

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Standard.Base.Runtime.Ref.Ref
77
from Standard.Base.Logging import all
88
from Standard.Base.Runtime import assert
99

10-
import project.Group.Group
1110
import project.Internal.Stack_Trace_Helpers
1211
import project.Spec_Result.Spec_Result
1312
import project.Suite_Config.Suite_Config
@@ -64,8 +63,7 @@ maybe_grey_text (text : Text) (config : Suite_Config) =
6463
if config.use_ansi_colors then (grey text) else text
6564

6665
## Print result for a single Spec run
67-
print_single_result : Text -> Test_Result -> Suite_Config -> Nothing
68-
print_single_result (group_name : Text) (test_result : Test_Result) (config : Suite_Config) =
66+
print_single_result (println:Text->Nothing) (group_name : Text) (test_result : Test_Result) (config : Suite_Config) =
6967
times_suffix =
7068
times = test_result.time_taken.total_milliseconds.to_text + "ms"
7169
"[" + times + "]"
@@ -78,29 +76,29 @@ print_single_result (group_name : Text) (test_result : Test_Result) (config : Su
7876
Spec_Result.Success ->
7977
if config.print_only_failures.not then
8078
txt = " - " + test_result.spec_name + " " + times_suffix
81-
IO.println (maybe_green_text txt config)
79+
println (maybe_green_text txt config)
8280
Spec_Result.Failure msg details ->
83-
report_github_error_message group_name+": "+test_result.spec_name msg
84-
IO.println ""
81+
report_github_error_message println group_name+": "+test_result.spec_name msg
82+
println ""
8583
txt = " - [FAILED] " + test_result.spec_name + " " + times_suffix
86-
IO.println (maybe_red_text txt config)
87-
IO.println (" Reason: " + msg)
84+
println (maybe_red_text txt config)
85+
println (" Reason: " + msg)
8886
if details.is_nothing.not then
89-
IO.println (decorate_stack_trace details)
90-
IO.println ""
87+
println (decorate_stack_trace details)
88+
println ""
9189
Spec_Result.Pending reason ->
92-
IO.println (maybe_grey_text (" - [PENDING] " + test_result.spec_name) config)
93-
IO.println (" Reason: " + reason)
90+
println (maybe_grey_text (" - [PENDING] " + test_result.spec_name) config)
91+
println (" Reason: " + reason)
9492

9593
## ---
9694
private: true
9795
---
9896
Reports an error message to show up as a note in GitHub Actions, only if
9997
running in the GitHub Actions environment.
100-
report_github_error_message (~title : Text) (~message : Text) =
98+
private report_github_error_message println (~title : Text) (~message : Text) =
10199
is_enabled = Environment.get "GITHUB_ACTIONS" == "true"
102100
if is_enabled then
103-
IO.println (generate_github_error_annotation title message)
101+
println (generate_github_error_annotation title message)
104102

105103
## ---
106104
private: true
@@ -142,20 +140,18 @@ generate_github_error_annotation (title : Text) (message : Text) =
142140
from multiple groups.
143141
- `builder`: StringBuilder or Nothing. If StringBuilder, then a jUnit XML
144142
format is appended to that StringBuilder.
145-
print_report : Vector Test_Result -> Suite_Config -> (StringBuilder | Nothing) -> Nothing
146-
print_report (test_results : Vector Test_Result) (config : Suite_Config) (builder : (StringBuilder | Nothing)) =
143+
private print_report (println:Text->Nothing) (test_results : Vector Test_Result) (config : Suite_Config) (builder : (StringBuilder | Nothing)) =
147144
distinct_group_names = test_results.map (_.group_name) . distinct
148145
results_per_group = distinct_group_names.fold Dictionary.empty acc-> group_name->
149146
group_results = test_results.filter res->
150147
res.group_name == group_name
151148
assert (group_results.length > 0)
152149
acc.insert group_name group_results
153150
results_per_group.each_with_key group_name-> group_results->
154-
print_group_report group_name group_results config builder
151+
print_group_report println group_name group_results config builder
155152

156153
## Prints a pending group, optionally writing it to a jUnit XML output.
157-
print_pending_group : Group -> Suite_Config -> (StringBuilder | Nothing) -> Nothing
158-
print_pending_group group config builder =
154+
private print_pending_group println group config builder =
159155
assert group.pending.is_nothing.not "Group in print_pending_group should be pending"
160156
if config.should_output_junit then
161157
assert builder.is_nothing.not "Builder must be specified when JUnit output is enabled"
@@ -165,15 +161,14 @@ print_pending_group group config builder =
165161
builder.append (' <skipped message="Reason: '+(escape_xml group.pending inside_attribute=True)+'"/>\n')
166162
builder.append (' </testcase>\n')
167163
builder.append ' </testsuite>\n'
168-
IO.println <| maybe_grey_text ("[PENDING] " + group.name) config
169-
IO.println (" Reason: " + group.pending)
164+
println <| maybe_grey_text ("[PENDING] " + group.name) config
165+
println (" Reason: " + group.pending)
170166

171167
## Prints report for test_results from a single group.
172168

173169
## Arguments
174170
- `test_results`: Test test_results from a single group
175-
print_group_report : Text -> Vector Test_Result -> Suite_Config -> (StringBuilder|Nothing) -> Nothing
176-
print_group_report group_name test_results config builder =
171+
private print_group_report (println:Text->Nothing) group_name test_results config builder =
177172
distinct_groups = test_results.distinct (res-> res.group_name)
178173
assert (distinct_groups.length == 1)
179174
total_time = test_results.fold Duration.zero acc-> res->
@@ -216,11 +211,11 @@ print_group_report group_name test_results config builder =
216211
counts = tests_succeeded.to_text + "/" + tests_executed.to_text
217212
times = total_time.total_milliseconds.to_text + "ms"
218213
group_name + ": " + "[" + counts + ", " + times + "]"
219-
IO.println <| case some_test_failed of
214+
println <| case some_test_failed of
220215
True -> maybe_red_text ("[FAILED] " + group_description) config
221216
False -> maybe_green_text group_description config
222217
test_results.each result->
223-
print_single_result group_name result config
218+
print_single_result println group_name result config
224219

225220
## ---
226221
private: true

test/Test_Tests/src/Teardown_Spec.enso

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ add_specs suite_builder =
3434
messages = Vector.build builder->
3535
test_suite = Test.build suite_builder->
3636
failing_teardown_suite builder.append suite_builder
37-
result = test_suite.run_with_filter should_exit=False
37+
result = test_suite.run_with_filter should_exit=False output=(\_->Nothing)
3838
# tear down fails, but doesn't crash
3939
result:Boolean . should_be_false
4040

0 commit comments

Comments
 (0)