File: testfilelister.cpp

package info (click to toggle)
cppcheck 2.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,132 kB
  • sloc: cpp: 268,935; python: 20,890; ansic: 8,090; sh: 1,045; makefile: 1,008; xml: 1,005; cs: 291
file content (203 lines) | stat: -rw-r--r-- 7,546 bytes parent folder | download
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
/*
 * 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 "filelister.h"
#include "filesettings.h"
#include "fixture.h"
#include "path.h"
#include "pathmatch.h"
#include "standards.h"

#include <algorithm>
#include <list>
#include <stdexcept>
#include <string>

class TestFileLister : public TestFixture {
public:
    TestFileLister() : TestFixture("TestFileLister") {}

private:
    void run() override {
        TEST_CASE(recursiveAddFiles);
        TEST_CASE(recursiveAddFilesEmptyPath);
        TEST_CASE(excludeFile1);
        TEST_CASE(excludeFile2);
        TEST_CASE(excludeDir);
        TEST_CASE(addFiles);
    }

    // TODO: generate file list instead
    static std::string findBaseDir() {
        std::string basedir;
        while (!Path::isDirectory(Path::join(basedir, ".github"))) {
            const std::string abspath = Path::getAbsoluteFilePath(basedir);
            basedir += "../";
            // no more going up
            if (Path::getAbsoluteFilePath(basedir) == abspath)
                throw std::runtime_error("could not find repository root directory");
        }
        return basedir;
    }

    void recursiveAddFiles() const {
        const std::string adddir = findBaseDir() + ".";

        // Recursively add add files..
        std::list<FileWithDetails> files;
        std::string err = FileLister::recursiveAddFiles(files, adddir, {}, PathMatch());
        ASSERT_EQUALS("", err);

        ASSERT(!files.empty());

#ifdef _WIN32
        std::string dirprefix;
        if (adddir != ".")
            dirprefix = adddir + "/";
#else
        const std::string dirprefix = adddir + "/";
#endif

        const auto find_file = [&](const std::string& name) {
            return std::find_if(files.cbegin(), files.cend(), [&name](const FileWithDetails& entry) {
                return entry.path() == name;
            });
        };

        // Make sure source files are added..
        auto it = find_file(dirprefix + "cli/main.cpp");
        ASSERT(it != files.end());
        ASSERT_EQUALS_ENUM(Standards::Language::CPP, it->lang());

        it = find_file(dirprefix + "lib/token.cpp");
        ASSERT(it != files.end());
        ASSERT_EQUALS_ENUM(Standards::Language::CPP, it->lang());

        it = find_file(dirprefix + "lib/tokenize.cpp");
        ASSERT(it != files.end());
        ASSERT_EQUALS_ENUM(Standards::Language::CPP, it->lang());

        it = find_file(dirprefix + "gui/main.cpp");
        ASSERT(it != files.end());
        ASSERT_EQUALS_ENUM(Standards::Language::CPP, it->lang());

        it = find_file(dirprefix + "test/testfilelister.cpp");
        ASSERT(it != files.end());
        ASSERT_EQUALS_ENUM(Standards::Language::CPP, it->lang());

        // Make sure headers are not added..
        ASSERT(find_file(dirprefix + "lib/tokenize.h") == files.end());
    }

    void recursiveAddFilesEmptyPath() const {
        std::list<FileWithDetails> files;
        const std::string err = FileLister::recursiveAddFiles(files, "", {}, PathMatch());
        ASSERT_EQUALS("no path specified", err);
    }

    void excludeFile1() const {
        const std::string basedir = findBaseDir();

        std::list<FileWithDetails> files;
        PathMatch matcher({"lib/token.cpp"});
        std::string err = FileLister::recursiveAddFiles(files, basedir + "lib/token.cpp", {}, matcher);
        ASSERT_EQUALS("", err);
        ASSERT(files.empty());
    }

    void excludeFile2() const {
        const std::string basedir = findBaseDir();

        std::list<FileWithDetails> files;
        std::string err = FileLister::recursiveAddFiles(files, basedir + "lib/token.cpp", {}, PathMatch());
        ASSERT_EQUALS("", err);
        ASSERT_EQUALS(1, files.size());
        ASSERT_EQUALS(basedir + "lib/token.cpp", files.begin()->path());
    }

    void excludeDir() const {
        const std::string basedir = findBaseDir() + ".";

        std::list<FileWithDetails> files;
        PathMatch matcher({"lib/"});
        std::string err = FileLister::recursiveAddFiles(files, basedir, {}, matcher);
        ASSERT_EQUALS("", err);
        ASSERT(!files.empty());
        const auto it = std::find_if(files.cbegin(), files.cend(), [](const FileWithDetails& f){
            return f.spath().find("/lib/") != std::string::npos;
        });
        ASSERT(it == files.cend());
    }

    void addFiles() const {
        const std::string adddir = findBaseDir() + ".";

        // TODO: on Windows the prefix is different from when a recursive a folder (see recursiveAddFiles test)
        const std::string dirprefix = adddir + "/";
#ifdef _WIN32
        const std::string dirprefix_nat = Path::toNativeSeparators(dirprefix);
#endif

        std::list<FileWithDetails> files;

        {
            const std::string addfile = Path::join(Path::join(adddir, "cli"), "main.cpp");
            const std::string err = FileLister::addFiles(files, addfile, {}, true,PathMatch());
            ASSERT_EQUALS("", err);
        }
        {
            const std::string addfile = Path::join(Path::join(adddir, "lib"), "token.cpp");
            const std::string err = FileLister::addFiles(files, addfile, {}, true,PathMatch());
            ASSERT_EQUALS("", err);
        }
        {
            const std::string addfile = Path::join(Path::join(adddir, "cli"), "token.cpp"); // does not exist
            const std::string err = FileLister::addFiles(files, addfile, {}, true,PathMatch());
            ASSERT_EQUALS("", err);
        }
        {
            const std::string addfile = Path::join(Path::join(adddir, "lib2"), "token.cpp"); // does not exist
            const std::string err = FileLister::addFiles(files, addfile, {}, true,PathMatch());
            ASSERT_EQUALS("", err);
        }
        {
            const std::string addfile = Path::join(Path::join(adddir, "lib"), "matchcompiler.h");
            const std::string err = FileLister::addFiles(files, addfile, {}, true,PathMatch());
            ASSERT_EQUALS("", err);
        }

        ASSERT_EQUALS(3, files.size());
        auto it = files.cbegin();
        ASSERT_EQUALS(dirprefix + "cli/main.cpp", it->path());
        ASSERT_EQUALS(Path::simplifyPath(dirprefix + "cli/main.cpp"), it->spath());
        ASSERT_EQUALS_ENUM(Standards::Language::None, it->lang());
        it++;
        ASSERT_EQUALS(dirprefix + "lib/token.cpp", it->path());
        ASSERT_EQUALS(Path::simplifyPath(dirprefix + "lib/token.cpp"), it->spath());
        ASSERT_EQUALS_ENUM(Standards::Language::None, it->lang());
        it++;
        ASSERT_EQUALS(dirprefix + "lib/matchcompiler.h", it->path());
        ASSERT_EQUALS(Path::simplifyPath(dirprefix + "lib/matchcompiler.h"), it->spath());
        ASSERT_EQUALS_ENUM(Standards::Language::None, it->lang());
    }

    // TODO: test errors
    // TODO: test wildcards
};

REGISTER_TEST(TestFileLister)