Add explicit walk_path test.

GitOrigin-RevId: ec04003641d149168abe0b8c09af056ce50f48b6
This commit is contained in:
levlam 2018-09-11 22:17:01 +03:00
parent fd96a6722d
commit 2f90c47d03
2 changed files with 16 additions and 2 deletions

View File

@ -50,7 +50,7 @@ Result<std::pair<FileFd, string>> mkstemp(CSlice dir) TD_WARN_UNUSED_RESULT;
Result<string> mkdtemp(CSlice dir, Slice prefix) TD_WARN_UNUSED_RESULT;
template <class Func>
Status walk_path(CSlice path, Func &func) TD_WARN_UNUSED_RESULT;
Status walk_path(CSlice path, Func &&func) TD_WARN_UNUSED_RESULT;
#if TD_PORT_POSIX
@ -221,7 +221,7 @@ Status walk_path(CSlice path, Func &&func) {
path_slice.remove_suffix(1);
wpath.pop_back();
}
return detail::walk_path_dir(wpath.c_str(), func);
return detail::walk_path_dir(wpath.c_str(), std::forward<Func>(func));
}
#endif

View File

@ -16,6 +16,7 @@ using namespace td;
TEST(Port, files) {
CSlice main_dir = "test_dir";
rmrf(main_dir).ignore();
ASSERT_TRUE(FileFd::open(main_dir, FileFd::Write).is_error());
mkdir(main_dir).ensure();
mkdir(PSLICE() << main_dir << TD_DIR_SLASH << "A").ensure();
mkdir(PSLICE() << main_dir << TD_DIR_SLASH << "B").ensure();
@ -24,6 +25,19 @@ TEST(Port, files) {
std::string fd_path = PSTRING() << main_dir << TD_DIR_SLASH << "t.txt";
auto fd = FileFd::open(fd_path, FileFd::Write | FileFd::CreateNew).move_as_ok();
int cnt = 0;
const int ITER_COUNT = 1000;
for (int i = 0; i < ITER_COUNT; i++) {
walk_path(main_dir, [&](CSlice name, bool is_directory) {
if (!is_directory) {
ASSERT_EQ(fd_path, name);
}
cnt++;
});
}
ASSERT_EQ(4 * ITER_COUNT, cnt);
ASSERT_EQ(0u, fd.get_size());
ASSERT_EQ(12u, fd.write("Hello world!").move_as_ok());
ASSERT_EQ(4u, fd.pwrite("abcd", 1).move_as_ok());