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`
22 var toolcontext
= new ToolContext
23 toolcontext
.keep_going
= true
25 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
)
26 toolcontext
.tooldescription
= "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
28 toolcontext
.process_options
(args
)
29 var args
= toolcontext
.option_context
.rest
31 if toolcontext
.opt_gen_unit
.value
then
32 if toolcontext
.opt_pattern
.value
!= null then
33 print
"Option --pattern cannot be used with --gen-suite"
37 if toolcontext
.opt_gen_force
.value
then
38 print
"Option --force must be used with --gen-suite"
41 if toolcontext
.opt_gen_private
.value
then
42 print
"Option --private must be used with --gen-suite"
45 if toolcontext
.opt_gen_show
.value
then
46 print
"Option --only-show must be used with --gen-suite"
52 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
54 var module_files
= modelbuilder
.filter_nit_source
(args
)
56 var mmodules
= modelbuilder
.parse_full
(module_files
)
57 modelbuilder
.run_phases
59 if toolcontext
.opt_gen_unit
.value
then
60 modelbuilder
.gen_test_unit
(mmodules
.first
)
64 # When testing `nitunit`, disable time.
65 if "NIT_TESTING".environ
!= "" then
66 toolcontext
.opt_no_time
.value
= true
69 "NIT_TESTING".setenv
("true")
70 "NIT_TESTING_ID".setenv
(pid
.to_s
)
73 var test_dir
= toolcontext
.test_dir
75 "# This file prevents the Nit modules of the directory to be part of the package".write_to_file
(test_dir
/ "packages.ini")
77 var page
= new HTMLTag("testsuites")
79 if toolcontext
.opt_full
.value
then mmodules
= model
.mmodules
82 if not a
.file_exists
then
83 toolcontext
.fatal_error
(null, "Error: cannot load file or module `{a}`.")
85 # Try to load the file as a markdown document
86 var mdoc
= modelbuilder
.load_markdown
(a
)
87 page
.add modelbuilder
.test_mdoc
(mdoc
)
90 for a
in module_files
do
91 var g
= modelbuilder
.identify_group
(a
)
92 if g
== null then continue
93 page
.add modelbuilder
.test_group
(g
)
97 page
.add modelbuilder
.test_markdown
(m
)
98 var ts
= modelbuilder
.test_unit
(m
)
99 if ts
!= null then page
.add ts
102 var file
= toolcontext
.opt_output
.value
103 if file
== null then file
= "nitunit.xml"
104 page
.write_to_file
(file
)
107 printn
"Docunits: Entities: {modelbuilder.total_entities}; Documented ones: {modelbuilder.doc_entities}; With nitunits: {modelbuilder.unit_entities}"
108 if modelbuilder
.unit_entities
== 0 or toolcontext
.opt_noact
.value
then
111 printn
"; Failures: "
112 var cpt
= modelbuilder
.failed_entities
113 if toolcontext
.opt_no_color
.value
then
115 else if cpt
== 0 then
118 print cpt
.to_s
.red
.bold
121 printn
"Test suites: Classes: {modelbuilder.total_classes}; Test Cases: {modelbuilder.total_tests}"
122 if modelbuilder
.total_tests
== 0 or toolcontext
.opt_noact
.value
then
125 printn
"; Failures: "
126 var cpt
= modelbuilder
.failed_tests
127 if toolcontext
.opt_no_color
.value
then
129 else if cpt
== 0 then
132 print cpt
.to_s
.red
.bold
136 var total
= modelbuilder
.unit_entities
+ modelbuilder
.total_tests
137 var fail
= modelbuilder
.failed_entities
+ modelbuilder
.failed_tests
138 if toolcontext
.opt_noact
.value
then
140 else if total
== 0 then
141 var head
= "[NOTHING]"
142 if not toolcontext
.opt_no_color
.value
then
145 print
"{head} No unit tests to execute."
146 else if fail
== 0 then
147 var head
= "[SUCCESS]"
148 if not toolcontext
.opt_no_color
.value
then
149 head
= head
.green
.bold
151 print
"{head} All {total} tests passed."
153 var head
= "[FAILURE]"
154 if not toolcontext
.opt_no_color
.value
then
157 print
"{head} {fail}/{total} tests failed."
159 print
"`{test_dir}` is not removed for investigation."