blob: 941fe186981467e3ea725cadb25ace75b17ab68c [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"
Dmitri Gribenkoecf5db32023-07-10 08:56:17 -070012#include "absl/strings/str_cat.h"
13#include "absl/strings/string_view.h"
Marcel Hlopko39512852022-05-09 02:22:00 -070014#include "common/file_io.h"
Dmitri Gribenkoecf5db32023-07-10 08:56:17 -070015#include "llvm/ADT/StringRef.h"
Marcel Hlopko39512852022-05-09 02:22:00 -070016#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/Path.h"
18
19namespace crubit {
20
21static std::string MakeTmpdirForCurrentTest() {
22 absl::string_view current_test_name =
23 testing::UnitTest::GetInstance()->current_test_info()->name();
Luca Versaric21d92f2022-05-25 00:56:30 -070024 std::string current_test_tmpdir_path =
25 absl::StrCat(testing::TempDir(), "/", current_test_name, "/");
Marcel Hlopko39512852022-05-09 02:22:00 -070026 llvm::StringRef parent_dir =
27 llvm::sys::path::parent_path(current_test_tmpdir_path);
Lukasz Anforowicz2c34cae2022-08-26 07:19:20 -070028 CHECK(!llvm::sys::fs::create_directories(parent_dir));
Marcel Hlopko39512852022-05-09 02:22:00 -070029 return current_test_tmpdir_path;
30}
31
32std::string WriteFileForCurrentTest(absl::string_view filename,
33 absl::string_view content) {
34 std::string path = absl::StrCat(MakeTmpdirForCurrentTest(), "/", filename);
Lukasz Anforowicz2c34cae2022-08-26 07:19:20 -070035 CHECK(SetFileContents(path, content).ok());
Marcel Hlopko39512852022-05-09 02:22:00 -070036 return path;
37}
38
39std::vector<std::string> DefaultClangArgs() {
40 return {"-I", MakeTmpdirForCurrentTest()};
41}
42
43} // namespace crubit