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