19
19
#include < quick-lint-js/file.h>
20
20
#include < quick-lint-js/have.h>
21
21
#include < quick-lint-js/pipe.h>
22
+ #include < quick-lint-js/sloppy-result.h>
22
23
#include < quick-lint-js/string-view.h>
23
24
#include < quick-lint-js/temporary-directory.h>
24
25
#include < quick-lint-js/unreachable.h>
@@ -77,18 +78,20 @@ TEST_F(test_file, read_regular_file) {
77
78
std::string temp_file_path = this ->make_temporary_directory () + " /temp.js" ;
78
79
write_file (temp_file_path, u8" hello\n world!\n " );
79
80
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\n world!\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\n world!\n " ));
83
85
}
84
86
85
87
TEST_F (test_file, read_empty_regular_file) {
86
88
std::string temp_file_path = this ->make_temporary_directory () + " /temp.js" ;
87
89
write_file (temp_file_path, u8" " );
88
90
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" " ));
92
95
}
93
96
94
97
TEST_F (test_file, read_non_existing_file) {
@@ -103,6 +106,18 @@ TEST_F(test_file, read_non_existing_file) {
103
106
AnyOf (HasSubstr (" No such file" ), HasSubstr (" cannot find" )));
104
107
}
105
108
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
+
106
121
TEST_F (test_file, read_directory) {
107
122
std::string temp_file_path = this ->make_temporary_directory ();
108
123
@@ -118,6 +133,21 @@ TEST_F(test_file, read_directory) {
118
133
));
119
134
}
120
135
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
+
121
151
#if QLJS_HAVE_MKFIFO
122
152
TEST_F (test_file, read_fifo) {
123
153
std::string temp_file_path = this ->make_temporary_directory () + " /fifo.js" ;
@@ -126,9 +156,10 @@ TEST_F(test_file, read_fifo) {
126
156
std::thread writer_thread (
127
157
[&]() { write_file (temp_file_path, u8" hello from fifo" ); });
128
158
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" ));
132
163
133
164
writer_thread.join ();
134
165
}
@@ -139,9 +170,10 @@ TEST_F(test_file, read_empty_fifo) {
139
170
140
171
std::thread writer_thread ([&]() { write_file (temp_file_path, u8" " ); });
141
172
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" " ));
145
177
146
178
writer_thread.join ();
147
179
}
@@ -171,9 +203,10 @@ TEST_F(test_file, read_fifo_multiple_writes) {
171
203
}
172
204
});
173
205
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" ));
177
210
178
211
writer_thread.join ();
179
212
}
@@ -200,9 +233,10 @@ TEST_F(test_file, read_pipe_multiple_writes) {
200
233
pipe .writer .close ();
201
234
});
202
235
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" ));
206
240
207
241
writer_thread.join ();
208
242
}
@@ -233,9 +267,10 @@ TEST_F(test_file, read_pipe_empty_writes) {
233
267
pipe .writer .close ();
234
268
});
235
269
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" ));
239
274
240
275
writer_thread.join ();
241
276
}
0 commit comments