Skip to content

Commit 53a8c29

Browse files
committed
test(rglob): add regression tests for issue p-ranav#3
.. and invalid rglob matching when using a pattern like `foo/**` on a non-existing directory
1 parent cc99c5c commit 53a8c29

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

test/rglob_test.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
1+
#include <filesystem>
2+
#include <fstream>
13
#include <gtest/gtest.h>
2-
4+
#include <stdlib.h>
35

46
#ifdef USE_SINGLE_HEADER
57
#include "glob/glob.hpp"
68
#else
79
#include "glob/glob.h"
810
#endif
911

12+
namespace fs = std::filesystem;
13+
14+
fs::path mkdir_temp() {
15+
std::srand(std::time(nullptr));
16+
fs::path temp_dir = fs::temp_directory_path() / ("rglob_test_" + std::to_string(std::rand()));
1017

18+
fs::create_directories(temp_dir);
19+
return temp_dir;
20+
}
21+
22+
// regression test to avoid matching an non existing file
1123
TEST(rglobTest, MatchNonExistent) {
12-
auto matches = glob::rglob("non-existent/**");
13-
EXPECT_EQ(matches.size(), 0);
24+
auto matches = glob::rglob("non-existent/**");
25+
EXPECT_EQ(matches.size(), 0);
26+
}
27+
28+
// see https://github.com/p-ranav/glob/issues/3
29+
TEST(rglobTest, Issue3) {
30+
auto temp_dir = mkdir_temp();
31+
std::cout << "Temporary directory: " << temp_dir << std::endl;
32+
33+
fs::path sub1 = temp_dir / "sub";
34+
fs::path sub2 = sub1 / "sub";
35+
EXPECT_TRUE(fs::create_directory(sub1));
36+
EXPECT_TRUE(fs::create_directory(sub2));
37+
38+
std::ofstream(temp_dir / "file.txt").close();
39+
std::ofstream(sub1 / "file.txt").close();
40+
std::ofstream(sub2 / "file.txt").close();
41+
42+
auto pattern = temp_dir.string() + "/**/*.txt";
43+
std::cout << "Pattern: " << pattern << std::endl;
44+
45+
auto matches = glob::rglob(pattern);
46+
EXPECT_EQ(matches.size(), 3);
47+
EXPECT_EQ(matches[0].string(), (temp_dir / "file.txt").string());
48+
EXPECT_EQ(matches[1].string(), (sub1 / "file.txt").string());
49+
EXPECT_EQ(matches[2].string(), (sub2 / "file.txt").string());
1450
}

0 commit comments

Comments
 (0)