1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 # see `testing/README`
21 var toolcontext
= new ToolContext
23 toolcontext
.option_context
.add_option
(toolcontext
.opt_full
, toolcontext
.opt_output
, toolcontext
.opt_dir
, toolcontext
.opt_noact
, toolcontext
.opt_pattern
, toolcontext
.opt_autosav
, toolcontext
.opt_gen_unit
, toolcontext
.opt_gen_force
, toolcontext
.opt_gen_private
, toolcontext
.opt_gen_show
, toolcontext
.opt_nitc
)
24 toolcontext
.tooldescription
= "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
26 toolcontext
.process_options
(args
)
27 var args
= toolcontext
.option_context
.rest
29 if toolcontext
.opt_gen_unit
.value
then
30 if toolcontext
.opt_pattern
.value
!= null then
31 print
"Option --pattern cannot be used with --gen-suite"
35 if toolcontext
.opt_gen_force
.value
then
36 print
"Option --force must be used with --gen-suite"
39 if toolcontext
.opt_gen_private
.value
then
40 print
"Option --private must be used with --gen-suite"
43 if toolcontext
.opt_gen_show
.value
then
44 print
"Option --only-show must be used with --gen-suite"
50 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
52 var module_files
= modelbuilder
.filter_nit_source
(args
)
54 var mmodules
= modelbuilder
.parse_full
(module_files
)
55 modelbuilder
.run_phases
57 if toolcontext
.opt_gen_unit
.value
then
58 modelbuilder
.gen_test_unit
(mmodules
.first
)
62 # When testing `nitunit`, disable time.
63 if "NIT_TESTING".environ
!= "" then
64 toolcontext
.opt_no_time
.value
= true
67 "NIT_TESTING".setenv
("true")
68 "NIT_TESTING_ID".setenv
(pid
.to_s
)
71 var test_dir
= toolcontext
.test_dir
73 "# This file prevents the Nit modules of the directory to be part of the package".write_to_file
(test_dir
/ "packages.ini")
75 var page
= new HTMLTag("testsuites")
77 if toolcontext
.opt_full
.value
then mmodules
= model
.mmodules
80 if not a
.file_exists
then
81 toolcontext
.fatal_error
(null, "Error: cannot load file or module `{a}`.")
83 # Try to load the file as a markdown document
84 var mdoc
= modelbuilder
.load_markdown
(a
)
85 page
.add modelbuilder
.test_mdoc
(mdoc
)
88 for a
in module_files
do
89 var g
= modelbuilder
.identify_group
(a
)
90 if g
== null then continue
91 page
.add modelbuilder
.test_group
(g
)
95 page
.add modelbuilder
.test_markdown
(m
)
96 var ts
= modelbuilder
.test_unit
(m
)
97 if ts
!= null then page
.add ts
100 var file
= toolcontext
.opt_output
.value
101 if file
== null then file
= "nitunit.xml"
102 page
.write_to_file
(file
)
105 printn
"Docunits: Entities: {modelbuilder.total_entities}; Documented ones: {modelbuilder.doc_entities}; With nitunits: {modelbuilder.unit_entities}"
106 if modelbuilder
.unit_entities
== 0 or toolcontext
.opt_noact
.value
then
109 printn
"; Failures: "
110 var cpt
= modelbuilder
.failed_entities
111 if toolcontext
.opt_no_color
.value
then
113 else if cpt
== 0 then
116 print cpt
.to_s
.red
.bold
119 printn
"Test suites: Classes: {modelbuilder.total_classes}; Test Cases: {modelbuilder.total_tests}"
120 if modelbuilder
.total_tests
== 0 or toolcontext
.opt_noact
.value
then
123 printn
"; Failures: "
124 var cpt
= modelbuilder
.failed_tests
125 if toolcontext
.opt_no_color
.value
then
127 else if cpt
== 0 then
130 print cpt
.to_s
.red
.bold
134 var total
= modelbuilder
.unit_entities
+ modelbuilder
.total_tests
135 var fail
= modelbuilder
.failed_entities
+ modelbuilder
.failed_tests
136 if toolcontext
.opt_noact
.value
then
138 else if total
== 0 then
139 var head
= "[NOTHING]"
140 if not toolcontext
.opt_no_color
.value
then
143 print
"{head} No unit tests to execute."
144 else if fail
== 0 then
145 var head
= "[SUCCESS]"
146 if not toolcontext
.opt_no_color
.value
then
147 head
= head
.green
.bold
149 print
"{head} All {total} tests passed."
151 var head
= "[FAILURE]"
152 if not toolcontext
.opt_no_color
.value
then
155 print
"{head} {fail}/{total} tests failed."
157 print
"`{test_dir}` is not removed for investigation."