Skip to content

Fixed bug where including relative paths would fail to find the correct file #358

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

Merged
merged 5 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,16 @@ void XMLParser::Pimpl::loadDocImpl(BT_TinyXML2::XMLDocument* doc, bool add_inclu

opened_documents.emplace_back(new BT_TinyXML2::XMLDocument());
BT_TinyXML2::XMLDocument* next_doc = opened_documents.back().get();

// change current path to the included file for handling additional relative paths
const filesystem::path previous_path = current_path;
current_path = file_path.parent_path().make_absolute();

next_doc->LoadFile(file_path.str().c_str());
loadDocImpl(next_doc, add_includes);

// reset current path to the previous value
current_path = previous_path;
}

for (auto bt_node = xml_root->FirstChildElement("BehaviorTree");
Expand Down
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ elseif(BUILD_UNIT_TESTS)
bt_sample_nodes gtest gtest_main)
target_include_directories(${BEHAVIOR_TREE_LIBRARY}_test PRIVATE gtest/include ${GTEST_INCLUDE_DIRS})

add_custom_command(TARGET ${BEHAVIOR_TREE_LIBRARY}_test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/tests/trees
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/trees)

add_test(BehaviorTreeCoreTest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${BEHAVIOR_TREE_LIBRARY}_test)

endif()
55 changes: 55 additions & 0 deletions tests/gtest_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "action_test_node.h"
#include "condition_test_node.h"
#include "behaviortree_cpp_v3/xml_parsing.h"
#include "environment.h"
#include "../sample_nodes/crossdoor_nodes.h"
#include "../sample_nodes/dummy_nodes.h"

Expand Down Expand Up @@ -303,4 +304,58 @@ TEST(BehaviorTreeFactory, SubTreeWithRemapping)
ASSERT_FALSE( talk_bb->getAny("talk_out") );
}

#if !defined(USING_ROS) && !defined(USING_ROS2)
TEST(BehaviorTreeFactory, CreateTreeFromFile)
{
BehaviorTreeFactory factory;

// should not throw
Tree tree = factory.createTreeFromFile((environment->executable_path.parent_path() / "trees/parent_no_include.xml").str());
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickRoot());
}

TEST(BehaviorTreeFactory, CreateTreeFromFileWhichIncludesFileFromSameDirectory)
{
BehaviorTreeFactory factory;

// should not throw
Tree tree = factory.createTreeFromFile((environment->executable_path.parent_path() / "trees/child/child_include_sibling.xml").str());
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickRoot());
}

TEST(BehaviorTreeFactory, CreateTreeFromFileWhichIncludesFileFromChildDirectory)
{
BehaviorTreeFactory factory;

// should not throw
Tree tree = factory.createTreeFromFile((environment->executable_path.parent_path() / "trees/parent_include_child.xml").str());
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickRoot());
}

TEST(BehaviorTreeFactory, CreateTreeFromFileWhichIncludesFileFromChildDirectoryWhichIncludesFileFromSameDirectory)
{
BehaviorTreeFactory factory;

// should not throw
Tree tree = factory.createTreeFromFile((environment->executable_path.parent_path() / "trees/parent_include_child_include_sibling.xml").str());
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickRoot());
}

TEST(BehaviorTreeFactory, CreateTreeFromFileWhichIncludesFileFromChildDirectoryWhichIncludesFileFromChildDirectory)
{
BehaviorTreeFactory factory;

// should not throw
Tree tree = factory.createTreeFromFile((environment->executable_path.parent_path() / "trees/parent_include_child_include_child.xml").str());
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickRoot());
}

TEST(BehaviorTreeFactory, CreateTreeFromFileWhichIncludesFileFromChildDirectoryWhichIncludesFileFromParentDirectory)
{
BehaviorTreeFactory factory;

// should not throw
Tree tree = factory.createTreeFromFile((environment->executable_path.parent_path() / "trees/parent_include_child_include_parent.xml").str());
ASSERT_EQ(NodeStatus::SUCCESS, tree.tickRoot());
}
#endif
9 changes: 9 additions & 0 deletions tests/gtest_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "action_test_node.h"
#include "condition_test_node.h"
#include "behaviortree_cpp_v3/behavior_tree.h"
#include "environment.h"

