Skip to content

Commit bfba34f

Browse files
turbotoribiocopybara-github
authored andcommitted
* corrects the signature of RmDir in the header and fixes handling of case where directory might not exist to begin with so that it doesn't error out if that's the case
* adds unit test for untested functions (`Stem`, `MkDir`, `Touch`, `Size`, `LoadBinaryFile`, `ListDir`, `Filename`, `Parent`, and `RmDir`.) LiteRT-PiperOrigin-RevId: 826643032
1 parent 99c25b4 commit bfba34f

File tree

3 files changed

+121
-12
lines changed

3 files changed

+121
-12
lines changed

litert/core/filesystem.cc

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <filesystem> // NOLINT
2020
#include <fstream>
2121
#include <string>
22+
#include <system_error> // NOLINT
2223
#include <vector>
2324

2425
#include "absl/strings/str_format.h" // from @com_google_absl
@@ -160,20 +161,29 @@ Expected<std::string> Parent(absl::string_view path) {
160161
}
161162

162163
Expected<void> RmDir(std::string path_to_remove) {
163-
// remove_all recursively deletes the directory and all its contents.
164-
std::uintmax_t count = std::filesystem::remove_all(path_to_remove);
165-
166-
if (count > 0 && IsDir(path_to_remove)) {
167-
LITERT_LOG(LITERT_INFO,
168-
"Successfully removed directory and its contents: %s (%ju "
169-
"items deleted)",
170-
path_to_remove.c_str(), count);
164+
std::error_code error_code;
165+
std::uintmax_t count =
166+
std::filesystem::remove_all(path_to_remove, error_code);
167+
if (error_code) {
168+
return Error(kLiteRtStatusErrorFileIO,
169+
absl::StrFormat("Could not remove: %s, error: %s",
170+
path_to_remove.c_str(), error_code.message()));
171+
}
172+
173+
if (!Exists(path_to_remove)) {
174+
if (count > 0) {
175+
LITERT_LOG(LITERT_INFO,
176+
"Successfully removed directory and its contents: %s (%ju "
177+
"items deleted)",
178+
path_to_remove.c_str(), count);
179+
}
180+
// If count == 0 and it doesn't exist, it means it never existed. Still Ok.
181+
return {};
171182
} else {
172183
return Error(kLiteRtStatusErrorFileIO,
173-
absl::StrFormat("Could not remove or path did not exist: %s",
184+
absl::StrFormat("Could not fully remove: %s",
174185
path_to_remove.c_str()));
175186
}
176-
return {};
177187
}
178188

179189
} // namespace litert::internal

litert/core/filesystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ Expected<void> MkDir(absl::string_view path);
6262
// Get the parent directory of the given path.
6363
Expected<std::string> Parent(absl::string_view path);
6464

65-
Expected<void> RmDir();
65+
Expected<void> RmDir(std::string path_to_remove);
6666

6767
} // namespace litert::internal
6868

6969
#endif // ODML_LITERT_LITERT_CORE_FILESYSTEM_H_
70+

litert/core/filesystem_test.cc

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
// Copyright 2024 Google LLC.
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,19 +15,27 @@
1415

1516
#include "litert/core/filesystem.h"
1617

18+
#include <fstream>
19+
#include <ios>
20+
#include <string>
21+
22+
#include <gmock/gmock.h>
1723
#include <gtest/gtest.h>
1824
#include "absl/strings/str_format.h" // from @com_google_absl
1925
#include "absl/strings/string_view.h" // from @com_google_absl
2026

