brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 5323f3b Raw
128 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 canonical(const path& p);16// path canonical(const path& p, error_code& ec);17 18#include <filesystem>19#include <type_traits>20#include <cassert>21 22#include "assert_macros.h"23#include "test_macros.h"24#include "filesystem_test_helper.h"25#include "../../class.path/path_helper.h"26namespace fs = std::filesystem;27using namespace fs;28 29static void signature_test()30{31    const path p; ((void)p);32    std::error_code ec; ((void)ec);33    ASSERT_NOT_NOEXCEPT(canonical(p));34    ASSERT_NOT_NOEXCEPT(canonical(p, ec));35}36 37// There are 4 cases is the proposal for absolute path.38// Each scope tests one of the cases.39static void test_canonical()40{41    static_test_env static_env;42    CWDGuard guard;43    // has_root_name() && has_root_directory()44    const path Root = static_env.Root;45    const path RootName = Root.filename();46    const path DirName = static_env.Dir.filename();47    const path SymlinkName = static_env.SymlinkToFile.filename();48    struct TestCase {49        path p;50        path expect;51        path base;52        TestCase(path p1, path e, path b)53            : p(p1), expect(e), base(b) {}54    };55    const TestCase testCases[] = {56        { ".", Root, Root },57        { DirName / ".." / "." / DirName, static_env.Dir, Root },58        { static_env.Dir2 / "..",    static_env.Dir, Root },59        { static_env.Dir3 / "../..", static_env.Dir, Root },60        { static_env.Dir / ".",      static_env.Dir, Root },61        { Root / "." / DirName / ".." / DirName, static_env.Dir, Root },62        { path("..") / "." / RootName / DirName / ".." / DirName,63          static_env.Dir,64          Root },65        { static_env.SymlinkToFile,  static_env.File, Root },66        { SymlinkName, static_env.File, Root}67    };68    for (auto& TC : testCases) {69        std::error_code ec = GetTestEC();70        fs::current_path(TC.base);71        const path ret = canonical(TC.p, ec);72        assert(!ec);73        const path ret2 = canonical(TC.p);74        assert(PathEq(ret, TC.expect));75        assert(PathEq(ret, ret2));76        assert(ret.is_absolute());77    }78}79 80static void test_dne_path()81{82    static_test_env static_env;83    std::error_code ec = GetTestEC();84    {85        const path ret = canonical(static_env.DNE, ec);86        assert(ec != GetTestEC());87        assert(ec);88        assert(ret == path{});89    }90    {91        TEST_THROWS_TYPE(filesystem_error, canonical(static_env.DNE));92    }93}94 95static void test_exception_contains_paths()96{97#ifndef TEST_HAS_NO_EXCEPTIONS98    static_test_env static_env;99    CWDGuard guard;100    const path p = "blabla/dne";101    try {102        (void)canonical(p);103        assert(false);104    } catch (filesystem_error const& err) {105        assert(err.path1() == p);106        // libc++ provides the current path as the second path in the exception107        LIBCPP_ASSERT(err.path2() == current_path());108    }109    fs::current_path(static_env.Dir);110    try {111        (void)canonical(p);112        assert(false);113    } catch (filesystem_error const& err) {114        assert(err.path1() == p);115        LIBCPP_ASSERT(err.path2() == static_env.Dir);116    }117#endif118}119 120int main(int, char**) {121    signature_test();122    test_canonical();123    test_dne_path();124    test_exception_contains_paths();125 126    return 0;127}128