Skip to content

Commit 93401ae

Browse files
kichunyaVladislav Kalugin
and
Vladislav Kalugin
authored
Changes to launch a UTBot on open source projects (libbpf and curl) (#575)
* Changes for running UTbotCpp on libbpf and curl projects. * Skip searching update time of non-existing files. * Don't create return value for ERROR suite * Fix substitute remote path * Fix warning --------- Co-authored-by: Vladislav Kalugin <[email protected]>
1 parent 1bb82af commit 93401ae

File tree

15 files changed

+91
-56
lines changed

15 files changed

+91
-56
lines changed

server/proto/testgen.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ message ProjectContext {
8888
string projectPath = 2;
8989
string testDirPath = 3;
9090
string buildDirRelativePath = 4;
91+
string clientProjectPath = 5;
9192
}
9293

9394
enum ErrorMode {

server/src/ProjectContext.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@ namespace utbot {
88
ProjectContext::ProjectContext(std::string projectName,
99
fs::path projectPath,
1010
fs::path testDirPath,
11-
fs::path buildDirRelativePath)
11+
fs::path buildDirRelativePath,
12+
fs::path clientProjectPath)
1213
: projectName(std::move(projectName)), projectPath(std::move(projectPath)),
1314
testDirPath(std::move(testDirPath)),
14-
buildDirRelativePath(std::move(buildDirRelativePath)) {}
15+
buildDirRelativePath(std::move(buildDirRelativePath)),
16+
clientProjectPath(clientProjectPath) {}
1517

1618
ProjectContext::ProjectContext(const testsgen::ProjectContext &projectContext)
1719
: ProjectContext(projectContext.projectname(),
1820
projectContext.projectpath(),
1921
projectContext.testdirpath(),
20-
projectContext.builddirrelativepath()) {}
22+
projectContext.builddirrelativepath(),
23+
projectContext.clientprojectpath()) {}
2124

2225
ProjectContext::ProjectContext(const testsgen::SnippetRequest &request, fs::path serverBuildDir)
2326
: projectName(request.projectcontext().projectname()),
2427
projectPath(request.projectcontext().projectpath()),
2528
testDirPath(request.projectcontext().testdirpath()),
26-
buildDirRelativePath(request.projectcontext().builddirrelativepath()) { }
29+
buildDirRelativePath(request.projectcontext().builddirrelativepath()),
30+
clientProjectPath(request.projectcontext().clientprojectpath()) {}
2731

2832
fs::path ProjectContext::buildDir() const {
2933
return projectPath / buildDirRelativePath;

server/src/ProjectContext.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class ProjectContext {
1616
ProjectContext(std::string projectName,
1717
fs::path projectPath,
1818
fs::path testDirPath,
19-
fs::path buildDirRelativePath);
19+
fs::path buildDirRelativePath,
20+
fs::path serverBuildDir);
2021

2122
explicit ProjectContext(const testsgen::ProjectContext &projectContext);
2223

@@ -28,6 +29,7 @@ class ProjectContext {
2829
const fs::path projectPath;
2930
const fs::path testDirPath;
3031
const fs::path buildDirRelativePath;
32+
const fs::path clientProjectPath;
3133
};
3234
}
3335

server/src/Synchronizer.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ bool Synchronizer::isProbablyOutdatedStubs(const fs::path &srcFilePath) const {
6464
} catch (...) {
6565
return true;
6666
}
67-
srcTimestamp = Synchronizer::getFileOutdatedTime(srcFilePath);
67+
if (!fs::exists(srcFilePath)) {
68+
srcTimestamp = time(nullptr);
69+
} else {
70+
srcTimestamp = Synchronizer::getFileOutdatedTime(srcFilePath);
71+
}
6872
return stubTimestamp <= srcTimestamp;
6973
}
7074

