132 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: can-create-symlinks10// UNSUPPORTED: c++03, c++11, c++1411// UNSUPPORTED: no-filesystem12 13// <filesystem>14 15// bool exists(file_status s) noexcept16// bool exists(path const& p);17// bool exists(path const& p, std::error_code& ec) noexcept;18 19#include <filesystem>20#include <type_traits>21#include <cassert>22 23#include "assert_macros.h"24#include "test_macros.h"25#include "filesystem_test_helper.h"26namespace fs = std::filesystem;27using namespace fs;28 29static void signature_test()30{31 file_status s; ((void)s);32 const path p; ((void)p);33 std::error_code ec; ((void)ec);34 ASSERT_NOEXCEPT(exists(s));35 ASSERT_NOEXCEPT(exists(p, ec));36 ASSERT_NOT_NOEXCEPT(exists(p));37}38 39static void exists_status_test()40{41 struct TestCase {42 file_type type;43 bool expect;44 };45 const TestCase testCases[] = {46 {file_type::none, false},47 {file_type::not_found, false},48 {file_type::regular, true},49 {file_type::directory, true},50 {file_type::symlink, true},51 {file_type::block, true},52 {file_type::character, true},53 {file_type::fifo, true},54 {file_type::socket, true},55 {file_type::unknown, true}56 };57 for (auto& TC : testCases) {58 file_status s(TC.type);59 assert(exists(s) == TC.expect);60 }61}62 63static void test_exist_not_found()64{65 static_test_env static_env;66 const path p = static_env.DNE;67 assert(exists(p) == false);68 69 assert(exists(static_env.Dir) == true);70 assert(exists(static_env.Dir / "dne") == false);71 // Whether <dir>/dne/.. is considered to exist or not is not necessarily72 // something we need to define, but the platform specific behaviour73 // does affect a few other tests, so clarify the root cause here.74#ifdef _WIN3275 assert(exists(static_env.Dir / "dne" / "..") == true);76#else77 assert(exists(static_env.Dir / "dne" / "..") == false);78#endif79 80 std::error_code ec = GetTestEC();81 assert(exists(p, ec) == false);82 assert(!ec);83}84 85static void test_exists_fails()86{87#ifdef _WIN3288 // Windows doesn't support setting perms::none to trigger failures89 // reading directories; test using a special inaccessible directory90 // instead.91 const path p = GetWindowsInaccessibleDir();92 if (p.empty())93 return;94#else95 scoped_test_env env;96 const path dir = env.create_dir("dir");97 const path p = env.create_file("dir/file", 42);98 permissions(dir, perms::none);99#endif100 101 std::error_code ec;102 assert(exists(p, ec) == false);103 assert(ec);104 105 TEST_THROWS_TYPE(filesystem_error, exists(p));106}107 108#ifndef _WIN32109// Checking for the existence of an invalid long path name doesn't110// trigger errors on windows.111static void test_name_too_long() {112 std::string long_name(2500, 'a');113 const path file(long_name);114 115 std::error_code ec;116 assert(exists(file, ec) == false);117 assert(ec);118}119#endif // _WIN32120 121int main(int, char**) {122 signature_test();123 exists_status_test();124 test_exist_not_found();125 test_exists_fails();126#ifndef _WIN32127 test_name_too_long();128#endif129 130 return 0;131}132