-
Notifications
You must be signed in to change notification settings - Fork 834
Fix/duplicate behavior tree id registration #1133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KImanalieva
wants to merge
2
commits into
BehaviorTree:master
Choose a base branch
from
KImanalieva:fix/duplicate-behavior-tree-id-registration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| /* Copyright (C) 2026 - duplicate BehaviorTree ID registration tests */ | ||
|
|
||
| #include "behaviortree_cpp/bt_factory.h" | ||
|
|
||
| #include <chrono> | ||
| #include <filesystem> | ||
| #include <fstream> | ||
| #include <string> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| using namespace BT; | ||
|
|
||
| namespace | ||
| { | ||
| class TempSubdir | ||
| { | ||
| public: | ||
| TempSubdir() | ||
| { | ||
| const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count(); | ||
| path = std::filesystem::temp_directory_path() / | ||
| ("bt_duplicate_tree_" + std::to_string(stamp)); | ||
| std::filesystem::create_directories(path); | ||
| } | ||
|
|
||
| ~TempSubdir() | ||
| { | ||
| std::error_code ec; | ||
| std::filesystem::remove_all(path, ec); | ||
| } | ||
|
|
||
| std::filesystem::path path; | ||
|
|
||
| TempSubdir(const TempSubdir&) = delete; | ||
| TempSubdir& operator=(const TempSubdir&) = delete; | ||
| }; | ||
|
|
||
| void writeFile(const std::filesystem::path& p, const std::string& content) | ||
| { | ||
| std::ofstream out(p.string()); | ||
| out << content; | ||
| } | ||
|
|
||
| std::string minimalTreeXml(const std::string& tree_id) | ||
| { | ||
| return std::string(R"( | ||
| <root BTCPP_format="4"> | ||
| <BehaviorTree ID=")" + | ||
| tree_id + R"("> | ||
| <AlwaysSuccess/> | ||
| </BehaviorTree> | ||
| </root> | ||
| )"); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(DuplicateBehaviorTreeId, DuplicateIdAcrossFiles) | ||
| { | ||
| TempSubdir dir; | ||
| const auto path_a = dir.path / "a.xml"; | ||
| const auto path_b = dir.path / "b.xml"; | ||
| writeFile(path_a, minimalTreeXml("DupTree")); | ||
| writeFile(path_b, minimalTreeXml("DupTree")); | ||
|
|
||
| const std::string abs_a = std::filesystem::absolute(path_a).string(); | ||
| const std::string abs_b = std::filesystem::absolute(path_b).string(); | ||
|
|
||
| BehaviorTreeFactory factory; | ||
| ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path_a)); | ||
|
|
||
| try | ||
| { | ||
| factory.registerBehaviorTreeFromFile(path_b); | ||
| FAIL() << "expected RuntimeError"; | ||
| } | ||
| catch(const RuntimeError& e) | ||
| { | ||
| const std::string msg = e.what(); | ||
| EXPECT_NE(msg.find(abs_a), std::string::npos) << msg; | ||
| EXPECT_NE(msg.find(abs_b), std::string::npos) << msg; | ||
| } | ||
| } | ||
|
|
||
| TEST(DuplicateBehaviorTreeId, DuplicateIdWithinSameText) | ||
| { | ||
| const std::string xml = R"( | ||
| <root BTCPP_format="4"> | ||
| <BehaviorTree ID="foo"><AlwaysSuccess/></BehaviorTree> | ||
| <BehaviorTree ID="foo"><AlwaysFailure/></BehaviorTree> | ||
| </root> | ||
| )"; | ||
|
|
||
| BehaviorTreeFactory factory; | ||
| try | ||
| { | ||
| factory.registerBehaviorTreeFromText(xml); | ||
| FAIL() << "expected RuntimeError"; | ||
| } | ||
| catch(const RuntimeError& e) | ||
| { | ||
| const std::string msg = e.what(); | ||
| EXPECT_NE(msg.find("<inline XML>"), std::string::npos) << msg; | ||
| } | ||
| } | ||
|
|
||
| TEST(DuplicateBehaviorTreeId, DuplicateIdAcrossFileAndText) | ||
| { | ||
| TempSubdir dir; | ||
| const auto path = dir.path / "one.xml"; | ||
| writeFile(path, minimalTreeXml("shared_id")); | ||
|
|
||
| const std::string abs_path = std::filesystem::absolute(path).string(); | ||
|
|
||
| BehaviorTreeFactory factory; | ||
| ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path)); | ||
|
|
||
| const std::string xml = minimalTreeXml("shared_id"); | ||
| try | ||
| { | ||
| factory.registerBehaviorTreeFromText(xml); | ||
| FAIL() << "expected RuntimeError"; | ||
| } | ||
| catch(const RuntimeError& e) | ||
| { | ||
| const std::string msg = e.what(); | ||
| EXPECT_NE(msg.find(abs_path), std::string::npos) << msg; | ||
| EXPECT_NE(msg.find("<inline XML>"), std::string::npos) << msg; | ||
| } | ||
| } | ||
|
|
||
| TEST(DuplicateBehaviorTreeId, DuplicateIdSamePathTwice) | ||
| { | ||
| TempSubdir dir; | ||
| const auto path = dir.path / "reload.xml"; | ||
| writeFile(path, minimalTreeXml("same")); | ||
|
|
||
| const std::string abs_path = std::filesystem::absolute(path).string(); | ||
|
|
||
| BehaviorTreeFactory factory; | ||
| ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path)); | ||
|
|
||
| try | ||
| { | ||
| factory.registerBehaviorTreeFromFile(path); | ||
| FAIL() << "expected RuntimeError"; | ||
| } | ||
| catch(const RuntimeError& e) | ||
| { | ||
| const std::string msg = e.what(); | ||
| EXPECT_NE(msg.find(abs_path), std::string::npos) << msg; | ||
| } | ||
| } | ||
|
|
||
| TEST(DuplicateBehaviorTreeId, ClearAllowsReload) | ||
| { | ||
| TempSubdir dir; | ||
| const auto path = dir.path / "clear.xml"; | ||
| writeFile(path, minimalTreeXml("cleared")); | ||
|
|
||
| BehaviorTreeFactory factory; | ||
| ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path)); | ||
| factory.clearRegisteredBehaviorTrees(); | ||
| ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path)); | ||
| ASSERT_NO_THROW(static_cast<void>(factory.createTree("cleared"))); | ||
| } | ||
|
|
||
| TEST(DuplicateBehaviorTreeId, DifferentIdsCoexist) | ||
| { | ||
| TempSubdir dir; | ||
| const auto path_a = dir.path / "tree_a.xml"; | ||
| const auto path_b = dir.path / "tree_b.xml"; | ||
| writeFile(path_a, minimalTreeXml("TreeA")); | ||
| writeFile(path_b, minimalTreeXml("TreeB")); | ||
|
|
||
| BehaviorTreeFactory factory; | ||
| ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path_a)); | ||
| ASSERT_NO_THROW(factory.registerBehaviorTreeFromFile(path_b)); | ||
|
|
||
| const auto ids = factory.registeredBehaviorTrees(); | ||
| ASSERT_EQ(ids.size(), 2); | ||
| ASSERT_NO_THROW(static_cast<void>(factory.createTree("TreeA"))); | ||
| ASSERT_NO_THROW(static_cast<void>(factory.createTree("TreeB"))); | ||
| } | ||
|
|
||
| TEST(DuplicateBehaviorTreeId, IncludeDuplicateThrows) | ||
| { | ||
| TempSubdir dir; | ||
| const auto inner_path = dir.path / "inner.xml"; | ||
| const auto outer_path = dir.path / "outer.xml"; | ||
|
|
||
| writeFile(inner_path, minimalTreeXml("foo")); | ||
|
|
||
| const std::string outer_xml = R"( | ||
| <root BTCPP_format="4"> | ||
| <include path="inner.xml"/> | ||
| <BehaviorTree ID="foo"> | ||
| <AlwaysSuccess/> | ||
| </BehaviorTree> | ||
| </root> | ||
| )"; | ||
| writeFile(outer_path, outer_xml); | ||
|
|
||
| const std::string inner_abs = std::filesystem::absolute(inner_path).string(); | ||
|
|
||
| BehaviorTreeFactory factory; | ||
| try | ||
| { | ||
| factory.registerBehaviorTreeFromFile(std::filesystem::absolute(outer_path)); | ||
| FAIL() << "expected RuntimeError"; | ||
| } | ||
| catch(const RuntimeError& e) | ||
| { | ||
| const std::string msg = e.what(); | ||
| EXPECT_NE(msg.find(inner_abs), std::string::npos) << msg; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment struck me as odd. I would perhaps not add it. I don't think we should explain errors that can come from sub-function calls, that could get out of hand quickly.