@@ -75,7 +79,11 @@ bool Synchronizer::isProbablyOutdatedWrappers(const fs::path &srcFilePath) const
7579
}
7680
long long wrapperTimestamp, srcTimestamp;
7781
wrapperTimestamp = Synchronizer::getFileOutdatedTime(wrapperFilePath);
78-
srcTimestamp = Synchronizer::getFileOutdatedTime(srcFilePath);
82+
if (!fs::exists(srcFilePath)) {
83+
srcTimestamp = time(nullptr);
84+
} else {
85+
srcTimestamp = Synchronizer::getFileOutdatedTime(srcFilePath);
86+
}
7987
return wrapperTimestamp <= srcTimestamp;
8088
}
8189

server/src/building/CompileCommand.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,5 @@ namespace utbot {
114114
auto path = Paths::getCCJsonFileFullPath(Paths::replaceExtension(*this->sourcePath, ".o"), this->directory);
115115
this->output = std::next(addFlagsToBegin({"-o", path}));
116116
}
117-
118117
}
119118
}

server/src/building/ProjectBuildDatabse.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,19 @@ void ProjectBuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) {
176176
}
177177
targetInfo->addFile(currentFile);
178178
if (Paths::isObjectFile(currentFile)) {
179-
if (!CollectionUtils::containsKey(objectFileInfos, currentFile)) {
179+
if (!CollectionUtils::containsKey(objectFileInfos, currentFile) &&
180+
!CollectionUtils::containsKey(objectFileInfos,
181+
relative(currentFile, directory))) {
180182
throw CompilationDatabaseException(
181-
"compile_commands.json doesn't contain a command for object file "
182-
+ currentFile.string());
183+
"compile_commands.json doesn't contain a command for object file " +
184+
currentFile.string());
185+
}
186+
if (CollectionUtils::containsKey(objectFileInfos, currentFile)) {
187+
objectFileInfos[currentFile]->linkUnit = output;
188+
} else if (CollectionUtils::containsKey(objectFileInfos,
189+
relative(currentFile, directory))) {
190+
objectFileInfos[relative(currentFile, directory)]->linkUnit = output;
183191
}
184-
objectFileInfos[currentFile]->linkUnit = output;
185192
}
186193
}
187194
targetInfo->commands.emplace_back(command);

server/src/coverage/TestRunner.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ std::vector<UnitTest> TestRunner::getTestsToLaunch() {
108108
}
109109
} else {
110110
if (!StringUtils::endsWith(testFilePath.c_str(), "_test.h") &&
111-
!StringUtils::endsWith(testFilePath.stem().c_str(), "_stub")) {
112-
LOG_S(WARNING)
113-
<< "Found extra file in test directory: " << testFilePath;
111+
!StringUtils::endsWith(testFilePath.stem().c_str(), "_stub") &&
112+
!StringUtils::endsWith(testFilePath.c_str(), "_wrapper.c") &&
113+
!StringUtils::endsWith(testFilePath.c_str(), ".mk")) {
114+
LOG_S(WARNING) << "Found extra file in test directory: " << testFilePath;
114115
}
115116
}
116117
});

server/src/utils/CompilationUtils.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,16 @@ namespace CompilationUtils {
5757
}
5858
}
5959

