forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_util_test.cc
301 lines (253 loc) · 10.4 KB
/
file_util_test.cc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/util/file_util.h"
#include <algorithm>
#include <filesystem>
#include <string>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::HasSubstr;
#undef EXPECT_OK
#define EXPECT_OK(value) \
{ \
auto s = (value); \
EXPECT_TRUE(s.ok()) << s; \
}
#undef ASSERT_OK
#define ASSERT_OK(value) \
{ \
auto s = (value); \
ASSERT_TRUE(s.ok()) << s; \
}
namespace verible {
namespace util {
namespace {
using file::testing::ScopedTestFile;
TEST(FileUtil, Basename) {
EXPECT_EQ(file::Basename("/foo/bar/baz"), "baz");
EXPECT_EQ(file::Basename("foo/bar/baz"), "baz");
EXPECT_EQ(file::Basename("/foo/bar/"), "");
EXPECT_EQ(file::Basename("/"), "");
EXPECT_EQ(file::Basename(""), "");
}
TEST(FileUtil, Dirname) {
EXPECT_EQ(file::Dirname("/foo/bar/baz"), "/foo/bar");
EXPECT_EQ(file::Dirname("/foo/bar/baz.txt"), "/foo/bar");
EXPECT_EQ(file::Dirname("foo/bar/baz"), "foo/bar");
EXPECT_EQ(file::Dirname("/foo/bar/"), "/foo/bar");
EXPECT_EQ(file::Dirname("/"), "");
EXPECT_EQ(file::Dirname(""), "");
EXPECT_EQ(file::Dirname("."), ".");
EXPECT_EQ(file::Dirname("./"), ".");
}
TEST(FileUtil, Stem) {
EXPECT_EQ(file::Stem(""), "");
EXPECT_EQ(file::Stem("/foo/bar.baz"), "/foo/bar");
EXPECT_EQ(file::Stem("foo/bar.baz"), "foo/bar");
EXPECT_EQ(file::Stem("/foo/bar."), "/foo/bar");
EXPECT_EQ(file::Stem("/foo/bar"), "/foo/bar");
}
// Returns the forward-slashed path in platform-specific notation.
static std::string PlatformPath(const std::string& path) {
return std::filesystem::path(path).lexically_normal().string();
}
TEST(FileUtil, JoinPath) {
EXPECT_EQ(file::JoinPath("foo", ""), PlatformPath("foo/"));
EXPECT_EQ(file::JoinPath("", "bar"), "bar");
EXPECT_EQ(file::JoinPath("foo", "bar"), PlatformPath("foo/bar"));
// Assemble an absolute path
EXPECT_EQ(file::JoinPath("", "bar"), "bar");
EXPECT_EQ(file::JoinPath("/", "bar"), PlatformPath("/bar"));
// Lightly canonicalize multiple consecutive slashes
EXPECT_EQ(file::JoinPath("foo/", "bar"), PlatformPath("foo/bar"));
EXPECT_EQ(file::JoinPath("foo///", "bar"), PlatformPath("foo/bar"));
EXPECT_EQ(file::JoinPath("foo/", "/bar"), PlatformPath("foo/bar"));
EXPECT_EQ(file::JoinPath("foo/", "///bar"), PlatformPath("foo/bar"));
#ifdef _WIN32
EXPECT_EQ(file::JoinPath("C:\\foo", "bar"), "C:\\foo\\bar");
#endif
}
TEST(FileUtil, CreateDir) {
const std::string test_dir = file::JoinPath(testing::TempDir(), "test_dir");
const std::string test_file = file::JoinPath(test_dir, "foo");
const absl::string_view test_content = "directory create test";
EXPECT_OK(file::CreateDir(test_dir));
EXPECT_OK(file::CreateDir(test_dir)); // Creating twice should succeed
EXPECT_OK(file::SetContents(test_file, test_content));
std::string read_back_content;
EXPECT_OK(file::GetContents(test_file, &read_back_content));
EXPECT_EQ(test_content, read_back_content);
}
TEST(FileUtil, StatusErrorReporting) {
std::string content;
absl::Status status = file::GetContents("does-not-exist", &content);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kNotFound) << status;
const std::string test_file = file::JoinPath(testing::TempDir(), "test-err");
unlink(test_file.c_str()); // Remove file if left from previous test.
EXPECT_OK(file::SetContents(test_file, "foo"));
EXPECT_OK(file::GetContents(test_file, &content));
EXPECT_EQ(content, "foo");
#ifndef _WIN32
// The following chmod() is not working on Win32. So let's not use
// this test here.
// TODO: Can we make permission-denied test that works on Windows ?
chmod(test_file.c_str(), 0); // Enforce a permission denied situation
status = file::GetContents(test_file, &content);
EXPECT_FALSE(status.ok());
EXPECT_EQ(status.code(), absl::StatusCode::kPermissionDenied) << status;
#endif
}
TEST(FileUtil, ScopedTestFile) {
const absl::string_view test_content = "Hello World!";
ScopedTestFile test_file(testing::TempDir(), test_content);
std::string read_back_content;
EXPECT_OK(file::GetContents(test_file.filename(), &read_back_content));
EXPECT_EQ(test_content, read_back_content);
}
static ScopedTestFile TestFileGenerator(absl::string_view content) {
return ScopedTestFile(testing::TempDir(), content);
}
static void TestFileConsumer(ScopedTestFile&& f) {
ScopedTestFile temp(std::move(f));
}
TEST(FileUtil, ScopedTestFileMove) {
ScopedTestFile f(TestFileGenerator("barfoo"));
std::string tmpname(f.filename());
EXPECT_TRUE(file::FileExists(tmpname).ok());
TestFileConsumer(std::move(f));
EXPECT_FALSE(file::FileExists(tmpname).ok());
}
TEST(FileUtil, ScopedTestFileEmplace) {
std::vector<std::string> names;
{
std::vector<ScopedTestFile> files;
for (size_t i = 0; i < 10; ++i) {
files.emplace_back(::testing::TempDir(), "zzz");
names.push_back(std::string(files.back().filename()));
}
for (const auto& name : names) {
EXPECT_TRUE(file::FileExists(name).ok());
}
}
for (const auto& name : names) {
EXPECT_FALSE(file::FileExists(name).ok());
}
}
TEST(FileUtil, FileExistsDirectoryErrorMessage) {
absl::Status s;
s = file::FileExists(testing::TempDir());
EXPECT_FALSE(s.ok());
EXPECT_THAT(s.message(), HasSubstr("is a directory"));
}
static bool CreateFsStructure(absl::string_view base_dir,
const std::vector<absl::string_view>& tree) {
for (absl::string_view path : tree) {
const std::string full_path = file::JoinPath(base_dir, path);
if (absl::EndsWith(path, "/")) {
if (!file::CreateDir(full_path).ok()) return false;
} else {
if (!file::SetContents(full_path, "(content)").ok()) return false;
}
}
return true;
}
TEST(FileUtil, UpwardFileSearchTest) {
const std::string root_dir = file::JoinPath(testing::TempDir(), "up-search");
ASSERT_OK(file::CreateDir(root_dir));
ASSERT_TRUE(CreateFsStructure(root_dir, {
"toplevel-file",
"foo/",
"foo/foo-file",
"foo/bar/",
"foo/bar/baz/",
"foo/bar/baz/baz-file",
}));
std::string result;
// Same directory
EXPECT_OK(file::UpwardFileSearch(file::JoinPath(root_dir, "foo"), "foo-file",
&result));
// We don't compare the full path, as the UpwardFileSearch() makes it an
// realpath which might not entirely match our root_dir prefix anymore; but
// we do know that the suffix should be the same.
EXPECT_TRUE(absl::EndsWith(result, PlatformPath("up-search/foo/foo-file")));
// Somewhere below
EXPECT_OK(file::UpwardFileSearch(file::JoinPath(root_dir, "foo/bar/baz"),
"foo-file", &result));
EXPECT_TRUE(absl::EndsWith(result, PlatformPath("up-search/foo/foo-file")));
// Find toplevel file
EXPECT_OK(file::UpwardFileSearch(file::JoinPath(root_dir, "foo/bar/baz"),
"toplevel-file", &result));
EXPECT_TRUE(absl::EndsWith(result, PlatformPath("up-search/toplevel-file")));
// Negative test.
auto status = file::UpwardFileSearch(file::JoinPath(root_dir, "foo/bar/baz"),
"unknownfile", &result);
EXPECT_FALSE(status.ok());
}
TEST(FileUtil, ReadEmptyDirectory) {
const std::string test_dir = file::JoinPath(testing::TempDir(), "empty_dir");
ASSERT_TRUE(file::CreateDir(test_dir).ok());
auto dir_or = file::ListDir(test_dir);
ASSERT_TRUE(dir_or.ok());
const auto& dir = *dir_or;
EXPECT_EQ(dir.path, test_dir);
EXPECT_TRUE(dir.directories.empty());
EXPECT_TRUE(dir.files.empty());
}
TEST(FileUtil, ReadNonexistentDirectory) {
const std::string test_dir =
file::JoinPath(testing::TempDir(), "dir_not_there");
auto dir_or = file::ListDir(test_dir);
EXPECT_FALSE(dir_or.ok());
EXPECT_EQ(dir_or.status().code(), absl::StatusCode::kNotFound);
}
TEST(FileUtil, ListNotADirectory) {
ScopedTestFile tempfile(testing::TempDir(), "O HAI, WRLD");
auto dir_or = file::ListDir(tempfile.filename());
EXPECT_FALSE(dir_or.ok());
EXPECT_EQ(dir_or.status().code(), absl::StatusCode::kInvalidArgument);
}
TEST(FileUtil, ReadDirectory) {
const std::string test_dir = file::JoinPath(testing::TempDir(), "mixed_dir");
const std::string test_subdir1 = file::JoinPath(test_dir, "subdir1");
const std::string test_subdir2 = file::JoinPath(test_dir, "subdir2");
std::vector<std::string> test_directories{test_subdir1, test_subdir2};
ASSERT_TRUE(file::CreateDir(test_dir).ok());
ASSERT_TRUE(file::CreateDir(test_subdir1).ok());
ASSERT_TRUE(file::CreateDir(test_subdir2).ok());
const absl::string_view test_content = "Hello World!";
file::testing::ScopedTestFile test_file1(test_dir, test_content);
file::testing::ScopedTestFile test_file2(test_dir, test_content);
file::testing::ScopedTestFile test_file3(test_dir, test_content);
std::vector<std::string> test_files;
test_files.emplace_back(test_file1.filename());
test_files.emplace_back(test_file2.filename());
test_files.emplace_back(test_file3.filename());
std::sort(test_files.begin(), test_files.end());
auto dir_or = file::ListDir(test_dir);
ASSERT_TRUE(dir_or.ok()) << dir_or.status().message();
const auto& dir = *dir_or;
EXPECT_EQ(dir.path, test_dir);
EXPECT_EQ(dir.directories.size(), test_directories.size());
EXPECT_EQ(dir.directories, test_directories);
EXPECT_EQ(dir.files.size(), test_files.size());
EXPECT_EQ(dir.files, test_files);
}
} // namespace
} // namespace util
} // namespace verible