Skip to content

Commit 3eceab9

Browse files
vedgyAaronBallman
authored andcommitted
LibclangTest: remove libclang-test-* tmp dir reliably
Temporary directories created by two LibclangReparseTest tests - ReparseWithModule and clang_parseTranslationUnit2FullArgv - remained in the system temporary directory after running libclangTests, because not all files and subdirectories created in TestDir were added to set LibclangParseTest::Files. Differential Revision: https://reviews.llvm.org/D143415
1 parent 457c4fe commit 3eceab9

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

clang/unittests/libclang/LibclangTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ TEST_F(LibclangReparseTest, ReparseWithModule) {
515515
WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom);
516516
WriteFile(ModName, ModFile);
517517

518+
// Removing recursively is necessary to delete the module cache.
519+
RemoveTestDirRecursivelyDuringTeardown = true;
518520
std::string ModulesCache = std::string("-fmodules-cache-path=") + TestDir;
519521
const char *Args[] = { "-fmodules", ModulesCache.c_str(),
520522
"-I", TestDir.c_str() };

clang/unittests/libclang/TestUtils.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414
#include "llvm/Support/FileSystem.h"
1515
#include "llvm/Support/Path.h"
1616

17+
#include "gtest/gtest.h"
1718
#include <fstream>
19+
#include <functional>
1820
#include <memory>
1921
#include <string>
2022
#include <vector>
21-
#include "gtest/gtest.h"
2223

2324
class LibclangParseTest : public ::testing::Test {
24-
std::set<std::string> Files;
25+
// std::greater<> to remove files before their parent dirs in TearDown().
26+
std::set<std::string, std::greater<>> Files;
2527
typedef std::unique_ptr<std::string> fixed_addr_string;
2628
std::map<fixed_addr_string, fixed_addr_string> UnsavedFileContents;
2729
public:
2830
std::string TestDir;
31+
bool RemoveTestDirRecursivelyDuringTeardown = false;
2932
CXIndex Index;
3033
CXTranslationUnit ClangTU;
3134
unsigned TUFlags;
@@ -43,16 +46,26 @@ class LibclangParseTest : public ::testing::Test {
4346
void TearDown() override {
4447
clang_disposeTranslationUnit(ClangTU);
4548
clang_disposeIndex(Index);
49+
50+
namespace fs = llvm::sys::fs;
4651
for (const std::string &Path : Files)
47-
llvm::sys::fs::remove(Path);
48-
llvm::sys::fs::remove(TestDir);
52+
EXPECT_FALSE(fs::remove(Path, /*IgnoreNonExisting=*/false));
53+
if (RemoveTestDirRecursivelyDuringTeardown)
54+
EXPECT_FALSE(fs::remove_directories(TestDir, /*IgnoreErrors=*/false));
55+
else
56+
EXPECT_FALSE(fs::remove(TestDir, /*IgnoreNonExisting=*/false));
4957
}
5058
void WriteFile(std::string &Filename, const std::string &Contents) {
5159
if (!llvm::sys::path::is_absolute(Filename)) {
5260
llvm::SmallString<256> Path(TestDir);
53-
llvm::sys::path::append(Path, Filename);
61+
namespace path = llvm::sys::path;
62+
for (auto FileI = path::begin(Filename), FileEnd = path::end(Filename);
63+
FileI != FileEnd; ++FileI) {
64+
ASSERT_NE(*FileI, ".");
65+
path::append(Path, *FileI);
66+
Files.emplace(Path.str());
67+
}
5468
Filename = std::string(Path.str());
55-
Files.insert(Filename);
5669
}
5770
llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename));
5871
std::ofstream OS(Filename);

0 commit comments

Comments
 (0)