Skip to content

Commit d12b277

Browse files
committed
Replace some uses of read_file with read_file_2/read_file_sloppy
Some tests use read_file and inspect is_not_found_error. Leave these tests as-is, duplicating them to also test read_file_sloppy. This commit should not reduce test coverage.
1 parent cd75d2e commit d12b277

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

test/test-file.cpp

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <quick-lint-js/file.h>
2020
#include <quick-lint-js/have.h>
2121
#include <quick-lint-js/pipe.h>
22+
#include <quick-lint-js/sloppy-result.h>
2223
#include <quick-lint-js/string-view.h>
2324
#include <quick-lint-js/temporary-directory.h>
2425
#include <quick-lint-js/unreachable.h>
@@ -77,18 +78,20 @@ TEST_F(test_file, read_regular_file) {
7778
std::string temp_file_path = this->make_temporary_directory() + "/temp.js";
7879
write_file(temp_file_path, u8"hello\nworld!\n");
7980

80-
read_file_result file_content = read_file(temp_file_path.c_str());
81-
EXPECT_TRUE(file_content.ok()) << file_content.error;
82-
EXPECT_EQ(file_content.content, string8_view(u8"hello\nworld!\n"));
81+
sloppy_result<padded_string> file_content =
82+
read_file_sloppy(temp_file_path.c_str());
83+
EXPECT_TRUE(file_content.ok()) << file_content.error();
84+
EXPECT_EQ(*file_content, string8_view(u8"hello\nworld!\n"));
8385
}
8486

8587
TEST_F(test_file, read_empty_regular_file) {
8688
std::string temp_file_path = this->make_temporary_directory() + "/temp.js";
8789
write_file(temp_file_path, u8"");
8890

89-
read_file_result file_content = read_file(temp_file_path.c_str());
90-
EXPECT_TRUE(file_content.ok()) << file_content.error;
91-
EXPECT_EQ(file_content.content, string8_view(u8""));
91+
sloppy_result<padded_string> file_content =
92+
read_file_sloppy(temp_file_path.c_str());
93+
EXPECT_TRUE(file_content.ok()) << file_content.error();
94+
EXPECT_EQ(*file_content, string8_view(u8""));
9295
}
9396

9497
TEST_F(test_file, read_non_existing_file) {
@@ -103,6 +106,18 @@ TEST_F(test_file, read_non_existing_file) {
103106
AnyOf(HasSubstr("No such file"), HasSubstr("cannot find")));
104107
}
105108

109+
TEST_F(test_file, read_non_existing_file_sloppy_message) {
110+
std::string temp_file_path =
111+
this->make_temporary_directory() + "/does-not-exist.js";
112+
113+
sloppy_result<padded_string> file_content =
114+
read_file_sloppy(temp_file_path.c_str());
115+
EXPECT_FALSE(file_content.ok());
116+
EXPECT_THAT(file_content.error(), HasSubstr("does-not-exist.js"));
117+
EXPECT_THAT(file_content.error(),
118+
AnyOf(HasSubstr("No such file"), HasSubstr("cannot find")));
119+
}
120+
106121
TEST_F(test_file, read_directory) {
107122
std::string temp_file_path = this->make_temporary_directory();
108123

@@ -118,6 +133,21 @@ TEST_F(test_file, read_directory) {
118133
));
119134
}
120135

