Commit | Line | Data |
---|---|---|
1a9956b9 SM |
1 | /* |
2 | * SPDX-License-Identifier: GPL-2.0-only | |
3 | * | |
4 | * Copyright (C) 2024 EfficiOS, Inc. | |
5 | */ | |
6 | ||
1a9956b9 SM |
7 | #include <babeltrace2/babeltrace.h> |
8 | ||
4993dc41 SM |
9 | #include "common/common.h" |
10 | #include "cpp-common/bt2/exc.hpp" | |
c40b7b8a | 11 | #include "cpp-common/bt2/plugin-load.hpp" |
1a9956b9 | 12 | #include "cpp-common/bt2c/c-string-view.hpp" |
4993dc41 | 13 | #include "cpp-common/vendor/fmt/core.h" |
1a9956b9 SM |
14 | |
15 | #include "tap/tap.h" | |
16 | ||
17 | namespace { | |
18 | ||
19 | void testFailOnLoadErrorTrue(const char * const pluginDir) | |
20 | { | |
21 | plan_tests(1); | |
22 | ||
23 | try { | |
24 | bt2::findAllPluginsFromDir(pluginDir, false, true); | |
25 | bt_common_abort(); | |
26 | } catch (const bt2::Error& exc) { | |
27 | fmt::print("{}\n", exc.what()); | |
28 | ||
29 | const auto error = bt_current_thread_take_error(); | |
30 | ||
31 | /* | |
32 | * The last error cause must be the one which the initialization | |
33 | * function of our plugin appended. | |
34 | */ | |
35 | const auto cause = bt_error_borrow_cause_by_index(error, 0); | |
36 | const bt2c::CStringView msg {bt_error_cause_get_message(cause)}; | |
37 | ||
38 | ok(msg == "This is the error message", "Message of error cause 0 is expected"); | |
39 | bt_error_release(error); | |
40 | } | |
41 | } | |
42 | ||
43 | void testFailOnLoadErrorFalse(const char * const pluginDir) | |
44 | { | |
45 | plan_tests(1); | |
46 | ||
47 | const auto plugins = bt2::findAllPluginsFromDir(pluginDir, false, false); | |
48 | ||
49 | ok(!plugins, "No plugin set returned"); | |
50 | } | |
51 | ||
52 | } /* namespace */ | |
53 | ||
54 | int main(const int argc, const char ** const argv) | |
55 | { | |
56 | if (argc != 3) { | |
57 | fmt::print(stderr, | |
58 | "Usage: {} INIT-FAIL-PLUGIN-DIR FAIL-ON-LOAD-ERROR\n" | |
59 | "\n" | |
60 | "FAIL-ON-LOAD-ERROR must be `yes` or `no`\n", | |
61 | argv[0]); | |
62 | return 1; | |
63 | } | |
64 | ||
65 | const auto pluginDir = argv[1]; | |
66 | const bt2c::CStringView failOnLoadErrorStr {argv[2]}; | |
67 | ||
68 | if (failOnLoadErrorStr == "yes") { | |
69 | testFailOnLoadErrorTrue(pluginDir); | |
70 | } else if (failOnLoadErrorStr == "no") { | |
71 | testFailOnLoadErrorFalse(pluginDir); | |
72 | } else { | |
73 | fmt::print(stderr, | |
74 | "ERROR: Invalid value `{}` for FAIL-ON-LOAD-ERROR (expecting `yes` or `no`).\n", | |
75 | failOnLoadErrorStr); | |
76 | return 1; | |
77 | } | |
78 | ||
79 | return exit_status(); | |
80 | } |