[llvm] [Support] Report EISDIR when opening a directory (PR #79880)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 00:07:31 PDT 2024


================
@@ -1296,6 +1296,29 @@ TEST_F(FileSystemTest, UTF8ToUTF16DirectoryIteration) {
 }
 #endif
 
+TEST_F(FileSystemTest, OpenDirectoryAsFileForRead) {
+  std::string Buf(5, '?');
+  Expected<fs::file_t> FD = fs::openNativeFileForRead(TestDirectory);
+#ifdef _WIN32
+  ASSERT_EQ(errorToErrorCode(BytesRead.takeError()), errc::is_a_directory);
+#else
+  ASSERT_THAT_EXPECTED(FD, Succeeded());
+  auto Close = make_scope_exit([&] { fs::closeFile(*FD); });
+  Expected<size_t> BytesRead =
+      fs::readNativeFile(*FD, MutableArrayRef(&*Buf.begin(), Buf.size()));
+  ASSERT_EQ(errorToErrorCode(BytesRead.takeError()), errc::is_a_directory);
+#endif
+}
+
+TEST_F(FileSystemTest, OpenDirectoryAsFileForWrite) {
+  int FD;
+  std::error_code EC;
+  EC = fs::openFileForWrite(Twine(TestDirectory), FD);
+  if (!EC)
+    ::close(FD);
+  ASSERT_EQ(EC, errc::is_a_directory);
----------------
jh7370 wrote:

`ASSERT_*` should be used if the condition failing prevents the rest of the test from running. Otherwise, you should prefer the `EXPECT_*` macros (so `EXPECT_EQ` here).

Same goes for your other test too.

https://github.com/llvm/llvm-project/pull/79880


More information about the llvm-commits mailing list