Marcel Hlopko | 3951285 | 2022-05-09 02:22:00 -0700 | [diff] [blame] | 1 | // 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 Versari | c21d92f | 2022-05-25 00:56:30 -0700 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 11 | #include "absl/log/check.h" |
Marcel Hlopko | 3951285 | 2022-05-09 02:22:00 -0700 | [diff] [blame] | 12 | #include "common/file_io.h" |
| 13 | #include "llvm/Support/FileSystem.h" |
| 14 | #include "llvm/Support/Path.h" |
| 15 | |
| 16 | namespace crubit { |
| 17 | |
| 18 | static std::string MakeTmpdirForCurrentTest() { |
| 19 | absl::string_view current_test_name = |
| 20 | testing::UnitTest::GetInstance()->current_test_info()->name(); |
Luca Versari | c21d92f | 2022-05-25 00:56:30 -0700 | [diff] [blame] | 21 | std::string current_test_tmpdir_path = |
| 22 | absl::StrCat(testing::TempDir(), "/", current_test_name, "/"); |
Marcel Hlopko | 3951285 | 2022-05-09 02:22:00 -0700 | [diff] [blame] | 23 | llvm::StringRef parent_dir = |
| 24 | llvm::sys::path::parent_path(current_test_tmpdir_path); |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 25 | CHECK(!llvm::sys::fs::create_directories(parent_dir)); |
Marcel Hlopko | 3951285 | 2022-05-09 02:22:00 -0700 | [diff] [blame] | 26 | return current_test_tmpdir_path; |
| 27 | } |
| 28 | |
| 29 | std::string WriteFileForCurrentTest(absl::string_view filename, |
| 30 | absl::string_view content) { |
| 31 | std::string path = absl::StrCat(MakeTmpdirForCurrentTest(), "/", filename); |
Lukasz Anforowicz | 2c34cae | 2022-08-26 07:19:20 -0700 | [diff] [blame] | 32 | CHECK(SetFileContents(path, content).ok()); |
Marcel Hlopko | 3951285 | 2022-05-09 02:22:00 -0700 | [diff] [blame] | 33 | return path; |
| 34 | } |
| 35 | |
| 36 | std::vector<std::string> DefaultClangArgs() { |
| 37 | return {"-I", MakeTmpdirForCurrentTest()}; |
| 38 | } |
| 39 | |
| 40 | } // namespace crubit |