nitunit: separate the error message from the test output
[nit.git] / src / testing / testing_doc.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 from code comments.
16 module testing_doc
17
18 private import parser_util
19 import testing_base
20 import markdown
21 import html
22
23 # Extractor, Executor and Reporter for the tests in a module
24 class NitUnitExecutor
25 super HTMLDecorator
26
27 # Toolcontext used to parse Nit code blocks.
28 var toolcontext: ToolContext
29
30 # The prefix of the generated Nit source-file
31 var prefix: String
32
33 # The module to import, if any
34 var mmodule: nullable MModule
35
36 # The XML node associated to the module
37 var testsuite: HTMLTag
38
39 # Markdown processor used to parse markdown comments and extract code.
40 var mdproc = new MarkdownProcessor
41
42 init do
43 mdproc.emitter.decorator = new NitunitDecorator(self)
44 end
45
46 # The associated documentation object
47 var mdoc: nullable MDoc = null
48
49 # used to generate distinct names
50 var cpt = 0
51
52 # The last docunit extracted from a mdoc.
53 #
54 # Is used because a new code-block might just be added to it.
55 var last_docunit: nullable DocUnit = null
56
57 var xml_classname: String is noautoinit
58
59 var xml_name: String is noautoinit
60
61 # The entry point for a new `ndoc` node
62 # Fill `docunits` with new discovered unit of tests.
63 fun extract(mdoc: MDoc, xml_classname, xml_name: String)
64 do
65 last_docunit = null
66 self.xml_classname = xml_classname
67 self.xml_name = xml_name
68
69 self.mdoc = mdoc
70
71 # Populate `blocks` from the markdown decorator
72 mdproc.process(mdoc.content.join("\n"))
73 end
74
75 # All extracted docunits
76 var docunits = new Array[DocUnit]
77
78 fun mark_done(du: DocUnit)
79 do
80 du.is_done = true
81 end
82
83 # Execute all the docunits
84 fun run_tests
85 do
86 var simple_du = new Array[DocUnit]
87 for du in docunits do
88 # Skip existing errors
89 if du.error != null then
90 mark_done(du)
91 continue
92 end
93
94 var ast = toolcontext.parse_something(du.block)
95 if ast isa AExpr then
96 simple_du.add du
97 else
98 test_single_docunit(du)
99 end
100 end
101
102 test_simple_docunits(simple_du)
103
104 for du in docunits do
105 print du.to_screen
106 end
107
108 for du in docunits do
109 testsuite.add du.to_xml
110 end
111 end
112
113 # Executes multiples doc-units in a shared program.
114 # Used for docunits simple block of code (without modules, classes, functions etc.)
115 #
116 # In case of compilation error, the method fallbacks to `test_single_docunit` to
117 # * locate exactly the compilation problem in the problematic docunit.
118 # * permit the execution of the other docunits that may be correct.
119 fun test_simple_docunits(dus: Array[DocUnit])
120 do
121 if dus.is_empty then return
122
123 var file = "{prefix}-0.nit"
124
125 var dir = file.dirname
126 if dir != "" then dir.mkdir
127 var f
128 f = create_unitfile(file)
129 var i = 0
130 for du in dus do
131
132 i += 1
133 f.write("fun run_{i} do\n")
134 f.write("# {du.full_name}\n")
135 f.write(du.block)
136 f.write("end\n")
137 end
138 f.write("var a = args.first.to_i\n")
139 for j in [1..i] do
140 f.write("if a == {j} then run_{j}\n")
141 end
142 f.close
143
144 if toolcontext.opt_noact.value then return
145
146 var res = compile_unitfile(file)
147
148 if res != 0 then
149 # Compilation error.
150 # Fall-back to individual modes:
151 for du in dus do
152 test_single_docunit(du)
153 end
154 return
155 end
156
157 i = 0
158 for du in dus do
159 i += 1
160 toolcontext.info("Execute doc-unit {du.full_name} in {file} {i}", 1)
161 var res2 = toolcontext.safe_exec("{file.to_program_name}.bin {i} >'{file}.out1' 2>&1 </dev/null")
162 du.was_exec = true
163
164 var content = "{file}.out1".to_path.read_all
165 du.raw_output = content
166
167 if res2 != 0 then
168 du.error = "Runtime error in {file} with argument {i}"
169 toolcontext.modelbuilder.failed_entities += 1
170 end
171 mark_done(du)
172 end
173 end
174
175 # Executes a single doc-unit in its own program.
176 # Used for docunits larger than a single block of code (with modules, classes, functions etc.)
177 fun test_single_docunit(du: DocUnit)
178 do
179 cpt += 1
180 var file = "{prefix}-{cpt}.nit"
181
182 toolcontext.info("Execute doc-unit {du.full_name} in {file}", 1)
183
184 var f
185 f = create_unitfile(file)
186 f.write(du.block)
187 f.close
188
189 if toolcontext.opt_noact.value then return
190
191 var res = compile_unitfile(file)
192 var res2 = 0
193 if res == 0 then
194 res2 = toolcontext.safe_exec("{file.to_program_name}.bin >'{file}.out1' 2>&1 </dev/null")
195 du.was_exec = true
196 end
197
198 var content = "{file}.out1".to_path.read_all
199 du.raw_output = content
200
201 if res != 0 then
202 du.error = "Compilation error in {file}"
203 toolcontext.modelbuilder.failed_entities += 1
204 else if res2 != 0 then
205 du.error = "Runtime error in {file}"
206 toolcontext.modelbuilder.failed_entities += 1
207 end
208 mark_done(du)
209 end
210
211 # Create and fill the header of a unit file `file`.
212 #
213 # A unit file is a Nit source file generated from one
214 # or more docunits that will be compiled and executed.
215 #
216 # The handled on the file is returned and must be completed and closed.
217 #
218 # `file` should be a valid filepath for a Nit source file.
219 private fun create_unitfile(file: String): Writer
220 do
221 var dir = file.dirname
222 if dir != "" then dir.mkdir
223 var f
224 f = new FileWriter.open(file)
225 f.write("# GENERATED FILE\n")
226 f.write("# Docunits extracted from comments\n")
227 if mmodule != null then
228 f.write("import {mmodule.name}\n")
229 end
230 f.write("\n")
231 return f
232 end
233
234 # Compile an unit file and return the compiler return code
235 #
236 # Can terminate the program if the compiler is not found
237 private fun compile_unitfile(file: String): Int
238 do
239 var nitc = toolcontext.find_nitc
240 var opts = new Array[String]
241 if mmodule != null then
242 opts.add "-I {mmodule.filepath.dirname}"
243 end
244 var cmd = "{nitc} --ignore-visibility --no-color '{file}' {opts.join(" ")} >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
245 var res = toolcontext.safe_exec(cmd)
246 return res
247 end
248 end
249
250 private class NitunitDecorator
251 super HTMLDecorator
252
253 var executor: NitUnitExecutor
254
255 redef fun add_code(v, block) do
256 var code = block.raw_content
257 var meta = block.meta or else "nit"
258 # Do not try to test non-nit code.
259 if meta != "nit" then return
260 # Try to parse code blocks
261 var ast = executor.toolcontext.parse_something(code)
262
263 var mdoc = executor.mdoc
264 assert mdoc != null
265
266 # Skip pure comments
267 if ast isa TComment then return
268
269 # The location is computed according to the starts of the mdoc and the block
270 # Note, the following assumes that all the comments of the mdoc are correctly aligned.
271 var loc = block.block.location
272 var line_offset = loc.line_start + mdoc.location.line_start - 2
273 var column_offset = loc.column_start + mdoc.location.column_start
274 # Hack to handle precise location in blocks
275 # TODO remove when markdown is more reliable
276 if block isa BlockFence then
277 # Skip the starting fence
278 line_offset += 1
279 else
280 # Account a standard 4 space indentation
281 column_offset += 4
282 end
283
284 # We want executable code
285 if not (ast isa AModule or ast isa ABlockExpr or ast isa AExpr) then
286 var message
287 var l = ast.location
288 # Get real location of the node (or error)
289 var location = new Location(mdoc.location.file,
290 l.line_start + line_offset,
291 l.line_end + line_offset,
292 l.column_start + column_offset,
293 l.column_end + column_offset)
294 if ast isa AError then
295 message = ast.message
296 else
297 message = "Error: Invalid Nit code."
298 end
299
300 var du = new_docunit
301 du.block += code
302 du.error_location = location
303 du.error = message
304 executor.toolcontext.modelbuilder.failed_entities += 1
305 return
306 end
307
308 # Create a first block
309 # Or create a new block for modules that are more than a main part
310 var last_docunit = executor.last_docunit
311 if last_docunit == null or ast isa AModule then
312 last_docunit = new_docunit
313 executor.last_docunit = last_docunit
314 end
315
316 # Add it to the file
317 last_docunit.block += code
318
319 # In order to retrieve precise positions,
320 # the real position of each line of the raw_content is stored.
321 # See `DocUnit::real_location`
322 line_offset -= loc.line_start - 1
323 for i in [loc.line_start..loc.line_end] do
324 last_docunit.lines.add i + line_offset
325 last_docunit.columns.add column_offset
326 end
327 end
328
329 # Return and register a new empty docunit
330 fun new_docunit: DocUnit
331 do
332 var mdoc = executor.mdoc
333 assert mdoc != null
334
335 var next_number = 0
336 var name = executor.xml_name
337 if executor.docunits.not_empty and executor.docunits.last.mdoc == mdoc then
338 next_number = executor.docunits.last.number + 1
339 name += "+" + next_number.to_s
340 end
341
342 var res = new DocUnit(mdoc, next_number, "", executor.xml_classname, name)
343 executor.docunits.add res
344 executor.toolcontext.modelbuilder.unit_entities += 1
345 return res
346 end
347 end
348
349 # A unit-test extracted from some documentation.
350 #
351 # A docunit is extracted from the code-blocks of mdocs.
352 # Each mdoc can contains more than one docunit, and a single docunit can be made of more that a single code-block.
353 class DocUnit
354 super UnitTest
355
356 # The doc that contains self
357 var mdoc: MDoc
358
359 # The numbering of self in mdoc (starting with 0)
360 var number: Int
361
362 redef fun full_name do
363 var mentity = mdoc.original_mentity
364 if mentity != null then
365 return mentity.full_name
366 else
367 return xml_classname + "." + xml_name
368 end
369 end
370
371 # The text of the code to execute.
372 #
373 # This is the verbatim content on one, or more, code-blocks from `mdoc`
374 var block: String
375
376 # For each line in `block`, the associated line in the mdoc
377 #
378 # Is used to give precise locations
379 var lines = new Array[Int]
380
381 # For each line in `block`, the associated column in the mdoc
382 #
383 # Is used to give precise locations
384 var columns = new Array[Int]
385
386 # The location of the whole docunit.
387 #
388 # If `self` is made of multiple code-blocks, then the location
389 # starts at the first code-books and finish at the last one, thus includes anything between.
390 redef var location is lazy do
391 return new Location(mdoc.location.file, lines.first, lines.last+1, columns.first+1, 0)
392 end
393
394 # Compute the real location of a node on the `ast` based on `mdoc.location`
395 #
396 # The result is basically: ast_location + markdown location of the piece + mdoc.location
397 #
398 # The fun is that a single docunit can be made of various pieces of code blocks.
399 fun real_location(ast_location: Location): Location
400 do
401 var mdoc = self.mdoc
402 var res = new Location(mdoc.location.file, lines[ast_location.line_start-1],
403 lines[ast_location.line_end-1],
404 columns[ast_location.line_start-1] + ast_location.column_start,
405 columns[ast_location.line_end-1] + ast_location.column_end)
406 return res
407 end
408
409 redef fun to_xml
410 do
411 var res = super
412 res.open("system-out").append(block)
413 return res
414 end
415
416 redef var xml_classname
417 redef var xml_name
418 end
419
420 redef class ModelBuilder
421 # Total number analyzed `MEntity`
422 var total_entities = 0
423
424 # The number of `MEntity` that have some documentation
425 var doc_entities = 0
426
427 # The total number of executed docunits
428 var unit_entities = 0
429
430 # The number failed docunits
431 var failed_entities = 0
432
433 # Extracts and executes all the docunits in the `mmodule`
434 # Returns a JUnit-compatible `<testsuite>` XML element that contains the results of the executions.
435 fun test_markdown(mmodule: MModule): HTMLTag
436 do
437 var ts = new HTMLTag("testsuite")
438 toolcontext.info("nitunit: doc-unit {mmodule}", 2)
439
440 var nmodule = mmodule2node(mmodule)
441 if nmodule == null then return ts
442
443 # usualy, only the original module must be imported in the unit test.
444 var o = mmodule
445 var g = o.mgroup
446 if g != null and g.mpackage.name == "core" then
447 # except for a unit test in a module of `core`
448 # in this case, the whole `core` must be imported
449 o = get_mmodule_by_name(nmodule, g, g.mpackage.name).as(not null)
450 end
451
452 ts.attr("package", mmodule.full_name)
453
454 var prefix = toolcontext.test_dir
455 prefix = prefix.join_path(mmodule.to_s)
456 var d2m = new NitUnitExecutor(toolcontext, prefix, o, ts)
457
458 do
459 total_entities += 1
460 var nmoduledecl = nmodule.n_moduledecl
461 if nmoduledecl == null then break label x
462 var ndoc = nmoduledecl.n_doc
463 if ndoc == null then break label x
464 doc_entities += 1
465 # NOTE: jenkins expects a '.' in the classname attr
466 d2m.extract(ndoc.to_mdoc, "nitunit." + mmodule.full_name + ".<module>", "<module>")
467 end label x
468 for nclassdef in nmodule.n_classdefs do
469 var mclassdef = nclassdef.mclassdef
470 if mclassdef == null then continue
471 if nclassdef isa AStdClassdef then
472 total_entities += 1
473 var ndoc = nclassdef.n_doc
474 if ndoc != null then
475 doc_entities += 1
476 d2m.extract(ndoc.to_mdoc, "nitunit." + mmodule.full_name + "." + mclassdef.mclass.full_name, "<class>")
477 end
478 end
479 for npropdef in nclassdef.n_propdefs do
480 var mpropdef = npropdef.mpropdef
481 if mpropdef == null then continue
482 total_entities += 1
483 var ndoc = npropdef.n_doc
484 if ndoc != null then
485 doc_entities += 1
486 d2m.extract(ndoc.to_mdoc, "nitunit." + mmodule.full_name + "." + mclassdef.mclass.full_name, mpropdef.mproperty.full_name)
487 end
488 end
489 end
490
491 d2m.run_tests
492
493 return ts
494 end
495
496 # Extracts and executes all the docunits in the readme of the `mgroup`
497 # Returns a JUnit-compatible `<testsuite>` XML element that contains the results of the executions.
498 fun test_group(mgroup: MGroup): HTMLTag
499 do
500 var ts = new HTMLTag("testsuite")
501 toolcontext.info("nitunit: doc-unit group {mgroup}", 2)
502
503 # usually, only the default module must be imported in the unit test.
504 var o = mgroup.default_mmodule
505
506 ts.attr("package", mgroup.full_name)
507
508 var prefix = toolcontext.test_dir
509 prefix = prefix.join_path(mgroup.to_s)
510 var d2m = new NitUnitExecutor(toolcontext, prefix, o, ts)
511
512 total_entities += 1
513 var mdoc = mgroup.mdoc
514 if mdoc == null then return ts
515
516 doc_entities += 1
517 # NOTE: jenkins expects a '.' in the classname attr
518 d2m.extract(mdoc, "nitunit." + mgroup.full_name, "<group>")
519
520 d2m.run_tests
521
522 return ts
523 end
524
525 # Test a document object unrelated to a Nit entity
526 fun test_mdoc(mdoc: MDoc): HTMLTag
527 do
528 var ts = new HTMLTag("testsuite")
529 var file = mdoc.location.to_s
530
531 toolcontext.info("nitunit: doc-unit file {file}", 2)
532
533 ts.attr("package", file)
534
535 var prefix = toolcontext.test_dir / "file"
536 var d2m = new NitUnitExecutor(toolcontext, prefix, null, ts)
537
538 total_entities += 1
539 doc_entities += 1
540
541 # NOTE: jenkins expects a '.' in the classname attr
542 d2m.extract(mdoc, "nitunit.<file>", file)
543 d2m.run_tests
544
545 return ts
546 end
547 end