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 // path temp_directory_path(); 15 // path temp_directory_path(error_code& ec); 16 17 #include <experimental/filesystem> 18 #include <memory> 19 #include <cstdlib> 20 #include <cstring> 21 #include <cassert> 22 23 #include "test_macros.h" 24 #include "rapid-cxx-test.hpp" 25 #include "filesystem_test_helper.hpp" 26 27 using namespace std::experimental::filesystem; 28 29 void PutEnv(std::string var, std::string value) { 30 assert(::setenv(var.c_str(), value.c_str(), /* overwrite */ 1) == 0); 31 } 32 33 void UnsetEnv(std::string var) { 34 assert(::unsetenv(var.c_str()) == 0); 35 } 36 37 TEST_SUITE(filesystem_temp_directory_path_test_suite) 38 39 TEST_CASE(signature_test) 40 { 41 std::error_code ec; ((void)ec); 42 ASSERT_NOT_NOEXCEPT(temp_directory_path()); 43 ASSERT_NOT_NOEXCEPT(temp_directory_path(ec)); 44 } 45 46 TEST_CASE(basic_tests) 47 { 48 scoped_test_env env; 49 const path dne = env.make_env_path("dne"); 50 const path file = env.create_file("file", 42); 51 const path dir_perms = env.create_dir("bad_perms_dir"); 52 const path nested_dir = env.create_dir("bad_perms_dir/nested"); 53 permissions(dir_perms, perms::none); 54 const std::error_code expect_ec = std::make_error_code(std::errc::not_a_directory); 55 struct TestCase { 56 std::string name; 57 path p; 58 } cases[] = { 59 {"TMPDIR", env.create_dir("dir1")}, 60 {"TMP", env.create_dir("dir2")}, 61 {"TEMP", env.create_dir("dir3")}, 62 {"TEMPDIR", env.create_dir("dir4")} 63 }; 64 for (auto& TC : cases) { 65 PutEnv(TC.name, TC.p); 66 } 67 for (auto& TC : cases) { 68 std::error_code ec = GetTestEC(); 69 path ret = temp_directory_path(ec); 70 TEST_CHECK(!ec); 71 TEST_CHECK(ret == TC.p); 72 TEST_CHECK(is_directory(ret)); 73 74 // Set the env variable to a path that does not exist and check 75 // that it fails. 76 PutEnv(TC.name, dne); 77 ec = GetTestEC(); 78 ret = temp_directory_path(ec); 79 LIBCPP_ONLY(TEST_CHECK(ec == expect_ec)); 80 TEST_CHECK(ec != GetTestEC()); 81 TEST_CHECK(ec); 82 TEST_CHECK(ret == ""); 83 84 // Set the env variable to point to a file and check that it fails. 85 PutEnv(TC.name, file); 86 ec = GetTestEC(); 87 ret = temp_directory_path(ec); 88 LIBCPP_ONLY(TEST_CHECK(ec == expect_ec)); 89 TEST_CHECK(ec != GetTestEC()); 90 TEST_CHECK(ec); 91 TEST_CHECK(ret == ""); 92 93 // Set the env variable to point to a dir we can't access 94 PutEnv(TC.name, nested_dir); 95 ec = GetTestEC(); 96 ret = temp_directory_path(ec); 97 TEST_CHECK(ec == std::make_error_code(std::errc::permission_denied)); 98 TEST_CHECK(ret == ""); 99 100 // Set the env variable to point to a non-existent dir 101 PutEnv(TC.name, TC.p / "does_not_exist"); 102 ec = GetTestEC(); 103 ret = temp_directory_path(ec); 104 TEST_CHECK(ec != GetTestEC()); 105 TEST_CHECK(ec); 106 TEST_CHECK(ret == ""); 107 108 // Finally erase this env variable 109 UnsetEnv(TC.name); 110 } 111 // No env variables are defined 112 { 113 std::error_code ec = GetTestEC(); 114 path ret = temp_directory_path(ec); 115 TEST_CHECK(!ec); 116 TEST_CHECK(ret == "/tmp"); 117 TEST_CHECK(is_directory(ret)); 118 } 119 } 120 121 TEST_SUITE_END() 122