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
|
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pathmatch.h"
#include "fixture.h"
#include <string>
#include <utility>
#include <vector>
class TestPathMatch : public TestFixture {
public:
TestPathMatch() : TestFixture("TestPathMatch") {}
private:
const PathMatch emptyMatcher{std::vector<std::string>()};
const PathMatch srcMatcher{std::vector<std::string>(1, "src/")};
const PathMatch fooCppMatcher{std::vector<std::string>(1, "foo.cpp")};
const PathMatch srcFooCppMatcher{std::vector<std::string>(1, "src/foo.cpp")};
void run() override {
TEST_CASE(emptymaskemptyfile);
TEST_CASE(emptymaskpath1);
TEST_CASE(emptymaskpath2);
TEST_CASE(emptymaskpath3);
TEST_CASE(onemaskemptypath);
TEST_CASE(onemasksamepath);
TEST_CASE(onemasksamepathdifferentslash);
TEST_CASE(onemasksamepathdifferentcase);
TEST_CASE(onemasksamepathwithfile);
TEST_CASE(onemaskshorterpath);
TEST_CASE(onemaskdifferentdir1);
TEST_CASE(onemaskdifferentdir2);
TEST_CASE(onemaskdifferentdir3);
TEST_CASE(onemaskdifferentdir4);
TEST_CASE(onemasklongerpath1);
TEST_CASE(onemasklongerpath2);
TEST_CASE(onemasklongerpath3);
TEST_CASE(onemaskcwd);
TEST_CASE(twomasklongerpath1);
TEST_CASE(twomasklongerpath2);
TEST_CASE(twomasklongerpath3);
TEST_CASE(twomasklongerpath4);
TEST_CASE(filemask1);
TEST_CASE(filemaskdifferentcase);
TEST_CASE(filemask2);
TEST_CASE(filemask3);
TEST_CASE(filemaskcwd);
TEST_CASE(filemaskpath1);
TEST_CASE(filemaskpath2);
TEST_CASE(filemaskpath3);
TEST_CASE(filemaskpath4);
TEST_CASE(mixedallmatch);
}
// Test empty PathMatch
void emptymaskemptyfile() const {
ASSERT(!emptyMatcher.match(""));
}
void emptymaskpath1() const {
ASSERT(!emptyMatcher.match("src/"));
}
void emptymaskpath2() const {
ASSERT(!emptyMatcher.match("../src/"));
}
void emptymaskpath3() const {
ASSERT(!emptyMatcher.match("/home/user/code/src/"));
ASSERT(!emptyMatcher.match("d:/home/user/code/src/"));
}
// Test PathMatch containing "src/"
void onemaskemptypath() const {
ASSERT(!srcMatcher.match(""));
}
void onemasksamepath() const {
ASSERT(srcMatcher.match("src/"));
}
void onemasksamepathdifferentslash() const {
const PathMatch srcMatcher2{std::vector<std::string>(1, "src\\")};
ASSERT(srcMatcher2.match("src/"));
}
void onemasksamepathdifferentcase() const {
std::vector<std::string> masks(1, "sRc/");
PathMatch match(std::move(masks), false);
ASSERT(match.match("srC/"));
}
void onemasksamepathwithfile() const {
ASSERT(srcMatcher.match("src/file.txt"));
}
void onemaskshorterpath() const {
const std::string longerExclude("longersrc/");
const std::string shorterToMatch("src/");
ASSERT(shorterToMatch.length() < longerExclude.length());
PathMatch match(std::vector<std::string>(1, longerExclude));
ASSERT(match.match(longerExclude));
ASSERT(!match.match(shorterToMatch));
}
void onemaskdifferentdir1() const {
ASSERT(!srcMatcher.match("srcfiles/file.txt"));
}
void onemaskdifferentdir2() const {
ASSERT(!srcMatcher.match("proj/srcfiles/file.txt"));
}
void onemaskdifferentdir3() const {
ASSERT(!srcMatcher.match("proj/mysrc/file.txt"));
}
void onemaskdifferentdir4() const {
ASSERT(!srcMatcher.match("proj/mysrcfiles/file.txt"));
}
void onemasklongerpath1() const {
ASSERT(srcMatcher.match("/tmp/src/"));
ASSERT(srcMatcher.match("d:/tmp/src/"));
}
void onemasklongerpath2() const {
ASSERT(srcMatcher.match("src/module/"));
}
void onemasklongerpath3() const {
ASSERT(srcMatcher.match("project/src/module/"));
}
void onemaskcwd() const {
ASSERT(!srcMatcher.match("./src"));
}
void twomasklongerpath1() const {
std::vector<std::string> masks = { "src/", "module/" };
PathMatch match(std::move(masks));
ASSERT(!match.match("project/"));
}
void twomasklongerpath2() const {
std::vector<std::string> masks = { "src/", "module/" };
PathMatch match(std::move(masks));
ASSERT(match.match("project/src/"));
}
void twomasklongerpath3() const {
std::vector<std::string> masks = { "src/", "module/" };
PathMatch match(std::move(masks));
ASSERT(match.match("project/module/"));
}
void twomasklongerpath4() const {
std::vector<std::string> masks = { "src/", "module/" };
PathMatch match(std::move(masks));
ASSERT(match.match("project/src/module/"));
}
// Test PathMatch containing "foo.cpp"
void filemask1() const {
ASSERT(fooCppMatcher.match("foo.cpp"));
}
void filemaskdifferentcase() const {
std::vector<std::string> masks(1, "foo.cPp");
PathMatch match(std::move(masks), false);
ASSERT(match.match("fOo.cpp"));
}
void filemask2() const {
ASSERT(fooCppMatcher.match("../foo.cpp"));
}
void filemask3() const {
ASSERT(fooCppMatcher.match("src/foo.cpp"));
}
void filemaskcwd() const {
ASSERT(fooCppMatcher.match("./lib/foo.cpp"));
}
// Test PathMatch containing "src/foo.cpp"
void filemaskpath1() const {
ASSERT(srcFooCppMatcher.match("src/foo.cpp"));
}
void filemaskpath2() const {
ASSERT(srcFooCppMatcher.match("proj/src/foo.cpp"));
}
void filemaskpath3() const {
ASSERT(!srcFooCppMatcher.match("foo.cpp"));
}
void filemaskpath4() const {
ASSERT(!srcFooCppMatcher.match("bar/foo.cpp"));
}
void mixedallmatch() const { // #13570
// when trying to match a directory against a directory entry it erroneously modified a local variable also used for file matching
std::vector<std::string> masks = { "tests/", "file.c" };
PathMatch match(std::move(masks));
ASSERT(match.match("tests/"));
ASSERT(match.match("lib/file.c"));
}
};
REGISTER_TEST(TestPathMatch)
|