[libcxx-commits] [libcxx] 529ba61 - [libcxx] [test] Use error_code::default_error_condition to check errors against the expected codes

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 2 23:33:34 PST 2020


Author: Martin Storsjö
Date: 2020-11-03T09:32:52+02:00
New Revision: 529ba612b0774a86f581b68655ad3422075092c6

URL: https://github.com/llvm/llvm-project/commit/529ba612b0774a86f581b68655ad3422075092c6
DIFF: https://github.com/llvm/llvm-project/commit/529ba612b0774a86f581b68655ad3422075092c6.diff

LOG: [libcxx] [test] Use error_code::default_error_condition to check errors against the expected codes

error_code returned from functions might not be of the generic category,
but of the system category, which can have different error code values.
Use default_error_condition() to remap errors to the generic category
where possible, to allow comparing them to the expected values.

Use the ErrorIs() helper instead of a direct comparison against
an excpected value.

Differential Revision: https://reviews.llvm.org/D90602

Added: 
    

Modified: 
    libcxx/test/std/input.output/filesystems/class.rec.dir.itr/rec.dir.itr.members/increment.pass.cpp
    libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp
    libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp
    libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp
    libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp
    libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp
    libcxx/test/support/filesystem_test_helper.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/input.output/filesystems/class.rec.dir.itr/rec.dir.itr.members/increment.pass.cpp b/libcxx/test/std/input.output/filesystems/class.rec.dir.itr/rec.dir.itr.members/increment.pass.cpp
index 60f3c6a7175f..679ab5260b95 100644
--- a/libcxx/test/std/input.output/filesystems/class.rec.dir.itr/rec.dir.itr.members/increment.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/class.rec.dir.itr/rec.dir.itr.members/increment.pass.cpp
@@ -260,8 +260,7 @@ TEST_CASE(test_PR35078)
                 perms::group_exec|perms::owner_exec|perms::others_exec,
                 perm_options::remove);
 
-    const std::error_code eacess_ec =
-        std::make_error_code(std::errc::permission_denied);
+    const std::errc eacess = std::errc::permission_denied;
     std::error_code ec = GetTestEC();
 
     const recursive_directory_iterator endIt;
@@ -287,7 +286,7 @@ TEST_CASE(test_PR35078)
       ec = GetTestEC();
       it.increment(ec);
       TEST_CHECK(ec);
-      TEST_CHECK(ec == eacess_ec);
+      TEST_CHECK(ErrorIs(ec, eacess));
       TEST_CHECK(it == endIt);
     }
     {
@@ -346,8 +345,7 @@ TEST_CASE(test_PR35078_with_symlink)
                 perms::group_exec|perms::owner_exec|perms::others_exec,
                 perm_options::remove);
 
-    const std::error_code eacess_ec =
-        std::make_error_code(std::errc::permission_denied);
+    const std::errc eacess = std::errc::permission_denied;
     std::error_code ec = GetTestEC();
 
     const recursive_directory_iterator endIt;
@@ -397,7 +395,7 @@ TEST_CASE(test_PR35078_with_symlink)
         }
       } else {
         TEST_CHECK(ec);
-        TEST_CHECK(ec == eacess_ec);
+        TEST_CHECK(ErrorIs(ec, eacess));
         TEST_CHECK(it == endIt);
       }
     }
@@ -430,8 +428,7 @@ TEST_CASE(test_PR35078_with_symlink_file)
                 perms::group_exec|perms::owner_exec|perms::others_exec,
                 perm_options::remove);
 
-    const std::error_code eacess_ec =
-        std::make_error_code(std::errc::permission_denied);
+    const std::errc eacess = std::errc::permission_denied;
     std::error_code ec = GetTestEC();
 
     const recursive_directory_iterator EndIt;
@@ -488,7 +485,7 @@ TEST_CASE(test_PR35078_with_symlink_file)
         TEST_CHECK(it == EndIt);
       } else {
         TEST_CHECK(ec);
-        TEST_CHECK(ec == eacess_ec);
+        TEST_CHECK(ErrorIs(ec, eacess));
         TEST_CHECK(it == EndIt);
       }
     }

diff  --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp
index 14fc7a4ac2d9..7f0cc3f39811 100644
--- a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp
@@ -286,14 +286,14 @@ TEST_CASE(test_dir_create_symlink)
     {
         std::error_code ec = GetTestEC();
         fs::copy(dir, dest, copy_options::create_symlinks, ec);
-        TEST_CHECK(ec == std::make_error_code(std::errc::is_a_directory));
+        TEST_CHECK(ErrorIs(ec, std::errc::is_a_directory));
         TEST_CHECK(!exists(dest));
         TEST_CHECK(!is_symlink(dest));
     }
     {
         std::error_code ec = GetTestEC();
         fs::copy(dir, dest, copy_options::create_symlinks|copy_options::recursive, ec);
-        TEST_CHECK(ec == std::make_error_code(std::errc::is_a_directory));
+        TEST_CHECK(ErrorIs(ec, std::errc::is_a_directory));
         TEST_CHECK(!exists(dest));
         TEST_CHECK(!is_symlink(dest));
     }

