Skip to content

Commit 4718153

Browse files
author
Vladislav Kalugin
committed
Fix substitute remote path
1 parent 5a4e5c7 commit 4718153

File tree

11 files changed

+46
-33
lines changed

11 files changed

+46
-33
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/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/utils/CompilationUtils.cpp

Lines changed: 15 additions & 20 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)) {
@@ -96,7 +93,7 @@ namespace CompilationUtils {
9693
}
9794
}
9895

99-
// StringUtils::replaceAll(directoryField, userSystemProjectPath, projectPathStr);
96+
StringUtils::replaceAll(directoryField, userSystemProjectPath, projectPathStr);
10097
cmd[directoryFieldName] = directoryField;
10198

10299
if (cmd.contains(fileFieldName)) {
@@ -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/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: 5 additions & 5 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(

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));

vscode-plugin/src/client/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-async-promise-executor */
22
import * as grpc from 'grpc';
33
import * as vs from 'vscode';
4+
import * as vsUtils from "../utils/vscodeUtils";
45
import * as os from 'os';
56
import * as messages from '../config/notificationMessages';
67
import { Prefs } from '../config/prefs';
@@ -394,6 +395,7 @@ export class Client {
394395
projectContext.setProjectname(projectName);
395396
projectContext.setProjectpath(projectPath);
396397
projectContext.setBuilddirrelativepath(buildDirRelativePath);
398+
projectContext.setClientprojectpath(vsUtils.getProjectDirByOpenedFile().fsPath)
397399
const projectConfigRequest = new ProjectConfigRequest();
398400
projectConfigRequest.setProjectcontext(projectContext);
399401
projectConfigRequest.setConfigmode(configMode);
@@ -426,6 +428,7 @@ export class Client {
426428
projectContext.setProjectpath(buildDir[0]);
427429
projectContext.setTestdirpath(Prefs.getTestsDirPath());
428430
projectContext.setBuilddirrelativepath(buildDir[1]);
431+
projectContext.setClientprojectpath(vsUtils.getProjectDirByOpenedFile().fsPath)
429432
rpcRequest.setProjectcontext(projectContext);
430433
rpcRequest.setSettingscontext(Prefs.getSettingsContext());
431434

@@ -640,6 +643,7 @@ export class Client {
640643
projectContext.setProjectpath(params.projectPath);
641644
projectContext.setTestdirpath(Prefs.getTestsDirPath());
642645
projectContext.setBuilddirrelativepath(params.buildDirRelativePath);
646+
projectContext.setClientprojectpath(vsUtils.getProjectDirByOpenedFile().fsPath);
643647
rpcRequest.setProjectcontext(projectContext);
644648
rpcRequest.setSettingscontext(Prefs.getSettingsContext());
645649
rpcRequest.setCoverage(true);

0 commit comments

Comments
 (0)