60-
void substituteRemotePathToCCJsonForFile(const fs::path &projectPath,
61-
const std::string &buildDirRelativePath,
62-
const std::string &jsonFileName,
63-
const fs::path &newJsonDir) {
64-
fs::path compileCommandsJsonPath = projectPath / buildDirRelativePath / jsonFileName;
65-
fs::create_directories(newJsonDir);
60+
void substituteRemotePathToCCJsonForFile(const utbot::ProjectContext &projectContext,
61+
const std::string &jsonFileName) {
62+
fs::path compileCommandsJsonPath = projectContext.buildDir() / jsonFileName;
63+
fs::create_directories(Paths::getUTBotBuildDir(projectContext));
6664
if (!fs::exists(compileCommandsJsonPath)) {
6765
throw CompilationDatabaseException("Can't find " + compileCommandsJsonPath.string());
6866
}
6967
std::ifstream ifs(compileCommandsJsonPath);
7068
json json = json::parse(ifs);
71-
std::string projectPathStr = Paths::normalizedTrimmed(fs::absolute(projectPath)).string();
69+
std::string projectPathStr = Paths::normalizedTrimmed(fs::absolute(projectContext.projectPath)).string();
7270
Paths::removeBackTrailedSlash(projectPathStr);
7371

7472
const std::string directoryFieldName = "directory";
@@ -79,8 +77,7 @@ namespace CompilationUtils {
7977

8078
for (auto &cmd : json) {
8179
std::string directoryField = cmd[directoryFieldName];
82-
std::string userSystemProjectPath =
83-
Paths::subtractPath(directoryField, buildDirRelativePath);
80+
std::string userSystemProjectPath = Paths::normalizedTrimmed(projectContext.clientProjectPath);
8481
Paths::removeBackTrailedSlash(userSystemProjectPath);
8582

8683
if (cmd.contains(commandFieldName)) {
@@ -106,25 +103,23 @@ namespace CompilationUtils {
106103
} else {
107104
for (auto &currentFile : cmd[filesFieldName]) {
108105
std::string currentFileField = currentFile;
109-
StringUtils::replaceAll(currentFileField, userSystemProjectPath,
110-
projectPathStr);
106+
StringUtils::replaceAll(currentFileField, userSystemProjectPath, projectPathStr);
111107
currentFile = currentFileField;
112108
}
113109
}
114110
}
115-
fs::path newJsonPath = newJsonDir / jsonFileName;
111+
fs::path newJsonPath = Paths::getUTBotBuildDir(projectContext) / jsonFileName;
116112
JsonUtils::writeJsonToFile(newJsonPath, json);
117-
LOG_S(DEBUG) << jsonFileName << " for mount is written to: " << newJsonDir;
113+
LOG_S(DEBUG) << jsonFileName << " for mount is written to: " << newJsonPath;
118114
}
119115

120116
fs::path substituteRemotePathToCompileCommandsJsonPath(const utbot::ProjectContext &projectContext) {
121117
const std::string ccJsonFileName = "compile_commands.json";
122-
fs::path utbotBuildDir = Paths::getUTBotBuildDir(projectContext);
123-
substituteRemotePathToCCJsonForFile(projectContext.projectPath, projectContext.buildDirRelativePath,
124-
ccJsonFileName, utbotBuildDir);
125-
substituteRemotePathToCCJsonForFile(projectContext.projectPath, projectContext.buildDirRelativePath,
126-
"link_commands.json", utbotBuildDir);
127-
return utbotBuildDir;
118+
const std::string lcJsonFileName = "link_commands.json";
119+
120+
substituteRemotePathToCCJsonForFile(projectContext, ccJsonFileName);
121+
substituteRemotePathToCCJsonForFile(projectContext, lcJsonFileName);
122+
return Paths::getUTBotBuildDir(projectContext);
128123
}
129124

130125
fs::path getClangCompileCommandsJsonPath(const fs::path &buildCommandsJsonPath) {

server/src/utils/StringUtils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ namespace StringUtils {
7474
}
7575

7676
void replaceAll(std::string &str, const std::string &from, const std::string &to) {
77+
if (from.empty()) {
78+
return;
79+
}
7780
size_t start_pos = 0;
7881
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
7982
str.replace(start_pos, from.length(), to);

server/src/visitors/ParametrizedAssertsVisitor.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ namespace visitor {
1717
auto returnType = methodDescription.returnType.maybeReturnArray()
1818
? methodDescription.returnType.arrayClone(usage, pointerSize)
1919
: methodDescription.returnType;
20+
2021
functionCall = printer->constrVisitorFunctionCall(methodDescription, testCase, false, errorMode);
21-
if (testCase.returnValue.view->getEntryValue(nullptr) == PrinterUtils::C_NULL) {
22-
additionalPointersCount = methodDescription.returnType.countReturnPointers(true);
23-
printer->writeCodeLine(
24-
StringUtils::stringFormat("EXPECT_TRUE(%s)",
25-
PrinterUtils::getEqualString(functionCall, PrinterUtils::C_NULL)));
26-
return;
22+
if (!types::TypesHandler::skipTypeInReturn(methodDescription.returnType) && !testCase.isError()) {
23+
if (testCase.returnValue.view->getEntryValue(nullptr) == PrinterUtils::C_NULL) {
24+
additionalPointersCount = methodDescription.returnType.countReturnPointers(true);
25+
printer->writeCodeLine(
26+
StringUtils::stringFormat("EXPECT_TRUE(%s)",
27+
PrinterUtils::getEqualString(functionCall, PrinterUtils::C_NULL)));
28+
return;
29+
} else {
30+
additionalPointersCount = 0;
31+
visitAny(returnType, "", testCase.returnValue.view.get(), PrinterUtils::DEFAULT_ACCESS, 0);
32+
functionCall = {};
33+
additionalPointersCount = 0;
34+
}
2735
} else {
28-
additionalPointersCount = 0;
36+
printer->writeCodeLine(functionCall);
37+
return;
2938
}
30-
visitAny(returnType, "", testCase.returnValue.view.get(), PrinterUtils::DEFAULT_ACCESS, 0);
31-
functionCall = {};
32-
additionalPointersCount = 0;
3339
}
3440

3541
void ParametrizedAssertsVisitor::visitArray(const types::Type &type,

server/test/framework/BaseTest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BaseTest : public testing::Test {
2323

2424
CompilerName compilerName = CompilerName::CLANG;
2525
std::string buildDirRelativePath;
26+
std::string clientProjectPath = "";
2627
fs::path buildPath;
2728
std::vector<fs::path> srcPaths;
2829

server/test/framework/Server_Tests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ namespace {
12041204

12051205
testDirPath = getTestFilePath(pregeneratedTestsRelativeDir);
12061206
projectContext = std::make_unique<utbot::ProjectContext>(
1207-
projectName, suitePath, testDirPath, buildDirRelativePath);
1207+
projectName, suitePath, testDirPath, buildDirRelativePath, clientProjectPath);
12081208

12091209
basic_functions_c = getTestFilePath("basic_functions.c");
12101210
simple_loop_uncovered_c = getTestFilePath("simple_loop_uncovered.c");
@@ -1500,7 +1500,7 @@ namespace {
15001500
testUtils::checkMinNumberOfTests(testGen.tests, 11);
15011501

15021502
auto projectContext = std::make_unique<utbot::ProjectContext>(projectName, suitePath, suitePath / "tests",
1503-
buildDirRelativePath);
1503+
buildDirRelativePath, clientProjectPath);
15041504
auto testFilter = GrpcUtils::createTestFilterForFile(
15051505
Paths::sourcePathToTestPath(*projectContext, methods_with_asserts_cpp));
15061506
auto runRequest = createCoverageAndResultsRequest(
@@ -1548,7 +1548,7 @@ namespace {
15481548
testUtils::checkMinNumberOfTests(testGen.tests, 11);
15491549

15501550
auto projectContext = std::make_unique<utbot::ProjectContext>(projectName, suitePath, suitePath / "tests",
1551-
buildDirRelativePath);
1551+
buildDirRelativePath, clientProjectPath);
15521552
auto testFilter = GrpcUtils::createTestFilterForFile(
15531553
Paths::sourcePathToTestPath(*projectContext, methods_with_asserts_cpp));
15541554
auto runRequest = createCoverageAndResultsRequest(
@@ -1594,7 +1594,7 @@ namespace {
15941594
testUtils::checkMinNumberOfTests(testGen.tests, 8);
15951595

15961596
auto projectContext = std::make_unique<utbot::ProjectContext>(projectName, suitePath, suitePath / "tests",
1597-
buildDirRelativePath);
1597+
buildDirRelativePath, clientProjectPath);
15981598
auto testFilter = GrpcUtils::createTestFilterForFile(
15991599
Paths::sourcePathToTestPath(*projectContext, methods_with_exceptions_cpp));
16001600
auto runRequest = createCoverageAndResultsRequest(
@@ -1640,7 +1640,7 @@ namespace {
16401640
testUtils::checkMinNumberOfTests(testGen.tests, 8);
16411641

16421642
auto projectContext = std::make_unique<utbot::ProjectContext>(projectName, suitePath, suitePath / "tests",
1643-
buildDirRelativePath);
1643+
buildDirRelativePath, clientProjectPath);
16441644
auto testFilter = GrpcUtils::createTestFilterForFile(
16451645
Paths::sourcePathToTestPath(*projectContext, methods_with_exceptions_cpp));
16461646
auto runRequest = createCoverageAndResultsRequest(
@@ -1797,7 +1797,7 @@ namespace {
17971797
fs::path testsDirPath = getTestFilePath("tests");
17981798

17991799
fs::path linked_list_test_cpp = Paths::sourcePathToTestPath(
1800-
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath),
1800+
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
18011801
linked_list_c);
18021802
auto testFilter = GrpcUtils::createTestFilterForFile(linked_list_test_cpp);
18031803
auto runRequest = testUtils::createCoverageAndResultsRequest(
@@ -1835,7 +1835,7 @@ namespace {
18351835
fs::path testsDirPath = getTestFilePath("tests");
18361836

18371837
fs::path tree_test_cpp = Paths::sourcePathToTestPath(
1838-
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath),
1838+
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
18391839
tree_c);
18401840
auto testFilter = GrpcUtils::createTestFilterForFile(tree_test_cpp);
18411841
auto runRequest = testUtils::createCoverageAndResultsRequest(
@@ -1877,7 +1877,7 @@ namespace {
18771877
fs::path testsDirPath = getTestFilePath("tests");
18781878

18791879
fs::path input_output_test_cpp = Paths::sourcePathToTestPath(
1880-
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath),
1880+
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
18811881
input_output_c);
18821882
auto testFilter = GrpcUtils::createTestFilterForFile(input_output_test_cpp);
18831883
auto runRequest = testUtils::createCoverageAndResultsRequest(
@@ -1916,7 +1916,7 @@ namespace {
19161916
fs::path testsDirPath = getTestFilePath("tests");
19171917

19181918
fs::path file_test_cpp = Paths::sourcePathToTestPath(
1919-
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath),
1919+
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
19201920
file_c);
19211921
auto testFilter = GrpcUtils::createTestFilterForFile(file_test_cpp);
19221922
auto runRequest = testUtils::createCoverageAndResultsRequest(
@@ -1954,7 +1954,7 @@ namespace {
19541954
fs::path testsDirPath = getTestFilePath("tests");
19551955

19561956
fs::path hard_linked_list_test_cpp = Paths::sourcePathToTestPath(
1957-
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath),
1957+
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
19581958
hard_linked_list_c);
19591959
auto testFilter = GrpcUtils::createTestFilterForFile(hard_linked_list_test_cpp);
19601960
auto runRequest = testUtils::createCoverageAndResultsRequest(

server/test/framework/Stub_Tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace {
3737

3838
fs::path testsDirPath = getTestFilePath("tests");
3939
utbot::ProjectContext projectContext{ projectName, suitePath, testsDirPath,
40-
buildDirRelativePath };
40+
buildDirRelativePath, clientProjectPath };
4141
fs::path sum_test_cpp =
4242
Paths::sourcePathToTestPath(projectContext, calc_sum_c);
4343

@@ -319,7 +319,8 @@ namespace {
319319
fs::path testsDirPath = getTestFilePath("tests");
320320

321321
fs::path function_pointers_test_cpp = Paths::sourcePathToTestPath(
322-
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath), function_pointers_c);
322+
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
323+
function_pointers_c);
323324
auto testFilter = GrpcUtils::createTestFilterForFile(function_pointers_test_cpp);
324325
auto runRequest = testUtils::createCoverageAndResultsRequest(
325326
projectName, suitePath, testsDirPath, buildDirRelativePath, std::move(testFilter));

0 commit comments

Comments
 (0)