Jan Korous | 00e04b0 | 2019-09-05 18:10:29 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/FileUtilitiesTest.cpp - unit tests -----------===// |
| 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 "llvm/Support/FileUtilities.h" |
| 10 | #include "llvm/Support/Errc.h" |
| 11 | #include "llvm/Support/ErrorHandling.h" |
| 12 | #include "llvm/Support/FileSystem.h" |
| 13 | #include "llvm/Support/MemoryBuffer.h" |
| 14 | #include "llvm/Support/Path.h" |
Sergej Jaskiewicz | fad7559 | 2020-07-06 15:55:49 +0300 | [diff] [blame] | 15 | #include "llvm/Testing/Support/SupportHelpers.h" |
Jan Korous | 00e04b0 | 2019-09-05 18:10:29 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
| 17 | #include <fstream> |
| 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace llvm::sys; |
| 21 | |
Sergej Jaskiewicz | fad7559 | 2020-07-06 15:55:49 +0300 | [diff] [blame] | 22 | using llvm::unittest::TempDir; |
| 23 | |
Jan Korous | 00e04b0 | 2019-09-05 18:10:29 +0000 | [diff] [blame] | 24 | #define ASSERT_NO_ERROR(x) \ |
| 25 | if (std::error_code ASSERT_NO_ERROR_ec = x) { \ |
| 26 | SmallString<128> MessageStorage; \ |
| 27 | raw_svector_ostream Message(MessageStorage); \ |
| 28 | Message << #x ": did not return errc::success.\n" \ |
| 29 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 30 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 31 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
| 32 | } else { \ |
| 33 | } |
| 34 | |
| 35 | namespace { |
| 36 | TEST(writeFileAtomicallyTest, Test) { |
| 37 | // Create unique temporary directory for these tests |
Sergej Jaskiewicz | fad7559 | 2020-07-06 15:55:49 +0300 | [diff] [blame] | 38 | TempDir RootTestDirectory("writeFileAtomicallyTest", /*Unique*/ true); |
Jan Korous | 00e04b0 | 2019-09-05 18:10:29 +0000 | [diff] [blame] | 39 | |
Sergej Jaskiewicz | fad7559 | 2020-07-06 15:55:49 +0300 | [diff] [blame] | 40 | SmallString<128> FinalTestfilePath(RootTestDirectory.path()); |
Jan Korous | 00e04b0 | 2019-09-05 18:10:29 +0000 | [diff] [blame] | 41 | sys::path::append(FinalTestfilePath, "foo.txt"); |
Jonas Devlieghere | b2924d9 | 2020-01-29 21:14:15 -0800 | [diff] [blame] | 42 | const std::string TempUniqTestFileModel = |
| 43 | std::string(FinalTestfilePath) + "-%%%%%%%%"; |
Jan Korous | 00e04b0 | 2019-09-05 18:10:29 +0000 | [diff] [blame] | 44 | const std::string TestfileContent = "fooFOOfoo"; |
| 45 | |
| 46 | llvm::Error Err = llvm::writeFileAtomically(TempUniqTestFileModel, FinalTestfilePath, TestfileContent); |
| 47 | ASSERT_FALSE(static_cast<bool>(Err)); |
| 48 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 49 | std::ifstream FinalFileStream(std::string(FinalTestfilePath.str())); |
Jan Korous | 00e04b0 | 2019-09-05 18:10:29 +0000 | [diff] [blame] | 50 | std::string FinalFileContent; |
| 51 | FinalFileStream >> FinalFileContent; |
| 52 | ASSERT_EQ(FinalFileContent, TestfileContent); |
| 53 | } |
| 54 | } // anonymous namespace |