| 1 |
# ############################################################################## |
| 2 |
#(G)odot (U)nit (T)est class |
| 3 |
# |
| 4 |
# ############################################################################## |
| 5 |
# The MIT License (MIT) |
| 6 |
# ===================== |
| 7 |
# |
| 8 |
# Copyright (c) 2020 Tom "Butch" Wesley |
| 9 |
# |
| 10 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
# of this software and associated documentation files (the "Software"), to deal |
| 12 |
# in the Software without restriction, including without limitation the rights |
| 13 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
# copies of the Software, and to permit persons to whom the Software is |
| 15 |
# furnished to do so, subject to the following conditions: |
| 16 |
# |
| 17 |
# The above copyright notice and this permission notice shall be included in |
| 18 |
# all copies or substantial portions of the Software. |
| 19 |
# |
| 20 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 |
# THE SOFTWARE. |
| 27 |
# |
| 28 |
# ############################################################################## |
| 29 |
# Description |
| 30 |
# ----------- |
| 31 |
# Command line interface for the GUT unit testing tool. Allows you to run tests |
| 32 |
# from the command line instead of running a scene. Place this script along with |
| 33 |
# gut.gd into your scripts directory at the root of your project. Once there you |
| 34 |
# can run this script (from the root of your project) using the following command: |
| 35 |
# godot -s -d test/gut/gut_cmdln.gd |
| 36 |
# |
| 37 |
# See the readme for a list of options and examples. You can also use the -gh |
| 38 |
# option to get more information about how to use the command line interface. |
| 39 |
# ############################################################################## |
| 40 |
extends SceneTree |
| 41 |
|
| 42 |
var Optparse = load('res://addons/gut/optparse.gd') |
| 43 |
var Gut = load('res://addons/gut/gut.gd') |
| 44 |
|
| 45 |
# ------------------------------------------------------------------------------ |
| 46 |
# Helper class to resolve the various different places where an option can |
| 47 |
# be set. Using the get_value method will enforce the order of precedence of: |
| 48 |
# 1. command line value |
| 49 |
# 2. config file value |
| 50 |
# 3. default value |
| 51 |
# |
| 52 |
# The idea is that you set the base_opts. That will get you a copies of the |
| 53 |
# hash with null values for the other types of values. Lower precedented hashes |
| 54 |
# will punch through null values of higher precedented hashes. |
| 55 |
# ------------------------------------------------------------------------------ |
| 56 |
class OptionResolver: |
| 57 |
var base_opts = null |
| 58 |
var cmd_opts = null |
| 59 |
var config_opts = null |
| 60 |
|
| 61 |
|
| 62 |
func get_value(key): |
| 63 |
return _nvl(cmd_opts[key], _nvl(config_opts[key], base_opts[key])) |
| 64 |
|
| 65 |
func set_base_opts(opts): |
| 66 |
base_opts = opts |
| 67 |
cmd_opts = _null_copy(opts) |
| 68 |
config_opts = _null_copy(opts) |
| 69 |
|
| 70 |
# creates a copy of a hash with all values null. |
| 71 |
func _null_copy(h): |
| 72 |
var new_hash = {} |
| 73 |
for key in h: |
| 74 |
new_hash[key] = null |
| 75 |
return new_hash |
| 76 |
|
| 77 |
func _nvl(a, b): |
| 78 |
if(a == null): |
| 79 |
return b |
| 80 |
else: |
| 81 |
return a |
| 82 |
func _string_it(h): |
| 83 |
var to_return = '' |
| 84 |
for key in h: |
| 85 |
to_return += str('(',key, ':', _nvl(h[key], 'NULL'), ')') |
| 86 |
return to_return |
| 87 |
|
| 88 |
func to_s(): |
| 89 |
return str("base:\n", _string_it(base_opts), "\n", \ |
| 90 |
"config:\n", _string_it(config_opts), "\n", \ |
| 91 |
"cmd:\n", _string_it(cmd_opts), "\n", \ |
| 92 |
"resolved:\n", _string_it(get_resolved_values())) |
| 93 |
|
| 94 |
func get_resolved_values(): |
| 95 |
var to_return = {} |
| 96 |
for key in base_opts: |
| 97 |
to_return[key] = get_value(key) |
| 98 |
return to_return |
| 99 |
|
| 100 |
func to_s_verbose(): |
| 101 |
var to_return = '' |
| 102 |
var resolved = get_resolved_values() |
| 103 |
for key in base_opts: |
| 104 |
to_return += str(key, "\n") |
| 105 |
to_return += str(' default: ', _nvl(base_opts[key], 'NULL'), "\n") |
| 106 |
to_return += str(' config: ', _nvl(config_opts[key], ' --'), "\n") |
| 107 |
to_return += str(' cmd: ', _nvl(cmd_opts[key], ' --'), "\n") |
| 108 |
to_return += str(' final: ', _nvl(resolved[key], 'NULL'), "\n") |
| 109 |
|
| 110 |
return to_return |
| 111 |
|
| 112 |
# ------------------------------------------------------------------------------ |
| 113 |
# Here starts the actual script that uses the Options class to kick off Gut |
| 114 |
# and run your tests. |
| 115 |
# ------------------------------------------------------------------------------ |
| 116 |
var _utils = load('res://addons/gut/utils.gd').get_instance() |
| 117 |
# instance of gut |
| 118 |
var _tester = null |
| 119 |
# array of command line options specified |
| 120 |
var _final_opts = [] |
| 121 |
# Hash for easier access to the options in the code. Options will be |
| 122 |
# extracted into this hash and then the hash will be used afterwards so |
| 123 |
# that I don't make any dumb typos and get the neat code-sense when I |
| 124 |
# type a dot. |
| 125 |
var options = { |
| 126 |
background_color = Color(.15, .15, .15, 1).to_html(), |
| 127 |
config_file = 'res://.gutconfig.json', |
| 128 |
dirs = [], |
| 129 |
disable_colors = false, |
| 130 |
double_strategy = 'partial', |
| 131 |
font_color = Color(.8, .8, .8, 1).to_html(), |
| 132 |
font_name = 'CourierPrime', |
| 133 |
font_size = 16, |
| 134 |
hide_orphans = false, |
| 135 |
ignore_pause = false, |
| 136 |
include_subdirs = false, |
| 137 |
inner_class = '', |
| 138 |
log_level = 1, |
| 139 |
opacity = 100, |
| 140 |
post_run_script = '', |
| 141 |
pre_run_script = '', |
| 142 |
prefix = 'test_', |
| 143 |
selected = '', |
| 144 |
should_exit = false, |
| 145 |
should_exit_on_success = false, |
| 146 |
should_maximize = false, |
| 147 |
show_help = false, |
| 148 |
suffix = '.gd', |
| 149 |
tests = [], |
| 150 |
unit_test_name = '', |
| 151 |
} |
| 152 |
var valid_fonts = ['AnonymousPro', 'CourierPro', 'LobsterTwo', 'Default'] |
| 153 |
|
| 154 |
# flag to indicate if only a single script should be run. |
| 155 |
var _run_single = false |
| 156 |
|
| 157 |
|
| 158 |
func setup_options(): |
| 159 |
var opts = Optparse.new() |
| 160 |
opts.set_banner(('This is the command line interface for the unit testing tool Gut. With this ' + |
| 161 |
'interface you can run one or more test scripts from the command line. In order ' + |
| 162 |
'for the Gut options to not clash with any other godot options, each option starts ' + |
| 163 |
'with a "g". Also, any option that requires a value will take the form of ' + |
| 164 |
'"-g<name>=<value>". There cannot be any spaces between the option, the "=", or ' + |
| 165 |
'inside a specified value or godot will think you are trying to run a scene.')) |
| 166 |
opts.add('-gtest', [], 'Comma delimited list of full paths to test scripts to run.') |
| 167 |
opts.add('-gdir', options.dirs, 'Comma delimited list of directories to add tests from.') |
| 168 |
opts.add('-gprefix', options.prefix, 'Prefix used to find tests when specifying -gdir. Default "[default]".') |
| 169 |
opts.add('-gsuffix', options.suffix, 'Suffix used to find tests when specifying -gdir. Default "[default]".') |
| 170 |
opts.add('-ghide_orphans', false, 'Display orphan counts for tests and scripts. Default "[default]".') |
| 171 |
opts.add('-gmaximize', false, 'Maximizes test runner window to fit the viewport.') |
| 172 |
opts.add('-gexit', false, 'Exit after running tests. If not specified you have to manually close the window.') |
| 173 |
opts.add('-gexit_on_success', false, 'Only exit if all tests pass.') |
| 174 |
opts.add('-glog', options.log_level, 'Log level. Default [default]') |
| 175 |
opts.add('-gignore_pause', false, 'Ignores any calls to gut.pause_before_teardown.') |
| 176 |
opts.add('-gselect', '', ('Select a script to run initially. The first script that ' + |
| 177 |
'was loaded using -gtest or -gdir that contains the specified ' + |
| 178 |
'string will be executed. You may run others by interacting ' + |
| 179 |
'with the GUI.')) |
| 180 |
opts.add('-gunit_test_name', '', ('Name of a test to run. Any test that contains the specified ' + |
| 181 |
'text will be run, all others will be skipped.')) |
| 182 |
opts.add('-gh', false, 'Print this help, then quit') |
| 183 |
opts.add('-gconfig', 'res://.gutconfig.json', 'A config file that contains configuration information. Default is res://.gutconfig.json') |
| 184 |
opts.add('-ginner_class', '', 'Only run inner classes that contain this string') |
| 185 |
opts.add('-gopacity', options.opacity, 'Set opacity of test runner window. Use range 0 - 100. 0 = transparent, 100 = opaque.') |
| 186 |
opts.add('-gpo', false, 'Print option values from all sources and the value used, then quit.') |
| 187 |
opts.add('-ginclude_subdirs', false, 'Include subdirectories of -gdir.') |
| 188 |
opts.add('-gdouble_strategy', 'partial', 'Default strategy to use when doubling. Valid values are [partial, full]. Default "[default]"') |
| 189 |
opts.add('-gdisable_colors', false, 'Disable command line colors.') |
| 190 |
opts.add('-gpre_run_script', '', 'pre-run hook script path') |
| 191 |
opts.add('-gpost_run_script', '', 'post-run hook script path') |
| 192 |
opts.add('-gprint_gutconfig_sample', false, 'Print out json that can be used to make a gutconfig file then quit.') |
| 193 |
|
| 194 |
opts.add('-gfont_name', options.font_name, str('Valid values are: ', valid_fonts, '. Default "[default]"')) |
| 195 |
opts.add('-gfont_size', options.font_size, 'Font size, default "[default]"') |
| 196 |
opts.add('-gbackground_color', options.background_color, 'Background color as an html color, default "[default]"') |
| 197 |
opts.add('-gfont_color',options.font_color, 'Font color as an html color, default "[default]"') |
| 198 |
return opts |
| 199 |
|
| 200 |
|
| 201 |
# Parses options, applying them to the _tester or setting values |
| 202 |
# in the options struct. |
| 203 |
func extract_command_line_options(from, to): |
| 204 |
to.config_file = from.get_value('-gconfig') |
| 205 |
to.dirs = from.get_value('-gdir') |
| 206 |
to.disable_colors = from.get_value('-gdisable_colors') |
| 207 |
to.double_strategy = from.get_value('-gdouble_strategy') |
| 208 |
to.ignore_pause = from.get_value('-gignore_pause') |
| 209 |
to.include_subdirs = from.get_value('-ginclude_subdirs') |
| 210 |
to.inner_class = from.get_value('-ginner_class') |
| 211 |
to.log_level = from.get_value('-glog') |
| 212 |
to.opacity = from.get_value('-gopacity') |
| 213 |
to.post_run_script = from.get_value('-gpost_run_script') |
| 214 |
to.pre_run_script = from.get_value('-gpre_run_script') |
| 215 |
to.prefix = from.get_value('-gprefix') |
| 216 |
to.selected = from.get_value('-gselect') |
| 217 |
to.should_exit = from.get_value('-gexit') |
| 218 |
to.should_exit_on_success = from.get_value('-gexit_on_success') |
| 219 |
to.should_maximize = from.get_value('-gmaximize') |
| 220 |
to.hide_orphans = from.get_value('-ghide_orphans') |
| 221 |
to.suffix = from.get_value('-gsuffix') |
| 222 |
to.tests = from.get_value('-gtest') |
| 223 |
to.unit_test_name = from.get_value('-gunit_test_name') |
| 224 |
|
| 225 |
to.font_size = from.get_value('-gfont_size') |
| 226 |
to.font_name = from.get_value('-gfont_name') |
| 227 |
to.background_color = from.get_value('-gbackground_color') |
| 228 |
to.font_color = from.get_value('-gfont_color') |
| 229 |
|
| 230 |
|
| 231 |
func load_options_from_config_file(file_path, into): |
| 232 |
# SHORTCIRCUIT |
| 233 |
var f = File.new() |
| 234 |
if(!f.file_exists(file_path)): |
| 235 |
if(file_path != 'res://.gutconfig.json'): |
| 236 |
print('ERROR: Config File "', file_path, '" does not exist.') |
| 237 |
return -1 |
| 238 |
else: |
| 239 |
return 1 |
| 240 |
|
| 241 |
f.open(file_path, f.READ) |
| 242 |
var json = f.get_as_text() |
| 243 |
f.close() |
| 244 |
|
| 245 |
var results = JSON.parse(json) |
| 246 |
# SHORTCIRCUIT |
| 247 |
if(results.error != OK): |
| 248 |
print("\n\n",'!! ERROR parsing file: ', file_path) |
| 249 |
print(' at line ', results.error_line, ':') |
| 250 |
print(' ', results.error_string) |
| 251 |
return -1 |
| 252 |
|
| 253 |
# Get all the options out of the config file using the option name. The |
| 254 |
# options hash is now the default source of truth for the name of an option. |
| 255 |
for key in into: |
| 256 |
if(results.result.has(key)): |
| 257 |
into[key] = results.result[key] |
| 258 |
|
| 259 |
return 1 |
| 260 |
|
| 261 |
|
| 262 |
# Apply all the options specified to _tester. This is where the rubber meets |
| 263 |
# the road. |
| 264 |
func apply_options(opts): |
| 265 |
_tester = Gut.new() |
| 266 |
get_root().add_child(_tester) |
| 267 |
_tester.connect('tests_finished', self, '_on_tests_finished', [opts.should_exit, opts.should_exit_on_success]) |
| 268 |
_tester.set_yield_between_tests(true) |
| 269 |
_tester.set_modulate(Color(1.0, 1.0, 1.0, min(1.0, float(opts.opacity) / 100))) |
| 270 |
_tester.show() |
| 271 |
|
| 272 |
_tester.set_include_subdirectories(opts.include_subdirs) |
| 273 |
|
| 274 |
if(opts.should_maximize): |
| 275 |
_tester.maximize() |
| 276 |
|
| 277 |
if(opts.inner_class != ''): |
| 278 |
_tester.set_inner_class_name(opts.inner_class) |
| 279 |
_tester.set_log_level(opts.log_level) |
| 280 |
_tester.set_ignore_pause_before_teardown(opts.ignore_pause) |
| 281 |
|
| 282 |
for i in range(opts.dirs.size()): |
| 283 |
_tester.add_directory(opts.dirs[i], opts.prefix, opts.suffix) |
| 284 |
|
| 285 |
for i in range(opts.tests.size()): |
| 286 |
_tester.add_script(opts.tests[i]) |
| 287 |
|
| 288 |
if(opts.selected != ''): |
| 289 |
_tester.select_script(opts.selected) |
| 290 |
_run_single = true |
| 291 |
|
| 292 |
if(opts.double_strategy == 'full'): |
| 293 |
_tester.set_double_strategy(_utils.DOUBLE_STRATEGY.FULL) |
| 294 |
elif(opts.double_strategy == 'partial'): |
| 295 |
_tester.set_double_strategy(_utils.DOUBLE_STRATEGY.PARTIAL) |
| 296 |
|
| 297 |
_tester.set_unit_test_name(opts.unit_test_name) |
| 298 |
_tester.set_pre_run_script(opts.pre_run_script) |
| 299 |
_tester.set_post_run_script(opts.post_run_script) |
| 300 |
_tester.set_color_output(!opts.disable_colors) |
| 301 |
_tester.show_orphans(!opts.hide_orphans) |
| 302 |
|
| 303 |
_tester.get_gui().set_font_size(opts.font_size) |
| 304 |
_tester.get_gui().set_font(opts.font_name) |
| 305 |
if(opts.font_color != null and opts.font_color.is_valid_html_color()): |
| 306 |
_tester.get_gui().set_default_font_color(Color(opts.font_color)) |
| 307 |
if(opts.background_color != null and opts.background_color.is_valid_html_color()): |
| 308 |
_tester.get_gui().set_background_color(Color(opts.background_color)) |
| 309 |
|
| 310 |
|
| 311 |
func _print_gutconfigs(values): |
| 312 |
var header = """Here is a sample of a full .gutconfig.json file. |
| 313 |
You do not need to specify all values in your own file. The values supplied in |
| 314 |
this sample are what would be used if you ran gut w/o the -gprint_gutconfig_sample |
| 315 |
option (the resolved values where default < .gutconfig < command line).""" |
| 316 |
print("\n", header.replace("\n", ' '), "\n\n") |
| 317 |
var resolved = values |
| 318 |
|
| 319 |
# remove some options that don't make sense to be in config |
| 320 |
resolved.erase("config_file") |
| 321 |
resolved.erase("show_help") |
| 322 |
|
| 323 |
print("Here's a config with all the properties set based off of your current command and config.") |
| 324 |
var text = JSON.print(resolved) |
| 325 |
print(text.replace(',', ",\n")) |
| 326 |
|
| 327 |
for key in resolved: |
| 328 |
resolved[key] = null |
| 329 |
|
| 330 |
print("\n\nAnd here's an empty config for you fill in what you want.") |
| 331 |
text = JSON.print(resolved) |
| 332 |
print(text.replace(',', ",\n")) |
| 333 |
|
| 334 |
|
| 335 |
# parse options and run Gut |
| 336 |
func _run_gut(): |
| 337 |
var opt_resolver = OptionResolver.new() |
| 338 |
opt_resolver.set_base_opts(options) |
| 339 |
|
| 340 |
print("\n\n", ' --- Gut ---') |
| 341 |
var o = setup_options() |
| 342 |
|
| 343 |
var all_options_valid = o.parse() |
| 344 |
extract_command_line_options(o, opt_resolver.cmd_opts) |
| 345 |
var load_result = \ |
| 346 |
load_options_from_config_file(opt_resolver.get_value('config_file'), opt_resolver.config_opts) |
| 347 |
|
| 348 |
if(load_result == -1): # -1 indicates json parse error |
| 349 |
quit(1) |
| 350 |
else: |
| 351 |
if(!all_options_valid): |
| 352 |
quit(1) |
| 353 |
elif(o.get_value('-gh')): |
| 354 |
print(_utils.get_version_text()) |
| 355 |
o.print_help() |
| 356 |
quit() |
| 357 |
elif(o.get_value('-gpo')): |
| 358 |
print('All command line options and where they are specified. ' + |
| 359 |
'The "final" value shows which value will actually be used ' + |
| 360 |
'based on order of precedence (default < .gutconfig < cmd line).' + "\n") |
| 361 |
print(opt_resolver.to_s_verbose()) |
| 362 |
quit() |
| 363 |
elif(o.get_value('-gprint_gutconfig_sample')): |
| 364 |
_print_gutconfigs(opt_resolver.get_resolved_values()) |
| 365 |
quit() |
| 366 |
else: |
| 367 |
_final_opts = opt_resolver.get_resolved_values(); |
| 368 |
apply_options(_final_opts) |
| 369 |
_tester.test_scripts(!_run_single) |
| 370 |
|
| 371 |
|
| 372 |
# exit if option is set. |
| 373 |
func _on_tests_finished(should_exit, should_exit_on_success): |
| 374 |
if(_final_opts.dirs.size() == 0): |
| 375 |
if(_tester.get_summary().get_totals().scripts == 0): |
| 376 |
var lgr = _tester.get_logger() |
| 377 |
lgr.error('No directories configured. Add directories with options or a .gutconfig.json file. Use the -gh option for more information.') |
| 378 |
|
| 379 |
if(_tester.get_fail_count()): |
| 380 |
OS.exit_code = 1 |
| 381 |
|
| 382 |
# Overwrite the exit code with the post_script |
| 383 |
var post_inst = _tester.get_post_run_script_instance() |
| 384 |
if(post_inst != null and post_inst.get_exit_code() != null): |
| 385 |
OS.exit_code = post_inst.get_exit_code() |
| 386 |
|
| 387 |
if(should_exit or (should_exit_on_success and _tester.get_fail_count() == 0)): |
| 388 |
quit() |
| 389 |
else: |
| 390 |
print("Tests finished, exit manually") |
| 391 |
|
| 392 |
# ------------------------------------------------------------------------------ |
| 393 |
# MAIN |
| 394 |
# ------------------------------------------------------------------------------ |
| 395 |
func _init(): |
| 396 |
if(!_utils.is_version_ok()): |
| 397 |
print("\n\n", _utils.get_version_text()) |
| 398 |
push_error(_utils.get_bad_version_text()) |
| 399 |
OS.exit_code = 1 |
| 400 |
quit() |
| 401 |
else: |
| 402 |
_run_gut() |