brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 56b4a27 Raw
119 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 remove(const path& p);16// bool remove(const path& p, error_code& ec) noexcept;17 18#include <filesystem>19 20#include "test_macros.h"21#include "filesystem_test_helper.h"22namespace fs = std::filesystem;23using namespace fs;24 25static void test_signatures()26{27    const path p; ((void)p);28    std::error_code ec; ((void)ec);29    ASSERT_SAME_TYPE(decltype(fs::remove(p)), bool);30    ASSERT_SAME_TYPE(decltype(fs::remove(p, ec)), bool);31 32    ASSERT_NOT_NOEXCEPT(fs::remove(p));33    ASSERT_NOEXCEPT(fs::remove(p, ec));34}35 36static void test_error_reporting()37{38    auto checkThrow = [](path const& f, const std::error_code& ec)39    {40#ifndef TEST_HAS_NO_EXCEPTIONS41        try {42            fs::remove(f);43            return false;44        } catch (filesystem_error const& err) {45            return err.path1() == f46                && err.path2() == ""47                && err.code() == ec;48        }49#else50        ((void)f); ((void)ec);51        return true;52#endif53    };54    scoped_test_env env;55    const path non_empty_dir = env.create_dir("dir");56    env.create_file(non_empty_dir / "file1", 42);57    const path bad_perms_dir = env.create_dir("bad_dir");58    const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);59    permissions(bad_perms_dir, perms::none);60    const path testCases[] = {61        non_empty_dir,62#ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE63        // Windows doesn't support setting perms::none on a directory to64        // stop it from being accessed. And a fictional file under65        // GetWindowsInaccessibleDir() doesn't cause fs::remove() to report66        // errors, it just returns false cleanly.67        file_in_bad_dir,68#endif69    };70    for (auto& p : testCases) {71        std::error_code ec;72 73        assert(!fs::remove(p, ec));74        assert(ec);75        assert(checkThrow(p, ec));76    }77 78    // PR#3578079    const path testCasesNonexistant[] = {80        "",81        env.make_env_path("dne")82    };83 84    for (auto& p : testCasesNonexistant) {85        std::error_code ec;86 87        assert(!fs::remove(p, ec));88        assert(!ec);89    }90}91 92static void basic_remove_test()93{94    scoped_test_env env;95    const path dne = env.make_env_path("dne");96    const path link = env.create_symlink(dne, "link");97    const path nested_link = env.make_env_path("nested_link");98    create_symlink(link, nested_link);99    const path testCases[] = {100        env.create_file("file", 42),101        env.create_dir("empty_dir"),102        nested_link,103        link104    };105    for (auto& p : testCases) {106        std::error_code ec = std::make_error_code(std::errc::address_in_use);107        assert(remove(p, ec));108        assert(!ec);109        assert(!exists(symlink_status(p)));110    }111}112 113int main(int, char**) {114    test_signatures();115    test_error_reporting();116    basic_remove_test();117    return 0;118}119