Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/libshaderc_util/src/file_finder_test.cc
1560 views
1
// Copyright 2015 The Shaderc Authors. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "libshaderc_util/file_finder.h"
16
17
#include <gtest/gtest.h>
18
19
// We need getcwd
20
#if WIN32
21
#include <direct.h>
22
#else
23
#include <unistd.h>
24
#endif
25
26
#include "death_test.h"
27
28
29
namespace {
30
31
using shaderc_util::FileFinder;
32
33
// Returns the absolute path of the current working directory.
34
std::string GetCurrentDir() {
35
// Provide generous space to write the path.
36
char buf[1000];
37
#if WIN32
38
return _getcwd(buf, sizeof(buf));
39
#else
40
return getcwd(buf, sizeof(buf));
41
#endif
42
}
43
44
class FileFinderTest : public testing::Test {
45
protected:
46
FileFinder finder;
47
// Absolute path of the current working directory.
48
const std::string current_dir = GetCurrentDir();
49
};
50
51
TEST_F(FileFinderTest, PathStartsEmpty) {
52
EXPECT_TRUE(FileFinder().search_path().empty());
53
}
54
55
TEST_F(FileFinderTest, EmptyPath) {
56
finder.search_path().clear();
57
EXPECT_EQ("", finder.FindReadableFilepath("include_file.1"));
58
}
59
60
TEST_F(FileFinderTest, EmptyStringInPath) {
61
finder.search_path() = {""};
62
EXPECT_EQ("include_file.1", finder.FindReadableFilepath("include_file.1"));
63
EXPECT_EQ("dir/subdir/include_file.2",
64
finder.FindReadableFilepath("dir/subdir/include_file.2"));
65
}
66
67
TEST_F(FileFinderTest, SimplePath) {
68
finder.search_path() = {"dir"};
69
EXPECT_EQ("dir/subdir/include_file.2",
70
finder.FindReadableFilepath("subdir/include_file.2"));
71
}
72
73
TEST_F(FileFinderTest, PathEndsInSlash) {
74
finder.search_path() = {"dir/"};
75
EXPECT_EQ("dir/subdir/include_file.2",
76
finder.FindReadableFilepath("subdir/include_file.2"));
77
}
78
79
TEST_F(FileFinderTest, ParentDir) {
80
finder.search_path() = {"dir"};
81
EXPECT_EQ("dir/../include_file.1",
82
finder.FindReadableFilepath("../include_file.1"));
83
}
84
85
TEST_F(FileFinderTest, EntirePathIsActive) {
86
finder.search_path() = {"", "dir/subdir/"};
87
EXPECT_EQ("include_file.1", finder.FindReadableFilepath("include_file.1"));
88
EXPECT_EQ("dir/subdir/include_file.2",
89
finder.FindReadableFilepath("include_file.2"));
90
}
91
92
TEST_F(FileFinderTest, NonExistingFile) {
93
finder.search_path() = {"", "dir/subdir/"};
94
EXPECT_EQ("", finder.FindReadableFilepath("garbage.xyxyxyxyxyxz"));
95
}
96
97
TEST_F(FileFinderTest, FirstHitReturned) {
98
finder.search_path() = {".", "", "dir/../"};
99
EXPECT_EQ("./include_file.1", finder.FindReadableFilepath("include_file.1"));
100
}
101
102
TEST_F(FileFinderTest, IrrelevantPaths) {
103
finder.search_path() = {".", "garbage.xyxyxyxyxyz", "dir/../"};
104
EXPECT_EQ("", finder.FindReadableFilepath("include_file.2"));
105
finder.search_path().push_back("dir/subdir");
106
EXPECT_EQ("dir/subdir/include_file.2",
107
finder.FindReadableFilepath("include_file.2"));
108
}
109
110
TEST_F(FileFinderTest, CurrentDirectory) {
111
ASSERT_GE(current_dir.size(), 0u);
112
// Either the directory should start with / (if we are on Linux),
113
// Or it should beither X:/ or X:\ or // (if we are on Windows).
114
ASSERT_TRUE(current_dir.front() == '\\' || current_dir.front() == '/' ||
115
(current_dir.size() >= 3u && current_dir[1] == ':' &&
116
(current_dir[2] == '\\' || current_dir[2] == '/')));
117
}
118
119
TEST_F(FileFinderTest, AbsolutePath) {
120
ASSERT_NE('/', current_dir.back());
121
finder.search_path() = {current_dir};
122
EXPECT_EQ(current_dir + "/include_file.1",
123
finder.FindReadableFilepath("include_file.1"));
124
EXPECT_EQ(current_dir + "/dir/subdir/include_file.2",
125
finder.FindReadableFilepath("dir/subdir/include_file.2"));
126
}
127
128
TEST_F(FileFinderTest, AbsoluteFilename) {
129
ASSERT_NE('/', current_dir.back());
130
131
finder.search_path() = {""};
132
const std::string absolute_file1 = current_dir + "/include_file.1";
133
EXPECT_EQ(absolute_file1, finder.FindReadableFilepath(absolute_file1));
134
EXPECT_EQ("", finder.FindReadableFilepath("/dir/subdir/include_file.2"));
135
136
finder.search_path().push_back(".");
137
EXPECT_EQ(".//dir/subdir/include_file.2",
138
finder.FindReadableFilepath("/dir/subdir/include_file.2"));
139
}
140
141
TEST(FileFinderDeathTest, EmptyFilename) {
142
EXPECT_DEBUG_DEATH_IF_SUPPORTED(FileFinder().FindReadableFilepath(""),
143
"Assertion");
144
}
145
146
} // anonymous namespace
147
148