64 lines · cpp
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-exception6//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 18using namespace lldb_private;19 20extern const char *TestMainArgv0;21 22std::once_flag TestUtilities::g_debugger_initialize_flag;23 24std::string lldb_private::PrettyPrint(const llvm::json::Value &value) {25 return llvm::formatv("{0:2}", value).str();26}27 28std::string lldb_private::GetInputFilePath(const llvm::Twine &name) {29 llvm::SmallString<128> result = llvm::sys::path::parent_path(TestMainArgv0);30 llvm::sys::fs::make_absolute(result);31 llvm::sys::path::append(result, "Inputs", name);32 return std::string(result.str());33}34 35llvm::Expected<TestFile> TestFile::fromYaml(llvm::StringRef Yaml) {36 std::string Buffer;37 llvm::raw_string_ostream OS(Buffer);38 llvm::yaml::Input YIn(Yaml);39 std::string ErrorMsg("convertYAML() failed: ");40 if (!llvm::yaml::convertYAML(YIn, OS, [&ErrorMsg](const llvm::Twine &Msg) {41 ErrorMsg += Msg.str();42 }))43 return llvm::createStringError(llvm::inconvertibleErrorCode(), ErrorMsg);44 return TestFile(std::move(Buffer));45}46 47llvm::Expected<TestFile> TestFile::fromYamlFile(const llvm::Twine &Name) {48 auto BufferOrError =49 llvm::MemoryBuffer::getFile(GetInputFilePath(Name), /*IsText=*/false,50 /*RequiresNullTerminator=*/false);51 if (!BufferOrError)52 return llvm::errorCodeToError(BufferOrError.getError());53 return fromYaml(BufferOrError.get()->getBuffer());54}55 56llvm::Expected<llvm::sys::fs::TempFile> TestFile::writeToTemporaryFile() {57 llvm::Expected<llvm::sys::fs::TempFile> Temp =58 llvm::sys::fs::TempFile::create("temp%%%%%%%%%%%%%%%%");59 if (!Temp)60 return Temp.takeError();61 llvm::raw_fd_ostream(Temp->FD, /*shouldClose=*/false) << Buffer;62 return std::move(*Temp);63}64