FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppcheck/test/testpathmatch.cpp at main · cppcheck-opensource/cppcheck · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cppcheck-opensource
/
cppcheck
Public
Notifications
You must be signed in to change notification settings
Fork
1.6k
Star
6.7k
Code
Pull requests
201
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
cppcheck
/
test
/
testpathmatch.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
313 lines (268 loc) · 10.9 KB
Breadcrumbs
cppcheck
/
test
/
testpathmatch.cpp
Copy path
File metadata and controls
313 lines (268 loc) · 10.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
308
309
310
311
312
313
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 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
<
vector
>
class
TestPathMatch
:
public
TestFixture
{
public:
TestPathMatch
() : TestFixture(
"
TestPathMatch
"
) {}
private:
class
PathMatchTest
final
: public PathMatch
{
friend
class
TestPathMatch
;
};
static
constexpr
auto
unix = PathMatch::Syntax::unix;
static
constexpr
auto
windows = PathMatch::Syntax::windows;
static
constexpr
auto
ifreg = PathMatch::Filemode::regular;
static
constexpr
auto
ifdir = PathMatch::Filemode::directory;
#
ifdef
_WIN32
const
std::string basepath{
"
C:
\\
test
"
};
#
else
const
std::string basepath{
"
/test
"
};
#
endif
const
PathMatch emptyMatcher{{}, basepath};
const
PathMatch srcMatcher{{
"
src/
"
}, basepath};
const
PathMatch fooCppMatcher{{
"
foo.cpp
"
}, basepath};
const
PathMatch srcFooCppMatcher{{
"
src/foo.cpp
"
}, basepath};
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_CASE
(glob1);
TEST_CASE
(glob2);
TEST_CASE
(globstar1);
TEST_CASE
(globstar2);
TEST_CASE
(pathiterator);
}
//
Test empty PathMatch
void
emptymaskemptyfile
()
const
{
ASSERT
(!emptyMatcher.
match
(
"
"
));
}
void
emptymaskpath1
()
const
{
ASSERT
(!emptyMatcher.
match
(
"
src/
"
, ifdir));
}
void
emptymaskpath2
()
const
{
ASSERT
(!emptyMatcher.
match
(
"
../src/
"
, ifdir));
}
void
emptymaskpath3
()
const
{
ASSERT
(!emptyMatcher.
match
(
"
/home/user/code/src/
"
, ifdir));
ASSERT
(!emptyMatcher.
match
(
"
d:/home/user/code/src/
"
, ifdir));
}
//
Test PathMatch containing "src/"
void
onemaskemptypath
()
const
{
ASSERT
(!srcMatcher.
match
(
"
"
));
}
void
onemasksamepath
()
const
{
ASSERT
(srcMatcher.
match
(
"
src/
"
, ifdir));
ASSERT
(!srcMatcher.
match
(
"
src/
"
, ifreg));
}
void
onemasksamepathdifferentslash
()
const
{
PathMatch
srcMatcher2
({
"
src
\\
"
}, basepath, windows);
ASSERT
(srcMatcher2.
match
(
"
src/
"
, ifdir));
ASSERT
(!srcMatcher2.
match
(
"
src/
"
, ifreg));
}
void
onemasksamepathdifferentcase
()
const
{
PathMatch
match
({
"
sRc/
"
}, basepath, windows);
ASSERT
(match.
match
(
"
srC/
"
, ifdir));
ASSERT
(!match.
match
(
"
srC/
"
, ifreg));
}
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
({longerExclude});
ASSERT
(match.
match
(longerExclude, ifdir));
ASSERT
(!match.
match
(shorterToMatch, ifdir));
}
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/
"
, ifdir));
ASSERT
(srcMatcher.
match
(
"
d:/tmp/src/
"
, ifdir));
}
void
onemasklongerpath2
()
const
{
ASSERT
(srcMatcher.
match
(
"
src/module/
"
, ifdir));
}
void
onemasklongerpath3
()
const
{
ASSERT
(srcMatcher.
match
(
"
project/src/module/
"
, ifdir));
}
void
onemaskcwd
()
const
{
ASSERT
(srcMatcher.
match
(
"
./src
"
, ifdir));
}
void
twomasklongerpath1
()
const
{
PathMatch
match
({
"
src/
"
,
"
module/
"
});
ASSERT
(!match.
match
(
"
project/
"
, ifdir));
}
void
twomasklongerpath2
()
const
{
PathMatch
match
({
"
src/
"
,
"
module/
"
});
ASSERT
(match.
match
(
"
project/src/
"
, ifdir));
ASSERT
(!match.
match
(
"
project/src/
"
, ifreg));
}
void
twomasklongerpath3
()
const
{
PathMatch
match
({
"
src/
"
,
"
module/
"
});
ASSERT
(match.
match
(
"
project/module/
"
, ifdir));
ASSERT
(!match.
match
(
"
project/module/
"
, ifreg));
}
void
twomasklongerpath4
()
const
{
PathMatch
match
({
"
src/
"
,
"
module/
"
});
ASSERT
(match.
match
(
"
project/src/module/
"
, ifdir));
ASSERT
(match.
match
(
"
project/src/module/
"
, ifreg));
}
//
Test PathMatch containing "foo.cpp"
void
filemask1
()
const
{
ASSERT
(fooCppMatcher.
match
(
"
foo.cpp
"
));
}
void
filemaskdifferentcase
()
const
{
PathMatch
match
({
"
foo.cPp
"
}, basepath, windows);
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
PathMatch
match
({
"
tests/
"
,
"
file.c
"
});
ASSERT
(match.
match
(
"
tests/
"
, ifdir));
ASSERT
(!match.
match
(
"
tests/
"
, ifreg));
ASSERT
(match.
match
(
"
lib/file.c
"
));
}
void
glob1
()
const
{
PathMatch
match
({
"
test?.cpp
"
});
ASSERT
(match.
match
(
"
test1.cpp
"
));
ASSERT
(match.
match
(
"
src/test1.cpp
"
));
ASSERT
(match.
match
(
"
test1.cpp/src
"
));
ASSERT
(!match.
match
(
"
test1.c
"
));
ASSERT
(!match.
match
(
"
test.cpp
"
));
}
void
glob2
()
const
{
PathMatch
match
({
"
test*.cpp
"
});
ASSERT
(match.
match
(
"
test1.cpp
"
));
ASSERT
(match.
match
(
"
src/test1.cpp
"
));
ASSERT
(match.
match
(
"
test1.cpp/src
"
));
ASSERT
(!match.
match
(
"
test1.c
"
));
ASSERT
(match.
match
(
"
test.cpp
"
));
}
void
globstar1
()
const
{
PathMatch
match
({
"
src/**/foo.c
"
});
ASSERT
(match.
match
(
"
src/lib/foo/foo.c
"
));
ASSERT
(match.
match
(
"
src/lib/foo/bar/foo.c
"
));
ASSERT
(!match.
match
(
"
src/lib/foo/foo.cpp
"
));
ASSERT
(!match.
match
(
"
src/lib/foo/bar/foo.cpp
"
));
ASSERT
(!match.
match
(
"
src/foo.c
"
));
}
void
globstar2
()
const
{
PathMatch
match
({
"
./src/**/foo.c
"
});
ASSERT
(match.
match
(
"
src/lib/foo/foo.c
"
));
ASSERT
(match.
match
(
"
src/lib/foo/bar/foo.c
"
));
ASSERT
(!match.
match
(
"
src/lib/foo/foo.cpp
"
));
ASSERT
(!match.
match
(
"
src/lib/foo/bar/foo.cpp
"
));
ASSERT
(!match.
match
(
"
src/foo.c
"
));
}
void
pathiterator
()
const
{
/*
See https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats
* for information on Windows path syntax.
*/
using
PathIterator = PathMatchTest::PathIterator;
ASSERT_EQUALS
(
"
/
"
,
PathIterator
(
"
/
"
,
nullptr
, unix).
read
());
ASSERT_EQUALS
(
"
/
"
,
PathIterator
(
"
//
"
,
nullptr
, unix).
read
());
ASSERT_EQUALS
(
"
/
"
,
PathIterator
(
"
/
"
,
"
/
"
, unix).
read
());
ASSERT_EQUALS
(
"
/hello/world
"
,
PathIterator
(
"
/hello/universe/.
"
,
"
../world//
"
, unix).
read
());
ASSERT_EQUALS
(
"
/
"
,
PathIterator
(
"
//./..//.///.
"
,
"
../../..///
"
, unix).
read
());
ASSERT_EQUALS
(
"
/foo/bar
"
,
PathIterator
(
nullptr
,
"
/foo/bar/.
"
, unix).
read
());
ASSERT_EQUALS
(
"
/foo/bar
"
,
PathIterator
(
"
/foo/bar/.
"
,
nullptr
, unix).
read
());
ASSERT_EQUALS
(
"
/foo/bar
"
,
PathIterator
(
"
/foo
"
,
"
bar
"
, unix).
read
());
ASSERT_EQUALS
(
"
"
,
PathIterator
(
"
"
,
"
"
, unix).
read
());
ASSERT_EQUALS
(
"
"
,
PathIterator
(
"
"
,
nullptr
, unix).
read
());
ASSERT_EQUALS
(
"
"
,
PathIterator
(
nullptr
,
"
"
, unix).
read
());
ASSERT_EQUALS
(
"
"
,
PathIterator
(
nullptr
,
nullptr
, unix).
read
());
ASSERT_EQUALS
(
"
c:
"
,
PathIterator
(
"
C:
"
,
nullptr
, windows).
read
());
/*
C: without slash is a bit ambiguous. It should probably not be considered a root because it's
* not fully qualified (it designates the current directory on the C drive),
* so this test could be considered to be unspecified behavior.
*/
ASSERT_EQUALS
(
"
c:
"
,
PathIterator
(
"
C:
"
,
"
../..
"
, windows).
read
());
ASSERT_EQUALS
(
"
c:/windows/system32
"
,
PathIterator
(
"
C:
"
,
"
Windows
\\
System32
\\
Drivers
\\
..
\\
.
"
, windows).
read
());
ASSERT_EQUALS
(
"
c:/
"
,
PathIterator
(
"
C:
\\
Program Files
\\
"
,
"
..
"
, windows).
read
());
ASSERT_EQUALS
(
"
//./
"
,
PathIterator
(
"
\\\\
.
\\
C:
\\
"
,
"
../..
"
, windows).
read
());
ASSERT_EQUALS
(
"
//./
"
,
PathIterator
(
"
\\\\
.
\\
"
,
"
..
\\
..
"
, windows).
read
());
ASSERT_EQUALS
(
"
//?/
"
,
PathIterator
(
"
\\\\
?
\\
"
,
"
..
\\
..
"
, windows).
read
());
/*
The server and share should actually be considered part of the root and not be removed
*/
ASSERT_EQUALS
(
"
//
"
,
PathIterator
(
"
\\\\
Server
\\
Share
\\
Directory
"
,
"
../..
\\
../..
"
, windows).
read
());
}
};
REGISTER_TEST
(TestPathMatch)
Back
|
FazBrowse Home
|
New Git URL