julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include "remote_test_runner.h" |
| 8 | #include <protocols/service/test_runner/packed-c/status.h> |
| 9 | #include <vector> |
| 10 | #include <string> |
| 11 | #include <cstring> |
| 12 | #include <cstdio> |
| 13 | |
| 14 | remote_test_runner::remote_test_runner() : |
| 15 | m_client(NULL) |
| 16 | { |
| 17 | |
| 18 | } |
| 19 | |
| 20 | remote_test_runner::remote_test_runner(test_runner_client *client) : |
| 21 | m_client(client) |
| 22 | { |
| 23 | |
| 24 | } |
| 25 | |
| 26 | remote_test_runner::~remote_test_runner() |
| 27 | { |
| 28 | |
| 29 | } |
| 30 | |
| 31 | void remote_test_runner::set_client(test_runner_client *client) |
| 32 | { |
| 33 | m_client = client; |
| 34 | } |
| 35 | |
| 36 | int remote_test_runner::execute(int argc, char *argv[]) |
| 37 | { |
| 38 | int test_status = TS_TEST_RUNNER_STATUS_ERROR; |
| 39 | struct test_spec spec; |
| 40 | |
| 41 | /* Parse command line parameters */ |
| 42 | bool list_only = option_selected("-l", argc, argv); |
| 43 | parse_test_spec_params(argc, argv, spec); |
| 44 | |
| 45 | /* Run or list tests qualified bu spec */ |
| 46 | struct test_summary summary; |
| 47 | std::vector<struct test_result> results; |
| 48 | |
| 49 | if (list_only) { |
| 50 | |
| 51 | test_status = m_client->list_tests(spec, summary, results); |
| 52 | output_list(summary, results); |
| 53 | } |
| 54 | else { |
| 55 | |
| 56 | test_status = m_client->run_tests(spec, summary, results); |
| 57 | output_results(summary, results); |
| 58 | } |
| 59 | |
| 60 | if (test_status != TS_TEST_RUNNER_STATUS_SUCCESS) { |
| 61 | |
| 62 | printf("Tests failed to run with error: %d\n", test_status); |
| 63 | } |
| 64 | |
| 65 | return test_status; |
| 66 | } |
| 67 | |
| 68 | void remote_test_runner::parse_test_spec_params(int argc, char *argv[], struct test_spec &spec) const |
| 69 | { |
| 70 | std::string name = parse_option("-n", argc, argv); |
| 71 | std::string group = parse_option("-g", argc, argv); |
| 72 | |
| 73 | memset(spec.name, 0, TEST_NAME_MAX_LEN); |
| 74 | name.copy(spec.name, TEST_NAME_MAX_LEN - 1); |
| 75 | |
| 76 | memset(spec.group, 0, TEST_GROUP_MAX_LEN); |
| 77 | group.copy(spec.group, TEST_GROUP_MAX_LEN - 1); |
| 78 | } |
| 79 | |
| 80 | std::string remote_test_runner::parse_option(const char *option_switch, int argc, char *argv[]) const |
| 81 | { |
| 82 | std::string option; |
| 83 | |
| 84 | for (int i = 1; i + 1 < argc; ++i) { |
| 85 | |
| 86 | if (strcmp(argv[i], option_switch) == 0) { |
| 87 | |
| 88 | option = std::string(argv[i +1]); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return option; |
| 94 | } |
| 95 | |
| 96 | bool remote_test_runner::option_selected(const char *option_switch, int argc, char *argv[]) const |
| 97 | { |
| 98 | bool selected = false; |
| 99 | |
| 100 | for (int i = 1; (i < argc) && !selected; ++i) { |
| 101 | |
| 102 | selected = (strcmp(argv[i], option_switch) == 0); |
| 103 | } |
| 104 | |
| 105 | return selected; |
| 106 | } |
| 107 | |
| 108 | void remote_test_runner::output_summary(const struct test_summary &summary) |
| 109 | { |
| 110 | printf("\n"); |
| 111 | |
| 112 | if (summary.num_failed == 0) printf("OK ("); |
| 113 | else printf("Errors (%d failures, ", summary.num_failed); |
| 114 | |
| 115 | printf("%d tests, %d ran)\n", summary.num_tests, summary.num_failed + summary.num_passed); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void remote_test_runner::output_list(const struct test_summary &summary, |
| 120 | const std::vector<struct test_result> &results) |
| 121 | { |
| 122 | |
| 123 | } |
| 124 | |
| 125 | void remote_test_runner::output_results(const struct test_summary &summary, |
| 126 | const std::vector<struct test_result> &results) |
| 127 | { |
| 128 | for (int i = 0; i < results.size(); ++i) { |
| 129 | |
| 130 | printf("TEST(%s, %s) ", results[i].group, results[i].name); |
| 131 | |
| 132 | if (results[i].run_state == TEST_RUN_STATE_PASSED) { |
| 133 | |
| 134 | printf("OK\n"); |
| 135 | } |
| 136 | else if (results[i].run_state == TEST_RUN_STATE_FAILED) { |
| 137 | |
| 138 | printf("error\n"); |
| 139 | } |
| 140 | else { |
| 141 | |
| 142 | printf("did not run\n"); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | output_summary(summary); |
| 147 | } |