Home | History | Annotate | Download | only in fs.op.create_directory
      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 // <experimental/filesystem>
     13 
     14 // bool create_directory(const path& p, const path& attr);
     15 // bool create_directory(const path& p, const path& attr, error_code& ec) noexcept;
     16 
     17 #include <experimental/filesystem>
     18 #include <type_traits>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 #include "rapid-cxx-test.hpp"
     23 #include "filesystem_test_helper.hpp"
     24 
     25 using namespace std::experimental::filesystem;
     26 namespace fs = std::experimental::filesystem;
     27 
     28 TEST_SUITE(filesystem_create_directory_test_suite)
     29 
     30 TEST_CASE(test_signatures)
     31 {
     32     const path p; ((void)p);
     33     std::error_code ec; ((void)ec);
     34     ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p)), bool);
     35     ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p, ec)), bool);
     36     ASSERT_NOT_NOEXCEPT(fs::create_directory(p, p));
     37     ASSERT_NOEXCEPT(fs::create_directory(p, p, ec));
     38 }
     39 
     40 TEST_CASE(create_existing_directory)
     41 {
     42     scoped_test_env env;
     43     const path dir = env.create_dir("dir1");
     44     const path dir2 = env.create_dir("dir2");
     45 
     46     const perms orig_p = status(dir).permissions();
     47     permissions(dir2, perms::none);
     48 
     49     std::error_code ec;
     50     TEST_CHECK(fs::create_directory(dir, dir2, ec) == false);
     51     TEST_CHECK(!ec);
     52 
     53     // Check that the permissions were unchanged
     54     TEST_CHECK(orig_p == status(dir).permissions());
     55 
     56     // Test throwing version
     57     TEST_CHECK(fs::create_directory(dir, dir2) == false);
     58 }
     59 
     60 TEST_CASE(create_directory_one_level)
     61 {
     62     scoped_test_env env;
     63     // Remove setgid which mkdir would inherit
     64     permissions(env.test_root, perms::remove_perms | perms::set_gid);
     65 
     66     const path dir = env.make_env_path("dir1");
     67     const path attr_dir = env.create_dir("dir2");
     68     permissions(attr_dir, perms::none);
     69 
     70     std::error_code ec;
     71     TEST_CHECK(fs::create_directory(dir, attr_dir, ec) == true);
     72     TEST_CHECK(!ec);
     73     TEST_CHECK(is_directory(dir));
     74 
     75     // Check that the new directory has the same permissions as attr_dir
     76     auto st = status(dir);
     77     TEST_CHECK(st.permissions() == perms::none);
     78 }
     79 
     80 TEST_CASE(create_directory_multi_level)
     81 {
     82     scoped_test_env env;
     83     const path dir = env.make_env_path("dir1/dir2");
     84     const path dir1 = env.make_env_path("dir1");
     85     const path attr_dir = env.create_dir("attr_dir");
     86     std::error_code ec;
     87     TEST_CHECK(fs::create_directory(dir, attr_dir, ec) == false);
     88     TEST_CHECK(ec);
     89     TEST_CHECK(!is_directory(dir));
     90     TEST_CHECK(!is_directory(dir1));
     91 }
     92 
     93 TEST_CASE(dest_is_file)
     94 {
     95     scoped_test_env env;
     96     const path file = env.create_file("file", 42);
     97     const path attr_dir = env.create_dir("attr_dir");
     98     std::error_code ec;
     99     TEST_CHECK(fs::create_directory(file, attr_dir, ec) == false);
    100     TEST_CHECK(ec);
    101     TEST_CHECK(is_regular_file(file));
    102 }
    103 
    104 TEST_SUITE_END()
    105