summaryrefslogtreecommitdiff
path: root/tests/auto/qdoc/qdoc/boundaries/filesystem/catch_filepath.cpp
blob: 0c2877a1733a49c78a81fe8603f7ffb5ca64e187 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <qdoc_catch_conversions.h>

#include <catch.hpp>

#include <boundaries/filesystem/filepath.h>

#include <qdoc_catch_generators.h>

#include <QFileInfo>
#include <QTemporaryDir>
#include <QDir>
#include <QIODeviceBase>

SCENARIO("Obtaining a FilePath", "[FilePath][Boundaries][Validation][Canonicalization][Path]") {

    GIVEN("Any string representing a path that does not represent an existing element on the filesystem") {
        QString path = GENERATE(take(100, filter([](auto path){ return !QFileInfo{path}.exists(); }, qdoc::catch_generators::native_path())));
        CAPTURE(path);

        WHEN("A FilePath instance is requested from that string") {
            auto maybe_filepath{FilePath::refine(path)};

            THEN("A FilePath instance is not obtained") {
                REQUIRE(!maybe_filepath);
            }
        }
    }

    GIVEN("Any string representing a path to a directory") {
        QString relative_path = GENERATE(take(100, qdoc::catch_generators::native_relative_directory_path()));
        CAPTURE(relative_path);

        QTemporaryDir working_directory{};
        REQUIRE(working_directory.isValid());

        QString path_to_directory = working_directory.path() + "/" + relative_path;

        AND_GIVEN("That the path represents an existing directory on the filesystem") {
            REQUIRE(QDir{working_directory.path()}.mkpath(relative_path));

            WHEN("A FilePath instance is requested from that string") {
                auto maybe_filepath{FilePath::refine(path_to_directory)};

                THEN("A FilePath instance is not obtained") {
                    REQUIRE(!maybe_filepath);
                }
            }
        }
    }

#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)

    GIVEN("Any string representing a path to a file") {
        QString relative_path = GENERATE(take(100, qdoc::catch_generators::native_relative_file_path()));
        CAPTURE(relative_path);

        QTemporaryDir working_directory{};
        REQUIRE(working_directory.isValid());

        QString path_to_file = working_directory.path() + "/" + relative_path;

        AND_GIVEN("That the path represents an existing file on the filesystem") {
            REQUIRE(QDir{working_directory.path()}.mkpath(QFileInfo{relative_path}.path()));
            REQUIRE(QFile{path_to_file}.open(QIODeviceBase::ReadWrite | QIODeviceBase::NewOnly));

            AND_GIVEN("That the file represented by the path is not readable") {
                REQUIRE(QFile::setPermissions(path_to_file, QFileDevice::WriteOwner  |
                                                           QFileDevice::ExeOwner    |
                                                           QFileDevice::WriteGroup  |
                                                           QFileDevice::ExeGroup    |
                                                           QFileDevice::WriteOther  |
                                                           QFileDevice::ExeOther));

                WHEN("A FilePath instance is requested from that string") {
                    auto maybe_filepath{FilePath::refine(path_to_file)};

                    THEN("A FilePath instance is not obtained") {
                        // REMARK: [temporary_directory_cleanup]
                        CHECK(!maybe_filepath);
                        REQUIRE(QFile::setPermissions(path_to_file, QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser));
                    }
                }
            }

            AND_GIVEN("That the file represented by the path is readable") {
                REQUIRE(QFile::setPermissions(path_to_file, QFileDevice::ReadOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther));

                WHEN("A FilePath instance is requested from that string") {
                    auto maybe_filepath{FilePath::refine(path_to_file)};

                    THEN("A FilePath instance is obtained") {
                        // REMARK: [temporary_directory_cleanup]
                        CHECK(maybe_filepath);
                        REQUIRE(QFile::setPermissions(path_to_file, QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser));
                    }
                }
            }
        }
    }

#endif

}

SCENARIO("Inspecting the contents of a FilePath", "[FilePath][Boundaries][Canonicalization][Path][Contents]") {
    GIVEN("Any string representing a path from which a FilePath instance can be obtained") {
        QString relative_path = GENERATE(take(100, qdoc::catch_generators::native_relative_file_path()));
        CAPTURE(relative_path);

        QTemporaryDir working_directory{};
        REQUIRE(working_directory.isValid());

        QString path_to_file = QFileInfo{working_directory.path() + "/" + relative_path}.filePath();

        REQUIRE(QDir{working_directory.path()}.mkpath(QFileInfo{relative_path}.path()));
        REQUIRE(QFile{path_to_file}.open(QIODeviceBase::ReadWrite | QIODeviceBase::NewOnly));

        AND_GIVEN("A FilePath instance obtained from that path") {
            auto maybe_filepath{FilePath::refine(path_to_file)};
            REQUIRE(maybe_filepath);

            auto filepath{*maybe_filepath};

            WHEN("The path that the FilePath contains is inspected") {
                auto path{filepath.value()};

                THEN("That path is the same as the canonicazlied version of the path that the FilePath was built from") {
                    REQUIRE(path == QFileInfo(path_to_file).canonicalFilePath());
                }
            }
        }
    }
}