Skip to content

Commit

Permalink
Fix substitute remote path
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Kalugin committed Feb 9, 2023
1 parent 4b22e1a commit 6dc08de
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 37 deletions.
1 change: 1 addition & 0 deletions server/proto/testgen.proto
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ message ProjectContext {
string projectPath = 2;
string testDirPath = 3;
string buildDirRelativePath = 4;
string clientProjectPath = 5;
}

enum ErrorMode {
Expand Down
12 changes: 8 additions & 4 deletions server/src/ProjectContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ namespace utbot {
ProjectContext::ProjectContext(std::string projectName,
fs::path projectPath,
fs::path testDirPath,
fs::path buildDirRelativePath)
fs::path buildDirRelativePath,
fs::path clientProjectPath)
: projectName(std::move(projectName)), projectPath(std::move(projectPath)),
testDirPath(std::move(testDirPath)),
buildDirRelativePath(std::move(buildDirRelativePath)) {}
buildDirRelativePath(std::move(buildDirRelativePath)),
clientProjectPath(clientProjectPath) {}

ProjectContext::ProjectContext(const testsgen::ProjectContext &projectContext)
: ProjectContext(projectContext.projectname(),
projectContext.projectpath(),
projectContext.testdirpath(),
projectContext.builddirrelativepath()) {}
projectContext.builddirrelativepath(),
projectContext.clientprojectpath()) {}

ProjectContext::ProjectContext(const testsgen::SnippetRequest &request, fs::path serverBuildDir)
: projectName(request.projectcontext().projectname()),
projectPath(request.projectcontext().projectpath()),
testDirPath(request.projectcontext().testdirpath()),
buildDirRelativePath(request.projectcontext().builddirrelativepath()) { }
buildDirRelativePath(request.projectcontext().builddirrelativepath()),
clientProjectPath(request.projectcontext().clientprojectpath()) {}

fs::path ProjectContext::buildDir() const {
return projectPath / buildDirRelativePath;
Expand Down
4 changes: 3 additions & 1 deletion server/src/ProjectContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class ProjectContext {
ProjectContext(std::string projectName,
fs::path projectPath,
fs::path testDirPath,
fs::path buildDirRelativePath);
fs::path buildDirRelativePath,
fs::path serverBuildDir);

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

Expand All @@ -28,6 +29,7 @@ class ProjectContext {
const fs::path projectPath;
const fs::path testDirPath;
const fs::path buildDirRelativePath;
const fs::path clientProjectPath;
};
}

Expand Down
1 change: 0 additions & 1 deletion server/src/building/CompileCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,5 @@ namespace utbot {
auto path = Paths::getCCJsonFileFullPath(Paths::replaceExtension(*this->sourcePath, ".o"), this->directory);
this->output = std::next(addFlagsToBegin({"-o", path}));
}

}
}
35 changes: 15 additions & 20 deletions server/src/utils/CompilationUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@ namespace CompilationUtils {
}
}

