Path: blob/main/libshaderc_util/src/file_finder_test.cc
1560 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "libshaderc_util/file_finder.h"1516#include <gtest/gtest.h>1718// We need getcwd19#if WIN3220#include <direct.h>21#else22#include <unistd.h>23#endif2425#include "death_test.h"262728namespace {2930using shaderc_util::FileFinder;3132// Returns the absolute path of the current working directory.33std::string GetCurrentDir() {34// Provide generous space to write the path.35char buf[1000];36#if WIN3237return _getcwd(buf, sizeof(buf));38#else39return getcwd(buf, sizeof(buf));40#endif41}4243class FileFinderTest : public testing::Test {44protected:45FileFinder finder;46// Absolute path of the current working directory.47const std::string current_dir = GetCurrentDir();48};4950TEST_F(FileFinderTest, PathStartsEmpty) {51EXPECT_TRUE(FileFinder().search_path().empty());52}5354TEST_F(FileFinderTest, EmptyPath) {55finder.search_path().clear();56EXPECT_EQ("", finder.FindReadableFilepath("include_file.1"));57}5859TEST_F(FileFinderTest, EmptyStringInPath) {60finder.search_path() = {""};61EXPECT_EQ("include_file.1", finder.FindReadableFilepath("include_file.1"));62EXPECT_EQ("dir/subdir/include_file.2",63finder.FindReadableFilepath("dir/subdir/include_file.2"));64}6566TEST_F(FileFinderTest, SimplePath) {67finder.search_path() = {"dir"};68EXPECT_EQ("dir/subdir/include_file.2",69finder.FindReadableFilepath("subdir/include_file.2"));70}7172TEST_F(FileFinderTest, PathEndsInSlash) {73finder.search_path() = {"dir/"};74EXPECT_EQ("dir/subdir/include_file.2",75finder.FindReadableFilepath("subdir/include_file.2"));76}7778TEST_F(FileFinderTest, ParentDir) {79finder.search_path() = {"dir"};80EXPECT_EQ("dir/../include_file.1",81finder.FindReadableFilepath("../include_file.1"));82}8384TEST_F(FileFinderTest, EntirePathIsActive) {85finder.search_path() = {"", "dir/subdir/"};86EXPECT_EQ("include_file.1", finder.FindReadableFilepath("include_file.1"));87EXPECT_EQ("dir/subdir/include_file.2",88finder.FindReadableFilepath("include_file.2"));89}9091TEST_F(FileFinderTest, NonExistingFile) {92finder.search_path() = {"", "dir/subdir/"};93EXPECT_EQ("", finder.FindReadableFilepath("garbage.xyxyxyxyxyxz"));94}9596TEST_F(FileFinderTest, FirstHitReturned) {97finder.search_path() = {".", "", "dir/../"};98EXPECT_EQ("./include_file.1", finder.FindReadableFilepath("include_file.1"));99}100101TEST_F(FileFinderTest, IrrelevantPaths) {102finder.search_path() = {".", "garbage.xyxyxyxyxyz", "dir/../"};103EXPECT_EQ("", finder.FindReadableFilepath("include_file.2"));104finder.search_path().push_back("dir/subdir");105EXPECT_EQ("dir/subdir/include_file.2",106finder.FindReadableFilepath("include_file.2"));107}108109TEST_F(FileFinderTest, CurrentDirectory) {110ASSERT_GE(current_dir.size(), 0u);111// Either the directory should start with / (if we are on Linux),112// Or it should beither X:/ or X:\ or // (if we are on Windows).113ASSERT_TRUE(current_dir.front() == '\\' || current_dir.front() == '/' ||114(current_dir.size() >= 3u && current_dir[1] == ':' &&115(current_dir[2] == '\\' || current_dir[2] == '/')));116}117118TEST_F(FileFinderTest, AbsolutePath) {119ASSERT_NE('/', current_dir.back());120finder.search_path() = {current_dir};121EXPECT_EQ(current_dir + "/include_file.1",122finder.FindReadableFilepath("include_file.1"));123EXPECT_EQ(current_dir + "/dir/subdir/include_file.2",124finder.FindReadableFilepath("dir/subdir/include_file.2"));125}126127TEST_F(FileFinderTest, AbsoluteFilename) {128ASSERT_NE('/', current_dir.back());129130finder.search_path() = {""};131const std::string absolute_file1 = current_dir + "/include_file.1";132EXPECT_EQ(absolute_file1, finder.FindReadableFilepath(absolute_file1));133EXPECT_EQ("", finder.FindReadableFilepath("/dir/subdir/include_file.2"));134135finder.search_path().push_back(".");136EXPECT_EQ(".//dir/subdir/include_file.2",137finder.FindReadableFilepath("/dir/subdir/include_file.2"));138}139140TEST(FileFinderDeathTest, EmptyFilename) {141EXPECT_DEBUG_DEATH_IF_SUPPORTED(FileFinder().FindReadableFilepath(""),142"Assertion");143}144145} // anonymous namespace146147148