summaryrefslogtreecommitdiff
path: root/tests/auto/qdoc/qdoc/filesystem/catch_fileresolver.cpp
blob: 440de7bee1db0363929eed258ce2422f248d0f71 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// 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/catch.hpp>

#include <filesystem/fileresolver.h>

#include <qdoc_catch_generators.h>

#include <vector>
#include <algorithm>
#include <random>

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

SCENARIO("Inspecting the directories that will be used for searching", "[ResolvingFiles][Directory][Path][Canonicalization][Contents]") {
    GIVEN("Some collection of paths representing existing directories on the filesystem") {
        std::size_t directories_amount = GENERATE(take(10, random(2, 10)));

        std::vector<QTemporaryDir> working_directories(directories_amount);

        std::vector<DirectoryPath> directories;
        directories.reserve(directories_amount);

        std::transform(
            working_directories.begin(), working_directories.end(),
            std::back_inserter(directories),
            [](auto& dir){ return *DirectoryPath::refine(dir.path()); }
        );

        AND_GIVEN("That the collection of those paths is ordered and contains no duplicates") {
            std::sort(directories.begin(), directories.end());
            directories.erase(std::unique(directories.begin(), directories.end()), directories.end());

            WHEN("A mean of searching for files is obtained from that collection") {
                FileResolver file_resolver{std::vector(directories)};

                THEN("The collection of directories that will be used for searching is equivalent to the one from which a mean of searching for files was obtained") {
                    REQUIRE(file_resolver.get_search_directories() == directories);
                }
            }
        }

        AND_GIVEN("That the collection of those paths is potentially unordered but contains no duplicates") {
            std::sort(directories.begin(), directories.end());
            directories.erase(std::unique(directories.begin(), directories.end()), directories.end());

            std::shuffle(directories.begin(), directories.end(), std::mt19937{std::random_device{}()});

            WHEN("A mean of searching for files is obtained from that collection") {
                FileResolver file_resolver{std::vector(directories)};

                THEN("The collection of directories that will be used for searching is equivalent to the one from which a mean of searching for files was obtained if it was sorted") {
                    std::sort(directories.begin(), directories.end());

                    REQUIRE(file_resolver.get_search_directories() == directories);
                }
            }
        }

        AND_GIVEN("That the collection of those paths is ordered but contains duplicates") {
            directories.reserve(directories.size());

            std::transform(
                working_directories.begin(), working_directories.end(),
                std::back_inserter(directories),
                [](auto& dir){ return *DirectoryPath::refine(dir.path()); }
            );

            std::sort(directories.begin(), directories.end());

            WHEN("A mean of searching for files is obtained from that collection") {
                FileResolver file_resolver{std::vector(directories)};

                THEN("The collection of directories that will be used for searching is equivalent to the one from which a mean of searching for files was obtained if it contained no duplicates") {
                    directories.erase(std::unique(directories.begin(), directories.end()), directories.end());

                    REQUIRE(file_resolver.get_search_directories() == directories);
                }
            }
        }

        AND_GIVEN("That the collection of those paths is potentially unordered and contains duplicates") {
            directories.reserve(directories.size());

            std::transform(
                working_directories.begin(), working_directories.end(),
                std::back_inserter(directories),
                [](auto& dir){ return *DirectoryPath::refine(dir.path()); }
            );

            std::shuffle(directories.begin(), directories.end(), std::mt19937{std::random_device{}()});

            WHEN("A mean of searching for files is obtained from that collection") {
                FileResolver file_resolver{std::vector(directories)};

                THEN("The collection of directories that will be used for searching is equivalent to the one from which a mean of searching for files was obtained if it was sorted and it contained no duplicates") {
                    std::sort(directories.begin(), directories.end());
                    directories.erase(std::unique(directories.begin(), directories.end()), directories.end());

                    REQUIRE(file_resolver.get_search_directories() == directories);
                }
            }
        }
    }
}