2127
namespace litert::internal {
2228
namespace {
2329

30+
using ::testing::UnorderedElementsAre;
31+
2432
static constexpr absl::string_view kPrefix = "a/prefix";
2533
static constexpr absl::string_view kInfix = "an/infix";
2634
static constexpr absl::string_view kSuffix = "suffix.ext";
2735
static constexpr absl::string_view kPath = "a/prefix.ext";
2836
static constexpr absl::string_view kStem = "prefix";
2937

38+
3039
TEST(FilesystemTest, JoinTwo) {
3140
const auto path = Join({kPrefix, kSuffix});
3241
EXPECT_EQ(path, absl::StrFormat("%s/%s", kPrefix, kSuffix));
@@ -37,10 +46,99 @@ TEST(FilesystemTest, JoinMany) {
3746
EXPECT_EQ(path, absl::StrFormat("%s/%s/%s", kPrefix, kInfix, kSuffix));
3847
}
3948

40-
TEST(FilesystemTest, Stem){
49+
TEST(FilesystemTest, Stem) {
4150
const auto stem = Stem(kPath);
4251
EXPECT_EQ(stem, kStem);
4352
}
4453

54+
void WriteFile(absl::string_view path, absl::string_view content) {
55+
std::ofstream ofs((std::string(path)), std::ios::binary);
56+
ofs << content;
57+
}
58+
59+
TEST(FilesystemTest, MkDirExistsIsDir) {
60+
const std::string dir = Join({::testing::TempDir(), "test_dir"});
61+
EXPECT_FALSE(Exists(dir));
62+
auto status = MkDir(dir);
63+
ASSERT_TRUE(status);
64+
EXPECT_TRUE(Exists(dir));
65+
EXPECT_TRUE(IsDir(dir));
66+
EXPECT_FALSE(IsDir(Join({dir, "foo"})));
67+
}
68+
69+
TEST(FilesystemTest, TouchExists) {
70+
const std::string file = Join({::testing::TempDir(), "test_file"});
71+
EXPECT_FALSE(Exists(file));
72+
Touch(file);
73+
EXPECT_TRUE(Exists(file));
74+
EXPECT_FALSE(IsDir(file));
75+
}
76+
77+
TEST(FilesystemTest, Size) {
78+
const std::string file = Join({::testing::TempDir(), "test_file_size"});
79+
WriteFile(file, "1234");
80+
auto size = Size(file);
81+
ASSERT_TRUE(size);
82+
EXPECT_EQ(*size, 4);
83+
}
84+
85+
TEST(FilesystemTest, LoadBinaryFile) {
86+
const std::string file = Join({::testing::TempDir(), "test_file_load"});
87+
const std::string content = "12345";
88+
WriteFile(file, content);
89+
auto buffer = LoadBinaryFile(file);
90+
ASSERT_TRUE(buffer);
91+
EXPECT_EQ(buffer->Size(), 5);
92+
EXPECT_EQ(absl::string_view(buffer->StrData(), buffer->Size()), content);
93+
}
94+
95+
TEST(FilesystemTest, ListDir) {
96+
const std::string dir = Join({::testing::TempDir(), "list_dir_test"});
97+
auto status = MkDir(dir);
98+
ASSERT_TRUE(status);
99+
const std::string file1 = Join({dir, "file1.txt"});
100+
const std::string file2 = Join({dir, "file2.txt"});
101+
Touch(file1);
102+
Touch(file2);
103+
auto list = ListDir(dir);
104+
ASSERT_TRUE(list);
105+
EXPECT_THAT(*list, UnorderedElementsAre(file1, file2));
106+
}
107+
108+
TEST(FilesystemTest, Filename) {
109+
const std::string dir = Join({::testing::TempDir(), "filename_test"});
110+
auto status = MkDir(dir);
111+
ASSERT_TRUE(status);
112+
const std::string file = Join({dir, "file1.txt"});
113+
Touch(file);
114+
auto filename = Filename(file);
115+
ASSERT_TRUE(filename);
116+
EXPECT_EQ(*filename, "file1.txt");
117+
}
118+
119+
TEST(FilesystemTest, Parent) {
120+
const std::string dir = Join({::testing::TempDir(), "parent_test"});
121+
auto status = MkDir(dir);
122+
ASSERT_TRUE(status);
123+
const std::string file = Join({dir, "file1.txt"});
124+
Touch(file);
125+
auto parent = Parent(file);
126+
ASSERT_TRUE(parent);
127+
EXPECT_EQ(*parent, dir);
128+
}
129+
130+
TEST(FilesystemTest, RmDir) {
131+
const std::string dir = Join({::testing::TempDir(), "rm_dir_test"});
132+
auto status = MkDir(dir);
133+
ASSERT_TRUE(status);
134+
const std::string file = Join({dir, "file1.txt"});
135+
Touch(file);
136+
EXPECT_TRUE(Exists(dir));
137+
auto rm_status = RmDir(dir);
138+
ASSERT_TRUE(rm_status);
139+
EXPECT_FALSE(Exists(dir));
140+
}
141+
45142
} // namespace
46143
} // namespace litert::internal
144+

0 commit comments

Comments
 (0)