|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +import std; |
| 4 | +import mcpp.build.directives; |
| 5 | +import mcpp.toolchain.fingerprint; |
| 6 | + |
| 7 | +namespace dirs = mcpp::build::directives; |
| 8 | + |
| 9 | +namespace { |
| 10 | + |
| 11 | +struct GlobInputs : testing::Test { |
| 12 | + std::filesystem::path tree; |
| 13 | + std::filesystem::path root; |
| 14 | + |
| 15 | + void SetUp() override { |
| 16 | + tree = std::filesystem::temp_directory_path() / "mcpp_parent_glob_inputs"; |
| 17 | + std::filesystem::remove_all(tree); |
| 18 | + root = tree / "workspace" / "app"; |
| 19 | + std::filesystem::create_directories(root); |
| 20 | + } |
| 21 | + |
| 22 | + void TearDown() override { |
| 23 | + std::error_code ec; |
| 24 | + std::filesystem::remove_all(tree, ec); |
| 25 | + } |
| 26 | + |
| 27 | + void write(const std::filesystem::path& relative, std::string_view content = "x") { |
| 28 | + const auto path = root / relative; |
| 29 | + std::filesystem::create_directories(path.parent_path()); |
| 30 | + std::ofstream out(path); |
| 31 | + out << content; |
| 32 | + } |
| 33 | + |
| 34 | + std::string fingerprint(std::string_view pattern) { |
| 35 | + return dirs::glob_fingerprint(root, pattern, "target"); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +} // namespace |
| 40 | + |
| 41 | +TEST_F(GlobInputs, ParentDirectoryMembershipInvalidatesTheFingerprint) { |
| 42 | + write("../inputs/a.in"); |
| 43 | + const auto one = fingerprint("../inputs/**/*.in"); |
| 44 | + EXPECT_NE(one, mcpp::toolchain::hash_string("")); |
| 45 | + |
| 46 | + write("../inputs/nested/b.in"); |
| 47 | + EXPECT_NE(fingerprint("../inputs/**/*.in"), one); |
| 48 | + std::filesystem::remove(root / "../inputs/nested/b.in"); |
| 49 | + EXPECT_EQ(fingerprint("../inputs/**/*.in"), one); |
| 50 | + |
| 51 | + write("../inputs/a.in", "different contents"); |
| 52 | + write("../inputs/ignored.txt"); |
| 53 | + write("../unrelated/c.in"); |
| 54 | + EXPECT_EQ(fingerprint("../inputs/**/*.in"), one); |
| 55 | +} |
| 56 | + |
| 57 | +TEST_F(GlobInputs, MultipleParentLevelsAndExactPathsAreWatched) { |
| 58 | + const auto before = fingerprint("../../inputs/*.in"); |
| 59 | + write("../../inputs/a.in"); |
| 60 | + EXPECT_NE(fingerprint("../../inputs/*.in"), before); |
| 61 | + EXPECT_NE(fingerprint("../../inputs/a.in"), before); |
| 62 | + std::filesystem::remove(root / "../../inputs/a.in"); |
| 63 | + EXPECT_EQ(fingerprint("../../inputs/*.in"), before); |
| 64 | +} |
| 65 | + |
| 66 | +TEST_F(GlobInputs, MissingLiteralDirectoryDoesNotHideLaterFiles) { |
| 67 | + const auto before = fingerprint("../missing/**/*.in"); |
| 68 | + write("../missing/ignored.txt"); |
| 69 | + EXPECT_EQ(fingerprint("../missing/**/*.in"), before); |
| 70 | + write("../missing/nested/a.in"); |
| 71 | + EXPECT_NE(fingerprint("../missing/**/*.in"), before); |
| 72 | +} |
| 73 | + |
| 74 | +TEST_F(GlobInputs, LiteralPrefixDoesNotBypassExcludedDirectories) { |
| 75 | + const auto before = fingerprint("**"); |
| 76 | + write("target/generated.in"); |
| 77 | + write(".git/index.in"); |
| 78 | + write("../inputs/target/generated.in"); |
| 79 | + write("../inputs/.git/index.in"); |
| 80 | + EXPECT_EQ(fingerprint("**"), before); |
| 81 | + for (const auto pattern : {"target/**", ".git/**", "../inputs/target/**", |
| 82 | + "../inputs/.git/**", "../inputs/**"}) |
| 83 | + EXPECT_EQ(fingerprint(pattern), mcpp::toolchain::hash_string("")) << pattern; |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(GlobInputs, ParentPatternsRetainPathSetSemantics) { |
| 87 | + write("../inputs/a.in"); |
| 88 | + const auto before = fingerprint("../inputs/**"); |
| 89 | + write("../inputs/a.in", "longer content"); |
| 90 | + std::filesystem::last_write_time(root / "../inputs/a.in", |
| 91 | + std::filesystem::file_time_type::clock::now() + std::chrono::hours(1)); |
| 92 | + EXPECT_EQ(fingerprint("../inputs/**"), before); |
| 93 | + write("../inputs/nested/b.in"); |
| 94 | + EXPECT_NE(fingerprint("../inputs/**"), before); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_F(GlobInputs, DirectorySymlinksAreNotFollowedThroughTheLiteralPrefix) { |
| 98 | + write("../real/nested/a.in"); |
| 99 | + std::error_code ec; |
| 100 | + std::filesystem::create_directory_symlink(tree / "workspace" / "real", |
| 101 | + root / "../link", ec); |
| 102 | + if (ec) GTEST_SKIP() << "Directory symlinks are unavailable: " << ec.message(); |
| 103 | + EXPECT_EQ(fingerprint("../link/**/*.in"), mcpp::toolchain::hash_string("")); |
| 104 | + EXPECT_EQ(fingerprint("../link/nested/*.in"), mcpp::toolchain::hash_string("")); |
| 105 | + const auto before = fingerprint("../**/*.in"); |
| 106 | + write("../real/nested/b.in"); |
| 107 | + EXPECT_NE(fingerprint("../**/*.in"), before); |
| 108 | +} |
0 commit comments