nitunit: do some kind of shenanigans to import the main module
[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 # Program to extract and execute unit tests from nit source files
16 module nitunit
17
18 import modelize_property
19 intrude import markdown
20 import parser_util
21
22 # Extractor, Executor an Reporter for the tests in a module
23 class NitUnitExecutor
24 super Doc2Mdwn
25
26 # The name of the module to import
27 var modname: String
28
29 # The prefix of the generated Nit source-file
30 var prefix: String
31
32 # The XML node associated to the module
33 var testsuite: HTMLTag
34
35 # Initialize a new e
36 init(toolcontext: ToolContext, prefix: String, modname: String, testsuite: HTMLTag)
37 do
38 super(toolcontext)
39 self.prefix = prefix
40 self.modname = modname
41 self.testsuite = testsuite
42 end
43
44 # All blocks of code from a same `ADoc`
45 var block = new Array[String]
46
47 redef fun process_code(n: HTMLTag, text: String)
48 do
49 # Try to parse it
50 var ast = toolcontext.parse_something(text)
51
52 # We want executable code
53 if not (ast isa AModule or ast isa ABlockExpr or ast isa AExpr) then return
54
55 # Search `assert` in the AST
56 var v = new SearchAssertVisitor
57 v.enter_visit(ast)
58 if not v.foundit then return
59
60 # Add it to the file
61 block.add(text)
62 end
63
64 # used to generate distinct names
65 var cpt = 0
66
67 # The entry point for a new `ndoc` node
68 # Fill the prepated `tc` (testcase) XTM node
69 fun extract(ndoc: ADoc, tc: HTMLTag)
70 do
71 block.clear
72
73 work(ndoc)
74
75 if block.is_empty then return
76
77 cpt += 1
78 var file = "{prefix}{cpt}.nit"
79
80 toolcontext.info("Execute {tc.attrs["classname"]}.{tc.attrs["name"]} in {file}", 2)
81
82 var dir = file.dirname
83 if dir != "" then dir.mkdir
84 var f
85 f = new OFStream.open(file)
86 f.write("# GENERATED FILE\n")
87 f.write("# Example extracted from a documentation\n")
88 var modname = self.modname
89 f.write("import {modname}\n")
90 f.write("\n")
91 for text in block do
92 f.write(text)
93 end
94 f.close
95
96 var cmd = "../bin/nitg --no-color '{file}' -I . >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
97 var res = sys.system(cmd)
98 var res2 = 0
99 if res == 0 then
100 res2 = sys.system("./{file}.bin >>'{file}.out1' 2>&1 </dev/null")
101 end
102
103 var msg
104 f = new IFStream.open("{file}.out1")
105 var n2
106 n2 = new HTMLTag("system-err")
107 tc.add n2
108 msg = f.read_all
109 f.close
110
111 n2 = new HTMLTag("system-out")
112 tc.add n2
113 for text in block do n2.append(text)
114
115
116 if res != 0 then
117 var ne = new HTMLTag("failure")
118 ne.attr("message", msg)
119 tc.add ne
120 toolcontext.warning(ndoc.location, "FAILURE: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
121 else if res2 != 0 then
122 var ne = new HTMLTag("error")
123 ne.attr("message", msg)
124 tc.add ne
125 toolcontext.warning(ndoc.location, "ERROR: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
126 end
127 toolcontext.check_errors
128
129 testsuite.add(tc)
130 end
131 end
132
133 class SearchAssertVisitor
134 super Visitor
135 var foundit = false
136 redef fun visit(node)
137 do
138 if foundit then
139 return
140 else if node isa AAssertExpr then
141 foundit = true
142 return
143 else
144 node.visit_all(self)
145 end
146 end
147 end
148
149 redef class ModelBuilder
150 fun test_markdown(mmodule: MModule): HTMLTag
151 do
152 var ts = new HTMLTag("testsuite")
153 toolcontext.info("nitunit: {mmodule}", 2)
154 if not mmodule2nmodule.has_key(mmodule) then return ts
155
156 var nmodule = mmodule2nmodule[mmodule]
157 assert nmodule != null
158
159 # what module to import in the unit test.
160 # try to detect the main module of the project
161 # TODO do things correctly once the importation of arbitraty nested module is legal
162 var o = mmodule
163 var g = o.mgroup
164 if g != null then
165 o = get_mmodule_by_name(nmodule, mmodule, g.mproject.name).as(not null)
166 end
167
168 ts.attr("package", mmodule.full_name)
169
170 var prefix = toolcontext.opt_dir.value
171 if prefix == null then prefix = ".nitunit"
172 prefix = prefix.join_path(mmodule.to_s)
173 var d2m = new NitUnitExecutor(toolcontext, prefix, o.name, ts)
174
175 var tc
176
177 do
178 var nmoduledecl = nmodule.n_moduledecl
179 if nmoduledecl == null then break label x
180 var ndoc = nmoduledecl.n_doc
181 if ndoc == null then break label x
182 tc = new HTMLTag("testcase")
183 # NOTE: jenkins expects a '.' in the classname attr
184 tc.attr("classname", mmodule.full_name + ".<module>")
185 tc.attr("name", "<module>")
186 d2m.extract(ndoc, tc)
187 end label x
188 for nclassdef in nmodule.n_classdefs do
189 var mclassdef = nclassdef.mclassdef.as(not null)
190 if nclassdef isa AStdClassdef then
191 var ndoc = nclassdef.n_doc
192 if ndoc != null then
193 tc = new HTMLTag("testcase")
194 tc.attr("classname", mmodule.full_name + "." + mclassdef.mclass.full_name)
195 tc.attr("name", "<class>")
196 d2m.extract(ndoc, tc)
197 end
198 end
199 for npropdef in nclassdef.n_propdefs do
200 var mpropdef = npropdef.mpropdef.as(not null)
201 var ndoc = npropdef.n_doc
202 if ndoc != null then
203 tc = new HTMLTag("testcase")
204 tc.attr("classname", mmodule.full_name + "." + mclassdef.mclass.full_name)
205 tc.attr("name", mpropdef.mproperty.full_name)
206 d2m.extract(ndoc, tc)
207 end
208 end
209 end
210
211 return ts
212 end
213 end
214
215 redef class ToolContext
216 var opt_full = new OptionBool("Process also imported modules", "--full")
217 var opt_output = new OptionString("Output name (default is 'nitunit.xml')", "-o", "--output")
218 var opt_dir = new OptionString("Working directory (default is '.nitunit')", "--dir")
219 end
220
221 var toolcontext = new ToolContext
222
223 toolcontext.option_context.add_option(toolcontext.opt_full, toolcontext.opt_output, toolcontext.opt_dir)
224
225
226 toolcontext.process_options
227 var args = toolcontext.option_context.rest
228 if args.is_empty or toolcontext.opt_help.value then
229 print "usage: nitunit [options] file.nit..."
230 toolcontext.option_context.usage
231 return
232 end
233
234 var model = new Model
235 var modelbuilder = new ModelBuilder(model, toolcontext)
236
237 var mmodules = modelbuilder.parse(args)
238 modelbuilder.run_phases
239
240 var page = new HTMLTag("testsuites")
241
242 if toolcontext.opt_full.value then mmodules = model.mmodules
243
244 for m in mmodules do
245 page.add modelbuilder.test_markdown(m)
246 end
247
248 var file = toolcontext.opt_output.value
249 if file == null then file = "nitunit.xml"
250 page.save(file)