# a condition trigger descriptor (base)
class _CondTriggerDescriptor:
- def __init__(self, index: int, trigger_name: str, cond_id: str):
- self._index = index
+ def __init__(self, trigger_name: str, cond_id: str):
self._trigger_name = trigger_name
self._cond_id = cond_id
- @property
- def index(self):
- return self._index
-
@property
def trigger_name(self):
return self._trigger_name
def _test(self: unittest.TestCase, descriptor: _CondTriggerDescriptor):
# Execute:
#
- # $ conds-triggers run <index>
+ # $ conds-triggers run <trigger-name>
#
- # where `<index>` is the descriptor's index.
+ # where `<trigger-name>` is the descriptor trigger name.
+ print("# Running trigger `{}`".format(descriptor.trigger_name))
with subprocess.Popen(
- [_CONDS_TRIGGERS_PATH, "run", str(descriptor.index)],
+ [_CONDS_TRIGGERS_PATH, "run", descriptor.trigger_name],
stderr=subprocess.PIPE,
universal_newlines=True,
) as proc:
descriptors = [] # type: list[_CondTriggerDescriptor]
descriptor_names = set() # type: set[str]
- for index, json_descr in enumerate(json_descr_array.iter(tjson.ObjVal)):
+ for json_descr in json_descr_array.iter(tjson.ObjVal):
# sanity check: check for duplicate
trigger_name = json_descr.at("name", tjson.StrVal).val
else:
raise ValueError("Invalid condition ID `{}`".format(cond_id))
- descriptors.append(cond_type(index, trigger_name, cond_id))
+ descriptors.append(cond_type(trigger_name, cond_id))
descriptor_names.add(trigger_name)
return descriptors
* Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
*/
+#include <unordered_set>
+
#include <glib.h>
-#include <stdlib.h>
#include <string.h>
#include <babeltrace2/babeltrace.h>
fmt::println("{}", condTriggerArray.dump());
}
+void checkNamesUnique(const CondTriggers& condTriggers)
+{
+ std::unordered_set<std::string> names;
+
+ for (const auto& trigger : condTriggers) {
+ const auto res = names.insert(trigger->name());
+
+ if (!res.second) {
+ fmt::println(stderr, "Duplicate test name `{}`", trigger->name());
+ std::exit(1);
+ }
+ }
+}
+
} /* namespace */
void condMain(const bt2s::span<const char * const> argv, const CondTriggers& condTriggers) noexcept
{
BT_ASSERT(argv.size() >= 2);
+ checkNamesUnique(condTriggers);
if (strcmp(argv[1], "list") == 0) {
listCondTriggers(condTriggers);
} else if (strcmp(argv[1], "run") == 0) {
/*
- * It's expected that calling `*condTriggers[index]` below
- * aborts (calls bt_common_abort()). In this testing context, we
- * don't want any custom abortion command to run.
+ * It's expected that calling the trigger below aborts (calls
+ * bt_common_abort()). In this testing context, we don't want
+ * any custom abortion command to run.
*/
g_unsetenv("BABELTRACE_EXEC_ON_ABORT");
- /* Call the trigger */
- BT_ASSERT(argv.size() >= 3);
+ /* Find the trigger */
+ BT_ASSERT(argv.size() == 3);
- const auto index = atoi(argv[2]);
+ const auto name = argv[2];
+ const auto it = std::find_if(condTriggers.begin(), condTriggers.end(),
+ [&](const CondTrigger::UP& trigger) {
+ return trigger->name() == name;
+ });
- BT_ASSERT(index >= 0 && index < condTriggers.size());
- (*condTriggers[index])();
+ if (it == condTriggers.end()) {
+ fmt::println(stderr, "No trigger named `{}` found.", name);
+ std::exit(1);
+ }
+
+ /* Call the trigger */
+ (**it)();
}
}