nitunit: learn to use `nit_dir` instead of hardcoding the path of nitg
[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.to_mdoc)
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 nit_dir = toolcontext.nit_dir
97 var nitg = "{nit_dir}/bin/nitg"
98 if nit_dir == null or not nitg.file_exists then
99 toolcontext.error(null, "Cannot find nitg. Set envvar NIT_DIR.")
100 toolcontext.check_errors
101 end
102 var cmd = "{nitg} --ignore-visibility --no-color '{file}' -I . >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
103 var res = sys.system(cmd)
104 var res2 = 0
105 if res == 0 then
106 res2 = sys.system("./{file}.bin >>'{file}.out1' 2>&1 </dev/null")
107 end
108
109 var msg
110 f = new IFStream.open("{file}.out1")
111 var n2
112 n2 = new HTMLTag("system-err")
113 tc.add n2
114 msg = f.read_all
115 f.close
116
117 n2 = new HTMLTag("system-out")
118 tc.add n2
119 for text in block do n2.append(text)
120
121
122 if res != 0 then
123 var ne = new HTMLTag("failure")
124 ne.attr("message", msg)
125 tc.add ne
126 toolcontext.warning(ndoc.location, "FAILURE: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
127 else if res2 != 0 then
128 var ne = new HTMLTag("error")
129 ne.attr("message", msg)
130 tc.add ne
131 toolcontext.warning(ndoc.location, "ERROR: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
132 end
133 toolcontext.check_errors
134
135 testsuite.add(tc)
136 end
137 end
138
139 class SearchAssertVisitor
140 super Visitor
141 var foundit = false
142 redef fun visit(node)
143 do
144 if foundit then
145 return
146 else if node isa AAssertExpr then
147 foundit = true
148 return
149 else
150 node.visit_all(self)
151 end
152 end
153 end
154
155 redef class ModelBuilder
156 fun test_markdown(mmodule: MModule): HTMLTag
157 do
158 var ts = new HTMLTag("testsuite")
159 toolcontext.info("nitunit: {mmodule}", 2)
160 if not mmodule2nmodule.has_key(mmodule) then return ts
161
162 var nmodule = mmodule2nmodule[mmodule]
163 assert nmodule != null
164
165 # what module to import in the unit test.
166 # try to detect the main module of the project
167 # TODO do things correctly once the importation of arbitraty nested module is legal
168 var o = mmodule
169 var g = o.mgroup
170 if g != null then
171 o = get_mmodule_by_name(nmodule, g, g.mproject.name).as(not null)
172 end
173
174 ts.attr("package", mmodule.full_name)
175
176 var prefix = toolcontext.opt_dir.value
177 if prefix == null then prefix = ".nitunit"
178 prefix = prefix.join_path(mmodule.to_s)
179 var d2m = new NitUnitExecutor(toolcontext, prefix, o.name, ts)
180
181 var tc
182
183 do
184 var nmoduledecl = nmodule.n_moduledecl
185 if nmoduledecl == null then break label x
186 var ndoc = nmoduledecl.n_doc
187 if ndoc == null then break label x
188 tc = new HTMLTag("testcase")
189 # NOTE: jenkins expects a '.' in the classname attr
190 tc.attr("classname", mmodule.full_name + ".<module>")
191 tc.attr("name", "<module>")
192 d2m.extract(ndoc, tc)
193 end label x
194 for nclassdef in nmodule.n_classdefs do
195 var mclassdef = nclassdef.mclassdef.as(not null)
196 if nclassdef isa AStdClassdef then
197 var ndoc = nclassdef.n_doc
198 if ndoc != null then
199 tc = new HTMLTag("testcase")
200 tc.attr("classname", mmodule.full_name + "." + mclassdef.mclass.full_name)
201 tc.attr("name", "<class>")
202 d2m.extract(ndoc, tc)
203 end
204 end
205 for npropdef in nclassdef.n_propdefs do
206 var mpropdef = npropdef.mpropdef.as(not null)
207 var ndoc = npropdef.n_doc
208 if ndoc != null then
209 tc = new HTMLTag("testcase")
210 tc.attr("classname", mmodule.full_name + "." + mclassdef.mclass.full_name)
211 tc.attr("name", mpropdef.mproperty.full_name)
212 d2m.extract(ndoc, tc)
213 end
214 end
215 end
216
217 return ts
218 end
219 end
220
221 redef class ToolContext
222 var opt_full = new OptionBool("Process also imported modules", "--full")
223 var opt_output = new OptionString("Output name (default is 'nitunit.xml')", "-o", "--output")
224 var opt_dir = new OptionString("Working directory (default is '.nitunit')", "--dir")
225 end
226
227 var toolcontext = new ToolContext
228
229 toolcontext.option_context.add_option(toolcontext.opt_full, toolcontext.opt_output, toolcontext.opt_dir)
230 toolcontext.tooldescription = "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
231
232 toolcontext.process_options(args)
233 var args = toolcontext.option_context.rest
234
235 var model = new Model
236 var modelbuilder = new ModelBuilder(model, toolcontext)
237
238 var mmodules = modelbuilder.parse(args)
239 modelbuilder.run_phases
240
241 var page = new HTMLTag("testsuites")
242
243 if toolcontext.opt_full.value then mmodules = model.mmodules
244
245 for m in mmodules do
246 page.add modelbuilder.test_markdown(m)
247 end
248
249 var file = toolcontext.opt_output.value
250 if file == null then file = "nitunit.xml"
251 page.write_to_file(file)