create_directory.pass.cpp (3033B)
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 // bool create_directory(const path& p); 15 // bool create_directory(const path& p, error_code& ec) noexcept; 16 // bool create_directory(const path& p, const path& attr); 17 // bool create_directory(const path& p, const path& attr, error_code& ec) noexcept; 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 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 30 using namespace fs; 31 32 fs::perms read_umask() { 33 mode_t old_mask = umask(0); 34 umask(old_mask); // reset the mask to the old value. 35 return static_cast<fs::perms>(old_mask); 36 } 37 38 TEST_SUITE(filesystem_create_directory_test_suite) 39 40 TEST_CASE(test_signatures) 41 { 42 const path p; ((void)p); 43 std::error_code ec; ((void)ec); 44 ASSERT_SAME_TYPE(decltype(fs::create_directory(p)), bool); 45 ASSERT_SAME_TYPE(decltype(fs::create_directory(p, ec)), bool); 46 ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p)), bool); 47 ASSERT_SAME_TYPE(decltype(fs::create_directory(p, p, ec)), bool); 48 ASSERT_NOT_NOEXCEPT(fs::create_directory(p)); 49 ASSERT_NOEXCEPT(fs::create_directory(p, ec)); 50 ASSERT_NOT_NOEXCEPT(fs::create_directory(p, p)); 51 ASSERT_NOEXCEPT(fs::create_directory(p, p, ec)); 52 } 53 54 55 TEST_CASE(create_existing_directory) 56 { 57 scoped_test_env env; 58 const path dir = env.create_dir("dir1"); 59 std::error_code ec; 60 TEST_CHECK(fs::create_directory(dir, ec) == false); 61 TEST_CHECK(!ec); 62 TEST_CHECK(is_directory(dir)); 63 // Test throwing version 64 TEST_CHECK(fs::create_directory(dir) == false); 65 } 66 67 TEST_CASE(create_directory_one_level) 68 { 69 scoped_test_env env; 70 const path dir = env.make_env_path("dir1"); 71 std::error_code ec; 72 TEST_CHECK(fs::create_directory(dir, ec) == true); 73 TEST_CHECK(!ec); 74 TEST_CHECK(is_directory(dir)); 75 76 auto st = status(dir); 77 const perms expect_perms = perms::all & ~(read_umask()); 78 TEST_CHECK((st.permissions() & perms::all) == expect_perms); 79 } 80 81 TEST_CASE(create_directory_multi_level) 82 { 83 scoped_test_env env; 84 const path dir = env.make_env_path("dir1/dir2"); 85 const path dir1 = env.make_env_path("dir1"); 86 std::error_code ec; 87 TEST_CHECK(fs::create_directory(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 std::error_code ec = GetTestEC(); 98 TEST_CHECK(fs::create_directory(file, ec) == false); 99 TEST_CHECK(!ec); 100 TEST_CHECK(is_regular_file(file)); 101 } 102 103 TEST_SUITE_END()