From 6c0ac04f912917f2c496f473c2695952fd655b42 Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Tue, 22 Feb 2022 14:59:55 -0600 Subject: [PATCH] Add common::testing module Signed-off-by: Michael Carroll --- CMakeLists.txt | 2 +- .../include/ignition/common/CMakeLists.txt | 0 .../ignition/common/testing/AutoLogFixture.hh | 109 ++++++++++++++++ .../ignition/common/testing/BazelTestPaths.hh | 37 ++++++ .../ignition/common/testing/CMakeLists.txt | 2 + .../ignition/common/testing/CMakeTestPaths.hh | 37 ++++++ .../ignition/common/testing/TestPaths.hh | 118 ++++++++++++++++++ .../include/ignition/common/testing/Utils.hh | 59 +++++++++ testing/src/BazelTestPaths.cc | 53 ++++++++ testing/src/BazelTestPaths_TEST.cc | 90 +++++++++++++ testing/src/CMakeLists.txt | 14 +++ testing/src/CMakeTestPaths.cc | 44 +++++++ testing/src/CMakeTestPaths_TEST.cc | 98 +++++++++++++++ testing/src/TestPaths.cc | 96 ++++++++++++++ testing/test_files/example.txt | 0 15 files changed, 758 insertions(+), 1 deletion(-) create mode 100644 testing/include/ignition/common/CMakeLists.txt create mode 100644 testing/include/ignition/common/testing/AutoLogFixture.hh create mode 100644 testing/include/ignition/common/testing/BazelTestPaths.hh create mode 100644 testing/include/ignition/common/testing/CMakeLists.txt create mode 100644 testing/include/ignition/common/testing/CMakeTestPaths.hh create mode 100644 testing/include/ignition/common/testing/TestPaths.hh create mode 100644 testing/include/ignition/common/testing/Utils.hh create mode 100644 testing/src/BazelTestPaths.cc create mode 100644 testing/src/BazelTestPaths_TEST.cc create mode 100644 testing/src/CMakeLists.txt create mode 100644 testing/src/CMakeTestPaths.cc create mode 100644 testing/src/CMakeTestPaths_TEST.cc create mode 100644 testing/src/TestPaths.cc create mode 100644 testing/test_files/example.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index da71892bb..1ac0fd7db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,7 +125,7 @@ configure_file("${PROJECT_SOURCE_DIR}/cppcheck.suppress.in" ${PROJECT_BINARY_DIR}/cppcheck.suppress) ign_configure_build(QUIT_IF_BUILD_ERRORS - COMPONENTS av events graphics profiler) + COMPONENTS av events graphics profiler testing) #============================================================================ # Create package information diff --git a/testing/include/ignition/common/CMakeLists.txt b/testing/include/ignition/common/CMakeLists.txt new file mode 100644 index 000000000..e69de29bb diff --git a/testing/include/ignition/common/testing/AutoLogFixture.hh b/testing/include/ignition/common/testing/AutoLogFixture.hh new file mode 100644 index 000000000..b69987544 --- /dev/null +++ b/testing/include/ignition/common/testing/AutoLogFixture.hh @@ -0,0 +1,109 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ +#ifndef IGNITION_COMMON_TESTING_AUTOLOGFIXTURE_HH_ +#define IGNITION_COMMON_TESTING_AUTOLOGFIXTURE_HH_ + +#include + +#include +#include + +#include "ignition/common/Console.hh" +#include "ignition/common/Filesystem.hh" +#include "ignition/common/TempDirectory.hh" +#include "ignition/common/Util.hh" + +namespace ignition::common::testing +{ +/// \brief A utility class that stores test logs in ~/.ignition/test_logs. +/// This functionality is needed to keep all the log information reported +/// by ignition during continuous integration. Without this, debugging +/// failing tests is significantly more difficult. +class AutoLogFixture : public ::testing::Test +{ + /// \brief Setup the test fixture. This gets called by gtest. + protected: virtual void SetUp() + { + const ::testing::TestInfo *const testInfo = + ::testing::UnitTest::GetInstance()->current_test_info(); + + std::string testName = testInfo->name(); + std::string testCaseName = testInfo->test_case_name(); + this->logFilename = testCaseName + "_" + testName + ".log"; + + this->temp = std::make_unique( + "test", "ign_common", true); + ASSERT_TRUE(this->temp->Valid()); + common::setenv(IGN_HOMEDIR, this->temp->Path()); + + // Initialize Console + ignLogInit(common::joinPaths(this->temp->Path(), "test_logs"), + this->logFilename); + + ignition::common::Console::SetVerbosity(4); + + // Read the full path to the log directory. + this->logDirectory = ignLogDirectory(); + } + + /// \brief Get a string with the full log file path. + /// \return The full log file path as a string. + protected: std::string FullLogPath() const + { + return ignition::common::joinPaths( + this->logDirectory, this->logFilename); + } + + /// \brief Get a string with all the log content loaded from the disk. + /// \return A string with all the log content. + protected: std::string LogContent() const + { + std::string loggedString; + // Open the log file, and read back the string + std::ifstream ifs(this->FullLogPath().c_str(), std::ios::in); + + while (!ifs.eof()) + { + std::string line; + std::getline(ifs, line); + loggedString += line; + } + return loggedString; + } + + /// \brief Default destructor. + public: virtual ~AutoLogFixture() + { + ignLogClose(); + EXPECT_TRUE(ignition::common::unsetenv(IGN_HOMEDIR)); + } + + /// \brief String with the full path of the logfile + private: std::string logFilename; + + /// \brief String with the full path to log directory + private: std::string logDirectory; + + /// \brief String with the base path to log directory + private: std::string logBasePath; + + /// \brief Temporary directory to run test in + private: std::unique_ptr temp; +}; +} // namespace ignition::common::testing + +#endif // IGNITION_COMMON_TESTING_AUTOLOGFIXTURE_HH_ diff --git a/testing/include/ignition/common/testing/BazelTestPaths.hh b/testing/include/ignition/common/testing/BazelTestPaths.hh new file mode 100644 index 000000000..74a1fa131 --- /dev/null +++ b/testing/include/ignition/common/testing/BazelTestPaths.hh @@ -0,0 +1,37 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ +#ifndef IGNITION_COMMON_TESTING_BAZELTESTPATHS_HH_ +#define IGNITION_COMMON_TESTING_BAZELTESTPATHS_HH_ + +#include + +#include "ignition/common/testing/TestPaths.hh" + +namespace ignition::common::testing +{ + +class BazelTestPaths: public TestPaths +{ + public: using TestPaths::TestPaths; + public: ~BazelTestPaths() override; + public: bool ProjectSourcePath(std::string &_sourceDir) override; + public: bool TestTmpPath(std::string &_tmpDir) override; +}; + +} // namespace ignition::common::testing + +#endif // IGNITION_COMMON_TESTING_AUTOLOGFIXTURE_HH_ diff --git a/testing/include/ignition/common/testing/CMakeLists.txt b/testing/include/ignition/common/testing/CMakeLists.txt new file mode 100644 index 000000000..06f5b9aa3 --- /dev/null +++ b/testing/include/ignition/common/testing/CMakeLists.txt @@ -0,0 +1,2 @@ + +ign_install_all_headers(COMPONENT testing) diff --git a/testing/include/ignition/common/testing/CMakeTestPaths.hh b/testing/include/ignition/common/testing/CMakeTestPaths.hh new file mode 100644 index 000000000..8adbad50b --- /dev/null +++ b/testing/include/ignition/common/testing/CMakeTestPaths.hh @@ -0,0 +1,37 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ +#ifndef IGNITION_COMMON_TESTING_CMAKETESTPATHS_HH_ +#define IGNITION_COMMON_TESTING_CMAKETESTPATHS_HH_ + +#include + +#include "ignition/common/testing/TestPaths.hh" + +namespace ignition::common::testing +{ + +class CMakeTestPaths: public TestPaths +{ + public: using TestPaths::TestPaths; + public: ~CMakeTestPaths() override; + public: bool ProjectSourcePath(std::string &_sourceDir) override; + public: bool TestTmpPath(std::string &_tmpDir) override; +}; + +} // namespace ignition::common::testing + +#endif // IGNITION_COMMON_TESTING_CMAKETESTPATHS_HH_ diff --git a/testing/include/ignition/common/testing/TestPaths.hh b/testing/include/ignition/common/testing/TestPaths.hh new file mode 100644 index 000000000..bec6968e4 --- /dev/null +++ b/testing/include/ignition/common/testing/TestPaths.hh @@ -0,0 +1,118 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ +#ifndef IGNITION_COMMON_TESTING_TESTPATHS_HH_ +#define IGNITION_COMMON_TESTING_TESTPATHS_HH_ + +#include +#include + +#include "ignition/common/Filesystem.hh" +#include "ignition/common/TempDirectory.hh" +#include "ignition/common/Util.hh" + +#ifndef TESTING_PROJECT_SOURCE_DIR +#define TESTING_PROJECT_SOURCE_DIR "" +#endif + +namespace ignition::common::testing +{ + +////////////////////////////////////////////////// +constexpr char kTestingProjectSourceDir[] = TESTING_PROJECT_SOURCE_DIR; + +////////////////////////////////////////////////// +enum class BuildType +{ + kUnknown, + kCMake, + kBazel +}; + +////////////////////////////////////////////////// +class TestPaths +{ + public: explicit TestPaths(const std::string &_projectSourcePath = + kTestingProjectSourceDir); + public: virtual ~TestPaths() = 0; + public: virtual bool ProjectSourcePath(std::string &_sourceDir) = 0; + public: virtual bool TestTmpPath(std::string &_tmpDir) = 0; + + protected: std::string projectSourcePath; +}; + +////////////////////////////////////////////////// +std::shared_ptr +MakeTestTempDirectoryImpl(const std::string &_projectSourcePath, + const std::string &_prefix = "test", + const std::string &_subDir = "ignition", + bool _cleanup = true); + + +////////////////////////////////////////////////// +inline std::shared_ptr +MakeTestTempDirectory(const std::string &_prefix = "test", + const std::string &_subDir = "ignition", + bool _cleanup = true) +{ + return MakeTestTempDirectoryImpl(kTestingProjectSourceDir, + _prefix, + _subDir, + _cleanup); +} + +////////////////////////////////////////////////// +BuildType +TestBuildType( + const std::string &_projectSourcePath = kTestingProjectSourceDir); + +////////////////////////////////////////////////// +std::unique_ptr +TestPathFactory( + const std::string &_projectSourcePath = kTestingProjectSourceDir); + +////////////////////////////////////////////////// +template +std::string SourceFile(Args const &... args) +{ + auto testPaths = TestPathFactory(kTestingProjectSourceDir); + assert(nullptr != testPaths); + + std::string dataDir; + testPaths->ProjectSourcePath(dataDir); + return common::joinPaths(dataDir, args...); +} + +////////////////////////////////////////////////// +template +std::string TestFile(Args const &... args) +{ + return SourceFile("test", args...); +} + +////////////////////////////////////////////////// +template +std::string TempPath(Args const &... args) +{ + auto testPaths = TestPathFactory(kTestingProjectSourceDir); + std::string dataDir; + testPaths->TestTmpPath(dataDir); + return common::joinPaths(dataDir, args...); +} + +} // namespace ignition::common::testing + +#endif // IGNITION_COMMON_TESTING_TESTPATHS_HH_ diff --git a/testing/include/ignition/common/testing/Utils.hh b/testing/include/ignition/common/testing/Utils.hh new file mode 100644 index 000000000..16c1e5f5d --- /dev/null +++ b/testing/include/ignition/common/testing/Utils.hh @@ -0,0 +1,59 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ +#ifndef IGNITION_COMMON_TESTING_UTILS_HH_ +#define IGNITION_COMMON_TESTING_UTILS_HH_ + +#include +#include +#include +#include +#include + +namespace ignition::common::testing +{ + +/// \brief Get a random number based on an integer converted to string. +/// \return A random integer converted to string. +std::string getRandomNumber(int32_t _min = 0, int32_t _max = INT_MAX) +{ + // Initialize random number generator. + uint32_t seed = std::random_device {}(); + std::mt19937 randGenerator(seed); + + // Create a random number based on an integer converted to string. + std::uniform_int_distribution d(_min, _max); + + return std::to_string(d(randGenerator)); +} + +///////////////////////////////////////////////// +bool create_new_empty_file(const std::string &_filename) +{ + try + { + std::fstream fs(_filename, std::ios::out); + } + catch(...) + { + return false; + } + return true; +} + +} // namespace ignition::common::testing + +#endif // IGNITION_COMMON_TESTING_TESTPATHS_HH_ diff --git a/testing/src/BazelTestPaths.cc b/testing/src/BazelTestPaths.cc new file mode 100644 index 000000000..f3cb43a39 --- /dev/null +++ b/testing/src/BazelTestPaths.cc @@ -0,0 +1,53 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ + +#include "ignition/common/testing/BazelTestPaths.hh" + +#include +#include + +namespace ignition::common::testing +{ + +////////////////////////////////////////////////// +BazelTestPaths::~BazelTestPaths() = default; + +////////////////////////////////////////////////// +bool BazelTestPaths::ProjectSourcePath(std::string &_sourceDir) { + std::string test_srcdir, bazel_path; + + if (common::env("TEST_SRCDIR", test_srcdir) && + common::env("IGN_BAZEL_PATH", bazel_path)) + { + _sourceDir = ignition::common::joinPaths(test_srcdir, + "ignition", + bazel_path); + return true; + } + else + { + return false; + } +} + +////////////////////////////////////////////////// +bool BazelTestPaths::TestTmpPath(std::string &_tmpDir) { + return common::env("TEST_UNDECLARED_OUTPUTS_DIR", _tmpDir); +} + +} // namespace ignition::common::testing + diff --git a/testing/src/BazelTestPaths_TEST.cc b/testing/src/BazelTestPaths_TEST.cc new file mode 100644 index 000000000..a88c7d7a9 --- /dev/null +++ b/testing/src/BazelTestPaths_TEST.cc @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2022 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ +#include + +#include "ignition/common/Filesystem.hh" +#include "ignition/common/testing/TestPaths.hh" +#include "ignition/common/testing/BazelTestPaths.hh" + +using namespace ignition::common; + +char **kEnv; + +///////////////////////////////////////////////// +TEST(TestPaths, DISABLED_DumpEnv) +{ + for (char **env = kEnv ; *env != nullptr; env++) + { + char *varName = *env; + printf("%s\n", varName); + } +} + +///////////////////////////////////////////////// +TEST(BazelTestPaths, ProjectSourcePath) +{ + ignition::common::testing::BazelTestPaths testPaths; + + std::string sourceDir; + ASSERT_TRUE(testPaths.ProjectSourcePath(sourceDir)); + ASSERT_FALSE(sourceDir.empty()); + ASSERT_TRUE(exists(sourceDir)) << sourceDir; + ASSERT_TRUE(isDirectory(sourceDir)); + + auto installedDir = joinPaths(sourceDir, "testing", "test_files"); + EXPECT_TRUE(exists(installedDir)) << installedDir; + EXPECT_TRUE(isDirectory(installedDir)); + + auto installedFile = joinPaths(installedDir, "example.txt"); + EXPECT_TRUE(exists(installedFile)); + EXPECT_TRUE(isFile(installedFile)); +} + +///////////////////////////////////////////////// +TEST(BazelTestPaths, TestTmpPath) +{ + ignition::common::testing::BazelTestPaths testPaths; + + std::string tmpDir; + ASSERT_TRUE(testPaths.TestTmpPath(tmpDir)); + ASSERT_FALSE(tmpDir.empty()); + ASSERT_TRUE(exists(tmpDir)) << tmpDir; + ASSERT_TRUE(isDirectory(tmpDir)); +} + +///////////////////////////////////////////////// +TEST(BazelTestPaths, TestBuildType) +{ + using BuildType = ignition::common::testing::BuildType; + ASSERT_EQ(BuildType::kBazel, ignition::common::testing::TestBuildType()); +} + +///////////////////////////////////////////////// +TEST(BazelTestPaths, TestPathFactory) +{ + auto testPaths = ignition::common::testing::TestPathFactory(); + ASSERT_NE(nullptr, testPaths); +} + +///////////////////////////////////////////////// +int main(int argc, char **argv, char **envp) +{ + kEnv = envp; + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + diff --git a/testing/src/CMakeLists.txt b/testing/src/CMakeLists.txt new file mode 100644 index 000000000..fcd00b327 --- /dev/null +++ b/testing/src/CMakeLists.txt @@ -0,0 +1,14 @@ +set(sources + TestPaths.cc + CMakeTestPaths.cc + BazelTestPaths.cc +) + +set(test_sources + CMakeTestPaths_TEST.cc +) + +ign_add_component(testing SOURCES ${sources} GET_TARGET_NAME testing_target) + +ign_build_tests(TYPE UNIT SOURCES ${test_sources} + LIB_DEPS ${testing_target}) diff --git a/testing/src/CMakeTestPaths.cc b/testing/src/CMakeTestPaths.cc new file mode 100644 index 000000000..816126394 --- /dev/null +++ b/testing/src/CMakeTestPaths.cc @@ -0,0 +1,44 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ + +#include "ignition/common/testing/CMakeTestPaths.hh" + +namespace ignition::common::testing +{ + +////////////////////////////////////////////////// +CMakeTestPaths::~CMakeTestPaths() = default; + +////////////////////////////////////////////////// +bool CMakeTestPaths::ProjectSourcePath(std::string &_sourceDir) { + + if (!projectSourcePath.empty()) + { + _sourceDir = projectSourcePath; + return true; + } + + return false; +} + +////////////////////////////////////////////////// +bool CMakeTestPaths::TestTmpPath(std::string &_tmpDir) { + _tmpDir = ignition::common::tempDirectoryPath(); + return true; +} + +} // namespace ignition::common::testing diff --git a/testing/src/CMakeTestPaths_TEST.cc b/testing/src/CMakeTestPaths_TEST.cc new file mode 100644 index 000000000..f18aa0a8e --- /dev/null +++ b/testing/src/CMakeTestPaths_TEST.cc @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2022 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ +#include + +#include "ignition/common/Filesystem.hh" +#include "ignition/common/testing/TestPaths.hh" +#include "ignition/common/testing/CMakeTestPaths.hh" + +using namespace ignition::common; +using namespace ignition::common::testing; + +constexpr char kFakeTestPaths[] = ""; + +///////////////////////////////////////////////// +TEST(TestPaths, ProjectSourcePathUnset) +{ + EXPECT_EQ(BuildType::kUnknown, TestBuildType(kFakeTestPaths)); + + auto testPaths = TestPathFactory(kFakeTestPaths); + EXPECT_EQ(nullptr, testPaths); + + auto tempDir = MakeTestTempDirectoryImpl(kFakeTestPaths); + EXPECT_EQ(nullptr, tempDir); +} + +///////////////////////////////////////////////// +TEST(CMakeTestPaths, TestingProjectSourceDir) +{ + ASSERT_NE(0u, strlen(ignition::common::testing::kTestingProjectSourceDir)); +} + +///////////////////////////////////////////////// +TEST(CMakeTestPaths, ProjectSourcePathUnset) +{ + ignition::common::testing::CMakeTestPaths testPaths(kFakeTestPaths); + std::string sourceDir; + EXPECT_FALSE(testPaths.ProjectSourcePath(sourceDir)); + EXPECT_TRUE(sourceDir.empty()); +} + +///////////////////////////////////////////////// +TEST(CMakeTestPaths, TestBuildType) +{ + ASSERT_EQ(BuildType::kCMake, ignition::common::testing::TestBuildType()); +} + +///////////////////////////////////////////////// +TEST(CMakeTestPaths, ProjectSourcePath) +{ + ignition::common::testing::CMakeTestPaths testPaths; + + std::string sourceDir; + ASSERT_TRUE(testPaths.ProjectSourcePath(sourceDir)); + ASSERT_FALSE(sourceDir.empty()); + ASSERT_TRUE(exists(sourceDir)) << sourceDir; + ASSERT_TRUE(isDirectory(sourceDir)); + + auto installedDir = joinPaths(sourceDir, "testing", "test_files"); + EXPECT_TRUE(exists(installedDir)) << installedDir; + EXPECT_TRUE(isDirectory(installedDir)); + + auto installedFile = joinPaths(installedDir, "example.txt"); + EXPECT_TRUE(exists(installedFile)); + EXPECT_TRUE(isFile(installedFile)); +} + +///////////////////////////////////////////////// +TEST(CMakeTestPaths, TestTmpPath) +{ + ignition::common::testing::CMakeTestPaths testPaths; + + std::string tmpDir; + ASSERT_TRUE(testPaths.TestTmpPath(tmpDir)); + ASSERT_FALSE(tmpDir.empty()); + ASSERT_TRUE(exists(tmpDir)) << tmpDir; + ASSERT_TRUE(isDirectory(tmpDir)); +} + +///////////////////////////////////////////////// +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/testing/src/TestPaths.cc b/testing/src/TestPaths.cc new file mode 100644 index 000000000..80c573754 --- /dev/null +++ b/testing/src/TestPaths.cc @@ -0,0 +1,96 @@ +/* +* Copyright (C) 2022 Open Source Robotics Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ +#include "ignition/common/testing/TestPaths.hh" +#include "ignition/common/testing/BazelTestPaths.hh" +#include "ignition/common/testing/CMakeTestPaths.hh" + +#include +#include + +namespace ignition::common::testing +{ +////////////////////////////////////////////////// +TestPaths::TestPaths(const std::string &_projectSourcePath) + : projectSourcePath(_projectSourcePath) +{ +} + +////////////////////////////////////////////////// +TestPaths::~TestPaths() = default; + +////////////////////////////////////////////////// +BuildType TestBuildType(const std::string &_projectSourcePath) +{ + std::string ign_bazel; + bool ign_bazel_set = common::env("IGN_BAZEL", ign_bazel); + bool ign_cmake_set = !_projectSourcePath.empty(); + + if (ign_bazel_set) + return BuildType::kBazel; + else if (ign_cmake_set) + return BuildType::kCMake; + else + return BuildType::kUnknown; +} + +////////////////////////////////////////////////// +std::unique_ptr +TestPathFactory(const std::string &_projectSourcePath) +{ + std::unique_ptr ret {nullptr}; + + switch(TestBuildType(_projectSourcePath)) + { + case BuildType::kBazel: + ret = std::make_unique(_projectSourcePath); + break; + case BuildType::kCMake: + ret = std::make_unique(_projectSourcePath); + break; + case BuildType::kUnknown: + ret = nullptr; + break; + default: + ret = nullptr; + break; + } + return ret; +} + +////////////////////////////////////////////////// +std::shared_ptr +MakeTestTempDirectoryImpl(const std::string &_projectSourcePath, + const std::string &_prefix, + const std::string &_subDir, + bool _cleanup) +{ + auto testPaths = TestPathFactory(_projectSourcePath); + + if (!testPaths) + return nullptr; + + std::string dataDir; + testPaths->TestTmpPath(dataDir); + + if (dataDir.empty()) + return nullptr; + + return std::make_shared( + dataDir, _prefix, _subDir, _cleanup); +} + +} // namespace ignition::common::testing diff --git a/testing/test_files/example.txt b/testing/test_files/example.txt new file mode 100644 index 000000000..e69de29bb