Skip to content

Commit

Permalink
Tolerate glob on non-existent path to return a NotFound error.
Browse files Browse the repository at this point in the history
Also add documentation for the API with tests.

PiperOrigin-RevId: 715402197
  • Loading branch information
xinhaoyuan authored and copybara-github committed Jan 16, 2025
1 parent 7c4fcba commit 6b151a4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ cc_library(
":remote_file",
":test_util",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
],
Expand Down
2 changes: 2 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@ fuzztest_cc_test(
fuzztest::remote_file
fuzztest::test_util
absl::check
absl::status
absl::time
GTest::gmock_main
)
3 changes: 3 additions & 0 deletions common/remote_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ absl::StatusOr<int64_t> RemoteFileGetSize(std::string_view path);

// Finds all files matching `glob` and appends them to `matches`.
//
// Returns Ok when matches were found, or NotFound when no matches were found.
// Otherwise returns a non-NotFound error.
//
// Note: The OSS implementation of this function fails on Windows, Android, and
// Fuchsia. Instead of using this function, consider whether your use case can
// be solved in a more specific way, e.g., by listing files in a directory and
Expand Down
4 changes: 3 additions & 1 deletion common/remote_file_oss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ namespace {

#if defined(FUZZTEST_HAS_OSS_GLOB)
int HandleGlobError(const char *epath, int eerrno) {
if (eerrno == ENOENT) return 0;
LOG(FATAL) << "Error while globbing path: " << VV(epath) << VV(eerrno);
return -1;
}
Expand Down Expand Up @@ -379,7 +380,8 @@ absl::Status RemoteGlobMatch(std::string_view glob,
::globfree(&glob_ret);
return absl::OkStatus();
#else
LOG(FATAL) << __func__ << "() is not supported on this platform.";
return absl::UnimplementedError(
absl::StrCat(__func__, "() is not supported on this platform"));
#endif // defined(FUZZTEST_HAS_OSS_GLOB)
}

Expand Down
65 changes: 65 additions & 0 deletions common/remote_file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "./common/logging.h"
Expand Down Expand Up @@ -167,5 +168,69 @@ TEST(RemotePathTouchExistingFile, UpdatesTheLastModifiedTime) {
EXPECT_GT(last_modified_time, start_time);
}

TEST(RemoteGlobMatch, DoesNotRecurseIntoSubdirectories) {
const fs::path temp_dir = GetTestTempDir(test_info_->name());

const std::string file1_path = temp_dir / "file_01";
CreateFileOrDie(file1_path);
const fs::path dir1_path = temp_dir / "dir_01";
fs::create_directories(dir1_path);
const std::string file2_path = dir1_path / "file_02";
CreateFileOrDie(file2_path);

std::vector<std::string> files;
const absl::Status status = RemoteGlobMatch((temp_dir / "*").string(), files);
if (absl::IsUnimplemented(status)) {
GTEST_SKIP()
<< "Skipping RemoteGlobMatch() tests since it is unimplemented: "
<< status;
}
ASSERT_TRUE(status.ok()) << status;
EXPECT_THAT(files, UnorderedElementsAre(file1_path, dir1_path));
}

TEST(RemoteGlobMatch, ReturnsSinglePathWhenGlobIsExistingPath) {
const fs::path temp_dir = GetTestTempDir(test_info_->name());

const std::string file1_path = temp_dir / "file_01";
CreateFileOrDie(file1_path);

std::vector<std::string> files;
const absl::Status status = RemoteGlobMatch(temp_dir.string(), files);
if (absl::IsUnimplemented(status)) {
GTEST_SKIP()
<< "Skipping RemoteGlobMatch() tests since it is unimplemented: "
<< status;
}
ASSERT_TRUE(status.ok()) << status;
EXPECT_THAT(files, UnorderedElementsAre(temp_dir.string()));
}

TEST(RemoteGlobMatch, ReturnsNotFoundErrorWithEmptyVectorWhenGlobMatchNothing) {
const fs::path temp_dir = GetTestTempDir(test_info_->name());
std::vector<std::string> files;
const absl::Status status = RemoteGlobMatch((temp_dir / "*").string(), files);
if (absl::IsUnimplemented(status)) {
GTEST_SKIP()
<< "Skipping RemoteGlobMatch() tests since it is unimplemented: "
<< status;
}
ASSERT_TRUE(absl::IsNotFound(status)) << status;
EXPECT_THAT(files, IsEmpty());
}

TEST(RemoteGlobMatch, ReturnsNotFoundErrorWithEmptyVectorWhenPathDoesNotExist) {
std::vector<std::string> files;
const absl::Status status =
RemoteGlobMatch("/this/file/path/does/not/exist", files);
if (absl::IsUnimplemented(status)) {
GTEST_SKIP()
<< "Skipping RemoteGlobMatch() tests since it is unimplemented: "
<< status;
}
ASSERT_TRUE(absl::IsNotFound(status)) << status;
EXPECT_THAT(files, IsEmpty());
}

} // namespace
} // namespace centipede

0 comments on commit 6b151a4

Please sign in to comment.