-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_platform_fs.cpp
More file actions
105 lines (88 loc) · 4.07 KB
/
Copy pathtest_platform_fs.cpp
File metadata and controls
105 lines (88 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <gtest/gtest.h>
import std;
import mcpp.platform.fs;
import mcpp.platform.common;
namespace {
struct TempDir {
std::filesystem::path path = std::filesystem::temp_directory_path()
/ std::format("mcpp-platform-fs-{}",
std::chrono::steady_clock::now().time_since_epoch().count());
TempDir() { std::filesystem::create_directories(path); }
~TempDir() {
std::error_code ec;
std::filesystem::remove_all(path, ec);
}
};
void write_file(const std::filesystem::path& path, std::string_view text) {
std::ofstream out(path);
out << text;
}
std::string read_file(const std::filesystem::path& path) {
std::ifstream in(path);
return {std::istreambuf_iterator<char>(in), {}};
}
TEST(PlatformFs, ReplaceFileAtomicallyReplacesExistingFile) {
TempDir temp;
auto source = temp.path / "new.tmp";
auto destination = temp.path / "compile_commands.json";
write_file(source, "new");
write_file(destination, "old");
std::error_code ec;
EXPECT_TRUE(mcpp::platform::fs::replace_file(source, destination, ec))
<< ec.message();
EXPECT_FALSE(std::filesystem::exists(source));
EXPECT_EQ(read_file(destination), "new");
}
TEST(PlatformFs, ReplaceFailureKeepsExistingDestination) {
TempDir temp;
auto destination = temp.path / "compile_commands.json";
write_file(destination, "last-known-good");
std::error_code ec;
EXPECT_FALSE(mcpp::platform::fs::replace_file(
temp.path / "missing.tmp", destination, ec));
EXPECT_TRUE(ec);
EXPECT_EQ(read_file(destination), "last-known-good");
}
} // namespace
// ── extended_length (mcpp#641, item 3) ────────────────────────────────────
// Outside Windows there is no path limit of this kind, so the helper is the
// identity: a relative path stays relative and nothing is spelled differently.
TEST(PlatformFs, ExtendedLengthIsTheIdentityOutsideWindows) {
if constexpr (mcpp::platform::is_windows) {
GTEST_SKIP() << "the Windows branch is covered by the spelling tests";
} else {
const std::filesystem::path rel{"obj/pkg/__pkg/dep/a.cpp.ddi"};
EXPECT_EQ(mcpp::platform::fs::extended_length(rel), rel);
const std::filesystem::path abs{"/tmp/x/../y"};
EXPECT_EQ(mcpp::platform::fs::extended_length(abs), abs);
EXPECT_TRUE(mcpp::platform::fs::extended_length({}).empty());
}
}
// The Windows rule, as a pure function, so every host tests it.
TEST(PlatformFs, WindowsExtendedLengthSpellsADrivePath) {
using mcpp::platform::fs::windows_extended_length_spelling;
EXPECT_EQ(windows_extended_length_spelling("C:/Users/runner/.mcpp/obj/a.ddi"),
"\\\\?\\C:\\Users\\runner\\.mcpp\\obj\\a.ddi");
// `.` and `..` are resolved lexically, because the prefix turns their
// processing off; `..` does not climb above the drive.
EXPECT_EQ(windows_extended_length_spelling("C:\\a\\.\\b\\..\\c\\"),
"\\\\?\\C:\\a\\c");
EXPECT_EQ(windows_extended_length_spelling("C:/../../x"), "\\\\?\\C:\\x");
EXPECT_EQ(windows_extended_length_spelling("D:/"), "\\\\?\\D:\\");
}
TEST(PlatformFs, WindowsExtendedLengthSpellsAUncPath) {
using mcpp::platform::fs::windows_extended_length_spelling;
EXPECT_EQ(windows_extended_length_spelling("//server/share/dir/../f.o"),
"\\\\?\\UNC\\server\\share\\f.o");
// `..` does not climb above the share.
EXPECT_EQ(windows_extended_length_spelling("\\\\server\\share\\..\\..\\f"),
"\\\\?\\UNC\\server\\share\\f");
}
TEST(PlatformFs, WindowsExtendedLengthLeavesPrefixedAndRelativePathsAlone) {
using mcpp::platform::fs::windows_extended_length_spelling;
// Idempotent: a path handed on to a child process is converted again there.
EXPECT_EQ(windows_extended_length_spelling("\\\\?\\C:\\a\\b"), "\\\\?\\C:\\a\\b");
EXPECT_EQ(windows_extended_length_spelling("\\\\.\\pipe\\x"), "\\\\.\\pipe\\x");
EXPECT_EQ(windows_extended_length_spelling("obj/a.ddi"), "obj/a.ddi");
EXPECT_EQ(windows_extended_length_spelling(""), "");
}