|
| 1 | +#include <filesystem> |
| 2 | +#include <fstream> |
1 | 3 | #include <gtest/gtest.h>
|
2 |
| - |
| 4 | +#include <stdlib.h> |
3 | 5 |
|
4 | 6 | #ifdef USE_SINGLE_HEADER
|
5 | 7 | #include "glob/glob.hpp"
|
6 | 8 | #else
|
7 | 9 | #include "glob/glob.h"
|
8 | 10 | #endif
|
9 | 11 |
|
| 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())); |
10 | 17 |
|
| 18 | + fs::create_directories(temp_dir); |
| 19 | + return temp_dir; |
| 20 | +} |
| 21 | + |
| 22 | +// regression test to avoid matching an non existing file |
11 | 23 | 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()); |
14 | 50 | }
|
0 commit comments