void substituteRemotePathToCCJsonForFile(const fs::path &projectPath,
const std::string &buildDirRelativePath,
const std::string &jsonFileName,
const fs::path &newJsonDir) {
fs::path compileCommandsJsonPath = projectPath / buildDirRelativePath / jsonFileName;
fs::create_directories(newJsonDir);
void substituteRemotePathToCCJsonForFile(const utbot::ProjectContext &projectContext,
const std::string &jsonFileName) {
fs::path compileCommandsJsonPath = projectContext.buildDir() / jsonFileName;
fs::create_directories(Paths::getUTBotBuildDir(projectContext));
if (!fs::exists(compileCommandsJsonPath)) {
throw CompilationDatabaseException("Can't find " + compileCommandsJsonPath.string());
}
std::ifstream ifs(compileCommandsJsonPath);
json json = json::parse(ifs);
std::string projectPathStr = Paths::normalizedTrimmed(fs::absolute(projectPath)).string();
std::string projectPathStr = Paths::normalizedTrimmed(fs::absolute(projectContext.projectPath)).string();
Paths::removeBackTrailedSlash(projectPathStr);

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

for (auto &cmd : json) {
std::string directoryField = cmd[directoryFieldName];
std::string userSystemProjectPath =
Paths::subtractPath(directoryField, buildDirRelativePath);
std::string userSystemProjectPath = Paths::normalizedTrimmed(projectContext.clientProjectPath);
Paths::removeBackTrailedSlash(userSystemProjectPath);

if (cmd.contains(commandFieldName)) {
Expand All @@ -96,7 +93,7 @@ namespace CompilationUtils {
}
}

// StringUtils::replaceAll(directoryField, userSystemProjectPath, projectPathStr);
StringUtils::replaceAll(directoryField, userSystemProjectPath, projectPathStr);
cmd[directoryFieldName] = directoryField;

if (cmd.contains(fileFieldName)) {
Expand All @@ -106,25 +103,23 @@ namespace CompilationUtils {
} else {
for (auto &currentFile : cmd[filesFieldName]) {
std::string currentFileField = currentFile;
StringUtils::replaceAll(currentFileField, userSystemProjectPath,
projectPathStr);
StringUtils::replaceAll(currentFileField, userSystemProjectPath, projectPathStr);
currentFile = currentFileField;
}
}
}
fs::path newJsonPath = newJsonDir / jsonFileName;
fs::path newJsonPath = Paths::getUTBotBuildDir(projectContext) / jsonFileName;
JsonUtils::writeJsonToFile(newJsonPath, json);
LOG_S(DEBUG) << jsonFileName << " for mount is written to: " << newJsonDir;
LOG_S(DEBUG) << jsonFileName << " for mount is written to: " << newJsonPath;
}

fs::path substituteRemotePathToCompileCommandsJsonPath(const utbot::ProjectContext &projectContext) {
const std::string ccJsonFileName = "compile_commands.json";
fs::path utbotBuildDir = Paths::getUTBotBuildDir(projectContext);
substituteRemotePathToCCJsonForFile(projectContext.projectPath, projectContext.buildDirRelativePath,
ccJsonFileName, utbotBuildDir);
substituteRemotePathToCCJsonForFile(projectContext.projectPath, projectContext.buildDirRelativePath,
"link_commands.json", utbotBuildDir);
return utbotBuildDir;
const std::string lcJsonFileName = "link_commands.json";

substituteRemotePathToCCJsonForFile(projectContext, ccJsonFileName);
substituteRemotePathToCCJsonForFile(projectContext, lcJsonFileName);
return Paths::getUTBotBuildDir(projectContext);
}

fs::path getClangCompileCommandsJsonPath(const fs::path &buildCommandsJsonPath) {
Expand Down
3 changes: 3 additions & 0 deletions server/src/utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ namespace StringUtils {
}

void replaceAll(std::string &str, const std::string &from, const std::string &to) {
if (from.empty()) {
return;
}
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
Expand Down
1 change: 1 addition & 0 deletions server/test/framework/BaseTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BaseTest : public testing::Test {

CompilerName compilerName = CompilerName::CLANG;
std::string buildDirRelativePath;
std::string clientProjectPath;
fs::path buildPath;
std::vector<fs::path> srcPaths;

Expand Down
10 changes: 5 additions & 5 deletions server/test/framework/Server_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ namespace {

testDirPath = getTestFilePath(pregeneratedTestsRelativeDir);
projectContext = std::make_unique<utbot::ProjectContext>(
projectName, suitePath, testDirPath, buildDirRelativePath);
projectName, suitePath, testDirPath, buildDirRelativePath, clientProjectPath);

basic_functions_c = getTestFilePath("basic_functions.c");
simple_loop_uncovered_c = getTestFilePath("simple_loop_uncovered.c");
Expand Down Expand Up @@ -1500,7 +1500,7 @@ namespace {
testUtils::checkMinNumberOfTests(testGen.tests, 11);

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

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

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

auto projectContext = std::make_unique<utbot::ProjectContext>(projectName, suitePath, suitePath / "tests",
buildDirRelativePath);
buildDirRelativePath, clientProjectPath);
auto testFilter = GrpcUtils::createTestFilterForFile(
Paths::sourcePathToTestPath(*projectContext, methods_with_exceptions_cpp));
auto runRequest = createCoverageAndResultsRequest(
Expand Down
5 changes: 3 additions & 2 deletions server/test/framework/Stub_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace {

fs::path testsDirPath = getTestFilePath("tests");
utbot::ProjectContext projectContext{ projectName, suitePath, testsDirPath,
buildDirRelativePath };
buildDirRelativePath, clientProjectPath };
fs::path sum_test_cpp =
Paths::sourcePathToTestPath(projectContext, calc_sum_c);

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

fs::path function_pointers_test_cpp = Paths::sourcePathToTestPath(
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath), function_pointers_c);
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
function_pointers_c);
auto testFilter = GrpcUtils::createTestFilterForFile(function_pointers_test_cpp);
auto runRequest = testUtils::createCoverageAndResultsRequest(
projectName, suitePath, testsDirPath, buildDirRelativePath, std::move(testFilter));
Expand Down
8 changes: 4 additions & 4 deletions server/test/framework/Syntax_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,7 @@ namespace {
fs::path testsDirPath = getTestFilePath("tests");

fs::path linked_list_test_cpp = Paths::sourcePathToTestPath(utbot::ProjectContext(
projectName, suitePath, testsDirPath, buildDirRelativePath), linked_list_c);
projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath), linked_list_c);
auto testFilter = GrpcUtils::createTestFilterForFile(linked_list_test_cpp);
auto runRequest = testUtils::createCoverageAndResultsRequest(
projectName, suitePath, testsDirPath, buildDirRelativePath, std::move(testFilter));
Expand Down Expand Up @@ -2281,7 +2281,7 @@ namespace {
fs::path testsDirPath = getTestFilePath("tests");

fs::path tree_test_cpp = Paths::sourcePathToTestPath(utbot::ProjectContext(
projectName, suitePath, testsDirPath, buildDirRelativePath), tree_c);
projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath), tree_c);
auto testFilter = GrpcUtils::createTestFilterForFile(tree_test_cpp);
auto runRequest = testUtils::createCoverageAndResultsRequest(
projectName, suitePath, testsDirPath, buildDirRelativePath, std::move(testFilter));
Expand Down Expand Up @@ -3236,7 +3236,7 @@ namespace {
fs::path testsDirPath = getTestFilePath("tests");

fs::path input_output_test_cpp = Paths::sourcePathToTestPath(
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath),
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
input_output_c);
auto testFilter = GrpcUtils::createTestFilterForFile(input_output_test_cpp);
auto runRequest = testUtils::createCoverageAndResultsRequest(
Expand Down Expand Up @@ -3428,7 +3428,7 @@ namespace {
fs::path testsDirPath = getTestFilePath("tests");

fs::path file_test_cpp = Paths::sourcePathToTestPath(
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath),
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
file_c);
auto testFilter = GrpcUtils::createTestFilterForFile(file_test_cpp);
auto runRequest = testUtils::createCoverageAndResultsRequest(
Expand Down
4 changes: 4 additions & 0 deletions vscode-plugin/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-async-promise-executor */
import * as grpc from 'grpc';
import * as vs from 'vscode';
import * as vsUtils from "../utils/vscodeUtils";
import * as os from 'os';
import * as messages from '../config/notificationMessages';
import { Prefs } from '../config/prefs';
Expand Down Expand Up @@ -394,6 +395,7 @@ export class Client {
projectContext.setProjectname(projectName);
projectContext.setProjectpath(projectPath);
projectContext.setBuilddirrelativepath(buildDirRelativePath);
projectContext.setClientprojectpath(vsUtils.getProjectDirByOpenedFile().fsPath)
const projectConfigRequest = new ProjectConfigRequest();
projectConfigRequest.setProjectcontext(projectContext);
projectConfigRequest.setConfigmode(configMode);
Expand Down Expand Up @@ -426,6 +428,7 @@ export class Client {
projectContext.setProjectpath(buildDir[0]);
projectContext.setTestdirpath(Prefs.getTestsDirPath());
projectContext.setBuilddirrelativepath(buildDir[1]);
projectContext.setClientprojectpath(vsUtils.getProjectDirByOpenedFile().fsPath)
rpcRequest.setProjectcontext(projectContext);
rpcRequest.setSettingscontext(Prefs.getSettingsContext());

Expand Down Expand Up @@ -640,6 +643,7 @@ export class Client {
projectContext.setProjectpath(params.projectPath);
projectContext.setTestdirpath(Prefs.getTestsDirPath());
projectContext.setBuilddirrelativepath(params.buildDirRelativePath);
projectContext.setClientprojectpath(vsUtils.getProjectDirByOpenedFile().fsPath);
rpcRequest.setProjectcontext(projectContext);
rpcRequest.setSettingscontext(Prefs.getSettingsContext());
rpcRequest.setCoverage(true);
Expand Down
3 changes: 3 additions & 0 deletions vscode-plugin/src/requests/protos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from "../proto-ts/testgen_pb";
import { PredicateInfo, SourceInfo, ValidationType } from "../proto-ts/util_pb";
import { RequestTestsParams } from "./params";
import * as vsUtils from "../utils/vscodeUtils";


export class Protos {
public static projectRequestByParams(params: RequestTestsParams): ProjectRequest {
Expand All @@ -37,6 +39,7 @@ export class Protos {
projectContext.setProjectpath(projectPath);
projectContext.setTestdirpath(Prefs.getTestsDirPath());
projectContext.setBuilddirrelativepath(buildDirRelativePath);
projectContext.setClientprojectpath(vsUtils.getProjectDirByOpenedFile().fsPath)
projectInfo.setProjectcontext(projectContext);
projectInfo.setSettingscontext(Prefs.getSettingsContext());
projectInfo.setSourcepathsList(srcPathsList);
Expand Down

0 comments on commit 6dc08de

Please sign in to comment.