| 1 | //===-- TestUtilities.cpp -------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "TestUtilities.h" |
| 10 | #include "llvm/ADT/SmallString.h" |
| 11 | #include "llvm/ObjectYAML/yaml2obj.h" |
| 12 | #include "llvm/Support/FileSystem.h" |
| 13 | #include "llvm/Support/Path.h" |
| 14 | #include "llvm/Support/Program.h" |
| 15 | #include "llvm/Support/YAMLTraits.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | extern const char *TestMainArgv0; |
| 21 | |
| 22 | std::once_flag TestUtilities::g_debugger_initialize_flag; |
| 23 | std::string lldb_private::GetInputFilePath(const llvm::Twine &name) { |
| 24 | llvm::SmallString<128> result = llvm::sys::path::parent_path(path: TestMainArgv0); |
| 25 | llvm::sys::fs::make_absolute(path&: result); |
| 26 | llvm::sys::path::append(path&: result, a: "Inputs", b: name); |
| 27 | return std::string(result.str()); |
| 28 | } |
| 29 | |
| 30 | llvm::Expected<TestFile> TestFile::fromYaml(llvm::StringRef Yaml) { |
| 31 | std::string Buffer; |
| 32 | llvm::raw_string_ostream OS(Buffer); |
| 33 | llvm::yaml::Input YIn(Yaml); |
| 34 | std::string ErrorMsg("convertYAML() failed: "); |
| 35 | if (!llvm::yaml::convertYAML(YIn, Out&: OS, ErrHandler: [&ErrorMsg](const llvm::Twine &Msg) { |
| 36 | ErrorMsg += Msg.str(); |
| 37 | })) |
| 38 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), S: ErrorMsg); |
| 39 | return TestFile(std::move(Buffer)); |
| 40 | } |
| 41 | |
| 42 | llvm::Expected<TestFile> TestFile::fromYamlFile(const llvm::Twine &Name) { |
| 43 | auto BufferOrError = |
| 44 | llvm::MemoryBuffer::getFile(Filename: GetInputFilePath(name: Name), /*IsText=*/false, |
| 45 | /*RequiresNullTerminator=*/false); |
| 46 | if (!BufferOrError) |
| 47 | return llvm::errorCodeToError(EC: BufferOrError.getError()); |
| 48 | return fromYaml(Yaml: BufferOrError.get()->getBuffer()); |
| 49 | } |
| 50 | |
| 51 | llvm::Expected<llvm::sys::fs::TempFile> TestFile::writeToTemporaryFile() { |
| 52 | llvm::Expected<llvm::sys::fs::TempFile> Temp = |
| 53 | llvm::sys::fs::TempFile::create(Model: "temp%%%%%%%%%%%%%%%%"); |
| 54 | if (!Temp) |
| 55 | return Temp.takeError(); |
| 56 | llvm::raw_fd_ostream(Temp->FD, /*shouldClose=*/false) << Buffer; |
| 57 | return std::move(*Temp); |
| 58 | } |
| 59 |
