1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Base options for testing tools.
19 private import parser_util
23 redef class ToolContext
25 var opt_full
= new OptionBool("Process also imported modules", "--full")
27 var opt_output
= new OptionString("Output name (default is 'nitunit.xml')", "-o", "--output")
29 var opt_dir
= new OptionString("Working directory (default is '.nitunit')", "--dir")
31 var opt_noact
= new OptionBool("Does not compile and run tests", "--no-act")
33 var opt_nitc
= new OptionString("nitc compiler to use", "--nitc")
35 var opt_no_time
= new OptionBool("Disable time information in XML", "--no-time")
37 # Working directory for testing.
38 fun test_dir
: String do
39 var dir
= opt_dir
.value
40 if dir
== null then return "nitunit.out"
44 # Search the `nitc` compiler to use
46 # If not `nitc` is suitable, then prints an error and quit.
49 var nitc
= opt_nitc
.value
51 if not nitc
.file_exists
then
52 fatal_error
(null, "error: cannot find `{nitc}` given by --nitc.")
60 if not nitc
.file_exists
then
61 fatal_error
(null, "error: cannot find `{nitc}` given by NITC.")
68 nitc
= nit_dir
/"bin/nitc"
69 if not nitc
.file_exists
then
70 fatal_error
(null, "Error: cannot find nitc. Set envvar NIT_DIR or NITC or use the --nitc option.")
76 # Execute a system command in a more safe context than `Sys::system`.
77 fun safe_exec
(command
: String): Int
80 var real_command
= """
82 ulimit -f {{{ulimit_file}}} 2> /dev/null
83 ulimit -t {{{ulimit_usertime}}} 2> /dev/null
87 return system
(real_command
)
90 # The maximum size (in KB) of files written by a command executed trough `safe_exec`
93 var ulimit_file
= 65536 is writable
95 # The maximum amount of cpu time (in seconds) for a command executed trough `safe_exec`
97 # Default: 10 CPU minute
98 var ulimit_usertime
= 600 is writable
100 # Show a single-line status to use as a progression.
102 # If `has_progress_bar` is true, then the output is a progress bar.
103 # The printed the line starts with `'\r'` and is not ended by a `'\n'`.
104 # So it is expected that:
105 # * no other output is printed between two calls
106 # * the last `show_unit_status` is followed by a new-line
108 # If `has_progress_bar` is false, then only the first and last state is shown
109 fun show_unit_status
(name
: String, tests
: SequenceRead[UnitTest])
111 var esc
= 27.code_point
.to_s
112 var line
= "\r\x1B[K==== {name} ["
113 var done
= tests
.length
116 if not t
.is_done
then
119 else if t
.error
== null then
120 line
+= ".".green
.bold
127 if not has_progress_bar
then
129 print
"==== {name} | tests: {tests.length}"
134 if done
< tests
.length
then
135 line
+= "] {done}/{tests.length}"
137 line
+= "] tests: {tests.length} "
139 line
+= "OK".green
.bold
141 line
+= "KO: {fails}".red
.bold
147 # Is a progress bar printed?
149 # true if color (because and non-verbose mode
150 # (because verbose mode messes up with the progress bar).
151 fun has_progress_bar
: Bool
153 return not opt_no_color
.value
and opt_verbose
.value
<= 0
156 # Clear the line if `has_progress_bar` (no-op else)
157 fun clear_progress_bar
159 if has_progress_bar
then printn
"\r\x1B[K"
162 # Show the full description of the test-case.
164 # The output honors `--no-color`.
166 # `more message`, if any, is added after the error message.
167 fun show_unit
(test
: UnitTest, more_message
: nullable String) do
168 print test
.to_screen
(more_message
, not opt_no_color
.value
)
172 # A unit test is an elementary test discovered, run and reported by nitunit.
174 # This class factorizes `DocUnit` and `TestCase`.
175 abstract class UnitTest
176 # The name of the unit to show in messages
177 fun full_name
: String is abstract
179 # The location of the unit test to show in messages.
180 fun location
: Location is abstract
182 # Flag that indicates if the unit test was compiled/run.
183 var is_done
: Bool = false is writable
185 # Error message occurred during test-case execution (or compilation).
187 # e.g.: `Runtime Error`
188 var error
: nullable String = null is writable
190 # Was the test case executed at least once?
192 # This will indicate the status of the test (failture or error)
193 var was_exec
= false is writable
195 # The raw output of the execution (or compilation)
197 # It merges the standard output and error output
198 var raw_output
: nullable String = null is writable
200 # The location where the error occurred, if it makes sense.
201 var error_location
: nullable Location = null is writable
203 # Additional noteworthy information when a test success.
204 var info
: nullable String = null
206 # Time for the execution, in seconds
207 var real_time
: Float = 0.0 is writable
209 # A colorful `[OK]` or `[KO]`.
210 fun status_tag
(color
: nullable Bool): String do
211 color
= color
or else true
214 else if error
!= null then
216 if color
then res
= res
.red
.bold
220 if color
then res
= res
.green
.bold
225 # The full (color) description of the test-case.
227 # `more message`, if any, is added after the error message.
228 fun to_screen
(more_message
: nullable String, color
: nullable Bool): String do
229 color
= color
or else true
231 var error
= self.error
232 if error
!= null then
233 if more_message
!= null then error
+= " " + more_message
234 var loc
= error_location
or else location
236 res
= "{status_tag(color)} {full_name}\n {loc.to_s.yellow}: {error}\n{loc.colored_line("1;31")}"
238 res
= "{status_tag(color)} {full_name}\n {loc}: {error}"
240 var output
= self.raw_output
241 if output
!= null then
242 res
+= "\n Output\n\t{output.chomp.replace("\n", "\n\t")}\n"
245 res
= "{status_tag(color)} {full_name}"
246 if more_message
!= null then res
+= more_message
255 # Return a `<testcase>` XML node in format compatible with Jenkins unit tests.
256 fun to_xml
: HTMLTag do
257 var tc
= new HTMLTag("testcase")
258 tc
.attr
("classname", xml_classname
)
259 tc
.attr
("name", xml_name
)
260 tc
.attr
("time", real_time
.to_s
)
262 var output
= self.raw_output
263 if output
!= null then output
= output
.trunc
(8192).filter_nonprintable
264 var error
= self.error
265 if error
!= null then
268 node
= tc
.open
("error").attr
("message", error
)
270 node
= tc
.open
("failure").attr
("message", error
)
272 if output
!= null then
275 else if output
!= null then
276 tc
.open
("system-err").append
(output
)
281 # The `classname` attribute of the XML format.
283 # NOTE: jenkins expects a '.' in the classname attr
286 fun xml_classname
: String is abstract
288 # The `name` attribute of the XML format.
291 fun xml_name
: String is abstract
295 # If needed, truncate `self` at `max_length` characters and append an informative `message`.
298 # assert "hello".trunc(10) == "hello"
299 # assert "hello".trunc(2) == "he[truncated. Full size is 5]"
300 # assert "hello".trunc(2, "...") == "he..."
302 fun trunc
(max_length
: Int, message
: nullable String): String
304 if length
<= max_length
then return self
305 if message
== null then message
= "[truncated. Full size is {length}]"
306 return substring
(0, max_length
) + message
309 # Use a special notation for whitespace characters that are not `'\n'` (LFD) or `' '` (space).
312 # assert "hello".filter_nonprintable == "hello"
313 # assert "\r\n\t".filter_nonprintable == "^13\n^9"
315 fun filter_nonprintable
: String
319 var cp
= c
.code_point
320 if cp
< 32 and c
!= '\n' then