|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <filesystem> |
| 4 | +#include <fstream> |
| 5 | +#include <random> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "codspeed.h" |
| 9 | + |
| 10 | +namespace fs = std::filesystem; |
| 11 | + |
| 12 | +// Builds a temporary tree, since the function checks that the file exists: |
| 13 | +// <tmp>/src/bench.cpp the benchmark file |
| 14 | +// <tmp>/build/ the compile directory |
| 15 | +// <tmp>/other/deep/ a directory to run the benchmark from |
| 16 | +class WorkspaceTest : public ::testing::Test { |
| 17 | + protected: |
| 18 | + void SetUp() override { |
| 19 | + root = CODSPEED_TEST_ROOT_DIR; |
| 20 | + if (root.empty()) { |
| 21 | + GTEST_SKIP() << "CODSPEED_ROOT_DIR is not set (git detection failed)"; |
| 22 | + } |
| 23 | + previous_cwd = fs::current_path(); |
| 24 | + |
| 25 | + // Canonical, so symlinks like /tmp -> /private/tmp on macOS don't change |
| 26 | + // the expected paths |
| 27 | + tmp = fs::canonical(fs::temp_directory_path()) / |
| 28 | + ("codspeed-workspace-test-" + std::to_string(std::random_device{}())); |
| 29 | + fs::create_directories(tmp / "src"); |
| 30 | + fs::create_directories(tmp / "build"); |
| 31 | + fs::create_directories(tmp / "other" / "deep"); |
| 32 | + std::ofstream(tmp / "src" / "bench.cpp").close(); |
| 33 | + |
| 34 | + file = tmp / "src" / "bench.cpp"; |
| 35 | + expected = fs::relative(file, root); |
| 36 | + } |
| 37 | + |
| 38 | + void TearDown() override { |
| 39 | + if (!previous_cwd.empty()) { |
| 40 | + fs::current_path(previous_cwd); |
| 41 | + } |
| 42 | + if (!tmp.empty()) { |
| 43 | + std::error_code ec; |
| 44 | + fs::remove_all(tmp, ec); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + static fs::path relative(const std::string& path, |
| 49 | + const std::string& compile_dir = "") { |
| 50 | + return codspeed::get_path_relative_to_workspace(path, compile_dir); |
| 51 | + } |
| 52 | + |
| 53 | + fs::path root; |
| 54 | + fs::path previous_cwd; |
| 55 | + fs::path tmp; |
| 56 | + fs::path file; |
| 57 | + fs::path expected; |
| 58 | +}; |
| 59 | + |
| 60 | +TEST_F(WorkspaceTest, AbsolutePath) { |
| 61 | + EXPECT_EQ(relative(file.string()), expected); |
| 62 | + EXPECT_EQ(relative(file.string(), (tmp / "build").string()), expected); |
| 63 | +} |
| 64 | + |
| 65 | +// ccache with `base_dir`: __FILE__ is relative to the compile directory |
| 66 | +TEST_F(WorkspaceTest, RelativeToCompileDir) { |
| 67 | + fs::current_path(tmp); |
| 68 | + EXPECT_EQ(relative("../src/bench.cpp", (tmp / "build").string()), expected); |
| 69 | +} |
| 70 | + |
| 71 | +// The benchmark runs from a directory that is not the compile directory's |
| 72 | +// parent |
| 73 | +TEST_F(WorkspaceTest, RelativeToCompileDirFromAnotherCwd) { |
| 74 | + fs::current_path(tmp / "other" / "deep"); |
| 75 | + EXPECT_EQ(relative("../src/bench.cpp", (tmp / "build").string()), expected); |
| 76 | +} |
| 77 | + |
| 78 | +// The build directory can be deleted after the build |
| 79 | +TEST_F(WorkspaceTest, RelativeToMissingCompileDir) { |
| 80 | + fs::current_path(tmp); |
| 81 | + EXPECT_EQ(relative("../src/bench.cpp", (tmp / "missing-build").string()), |
| 82 | + expected); |
| 83 | +} |
| 84 | + |
| 85 | +// `-fmacro-prefix-map=<root>/=` makes __FILE__ relative to the root, so the |
| 86 | +// file does not exist in the compile directory |
| 87 | +TEST_F(WorkspaceTest, FallsBackToCwdWhenMissingInCompileDir) { |
| 88 | + fs::current_path(tmp); |
| 89 | + EXPECT_EQ(relative("src/bench.cpp", (tmp / "build").string()), expected); |
| 90 | +} |
| 91 | + |
| 92 | +TEST_F(WorkspaceTest, RelativeToCwdWithoutCompileDir) { |
| 93 | + fs::current_path(tmp / "src"); |
| 94 | + EXPECT_EQ(relative("bench.cpp"), expected); |
| 95 | +} |
0 commit comments