File: Path.cpp

package info (click to toggle)
hyprutils 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 576 kB
  • sloc: cpp: 5,624; sh: 10; makefile: 3
file content (60 lines) | stat: -rw-r--r-- 2,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <hyprutils/path/Path.hpp>

#include <filesystem>
#include <gtest/gtest.h>

using namespace Hyprutils::Path;

static std::string cwd() {
    return std::filesystem::current_path().string();
}

static std::string home() {
    const char* h = getenv("HOME");
    return h ? std::string(h) : std::string{};
}

TEST(Path, resolvePath_absolute) {
    EXPECT_EQ(resolvePath("/foo/bar/baz").value(), "/foo/bar/baz");
    EXPECT_EQ(resolvePath("/foo/./bar/../baz").value(), "/foo/baz");
    EXPECT_EQ(resolvePath("/a/b/../../c").value(), "/c");
    EXPECT_EQ(resolvePath("/a/b/c/./././../d").value(), "/a/b/d");
}

TEST(Path, resolvePath_dot) {
    const std::string base = "/some/base";
    EXPECT_EQ(resolvePath("./here", base).value(), "/some/base/here");
    EXPECT_EQ(resolvePath("./a/b/c", base).value(), "/some/base/a/b/c");
    EXPECT_EQ(resolvePath("./here").value(), cwd() + "/here");
}

TEST(Path, resolvePath_dotdot) {
    const std::string base = "/some/base/dir";
    EXPECT_EQ(resolvePath("../here", base).value(), "/some/base/here");
    EXPECT_EQ(resolvePath("../../here", base).value(), "/some/here");
    EXPECT_EQ(resolvePath("../../../../././.here", base).value(), "/.here");
    EXPECT_EQ(resolvePath("./a/../../b/../c", base).value(), "/some/base/c");
}

TEST(Path, resolvePath_tilde) {
    const std::string h = home();
    if (h.empty())
        GTEST_SKIP() << "HOME not set, skipping ~ tests";

    EXPECT_EQ(resolvePath("~").value(), h);
    EXPECT_EQ(resolvePath("~/").value(), h + "/");
    EXPECT_EQ(resolvePath("~/foo/bar").value(), h + "/foo/bar");
    EXPECT_EQ(resolvePath("~/foo/../bar").value(), h + "/bar");
    EXPECT_EQ(resolvePath("~/./foo/./bar").value(), h + "/foo/bar");
}

TEST(Path, resolvePath_relative_base) {
    EXPECT_EQ(resolvePath("file.txt", "subdir").value(), cwd() + "/subdir/file.txt");
    EXPECT_EQ(resolvePath("../file.txt", "subdir").value(), cwd() + "/file.txt");
}

TEST(Path, resolvePath_errors) {
    EXPECT_FALSE(resolvePath("").has_value());

    EXPECT_FALSE(resolvePath("~nohome").has_value());
}