FileUtilitiesTest.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "llvm/Support/FileUtilities.h"
  9. #include "llvm/Support/Errc.h"
  10. #include "llvm/Support/ErrorHandling.h"
  11. #include "llvm/Support/FileSystem.h"
  12. #include "llvm/Support/MemoryBuffer.h"
  13. #include "llvm/Support/Path.h"
  14. #include "gtest/gtest.h"
  15. #include <fstream>
  16. using namespace llvm;
  17. using namespace llvm::sys;
  18. #define ASSERT_NO_ERROR(x) \
  19. if (std::error_code ASSERT_NO_ERROR_ec = x) { \
  20. SmallString<128> MessageStorage; \
  21. raw_svector_ostream Message(MessageStorage); \
  22. Message << #x ": did not return errc::success.\n" \
  23. << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
  24. << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
  25. GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
  26. } else { \
  27. }
  28. namespace {
  29. TEST(writeFileAtomicallyTest, Test) {
  30. // Create unique temporary directory for these tests
  31. SmallString<128> RootTestDirectory;
  32. ASSERT_NO_ERROR(
  33. fs::createUniqueDirectory("writeFileAtomicallyTest", RootTestDirectory));
  34. SmallString<128> FinalTestfilePath(RootTestDirectory);
  35. sys::path::append(FinalTestfilePath, "foo.txt");
  36. const std::string TempUniqTestFileModel = FinalTestfilePath.str().str() + "-%%%%%%%%";
  37. const std::string TestfileContent = "fooFOOfoo";
  38. llvm::Error Err = llvm::writeFileAtomically(TempUniqTestFileModel, FinalTestfilePath, TestfileContent);
  39. ASSERT_FALSE(static_cast<bool>(Err));
  40. std::ifstream FinalFileStream(FinalTestfilePath.str());
  41. std::string FinalFileContent;
  42. FinalFileStream >> FinalFileContent;
  43. ASSERT_EQ(FinalFileContent, TestfileContent);
  44. }
  45. } // anonymous namespace