136+
TEST_F(test_file, read_directory_sloppy_message) {
137+
std::string temp_file_path = this->make_temporary_directory();
138+
139+
sloppy_result<padded_string> file_content =
140+
read_file_sloppy(temp_file_path.c_str());
141+
EXPECT_FALSE(file_content.ok());
142+
EXPECT_THAT(file_content.error(), HasSubstr(temp_file_path));
143+
EXPECT_THAT(
144+
file_content.error(),
145+
testing::AnyOf(
146+
HasSubstr("Is a directory"),
147+
HasSubstr("Access is denied") // TODO(strager): Improve this message.
148+
));
149+
}
150+
121151
#if QLJS_HAVE_MKFIFO
122152
TEST_F(test_file, read_fifo) {
123153
std::string temp_file_path = this->make_temporary_directory() + "/fifo.js";
@@ -126,9 +156,10 @@ TEST_F(test_file, read_fifo) {
126156
std::thread writer_thread(
127157
[&]() { write_file(temp_file_path, u8"hello from fifo"); });
128158

129-
read_file_result file_content = read_file(temp_file_path.c_str());
130-
EXPECT_TRUE(file_content.ok()) << file_content.error;
131-
EXPECT_EQ(file_content.content, string8_view(u8"hello from fifo"));
159+
sloppy_result<padded_string> file_content =
160+
read_file_sloppy(temp_file_path.c_str());
161+
EXPECT_TRUE(file_content.ok()) << file_content.error();
162+
EXPECT_EQ(*file_content, string8_view(u8"hello from fifo"));
132163

133164
writer_thread.join();
134165
}
@@ -139,9 +170,10 @@ TEST_F(test_file, read_empty_fifo) {
139170

140171
std::thread writer_thread([&]() { write_file(temp_file_path, u8""); });
141172

142-
read_file_result file_content = read_file(temp_file_path.c_str());
143-
EXPECT_TRUE(file_content.ok()) << file_content.error;
144-
EXPECT_EQ(file_content.content, string8_view(u8""));
173+
sloppy_result<padded_string> file_content =
174+
read_file_sloppy(temp_file_path.c_str());
175+
EXPECT_TRUE(file_content.ok()) << file_content.error();
176+
EXPECT_EQ(*file_content, string8_view(u8""));
145177

146178
writer_thread.join();
147179
}
@@ -171,9 +203,10 @@ TEST_F(test_file, read_fifo_multiple_writes) {
171203
}
172204
});
173205

174-
read_file_result file_content = read_file(temp_file_path.c_str());
175-
EXPECT_TRUE(file_content.ok()) << file_content.error;
176-
EXPECT_EQ(file_content.content, string8_view(u8"hello from fifo"));
206+
sloppy_result<padded_string> file_content =
207+
read_file_sloppy(temp_file_path.c_str());
208+
EXPECT_TRUE(file_content.ok()) << file_content.error();
209+
EXPECT_EQ(*file_content, string8_view(u8"hello from fifo"));
177210

178211
writer_thread.join();
179212
}
@@ -200,9 +233,10 @@ TEST_F(test_file, read_pipe_multiple_writes) {
200233
pipe.writer.close();
201234
});
202235

203-
read_file_result file_content = read_file("<pipe>", pipe.reader.ref());
204-
EXPECT_TRUE(file_content.ok()) << file_content.error;
205-
EXPECT_EQ(file_content.content, string8_view(u8"hello from fifo"));
236+
sloppy_result<padded_string> file_content =
237+
read_file_sloppy("<pipe>", pipe.reader.ref());
238+
EXPECT_TRUE(file_content.ok()) << file_content.error();
239+
EXPECT_EQ(*file_content, string8_view(u8"hello from fifo"));
206240

207241
writer_thread.join();
208242
}
@@ -233,9 +267,10 @@ TEST_F(test_file, read_pipe_empty_writes) {
233267
pipe.writer.close();
234268
});
235269

236-
read_file_result file_content = read_file("<pipe>", pipe.reader.ref());
237-
EXPECT_TRUE(file_content.ok()) << file_content.error;
238-
EXPECT_EQ(file_content.content, string8_view(u8"helloworld"));
270+
sloppy_result<padded_string> file_content =
271+
read_file_sloppy("<pipe>", pipe.reader.ref());
272+
EXPECT_TRUE(file_content.ok()) << file_content.error();
273+
EXPECT_EQ(*file_content, string8_view(u8"helloworld"));
239274

240275
writer_thread.join();
241276
}

0 commit comments

Comments
 (0)