blob: ff973e235965f4c1e34379d1612108fed27fd01e [file] [log] [blame]
Jan Korous00e04b02019-09-05 18:10:29 +00001//===- 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 Jaskiewiczfad75592020-07-06 15:55:49 +030015#include "llvm/Testing/Support/SupportHelpers.h"
Jan Korous00e04b02019-09-05 18:10:29 +000016#include "gtest/gtest.h"
17#include <fstream>
18
19using namespace llvm;
20using namespace llvm::sys;
21
Sergej Jaskiewiczfad75592020-07-06 15:55:49 +030022using llvm::unittest::TempDir;
23
Jan Korous00e04b02019-09-05 18:10:29 +000024#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
35namespace {
36TEST(writeFileAtomicallyTest, Test) {
37 // Create unique temporary directory for these tests
Sergej Jaskiewiczfad75592020-07-06 15:55:49 +030038 TempDir RootTestDirectory("writeFileAtomicallyTest", /*Unique*/ true);
Jan Korous00e04b02019-09-05 18:10:29 +000039
Sergej Jaskiewiczfad75592020-07-06 15:55:49 +030040 SmallString<128> FinalTestfilePath(RootTestDirectory.path());
Jan Korous00e04b02019-09-05 18:10:29 +000041 sys::path::append(FinalTestfilePath, "foo.txt");
Jonas Devlieghereb2924d92020-01-29 21:14:15 -080042 const std::string TempUniqTestFileModel =
43 std::string(FinalTestfilePath) + "-%%%%%%%%";
Jan Korous00e04b02019-09-05 18:10:29 +000044 const std::string TestfileContent = "fooFOOfoo";
45
46 llvm::Error Err = llvm::writeFileAtomically(TempUniqTestFileModel, FinalTestfilePath, TestfileContent);
47 ASSERT_FALSE(static_cast<bool>(Err));
48
Benjamin Krameradcd0262020-01-28 20:23:46 +010049 std::ifstream FinalFileStream(std::string(FinalTestfilePath.str()));
Jan Korous00e04b02019-09-05 18:10:29 +000050 std::string FinalFileContent;
51 FinalFileStream >> FinalFileContent;
52 ASSERT_EQ(FinalFileContent, TestfileContent);
53}
54} // anonymous namespace