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