brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · af1949f Raw
105 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// path read_symlink(const path& p);16// path read_symlink(const path& p, error_code& ec);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::read_symlink(p)), fs::path);30    ASSERT_SAME_TYPE(decltype(fs::read_symlink(p, ec)), fs::path);31 32    ASSERT_NOT_NOEXCEPT(fs::read_symlink(p));33    // Not noexcept because of narrow contract34    ASSERT_NOT_NOEXCEPT(fs::read_symlink(p, ec));35}36 37static void test_error_reporting()38{39    auto checkThrow = [](path const& f, const std::error_code& ec)40    {41#ifndef TEST_HAS_NO_EXCEPTIONS42        try {43            (void)fs::read_symlink(f);44            return false;45        } catch (filesystem_error const& err) {46            return err.path1() == f47                && err.path2() == ""48                && err.code() == ec;49        }50#else51        ((void)f); ((void)ec);52        return true;53#endif54    };55 56    scoped_test_env env;57    const path cases[] = {58        env.make_env_path("dne"),59        env.create_file("file", 42),60        env.create_dir("dir")61    };62    for (path const& p : cases) {63        std::error_code ec;64        const path ret = fs::read_symlink(p, ec);65        assert(ec);66        assert(ret == path{});67        assert(checkThrow(p, ec));68    }69 70}71 72static void basic_symlink_test()73{74    scoped_test_env env;75    const path dne = env.make_env_path("dne");76    const path file = env.create_file("file", 42);77    const path dir = env.create_dir("dir");78    const path link = env.create_symlink(dne, "link");79    const path nested_link = env.make_env_path("nested_link");80    create_symlink(link, nested_link);81    struct TestCase {82      path symlink;83      path expected;84    } testCases[] = {85        {env.create_symlink(dne, "dne_link"), dne},86        {env.create_symlink(file, "file_link"), file},87        {env.create_directory_symlink(dir, "dir_link"), dir},88        {nested_link, link}89    };90    for (auto& TC : testCases) {91        std::error_code ec = std::make_error_code(std::errc::address_in_use);92        const path ret = read_symlink(TC.symlink, ec);93        assert(!ec);94        assert(ret == TC.expected);95    }96}97 98int main(int, char**) {99    test_signatures();100    test_error_reporting();101    basic_symlink_test();102 103    return 0;104}105