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 # Working directory for testing.
36 fun test_dir
: String do
37 var dir
= opt_dir
.value
38 if dir
== null then return ".nitunit"
42 # Search the `nitc` compiler to use
44 # If not `nitc` is suitable, then prints an error and quit.
47 var nitc
= opt_nitc
.value
49 if not nitc
.file_exists
then
50 fatal_error
(null, "error: cannot find `{nitc}` given by --nitc.")
58 if not nitc
.file_exists
then
59 fatal_error
(null, "error: cannot find `{nitc}` given by NITC.")
66 nitc
= nit_dir
/"bin/nitc"
67 if not nitc
.file_exists
then
68 fatal_error
(null, "Error: cannot find nitc. Set envvar NIT_DIR or NITC or use the --nitc option.")
74 # Execute a system command in a more safe context than `Sys::system`.
75 fun safe_exec
(command
: String): Int
78 var real_command
= """
80 ulimit -f {{{ulimit_file}}} 2> /dev/null
81 ulimit -t {{{ulimit_usertime}}} 2> /dev/null
85 return system
(real_command
)
88 # The maximum size (in KB) of files written by a command executed trough `safe_exec`
91 var ulimit_file
= 65536 is writable
93 # The maximum amount of cpu time (in seconds) for a command executed trough `safe_exec`
95 # Default: 10 CPU minute
96 var ulimit_usertime
= 600 is writable
98 # Show a single-line status to use as a progression.
100 # Note that the line starts with `'\r'` and is not ended by a `'\n'`.
101 # So it is expected that:
102 # * no other output is printed between two calls
103 # * the last `show_unit_status` is followed by a new-line
104 fun show_unit_status
(name
: String, tests
: SequenceRead[UnitTest], more_message
: nullable String)
106 var esc
= 27.code_point
.to_s
107 var line
= "\r{esc}[K* {name} ["
108 var done
= tests
.length
110 if not t
.is_done
then
113 else if t
.error
== null then
114 line
+= ".".green
.bold
119 line
+= "] {done}/{tests.length}"
120 if more_message
!= null then
121 line
+= " " + more_message
128 # A unit test is an elementary test discovered, run and reported by nitunit.
130 # This class factorizes `DocUnit` and `TestCase`.
131 abstract class UnitTest
132 # The name of the unit to show in messages
133 fun full_name
: String is abstract
135 # The location of the unit test to show in messages.
136 fun location
: Location is abstract
138 # Flag that indicates if the unit test was compiled/run.
139 var is_done
: Bool = false is writable
141 # Error message occurred during test-case execution (or compilation).
143 # e.g.: `Runtime Error`
144 var error
: nullable String = null is writable
146 # Was the test case executed at least once?
148 # This will indicate the status of the test (failture or error)
149 var was_exec
= false is writable
151 # The raw output of the execution (or compilation)
153 # It merges the standard output and error output
154 var raw_output
: nullable String = null is writable
156 # The location where the error occurred, if it makes sense.
157 var error_location
: nullable Location = null is writable
159 # A colorful `[OK]` or `[KO]`.
160 fun status_tag
: String do
163 else if error
!= null then
164 return "[KO]".red
.bold
166 return "[OK]".green
.bold
170 # The full (color) description of the test-case.
172 # `more message`, if any, is added after the error message.
173 fun to_screen
(more_message
: nullable String): String do
175 var error
= self.error
176 if error
!= null then
177 if more_message
!= null then error
+= " " + more_message
178 var loc
= error_location
or else location
179 res
= "{status_tag} {full_name}\n {loc.to_s.yellow}: {error}\n{loc.colored_line("1;31")}"
180 var output
= self.raw_output
181 if output
!= null then
182 res
+= "\n Output\n\t{output.chomp.replace("\n", "\n\t")}\n"
185 res
= "{status_tag} {full_name}"
186 if more_message
!= null then res
+= more_message
191 # Return a `<testcase>` XML node in format compatible with Jenkins unit tests.
192 fun to_xml
: HTMLTag do
193 var tc
= new HTMLTag("testcase")
194 tc
.attr
("classname", xml_classname
)
195 tc
.attr
("name", xml_name
)
196 var error
= self.error
197 if error
!= null then
199 tc
.open
("error").append
(error
)
201 tc
.open
("failure").append
(error
)
204 var output
= self.raw_output
205 if output
!= null then
206 tc
.open
("system-err").append
(output
.trunc
(8192).filter_nonprintable
)
211 # The `classname` attribute of the XML format.
213 # NOTE: jenkins expects a '.' in the classname attr
216 fun xml_classname
: String is abstract
218 # The `name` attribute of the XML format.
221 fun xml_name
: String is abstract
225 # If needed, truncate `self` at `max_length` characters and append an informative `message`.
228 # assert "hello".trunc(10) == "hello"
229 # assert "hello".trunc(2) == "he[truncated. Full size is 5]"
230 # assert "hello".trunc(2, "...") == "he..."
232 fun trunc
(max_length
: Int, message
: nullable String): String
234 if length
<= max_length
then return self
235 if message
== null then message
= "[truncated. Full size is {length}]"
236 return substring
(0, max_length
) + message
239 # Use a special notation for whitespace characters that are not `'\n'` (LFD) or `' '` (space).
242 # assert "hello".filter_nonprintable == "hello"
243 # assert "\r\n\t".filter_nonprintable == "^13\n^9"
245 fun filter_nonprintable
: String
249 var cp
= c
.code_point
250 if cp
< 32 and c
!= '\n' then