SCENARIO("Finding a file based on some root search directories", "[ResolvingFiles][File][Path][Validation]") {
    // TODO: Rewrite those tests under a single setup. Be careful of
    // how this is done as Catch will rerun sections only under
    // specific condition such that we may incur in collisions for the
    // path if done incorrectly.
    GIVEN("Some directory on the filesystem") {
        QTemporaryDir working_directory{};
        REQUIRE(working_directory.isValid());

        DirectoryPath directory{*DirectoryPath::refine(working_directory.path())};

        AND_GIVEN("A mean of searching for files based on that directory") {
            FileResolver file_resolver{std::vector{directory}};

            AND_GIVEN("A relative path that does not represent an element on the filesystem that is reachable from that directory") {
                QString relative_path = GENERATE(filter([](auto& path){ return path != "." && path != ".."; }, take(100, qdoc::catch_generators::native_relative_path())));
                CAPTURE(relative_path);

                REQUIRE(!QFileInfo{working_directory.path() + '/' + relative_path}.exists());

                WHEN("The relative path is used as a query to resolve a file") {
                    auto maybe_resolved_file{file_resolver.resolve(relative_path)};

                    THEN("The query cannot be resolved") {
                        REQUIRE(!maybe_resolved_file);
                    }
                }
            }
        }
    }

    GIVEN("Some directory on the filesystem") {
        QTemporaryDir working_directory{};
        REQUIRE(working_directory.isValid());

        DirectoryPath directory{*DirectoryPath::refine(working_directory.path())};

        AND_GIVEN("A mean of searching for files based on that directory") {
            FileResolver file_resolver{std::vector{directory}};

            AND_GIVEN("A relative path that represents an existing directory on the filesystem that is reachable from that directory") {
                QString relative_path = GENERATE(take(100, qdoc::catch_generators::native_relative_directory_path()));
                CAPTURE(relative_path);

                REQUIRE(QDir{working_directory.path()}.mkpath(relative_path));

                WHEN("The relative path is used as a query to resolve a file") {
                    auto maybe_resolved_file{file_resolver.resolve(relative_path)};

                    THEN("The query cannot be resolved") {
                        REQUIRE(!maybe_resolved_file);
                    }
                }
            }
        }
    }


    GIVEN("Some directory on the filesystem") {
        QTemporaryDir working_directory{};
        REQUIRE(working_directory.isValid());

        DirectoryPath directory{*DirectoryPath::refine(working_directory.path())};

        AND_GIVEN("A mean of searching for files based on that directory") {
            FileResolver file_resolver{std::vector{directory}};

            AND_GIVEN("A relative path that represents an existing file on the filesystem that is reachable from that directory") {
                QString relative_path = GENERATE(take(100, qdoc::catch_generators::native_relative_file_path()));
                CAPTURE(relative_path);

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

                WHEN("The relative path is used as a query to resolve a file") {
                    auto maybe_resolved_file{file_resolver.resolve(relative_path)};

                    THEN("The query can be resolved") {
                        REQUIRE(maybe_resolved_file);
                    }
                }
            }
        }
    }

    GIVEN("Some directories on the filesystem") {
        std::size_t directories_amount = GENERATE(take(10, random(2, 10)));

        std::vector<QTemporaryDir> working_directories(directories_amount);
        REQUIRE(std::all_of(working_directories.cbegin(), working_directories.cend(), [](auto& dir){ return dir.isValid(); }));

        std::vector<DirectoryPath> directories;
        directories.reserve(directories_amount);

        std::transform(
            working_directories.begin(), working_directories.end(),
            std::back_inserter(directories),
            [](auto& dir){ return *DirectoryPath::refine(dir.path()); }
        );

        AND_GIVEN("A relative path that represents an existing file on the filesystem that is reachable from exactly one of those directories") {
            QString relative_path = GENERATE(take(10, qdoc::catch_generators::native_relative_file_path()));
            CAPTURE(relative_path);

            std::size_t containing_directory_index = GENERATE_COPY(take(1, random(std::size_t{0}, directories_amount - 1)));
            CAPTURE(containing_directory_index);
            CAPTURE(working_directories[containing_directory_index].path());

            REQUIRE(QDir{working_directories[containing_directory_index].path()}.mkpath(QFileInfo{relative_path}.path()));
            REQUIRE(QFile{working_directories[containing_directory_index].path() + "/" + relative_path}.open(QIODeviceBase::ReadWrite | QIODeviceBase::NewOnly));

            AND_GIVEN("A mean of searching for files based on all of those directories") {
                FileResolver file_resolver{std::move(directories)};

                WHEN("The relative path is used as a query to resolve a file") {
                    auto maybe_resolved_file{file_resolver.resolve(relative_path)};

                    THEN("The query can be resolved") {
                        REQUIRE(maybe_resolved_file);
                    }
                }
            }
        }
    }
}

SCENARIO("Inspecting the content of a file that was resolved", "[ResolvingFiles][File][Path][Validation][Contents]") {
    GIVEN("A mean of resolving files based on some directory") {
        QTemporaryDir working_directory{};
        REQUIRE(working_directory.isValid());

        DirectoryPath directory{*DirectoryPath::refine(working_directory.path())};

        FileResolver file_resolver{std::vector{directory}};

        AND_GIVEN("A relative path that represents an existing file on the filesystem that is reachable from that directory") {
            QString relative_path = GENERATE(take(100, qdoc::catch_generators::native_relative_file_path()));
            CAPTURE(relative_path);

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

            WHEN("A file is resolved using that path as a query") {
                auto resolved_file{*file_resolver.resolve(relative_path)};

                THEN("The resolved file contains the query that was used to resolve it") {
                    REQUIRE(resolved_file.get_query() == relative_path);
                }

                THEN("The resolved file contains a canonicalized path pointing to the resolved file") {
                    REQUIRE(resolved_file.get_path() == QFileInfo{working_directory.path() + "/" + relative_path}.canonicalFilePath());
                }
            }
        }
    }
}

TEST_CASE(
    "When a query can be resolved in more than one search directory, it is resolved in the greatest lower bound of the set of directories",
    "[ResolvingFiles][File][Path][Validation][SpecialCase]"
) {
    std::size_t directories_amount = GENERATE(take(10, random(2, 10)));

    QString relative_path = GENERATE(take(10, qdoc::catch_generators::native_relative_file_path()));
    CAPTURE(relative_path);

    std::vector<QTemporaryDir> working_directories(directories_amount);
    REQUIRE(std::all_of(working_directories.cbegin(), working_directories.cend(), [](auto& dir){ return dir.isValid(); }));

    for (const auto& directory : working_directories) {
        CAPTURE(directory.path());
        REQUIRE(QDir{directory.path()}.mkpath(QFileInfo{relative_path}.path()));
        REQUIRE(QFile{directory.path() + "/" + relative_path}.open(QIODeviceBase::ReadWrite | QIODeviceBase::NewOnly));
    }

    std::vector<DirectoryPath> directories;
    directories.reserve(directories_amount);

    std::transform(
        working_directories.begin(), working_directories.end(),
        std::back_inserter(directories),
        [](auto& dir){ return *DirectoryPath::refine(dir.path()); }
    );

    FileResolver file_resolver{std::move(directories)};
    auto resolved_file{*file_resolver.resolve(relative_path)};

    auto& greatest_lower_bound{*std::min_element(file_resolver.get_search_directories().cbegin(), file_resolver.get_search_directories().cend())};

    REQUIRE(
        resolved_file.get_path()
        ==
        QFileInfo{greatest_lower_bound.value() + "/" + relative_path}.canonicalFilePath()
    );
}