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_directories(const path& p);
15 // bool create_directories(const path& p, error_code& ec) noexcept;
16 
17 #include "filesystem_include.hpp"
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 fs;
26 
27 TEST_SUITE(filesystem_create_directories_test_suite)
28 
TEST_CASE(test_signatures)29 TEST_CASE(test_signatures)
30 {
31     const path p; ((void)p);
32     std::error_code ec; ((void)ec);
33     ASSERT_SAME_TYPE(decltype(fs::create_directories(p)), bool);
34     ASSERT_SAME_TYPE(decltype(fs::create_directories(p, ec)), bool);
35     ASSERT_NOT_NOEXCEPT(fs::create_directories(p));
36     ASSERT_NOT_NOEXCEPT(fs::create_directories(p, ec));
37 }
38 
TEST_CASE(create_existing_directory)39 TEST_CASE(create_existing_directory)
40 {
41     scoped_test_env env;
42     const path dir = env.create_dir("dir1");
43     std::error_code ec;
44     TEST_CHECK(fs::create_directories(dir, ec) == false);
45     TEST_CHECK(!ec);
46     TEST_CHECK(is_directory(dir));
47 }
48 
TEST_CASE(create_directory_one_level)49 TEST_CASE(create_directory_one_level)
50 {
51     scoped_test_env env;
52     const path dir = env.make_env_path("dir1");
53     std::error_code ec;
54     TEST_CHECK(fs::create_directories(dir, ec) == true);
55     TEST_CHECK(!ec);
56     TEST_CHECK(is_directory(dir));
57 }
58 
TEST_CASE(create_directories_multi_level)59 TEST_CASE(create_directories_multi_level)
60 {
61     scoped_test_env env;
62     const path dir = env.make_env_path("dir1/dir2/dir3");
63     std::error_code ec;
64     TEST_CHECK(fs::create_directories(dir, ec) == true);
65     TEST_CHECK(!ec);
66     TEST_CHECK(is_directory(dir));
67 }
68 
TEST_CASE(create_directory_symlinks)69 TEST_CASE(create_directory_symlinks) {
70   scoped_test_env env;
71   const path root = env.create_dir("dir");
72   const path sym_dest_dead = env.make_env_path("dead");
73   const path dead_sym = env.create_symlink(sym_dest_dead, "dir/sym_dir");
74   const path target = env.make_env_path("dir/sym_dir/foo");
75   {
76     std::error_code ec = GetTestEC();
77     TEST_CHECK(create_directories(target, ec) == false);
78     TEST_CHECK(ec);
79     TEST_CHECK(!exists(sym_dest_dead));
80     TEST_CHECK(!exists(dead_sym));
81   }
82 }
83 
84 
TEST_CASE(create_directory_through_symlinks)85 TEST_CASE(create_directory_through_symlinks) {
86   scoped_test_env env;
87   const path root = env.create_dir("dir");
88   const path sym_dir = env.create_symlink(root, "sym_dir");
89   const path target = env.make_env_path("sym_dir/foo");
90   const path resolved_target = env.make_env_path("dir/foo");
91   TEST_REQUIRE(is_directory(sym_dir));
92   {
93     std::error_code ec = GetTestEC();
94     TEST_CHECK(create_directories(target, ec) == true);
95     TEST_CHECK(!ec);
96     TEST_CHECK(is_directory(target));
97     TEST_CHECK(is_directory(resolved_target));
98   }
99 }
100 
101 TEST_SUITE_END()
102