diff  --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp
index fe4ddee022a6..d7a8c8a82a82 100644
--- a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.permissions/permissions.pass.cpp
@@ -172,7 +172,10 @@ TEST_CASE(test_no_resolve_symlink_on_symlink)
 #endif
         std::error_code ec = GetTestEC();
         permissions(sym, TC.set_perms, TC.opts | perm_options::nofollow, ec);
-        TEST_CHECK(ec == expected_ec);
+        if (expected_ec)
+            TEST_CHECK(ErrorIs(ec, static_cast<std::errc>(expected_ec.value())));
+        else
+            TEST_CHECK(!ec);
         TEST_CHECK(status(file).permissions() == file_perms);
         TEST_CHECK(symlink_status(sym).permissions() == expected_link_perms);
     }

diff  --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp
index 7aece3196d70..8a4e352738e8 100644
--- a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.status/status.pass.cpp
@@ -34,8 +34,7 @@ TEST_CASE(signature_test)
 TEST_CASE(test_status_not_found)
 {
     static_test_env static_env;
-    const std::error_code expect_ec =
-        std::make_error_code(std::errc::no_such_file_or_directory);
+    const std::errc expect_errc = std::errc::no_such_file_or_directory;
     const path cases[] {
         static_env.DNE,
         static_env.BadSymlink
@@ -44,7 +43,7 @@ TEST_CASE(test_status_not_found)
         std::error_code ec = std::make_error_code(std::errc::address_in_use);
         // test non-throwing overload.
         file_status st = status(p, ec);
-        TEST_CHECK(ec == expect_ec);
+        TEST_CHECK(ErrorIs(ec, expect_errc));
         TEST_CHECK(st.type() == file_type::not_found);
         TEST_CHECK(st.permissions() == perms::unknown);
         // test throwing overload. It should not throw even though it reports
@@ -63,27 +62,24 @@ TEST_CASE(test_status_cannot_resolve)
     const path sym = env.create_symlink("dir/file", "sym");
     permissions(dir, perms::none);
 
-    const std::error_code set_ec =
-        std::make_error_code(std::errc::address_in_use);
-    const std::error_code perm_ec =
-        std::make_error_code(std::errc::permission_denied);
-    const std::error_code name_too_long_ec =
-        std::make_error_code(std::errc::filename_too_long);
+    const std::errc set_errc = std::errc::address_in_use;
+    const std::errc perm_errc = std::errc::permission_denied;
+    const std::errc name_too_long_errc = std::errc::filename_too_long;
 
     struct TestCase {
       path p;
-      std::error_code expect_ec;
+      std::errc expect_errc;
     } const TestCases[] = {
-      {file, perm_ec},
-      {sym, perm_ec},
-      {path(std::string(2500, 'a')), name_too_long_ec}
+      {file, perm_errc},
+      {sym, perm_errc},
+      {path(std::string(2500, 'a')), name_too_long_errc}
     };
     for (auto& TC : TestCases)
     {
         { // test non-throwing case
-            std::error_code ec = set_ec;
+            std::error_code ec = std::make_error_code(set_errc);
             file_status st = status(TC.p, ec);
-            TEST_CHECK(ec == TC.expect_ec);
+            TEST_CHECK(ErrorIs(ec, TC.expect_errc));
             TEST_CHECK(st.type() == file_type::none);
             TEST_CHECK(st.permissions() == perms::unknown);
         }
@@ -94,7 +90,7 @@ TEST_CASE(test_status_cannot_resolve)
             } catch (filesystem_error const& err) {
                 TEST_CHECK(err.path1() == TC.p);
                 TEST_CHECK(err.path2() == "");
-                TEST_CHECK(err.code() == TC.expect_ec);
+                TEST_CHECK(ErrorIs(err.code(), TC.expect_errc));
             }
         }
 #endif

diff  --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp
index d2116779ba1f..1e71bf7a01d5 100644
--- a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.symlink_status/symlink_status.pass.cpp
@@ -34,8 +34,7 @@ TEST_CASE(signature_test)
 TEST_CASE(test_symlink_status_not_found)
 {
     static_test_env static_env;
-    const std::error_code expect_ec =
-        std::make_error_code(std::errc::no_such_file_or_directory);
+    const std::errc expect_errc = std::errc::no_such_file_or_directory;
     const path cases[] {
         static_env.DNE
     };
@@ -43,7 +42,7 @@ TEST_CASE(test_symlink_status_not_found)
         std::error_code ec = std::make_error_code(std::errc::address_in_use);
         // test non-throwing overload.
         file_status st = symlink_status(p, ec);
-        TEST_CHECK(ec == expect_ec);
+        TEST_CHECK(ErrorIs(ec, expect_errc));
         TEST_CHECK(st.type() == file_type::not_found);
         TEST_CHECK(st.permissions() == perms::unknown);
         // test throwing overload. It should not throw even though it reports
@@ -63,10 +62,8 @@ TEST_CASE(test_symlink_status_cannot_resolve)
     const path sym_points_in_dir = env.create_symlink("dir/file", "sym");
     permissions(dir, perms::none);
 
-    const std::error_code set_ec =
-        std::make_error_code(std::errc::address_in_use);
-    const std::error_code expect_ec =
-        std::make_error_code(std::errc::permission_denied);
+    const std::errc set_errc = std::errc::address_in_use;
+    const std::errc expect_errc = std::errc::permission_denied;
 
     const path fail_cases[] = {
         file_in_dir, sym_in_dir
@@ -74,9 +71,9 @@ TEST_CASE(test_symlink_status_cannot_resolve)
     for (auto& p : fail_cases)
     {
         { // test non-throwing case
-            std::error_code ec = set_ec;
+            std::error_code ec = std::make_error_code(set_errc);
             file_status st = symlink_status(p, ec);
-            TEST_CHECK(ec == expect_ec);
+            TEST_CHECK(ErrorIs(ec, expect_errc));
             TEST_CHECK(st.type() == file_type::none);
             TEST_CHECK(st.permissions() == perms::unknown);
         }
@@ -87,7 +84,7 @@ TEST_CASE(test_symlink_status_cannot_resolve)
             } catch (filesystem_error const& err) {
                 TEST_CHECK(err.path1() == p);
                 TEST_CHECK(err.path2() == "");
-                TEST_CHECK(err.code() == expect_ec);
+                TEST_CHECK(ErrorIs(err.code(), expect_errc));
             }
         }
 #endif
@@ -95,7 +92,7 @@ TEST_CASE(test_symlink_status_cannot_resolve)
     // Test that a symlink that points into a directory without read perms
     // can be stat-ed using symlink_status
     {
-        std::error_code ec = set_ec;
+        std::error_code ec = std::make_error_code(set_errc);
         file_status st = symlink_status(sym_points_in_dir, ec);
         TEST_CHECK(!ec);
         TEST_CHECK(st.type() == file_type::symlink);

diff  --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp
index a0b30e17128d..c99614267a21 100644
--- a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.temp_dir_path/temp_directory_path.pass.cpp
@@ -50,7 +50,7 @@ TEST_CASE(basic_tests)
     const path dir_perms = env.create_dir("bad_perms_dir");
     const path nested_dir = env.create_dir("bad_perms_dir/nested");
     permissions(dir_perms, perms::none);
-    const std::error_code expect_ec = std::make_error_code(std::errc::not_a_directory);
+    LIBCPP_ONLY(const std::errc expect_errc = std::errc::not_a_directory);
     struct TestCase {
       std::string name;
       path p;
@@ -75,7 +75,7 @@ TEST_CASE(basic_tests)
         PutEnv(TC.name, dne);
         ec = GetTestEC();
         ret = temp_directory_path(ec);
-        LIBCPP_ONLY(TEST_CHECK(ec == expect_ec));
+        LIBCPP_ONLY(TEST_CHECK(ErrorIs(ec, expect_errc)));
         TEST_CHECK(ec != GetTestEC());
         TEST_CHECK(ec);
         TEST_CHECK(ret == "");
@@ -84,7 +84,7 @@ TEST_CASE(basic_tests)
         PutEnv(TC.name, file);
         ec = GetTestEC();
         ret = temp_directory_path(ec);
-        LIBCPP_ONLY(TEST_CHECK(ec == expect_ec));
+        LIBCPP_ONLY(TEST_CHECK(ErrorIs(ec, expect_errc)));
         TEST_CHECK(ec != GetTestEC());
         TEST_CHECK(ec);
         TEST_CHECK(ret == "");
@@ -93,7 +93,7 @@ TEST_CASE(basic_tests)
         PutEnv(TC.name, nested_dir);
         ec = GetTestEC();
         ret = temp_directory_path(ec);
-        TEST_CHECK(ec == std::make_error_code(std::errc::permission_denied));
+        TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
         TEST_CHECK(ret == "");
 
         // Set the env variable to point to a non-existent dir

diff  --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index ce0a0b683174..636eeecb637c 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -560,8 +560,9 @@ inline std::error_code GetTestEC(unsigned Idx = 0) {
 
 inline bool ErrorIsImp(const std::error_code& ec,
                        std::vector<std::errc> const& errors) {
+  std::error_condition cond = ec.default_error_condition();
   for (auto errc : errors) {
-    if (ec == std::make_error_code(errc))
+    if (cond.value() == static_cast<int>(errc))
       return true;
   }
   return false;


        


More information about the libcxx-commits mailing list