#include <sstream>
#include <string>
Expand Down Expand Up @@ -118,8 +119,16 @@ TEST_F(BehaviorTreeTest, PrintWithStream)
ASSERT_TRUE(std::getline(stream, line, '\n').fail());
}

// define extern variable from environment.h
Environment* environment;

int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);

// gtest will take ownership of this pointer and free it for us
environment = new Environment(argc, argv);
testing::AddGlobalTestEnvironment(environment);

return RUN_ALL_TESTS();
}
26 changes: 26 additions & 0 deletions tests/include/environment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H

#include <gtest/gtest.h>

#include "filesystem/path.h"

class Environment : public testing::Environment
{
public:
Environment(int argc, char** argv)
{
if (argc >= 1)
{
executable_path = filesystem::path(argv[0]).make_absolute();
}
}

// the absolute path to the test executable
filesystem::path executable_path;
};

// for accessing the environment within a test
extern Environment* environment;

#endif
5 changes: 5 additions & 0 deletions tests/trees/child/child/child_child_no_include.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<root main_tree_to_execute = "ChildChildNoInclude">
<BehaviorTree ID="ChildChildNoInclude">
<AlwaysSuccess />
</BehaviorTree>
</root>
10 changes: 10 additions & 0 deletions tests/trees/child/child_include_child.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute = "ChildIncludeChild">
<include path="child/child_child_no_include.xml" />

<BehaviorTree ID="ChildIncludeChild">
<Sequence>
<SubTree ID="ChildChildNoInclude" />
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
10 changes: 10 additions & 0 deletions tests/trees/child/child_include_parent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute = "ChildIncludeParent">
<include path="../parent_no_include.xml" />

<BehaviorTree ID="ChildIncludeParent">
<Sequence>
<SubTree ID="ParentNoInclude" />
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
10 changes: 10 additions & 0 deletions tests/trees/child/child_include_sibling.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute = "ChildIncludeSibling">
<include path="child_no_include.xml" />

<BehaviorTree ID="ChildIncludeSibling">
<Sequence>
<SubTree ID="ChildNoInclude" />
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
5 changes: 5 additions & 0 deletions tests/trees/child/child_no_include.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<root main_tree_to_execute = "ChildNoInclude">
<BehaviorTree ID="ChildNoInclude">
<AlwaysSuccess />
</BehaviorTree>
</root>
10 changes: 10 additions & 0 deletions tests/trees/parent_include_child.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute = "ParentIncludeChild">
<include path="child/child_no_include.xml" />

<BehaviorTree ID="ParentIncludeChild">
<Sequence>
<SubTree ID="ChildNoInclude" />
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
10 changes: 10 additions & 0 deletions tests/trees/parent_include_child_include_child.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute = "ParentIncludeChildIncludeChild">
<include path="child/child_include_child.xml" />

<BehaviorTree ID="ParentIncludeChildIncludeChild">
<Sequence>
<SubTree ID="ChildIncludeChild" />
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
10 changes: 10 additions & 0 deletions tests/trees/parent_include_child_include_parent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute = "ParentIncludeChildIncludeParent">
<include path="child/child_include_parent.xml" />

<BehaviorTree ID="ParentIncludeChildIncludeParent">
<Sequence>
<SubTree ID="ChildIncludeParent" />
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
10 changes: 10 additions & 0 deletions tests/trees/parent_include_child_include_sibling.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute = "ParentIncludeChildIncludeSibling">
<include path="child/child_include_sibling.xml" />

<BehaviorTree ID="ParentIncludeChildIncludeSibling">
<Sequence>
<SubTree ID="ChildIncludeSibling" />
<AlwaysSuccess />
</Sequence>
</BehaviorTree>
</root>
5 changes: 5 additions & 0 deletions tests/trees/parent_no_include.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<root main_tree_to_execute = "ParentNoInclude">
<BehaviorTree ID="ParentNoInclude">
<AlwaysSuccess />
</BehaviorTree>
</root>