libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

path.pass.cpp (5288B)


      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03
     11 
     12 // <filesystem>
     13 
     14 // class directory_entry
     15 
     16 // explicit directory_entry(const path);
     17 // directory_entry(const path&, error_code& ec);
     18 
     19 #include "filesystem_include.hpp"
     20 #include <type_traits>
     21 #include <cassert>
     22 
     23 #include "test_macros.h"
     24 #include "rapid-cxx-test.hpp"
     25 #include "filesystem_test_helper.hpp"
     26 #include "test_convertible.hpp"
     27 
     28 TEST_SUITE(directory_entry_path_ctor_suite)
     29 
     30 TEST_CASE(path_ctor) {
     31   using namespace fs;
     32   {
     33     static_assert(std::is_constructible<directory_entry, const path&>::value,
     34                   "directory_entry must be constructible from path");
     35     static_assert(
     36         !std::is_nothrow_constructible<directory_entry, const path&>::value,
     37         "directory_entry constructor should not be noexcept");
     38     static_assert(!std::is_convertible<path const&, directory_entry>::value,
     39                   "directory_entry constructor should be explicit");
     40   }
     41   {
     42     const path p("foo/bar/baz");
     43     const directory_entry e(p);
     44     TEST_CHECK(e.path() == p);
     45   }
     46 }
     47 
     48 TEST_CASE(path_ec_ctor) {
     49   using namespace fs;
     50   {
     51     static_assert(
     52         std::is_constructible<directory_entry, const path&,
     53                               std::error_code&>::value,
     54         "directory_entry must be constructible from path and error_code");
     55     static_assert(!std::is_nothrow_constructible<directory_entry, const path&,
     56                                                  std::error_code&>::value,
     57                   "directory_entry constructor should not be noexcept");
     58     static_assert(
     59         test_convertible<directory_entry, const path&, std::error_code&>(),
     60         "directory_entry constructor should not be explicit");
     61   }
     62   {
     63     std::error_code ec = GetTestEC();
     64     const directory_entry e(StaticEnv::File, ec);
     65     TEST_CHECK(e.path() == StaticEnv::File);
     66     TEST_CHECK(!ec);
     67   }
     68   {
     69     const path p("foo/bar/baz");
     70     std::error_code ec = GetTestEC();
     71     const directory_entry e(p, ec);
     72     TEST_CHECK(e.path() == p);
     73     TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
     74   }
     75 }
     76 
     77 TEST_CASE(path_ctor_calls_refresh) {
     78   using namespace fs;
     79   scoped_test_env env;
     80   const path dir = env.create_dir("dir");
     81   const path file = env.create_file("dir/file", 42);
     82   const path sym = env.create_symlink("dir/file", "sym");
     83 
     84   {
     85     directory_entry ent(file);
     86     std::error_code ec = GetTestEC();
     87     directory_entry ent_ec(file, ec);
     88     TEST_CHECK(!ec);
     89 
     90     LIBCPP_ONLY(remove(file));
     91 
     92     TEST_CHECK(ent.exists());
     93     TEST_CHECK(ent_ec.exists());
     94 
     95     TEST_CHECK(ent.file_size() == 42);
     96     TEST_CHECK(ent_ec.file_size() == 42);
     97   }
     98 
     99   env.create_file("dir/file", 101);
    100 
    101   {
    102     directory_entry ent(sym);
    103     std::error_code ec = GetTestEC();
    104     directory_entry ent_ec(sym, ec);
    105     TEST_CHECK(!ec);
    106 
    107     LIBCPP_ONLY(remove(file));
    108     LIBCPP_ONLY(remove(sym));
    109 
    110     TEST_CHECK(ent.is_symlink());
    111     TEST_CHECK(ent_ec.is_symlink());
    112 
    113     TEST_CHECK(ent.is_regular_file());
    114     TEST_CHECK(ent_ec.is_regular_file());
    115 
    116     TEST_CHECK(ent.file_size() == 101);
    117     TEST_CHECK(ent_ec.file_size() == 101);
    118   }
    119 }
    120 
    121 TEST_CASE(path_ctor_dne) {
    122   using namespace fs;
    123 
    124   {
    125     std::error_code ec = GetTestEC();
    126     directory_entry ent(StaticEnv::DNE, ec);
    127     TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
    128     TEST_CHECK(ent.path() == StaticEnv::DNE);
    129   }
    130   // don't report dead symlinks as an error.
    131   {
    132     std::error_code ec = GetTestEC();
    133     directory_entry ent(StaticEnv::BadSymlink, ec);
    134     TEST_CHECK(!ec);
    135     TEST_CHECK(ent.path() == StaticEnv::BadSymlink);
    136   }
    137   // DNE does not cause the constructor to throw
    138   {
    139     directory_entry ent(StaticEnv::DNE);
    140     TEST_CHECK(ent.path() == StaticEnv::DNE);
    141 
    142     directory_entry ent_two(StaticEnv::BadSymlink);
    143     TEST_CHECK(ent_two.path() == StaticEnv::BadSymlink);
    144   }
    145 }
    146 
    147 TEST_CASE(path_ctor_cannot_resolve) {
    148   using namespace fs;
    149   scoped_test_env env;
    150   const path dir = env.create_dir("dir");
    151   const path file = env.create_file("dir/file", 42);
    152   const path file_out_of_dir = env.create_file("file1", 101);
    153   const path sym_out_of_dir = env.create_symlink("dir/file", "sym");
    154   const path sym_in_dir = env.create_symlink("dir/file1", "dir/sym2");
    155   permissions(dir, perms::none);
    156 
    157   {
    158     std::error_code ec = GetTestEC();
    159     directory_entry ent(file, ec);
    160     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
    161     TEST_CHECK(ent.path() == file);
    162   }
    163   {
    164     std::error_code ec = GetTestEC();
    165     directory_entry ent(sym_in_dir, ec);
    166     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
    167     TEST_CHECK(ent.path() == sym_in_dir);
    168   }
    169   {
    170     std::error_code ec = GetTestEC();
    171     directory_entry ent(sym_out_of_dir, ec);
    172     TEST_CHECK(!ec);
    173     TEST_CHECK(ent.path() == sym_out_of_dir);
    174   }
    175   {
    176     TEST_CHECK_NO_THROW(directory_entry(file));
    177     TEST_CHECK_NO_THROW(directory_entry(sym_in_dir));
    178     TEST_CHECK_NO_THROW(directory_entry(sym_out_of_dir));
    179   }
    180 }
    181 
    182 TEST_SUITE_END()