nitunit: just process test_suite when given
[nit.git] / src / nitunit.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Testing tool.
16 # see `testing/README`
17 module nitunit
18
19 import testing
20
21 var toolcontext = new ToolContext
22
23 toolcontext.option_context.add_option(toolcontext.opt_full, toolcontext.opt_output, toolcontext.opt_dir, toolcontext.opt_noact, toolcontext.opt_pattern, toolcontext.opt_file, 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."
25
26 toolcontext.process_options(args)
27 var args = toolcontext.option_context.rest
28
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"
32 exit(0)
33 end
34 if toolcontext.opt_file.value != null then
35 print "Option --target-file cannot be used with --gen-suite"
36 exit(0)
37 end
38 else
39 if toolcontext.opt_gen_force.value then
40 print "Option --force must be used with --gen-suite"
41 exit(0)
42 end
43 if toolcontext.opt_gen_private.value then
44 print "Option --private must be used with --gen-suite"
45 exit(0)
46 end
47 if toolcontext.opt_gen_show.value then
48 print "Option --only-show must be used with --gen-suite"
49 exit(0)
50 end
51 end
52
53 var model = new Model
54 var modelbuilder = new ModelBuilder(model, toolcontext)
55
56 var module_files = modelbuilder.filter_nit_source(args)
57
58 var mmodules = modelbuilder.parse_full(module_files)
59 modelbuilder.run_phases
60
61 if toolcontext.opt_gen_unit.value then
62 modelbuilder.gen_test_unit(mmodules.first)
63 exit(0)
64 end
65
66 "NIT_TESTING".setenv("true")
67 "NIT_TESTING_ID".setenv(pid.to_s)
68 "SRAND".setenv("0")
69
70 var test_dir = toolcontext.test_dir
71 test_dir.mkdir
72 "# This file prevents the Nit modules of the directory to be part of the package".write_to_file(test_dir / "packages.ini")
73
74 var page = new HTMLTag("testsuites")
75
76 if toolcontext.opt_full.value then mmodules = model.mmodules
77
78 for a in args do
79 if not a.file_exists then
80 toolcontext.fatal_error(null, "Error: cannot load file or module `{a}`.")
81 end
82 # Try to load the file as a markdown document
83 var mdoc = modelbuilder.load_markdown(a)
84 page.add modelbuilder.test_mdoc(mdoc)
85 end
86
87 for a in module_files do
88 var g = modelbuilder.identify_group(a)
89 if g == null then continue
90 page.add modelbuilder.test_group(g)
91 end
92
93 for m in mmodules do
94 page.add modelbuilder.test_markdown(m)
95 var ts = modelbuilder.test_unit(m)
96 if ts != null then page.add ts
97 end
98
99 var file = toolcontext.opt_output.value
100 if file == null then file = "nitunit.xml"
101 page.write_to_file(file)
102
103 # Print results
104 printn "Docunits: Entities: {modelbuilder.total_entities}; Documented ones: {modelbuilder.doc_entities}; With nitunits: {modelbuilder.unit_entities}"
105 if modelbuilder.unit_entities == 0 or toolcontext.opt_noact.value then
106 print ""
107 else
108 printn "; Failures: "
109 var cpt = modelbuilder.failed_entities
110 if toolcontext.opt_no_color.value then
111 print cpt
112 else if cpt == 0 then
113 print "0".green.bold
114 else
115 print cpt.to_s.red.bold
116 end
117 end
118 printn "Test suites: Classes: {modelbuilder.total_classes}; Test Cases: {modelbuilder.total_tests}"
119 if modelbuilder.total_tests == 0 or toolcontext.opt_noact.value then
120 print ""
121 else
122 printn "; Failures: "
123 var cpt = modelbuilder.failed_tests
124 if toolcontext.opt_no_color.value then
125 print cpt
126 else if cpt == 0 then
127 print "0".green.bold
128 else
129 print cpt.to_s.red.bold
130 end
131 end
132
133 var total = modelbuilder.unit_entities + modelbuilder.total_tests
134 var fail = modelbuilder.failed_entities + modelbuilder.failed_tests
135 if toolcontext.opt_noact.value then
136 # nothing
137 else if total == 0 then
138 var head = "[NOTHING]"
139 if not toolcontext.opt_no_color.value then
140 head = head.yellow
141 end
142 print "{head} No unit tests to execute."
143 else if fail == 0 then
144 var head = "[SUCCESS]"
145 if not toolcontext.opt_no_color.value then
146 head = head.green.bold
147 end
148 print "{head} All {total} tests passed."
149 else
150 var head = "[FAILURE]"
151 if not toolcontext.opt_no_color.value then
152 head = head.red.bold
153 end
154 print "{head} {fail}/{total} tests failed."
155
156 print "`{test_dir}` is not removed for investigation."
157 exit 1
158 end
159
160 test_dir.rmdir