blob: 74f24d6dd8458751ae6f46700c403f5d3b33b7ff [file] [log] [blame]
Marcel Hlopko39512852022-05-09 02:22:00 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#include "common/test_utils.h"
6
7#include <string>
8#include <vector>
9
Luca Versaric21d92f2022-05-25 00:56:30 -070010#include "gtest/gtest.h"
Lukasz Anforowicz2c34cae2022-08-26 07:19:20 -070011#include "absl/log/check.h"
Marcel Hlopko39512852022-05-09 02:22:00 -070012#include "common/file_io.h"
13#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/Path.h"
15
16namespace crubit {
17
18static std::string MakeTmpdirForCurrentTest() {
19 absl::string_view current_test_name =
20 testing::UnitTest::GetInstance()->current_test_info()->name();
Luca Versaric21d92f2022-05-25 00:56:30 -070021 std::string current_test_tmpdir_path =
22 absl::StrCat(testing::TempDir(), "/", current_test_name, "/");
Marcel Hlopko39512852022-05-09 02:22:00 -070023 llvm::StringRef parent_dir =
24 llvm::sys::path::parent_path(current_test_tmpdir_path);
Lukasz Anforowicz2c34cae2022-08-26 07:19:20 -070025 CHECK(!llvm::sys::fs::create_directories(parent_dir));
Marcel Hlopko39512852022-05-09 02:22:00 -070026 return current_test_tmpdir_path;
27}
28
29std::string WriteFileForCurrentTest(absl::string_view filename,
30 absl::string_view content) {
31 std::string path = absl::StrCat(MakeTmpdirForCurrentTest(), "/", filename);
Lukasz Anforowicz2c34cae2022-08-26 07:19:20 -070032 CHECK(SetFileContents(path, content).ok());
Marcel Hlopko39512852022-05-09 02:22:00 -070033 return path;
34}
35
36std::vector<std::string> DefaultClangArgs() {
37 return {"-I", MakeTmpdirForCurrentTest()};
38}
39
40} // namespace crubit