diff options
| author | Jan Korous <jkorous@apple.com> | 2019-09-05 18:10:29 +0000 |
|---|---|---|
| committer | Jan Korous <jkorous@apple.com> | 2019-09-05 18:10:29 +0000 |
| commit | 00e04b0a6d51a415ea70133bbc2c6dad9cc72ecc (patch) | |
| tree | fe983610f9e0f9f5d3524b9c75186c4af43d5b9b /llvm/unittests | |
| parent | 1465a40cf80fff585f02005d2eef7d4728202cf1 (diff) | |
| download | bcm5719-llvm-00e04b0a6d51a415ea70133bbc2c6dad9cc72ecc.tar.gz bcm5719-llvm-00e04b0a6d51a415ea70133bbc2c6dad9cc72ecc.zip | |
[Support] Add writeFileAtomically() to FileUtilities
Differential Revision: https://reviews.llvm.org/D66859
llvm-svn: 371103
Diffstat (limited to 'llvm/unittests')
| -rw-r--r-- | llvm/unittests/Support/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | llvm/unittests/Support/FileUtilitiesTest.cpp | 52 |
2 files changed, 53 insertions, 0 deletions
diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index b0a7cda7a47..161891517cf 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -33,6 +33,7 @@ add_llvm_unittest(SupportTests FileCheckTest.cpp FileCollectorTest.cpp FileOutputBufferTest.cpp + FileUtilitiesTest.cpp FormatVariadicTest.cpp GlobPatternTest.cpp Host.cpp diff --git a/llvm/unittests/Support/FileUtilitiesTest.cpp b/llvm/unittests/Support/FileUtilitiesTest.cpp new file mode 100644 index 00000000000..2bf9dc5eb8c --- /dev/null +++ b/llvm/unittests/Support/FileUtilitiesTest.cpp @@ -0,0 +1,52 @@ +//===- llvm/unittest/Support/FileUtilitiesTest.cpp - unit tests -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" +#include "gtest/gtest.h" +#include <fstream> + +using namespace llvm; +using namespace llvm::sys; + +#define ASSERT_NO_ERROR(x) \ + if (std::error_code ASSERT_NO_ERROR_ec = x) { \ + SmallString<128> MessageStorage; \ + raw_svector_ostream Message(MessageStorage); \ + Message << #x ": did not return errc::success.\n" \ + << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ + << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ + GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ + } else { \ + } + +namespace { +TEST(writeFileAtomicallyTest, Test) { + // Create unique temporary directory for these tests + SmallString<128> RootTestDirectory; + ASSERT_NO_ERROR( + fs::createUniqueDirectory("writeFileAtomicallyTest", RootTestDirectory)); + + SmallString<128> FinalTestfilePath(RootTestDirectory); + sys::path::append(FinalTestfilePath, "foo.txt"); + const std::string TempUniqTestFileModel = FinalTestfilePath.str().str() + "-%%%%%%%%"; + const std::string TestfileContent = "fooFOOfoo"; + + llvm::Error Err = llvm::writeFileAtomically(TempUniqTestFileModel, FinalTestfilePath, TestfileContent); + ASSERT_FALSE(static_cast<bool>(Err)); + + std::ifstream FinalFileStream(FinalTestfilePath.str()); + std::string FinalFileContent; + FinalFileStream >> FinalFileContent; + ASSERT_EQ(FinalFileContent, TestfileContent); +} +} // anonymous namespace |

