xref: /llvm-project/llvm/lib/Testing/Support/SupportHelpers.cpp (revision 9ec23049a76cef425e3978e617cd3aaf2f8d2fd3)
1 
2 #include "llvm/Testing/Support/SupportHelpers.h"
3 
4 #include "llvm/ADT/SmallString.h"
5 #include "llvm/ADT/Twine.h"
6 #include "llvm/Support/Error.h"
7 #include "llvm/Support/FileSystem.h"
8 #include "llvm/Support/MemoryBuffer.h"
9 #include "llvm/Support/Path.h"
10 
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 using namespace llvm::unittest;
15 
16 extern const char *TestMainArgv0;
17 
18 SmallString<128> llvm::unittest::getInputFileDirectory() {
19   llvm::SmallString<128> Result = llvm::sys::path::parent_path(TestMainArgv0);
20   llvm::sys::fs::make_absolute(Result);
21   llvm::sys::path::append(Result, "llvm.srcdir.txt");
22 
23   EXPECT_TRUE(llvm::sys::fs::is_regular_file(Result))
24       << "Unit test source directory file does not exist.";
25 
26   auto File = MemoryBuffer::getFile(Result);
27 
28   EXPECT_TRUE(static_cast<bool>(File))
29       << "Could not open unit test source directory file.";
30 
31   Result.clear();
32   Result.append((*File)->getBuffer().trim());
33   llvm::sys::path::append(Result, "Inputs");
34   llvm::sys::path::native(Result);
35   return std::move(Result);
36 }
37