-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdirectory.cpp
More file actions
162 lines (125 loc) · 4.38 KB
/
directory.cpp
File metadata and controls
162 lines (125 loc) · 4.38 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-FileCopyrightText: (c) 2024 Ilya Andreev <bugdealer@icloud.com>
// SPDX-License-Identifier: MIT
#include <tmp/directory>
#include <gtest/gtest.h>
#include <filesystem>
#include <fstream>
#include <ios>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
namespace tmp {
namespace {
namespace fs = std::filesystem;
/// Temporary directory prefix for this test suite
constexpr std::string_view prefix = "com.github.bugdea1er.tmp";
/// Tests directory creation with prefix
TEST(directory, create_with_prefix) {
directory tmpdir = directory(prefix);
fs::path parent = tmpdir.path().parent_path();
EXPECT_TRUE(fs::exists(tmpdir));
EXPECT_TRUE(fs::is_directory(tmpdir));
EXPECT_TRUE(fs::equivalent(parent, fs::temp_directory_path()));
EXPECT_EQ(fs::canonical(tmpdir), tmpdir.path());
#ifdef _WIN32
constexpr std::wstring_view actual_prefix = L"com.github.bugdea1er.tmp.";
#else
constexpr std::string_view actual_prefix = "com.github.bugdea1er.tmp.";
#endif
fs::path::string_type filename = tmpdir.path().filename();
EXPECT_EQ(filename.substr(0, actual_prefix.length()), actual_prefix);
fs::perms permissions = fs::status(tmpdir).permissions();
#ifdef _WIN32
// Following the logic with GetTempFileNameW
EXPECT_EQ(permissions, fs::perms::all);
#else
// mkdtemp creates a directory with full access only for the owner
EXPECT_EQ(permissions, fs::perms::owner_all);
#endif
}
/// Tests directory creation without prefix
TEST(directory, create_without_prefix) {
directory tmpdir = directory();
fs::path parent = tmpdir.path().parent_path();
EXPECT_TRUE(fs::exists(tmpdir));
EXPECT_TRUE(fs::is_directory(tmpdir));
EXPECT_TRUE(fs::equivalent(parent, fs::temp_directory_path()));
}
/// Tests multiple directories creation with the same prefix
TEST(directory, create_multiple) {
directory fst = directory(prefix);
directory snd = directory(prefix);
EXPECT_FALSE(fs::equivalent(fst, snd));
}
/// Tests error handling with invalid prefixes
TEST(directory, create_invalid_prefix) {
EXPECT_THROW(directory("multi/segment"), std::invalid_argument);
EXPECT_THROW(directory("/root"), std::invalid_argument);
fs::path root = fs::temp_directory_path().root_name();
if (!root.empty()) {
EXPECT_THROW(directory(root.string() + "relative"), std::invalid_argument);
EXPECT_THROW(directory(root.string() + "/root"), std::invalid_argument);
}
#ifdef _WIN32
EXPECT_THROW(directory("multi\\segment"), std::invalid_argument);
#endif
}
/// Tests `operator/` of directory
TEST(directory, subpath) {
directory tmpdir = directory();
fs::path expected = tmpdir.path() / "child";
std::ofstream(expected) << "Hello, world!";
EXPECT_TRUE(fs::equivalent(expected, tmpdir / "child"));
EXPECT_TRUE(fs::equivalent(expected, tmpdir / L"child"));
EXPECT_TRUE(fs::equivalent(expected, tmpdir / std::string("child")));
EXPECT_TRUE(fs::equivalent(expected, tmpdir / std::wstring(L"child")));
EXPECT_TRUE(fs::equivalent(expected, tmpdir / std::string_view("child")));
EXPECT_TRUE(fs::equivalent(expected, tmpdir / std::wstring_view(L"child")));
EXPECT_TRUE(fs::equivalent(expected, tmpdir / fs::path("child")));
EXPECT_TRUE(fs::equivalent(expected, tmpdir / fs::path(L"child")));
}
/// Tests that destructor removes a directory
TEST(directory, destructor) {
fs::path path;
{
directory tmpdir = directory();
path = tmpdir;
}
EXPECT_FALSE(fs::exists(path));
}
/// Tests directory move constructor
TEST(directory, move_constructor) {
directory fst = directory();
fs::path path = fst;
directory snd = directory(std::move(fst));
EXPECT_TRUE(fs::equivalent(path, snd));
EXPECT_TRUE(fs::exists(snd));
}
/// Tests directory move assignment operator
TEST(directory, move_assignment) {
directory fst = directory();
{
directory snd = directory();
fs::path path1 = fst;
fs::path path2 = snd;
fst = std::move(snd);
EXPECT_FALSE(fs::exists(path1));
EXPECT_TRUE(fs::exists(path2));
EXPECT_TRUE(fs::exists(fst));
EXPECT_TRUE(fs::equivalent(fst, path2));
}
EXPECT_FALSE(fst.path().empty());
}
/// Tests directory swapping
TEST(directory, swap) {
directory fst = directory();
directory snd = directory();
fs::path fst_path = fst.path();
fs::path snd_path = snd.path();
std::swap(fst, snd);
EXPECT_EQ(fst.path(), snd_path);
EXPECT_EQ(snd.path(), fst_path);
}
} // namespace
} // namespace tmp