|
| 1 | +extends RefCounted |
| 2 | +class_name VestCLIRunner |
| 3 | + |
| 4 | +## Implements functionality to run tests |
| 5 | + |
| 6 | +var _peer: StreamPeerTCP = null |
| 7 | + |
| 8 | +## Run tests with [Params] |
| 9 | +func run(params: VestCLI.Params) -> int: |
| 10 | + var validation_errors := params.validate() |
| 11 | + if not validation_errors.is_empty(): |
| 12 | + for error in validation_errors: |
| 13 | + OS.alert(error) |
| 14 | + push_error(error) |
| 15 | + return 1 |
| 16 | + |
| 17 | + await _connect(params) |
| 18 | + |
| 19 | + var results := await _run_tests(params) |
| 20 | + _report(params, results) |
| 21 | + _send_results_over_network(params, results) |
| 22 | + |
| 23 | + _disconnect() |
| 24 | + |
| 25 | + if results.get_aggregate_status() == VestResult.TEST_PASS: |
| 26 | + print_rich("All tests [color=green]passed[/color]!") |
| 27 | + return 0 |
| 28 | + else: |
| 29 | + print_rich("There are test [color=red]failures[/color]!") |
| 30 | + return 1 |
| 31 | + |
| 32 | +func _run_tests(params: VestCLI.Params) -> VestResult.Suite: |
| 33 | + var runner := VestLocalRunner.new() |
| 34 | + runner.on_partial_result.connect(func(result: VestResult.Suite): |
| 35 | + if _peer != null: |
| 36 | + if result != null: |
| 37 | + _peer.put_var(result._to_wire(), true) |
| 38 | + else: |
| 39 | + _peer.put_var(result, true) |
| 40 | + ) |
| 41 | + |
| 42 | + var results: VestResult.Suite |
| 43 | + if params.run_file: |
| 44 | + results = await runner.run_script_at(params.run_file, params.only_mode) |
| 45 | + elif params.run_glob: |
| 46 | + results = await runner.run_glob(params.run_glob, params.only_mode) |
| 47 | + |
| 48 | + return results |
| 49 | + |
| 50 | +func _report(params: VestCLI.Params, results: VestResult.Suite): |
| 51 | + var report := TAPReporter.report(results) |
| 52 | + |
| 53 | + if params.report_format: |
| 54 | + if params.report_file in ["", "-"]: |
| 55 | + print(report) |
| 56 | + else: |
| 57 | + var fa := FileAccess.open(params.report_file, FileAccess.WRITE) |
| 58 | + fa.store_string(report) |
| 59 | + fa.close() |
| 60 | + |
| 61 | +func _connect(params: VestCLI.Params): |
| 62 | + if not params.host and params.port == -1: |
| 63 | + return |
| 64 | + |
| 65 | + var host := params.host |
| 66 | + var port := params.port |
| 67 | + |
| 68 | + if not host: host = "127.0.0.1" |
| 69 | + if port == -1: port = 54932 |
| 70 | + |
| 71 | + var peer := StreamPeerTCP.new() |
| 72 | + var err := peer.connect_to_host(host, port) |
| 73 | + if err != OK: |
| 74 | + push_warning("Couldn't connect on port %d! %s" % [port, error_string(err)]) |
| 75 | + return |
| 76 | + |
| 77 | + await Vest.until(func(): peer.poll(); return peer.get_status() != StreamPeerTCP.STATUS_CONNECTING) |
| 78 | + if peer.get_status() != StreamPeerTCP.STATUS_CONNECTED: |
| 79 | + push_warning("Connection failed! Socket status: %d" % [peer.get_status()]) |
| 80 | + return |
| 81 | + |
| 82 | + peer.set_no_delay(true) |
| 83 | + _peer = peer |
| 84 | + |
| 85 | +func _disconnect(): |
| 86 | + if _peer != null: |
| 87 | + _peer.disconnect_from_host() |
| 88 | + |
| 89 | +func _send_results_over_network(params: VestCLI.Params, results: VestResult.Suite): |
| 90 | + if not _peer: |
| 91 | + return |
| 92 | + |
| 93 | + _peer.put_var(results._to_wire(), true) |
0 commit comments