Skip to content

Commit 78f8afc

Browse files
committed
Replace some uses of read_file with read_file_2
1 parent 273a397 commit 78f8afc

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

test/test-file.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,20 @@ TEST_F(test_file, read_non_existing_file) {
9898
std::string temp_file_path =
9999
this->make_temporary_directory() + "/does-not-exist.js";
100100

101-
read_file_result file_content = read_file(temp_file_path.c_str());
102-
EXPECT_FALSE(file_content.ok());
103-
EXPECT_TRUE(file_content.is_not_found_error);
104-
EXPECT_THAT(file_content.error, HasSubstr("does-not-exist.js"));
105-
EXPECT_THAT(file_content.error,
106-
AnyOf(HasSubstr("No such file"), HasSubstr("cannot find")));
101+
boost::leaf::try_handle_all(
102+
[&]() -> boost::leaf::result<void> {
103+
boost::leaf::result<padded_string> file_content =
104+
read_file_2(temp_file_path.c_str());
105+
if (!file_content) return file_content.error();
106+
ADD_FAILURE() << "read_file_2 should have failed";
107+
return {};
108+
},
109+
make_file_not_found_handler([] {
110+
// Test passed.
111+
}),
112+
make_read_file_error_handlers([](const std::string& message) {
113+
ADD_FAILURE() << "expected file-not-found error, but got: " << message;
114+
}));
107115
}
108116

109117
TEST_F(test_file, read_non_existing_file_sloppy_message) {
@@ -121,16 +129,19 @@ TEST_F(test_file, read_non_existing_file_sloppy_message) {
121129
TEST_F(test_file, read_directory) {
122130
std::string temp_file_path = this->make_temporary_directory();
123131

124-
read_file_result file_content = read_file(temp_file_path.c_str());
125-
EXPECT_FALSE(file_content.ok());
126-
EXPECT_FALSE(file_content.is_not_found_error);
127-
EXPECT_THAT(file_content.error, HasSubstr(temp_file_path));
128-
EXPECT_THAT(
129-
file_content.error,
130-
testing::AnyOf(
131-
HasSubstr("Is a directory"),
132-
HasSubstr("Access is denied") // TODO(strager): Improve this message.
133-
));
132+
boost::leaf::try_handle_all(
133+
[&]() -> boost::leaf::result<void> {
134+
boost::leaf::result<padded_string> file_content =
135+
read_file_2(temp_file_path.c_str());
136+
if (!file_content) return file_content.error();
137+
ADD_FAILURE() << "read_file_2 should have failed";
138+
return {};
139+
},
140+
make_file_not_found_handler(
141+
[] { ADD_FAILURE() << "expected not file-not-found error"; }),
142+
make_read_file_error_handlers([](const std::string&) {
143+
// Test passed.
144+
}));
134145
}
135146

136147
TEST_F(test_file, read_directory_sloppy_message) {

0 commit comments

Comments
 (0)