File indexing completed on 2024-05-19 05:41:59
0001 // ct_lvtclp_clputil.t.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #include <ct_lvtclp_clputil.h> 0021 0022 #include <catch2-local-includes.h> 0023 #include <llvm/Support/GlobPattern.h> 0024 #include <test-project-paths.h> 0025 0026 using namespace Codethink; 0027 0028 TEST_CASE("normalisePath tests") 0029 { 0030 // Common usage cases 0031 REQUIRE(lvtclp::ClpUtil::normalisePath("/home/abc/xxx/project/abc/", "/home/abc/xxx/project") == "abc/"); 0032 REQUIRE(lvtclp::ClpUtil::normalisePath("/home/abc/xxx/project/../project/abc/", "/home/abc/xxx/project") == "abc/"); 0033 REQUIRE(lvtclp::ClpUtil::normalisePath("/home/abc/xxx/project/../abc/", "/home/abc/xxx") == "abc/"); 0034 0035 // Invalid prefix cases 0036 REQUIRE(lvtclp::ClpUtil::normalisePath("/home/abc/xxx/project/abc/", "/different/path/") 0037 == "/home/abc/xxx/project/abc/"); 0038 REQUIRE(lvtclp::ClpUtil::normalisePath("/home/abc/xxx/project/../project/abc/", "/different/path/") 0039 == "/home/abc/xxx/project/abc/"); 0040 0041 // Edge cases 0042 // Trailling '/' on the prefix path was being evaluated as invalid prefix. This test has been added to avoid 0043 // regression. 0044 REQUIRE(lvtclp::ClpUtil::normalisePath("/home/abc/xxx/project/abc/", "/home/abc/xxx/project/") == "abc/"); 0045 } 0046 0047 TEST_CASE("Lakosian rules matching tests") 0048 { 0049 REQUIRE(lvtclp::ClpUtil::isComponentOnPackageGroup("/abc/abcd/abcd_component.cpp")); 0050 REQUIRE(lvtclp::ClpUtil::isComponentOnPackageGroup("/abc/abcd/abcd_component.h")); 0051 REQUIRE(lvtclp::ClpUtil::isComponentOnPackageGroup("/xxx/xxxd/xxxd_component.cpp")); 0052 REQUIRE(lvtclp::ClpUtil::isComponentOnPackageGroup("/abc/abcd+otherstuff/abcd_component.cpp")); 0053 REQUIRE( 0054 lvtclp::ClpUtil::isComponentOnPackageGroup("/some/path/prefixes/doesnt/matter/xxx/xxxd/xxxd_component.cpp")); 0055 REQUIRE_FALSE(lvtclp::ClpUtil::isComponentOnPackageGroup("/xxxy/xxxyd/xxxyd_component.cpp")); 0056 REQUIRE_FALSE(lvtclp::ClpUtil::isComponentOnPackageGroup("/xx/xxd/xxd_component.cpp")); 0057 REQUIRE_FALSE(lvtclp::ClpUtil::isComponentOnPackageGroup("/xxx/randomname/xxx_component.cpp")); 0058 0059 REQUIRE(lvtclp::ClpUtil::isComponentOnStandalonePackage("/s_abcabc/s_abcabc_component.cpp")); 0060 REQUIRE(lvtclp::ClpUtil::isComponentOnStandalonePackage("/abcabc/s_abcabc_component.cpp")); 0061 REQUIRE(lvtclp::ClpUtil::isComponentOnStandalonePackage("/abcabc/ct_abcabc_component.cpp")); 0062 REQUIRE(lvtclp::ClpUtil::isComponentOnStandalonePackage("/abcabc/ct_abcabc_component_with_many_underlines.cpp")); 0063 REQUIRE( 0064 lvtclp::ClpUtil::isComponentOnStandalonePackage("/abcabc/ct_abcabc_component_with____many___underlines.cpp")); 0065 REQUIRE_FALSE(lvtclp::ClpUtil::isComponentOnStandalonePackage("/boost/ct_abcabc_component.cpp")); 0066 } 0067 0068 TEST_CASE("is File Ignored") 0069 { 0070 std::vector<llvm::GlobPattern> patterns; 0071 llvm::Expected<llvm::GlobPattern> res = llvm::GlobPattern::create("moc_*"); 0072 patterns.push_back(res.get()); 0073 0074 REQUIRE(lvtclp::ClpUtil::isFileIgnored("moc_katewaiter.cpp", patterns)); 0075 REQUIRE_FALSE(lvtclp::ClpUtil::isFileIgnored("someotherfile.cpp", patterns)); 0076 0077 res = llvm::GlobPattern::create("abcde*"); 0078 patterns.push_back(res.get()); 0079 0080 REQUIRE(lvtclp::ClpUtil::isFileIgnored("moc_katewaiter.cpp", patterns)); 0081 } 0082 0083 TEST_CASE("valid compile commands json") 0084 { 0085 auto const mockFiles = std::string{TEST_PRJ_PATH} + "/mock_compile_commands_json"; 0086 0087 { 0088 // empty path 0089 lvtclp::CombinedCompilationDatabase compDb; 0090 auto result = compDb.CombinedCompilationDatabase::addCompilationDatabase(""); 0091 REQUIRE(result.has_error()); 0092 REQUIRE(result.error().kind == lvtclp::CompilationDatabaseError::Kind::ErrorLoadingFromFile); 0093 } 0094 0095 { 0096 // file doesn't exist 0097 std::string path = "/no_exist_compile_commands.json"; 0098 lvtclp::CombinedCompilationDatabase compDb; 0099 auto result = compDb.CombinedCompilationDatabase::addCompilationDatabase(mockFiles + path); 0100 REQUIRE(result.has_error()); 0101 REQUIRE(result.error().kind == lvtclp::CompilationDatabaseError::Kind::ErrorLoadingFromFile); 0102 } 0103 0104 { 0105 // exists but empty 0106 std::string path = "/empty/compile_commands.json"; 0107 lvtclp::CombinedCompilationDatabase compDb; 0108 auto result = compDb.CombinedCompilationDatabase::addCompilationDatabase(mockFiles + path); 0109 REQUIRE(result.has_error()); 0110 REQUIRE(result.error().kind == lvtclp::CompilationDatabaseError::Kind::ErrorLoadingFromFile); 0111 } 0112 0113 { 0114 // exists with valid json but no commands or files 0115 std::string path = "/brackets_only/compile_commands.json"; 0116 lvtclp::CombinedCompilationDatabase compDb; 0117 auto result = compDb.CombinedCompilationDatabase::addCompilationDatabase(mockFiles + path); 0118 REQUIRE(result.has_error()); 0119 REQUIRE((result.error().kind == lvtclp::CompilationDatabaseError::Kind::CompileCommandsContainsNoCommands 0120 || result.error().kind == lvtclp::CompilationDatabaseError::Kind::CompileCommandsContainsNoFiles)); 0121 } 0122 }