| 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-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// REQUIRES: can-create-symlinks |
| 10 |
// UNSUPPORTED: c++03, c++11, c++14 |
| 11 |
// UNSUPPORTED: no-filesystem |
| 12 |
// UNSUPPORTED: availability-filesystem-missing |
| 13 |
|
| 14 |
// <filesystem> |
| 15 |
|
| 16 |
// path weakly_canonical(const path& p); |
| 17 |
// path weakly_canonical(const path& p, error_code& ec); |
| 18 |
|
| 19 |
#include <filesystem> |
| 20 |
#include <string> |
| 21 |
|
| 22 |
#include "assert_macros.h" |
| 23 |
#include "concat_macros.h" |
| 24 |
#include "test_macros.h" |
| 25 |
#include "test_iterators.h" |
| 26 |
#include "count_new.h" |
| 27 |
#include "filesystem_test_helper.h" |
| 28 |
#include "../../class.path/path_helper.h" |
| 29 |
namespace fs = std::filesystem; |
| 30 |
|
| 31 |
int main(int, char**) { |
| 32 |
static_test_env static_env; |
| 33 |
|
| 34 |
fs::path root = fs::current_path().root_path(); |
| 35 |
// clang-format off |
| 36 |
struct { |
| 37 |
fs::path input; |
| 38 |
fs::path expect; |
| 39 |
} TestCases[] = { |
| 40 |
{"", fs::current_path()}, |
| 41 |
{".", fs::current_path()}, |
| 42 |
{"/", root}, |
| 43 |
{"/foo", root / "foo"}, |
| 44 |
{"/.", root}, |
| 45 |
{"/./", root}, |
| 46 |
{"a/b", fs::current_path() / "a/b"}, |
| 47 |
{"a", fs::current_path() / "a"}, |
| 48 |
{"a/b/", fs::current_path() / "a/b/"}, |
| 49 |
{static_env.File, static_env.File}, |
| 50 |
{static_env.Dir, static_env.Dir}, |
| 51 |
{static_env.SymlinkToDir, static_env.Dir}, |
| 52 |
{static_env.SymlinkToDir / "dir2/.", static_env.Dir / "dir2"}, |
| 53 |
// Note: If the trailing separator occurs in a part of the path that exists, |
| 54 |
// it is omitted. Otherwise it is added to the end of the result. |
| 55 |
// MS STL and libstdc++ behave similarly. |
| 56 |
{static_env.SymlinkToDir / "dir2/./", static_env.Dir / "dir2"}, |
| 57 |
{static_env.SymlinkToDir / "dir2/DNE/./", static_env.Dir / "dir2/DNE/"}, |
| 58 |
{static_env.SymlinkToDir / "dir2", static_env.Dir2}, |
| 59 |
#ifdef _WIN32 |
| 60 |
// On Windows, this path is considered to exist (even though it |
| 61 |
// passes through a nonexistent directory), and thus is returned |
| 62 |
// without a trailing slash, see the note above. |
| 63 |
{static_env.SymlinkToDir / "dir2/../dir2/DNE/..", static_env.Dir2}, |
| 64 |
#else |
| 65 |
{static_env.SymlinkToDir / "dir2/../dir2/DNE/..", static_env.Dir2 / ""}, |
| 66 |
#endif |
| 67 |
{static_env.SymlinkToDir / "dir2/dir3/../DNE/DNE2", static_env.Dir2 / "DNE/DNE2"}, |
| 68 |
{static_env.Dir / "../dir1", static_env.Dir}, |
| 69 |
{static_env.Dir / "./.", static_env.Dir}, |
| 70 |
{static_env.Dir / "DNE/../foo", static_env.Dir / "foo"} |
| 71 |
}; |
| 72 |
// clang-format on |
| 73 |
for (auto& TC : TestCases) { |
| 74 |
fs::path p = TC.input; |
| 75 |
fs::path expect = TC.expect; |
| 76 |
expect.make_preferred(); |
| 77 |
|
| 78 |
{ |
| 79 |
const fs::path output = fs::weakly_canonical(p); |
| 80 |
TEST_REQUIRE(PathEq(output, expect), |
| 81 |
TEST_WRITE_CONCATENATED( |
| 82 |
"Input: ", TC.input.string(), "\nExpected: ", expect.string(), "\nOutput: ", output.string())); |
| 83 |
} |
| 84 |
|
| 85 |
// Test the error_code variant |
| 86 |
{ |
| 87 |
std::error_code ec; |
| 88 |
const fs::path output_c = fs::weakly_canonical(p, ec); |
| 89 |
|
| 90 |
TEST_REQUIRE(PathEq(output_c, expect), |
| 91 |
TEST_WRITE_CONCATENATED( |
| 92 |
"Input: ", TC.input.string(), "\nExpected: ", expect.string(), "\nOutput: ", output_c.string())); |
| 93 |
} |
| 94 |
} |
| 95 |
return 0; |
| 96 |
} |