From 6dc08de703b5d34aac24af93e84fbf1d05ef706a Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Thu, 9 Feb 2023 17:49:41 +0300 Subject: [PATCH] Fix substitute remote path --- server/proto/testgen.proto | 1 + server/src/ProjectContext.cpp | 12 ++++++--- server/src/ProjectContext.h | 4 ++- server/src/building/CompileCommand.cpp | 1 - server/src/utils/CompilationUtils.cpp | 35 +++++++++++--------------- server/src/utils/StringUtils.cpp | 3 +++ server/test/framework/BaseTest.h | 1 + server/test/framework/Server_Tests.cpp | 10 ++++---- server/test/framework/Stub_Tests.cpp | 5 ++-- server/test/framework/Syntax_Tests.cpp | 8 +++--- vscode-plugin/src/client/client.ts | 4 +++ vscode-plugin/src/requests/protos.ts | 3 +++ 12 files changed, 50 insertions(+), 37 deletions(-) diff --git a/server/proto/testgen.proto b/server/proto/testgen.proto index 707b4b0e1..91b8cc86c 100644 --- a/server/proto/testgen.proto +++ b/server/proto/testgen.proto @@ -88,6 +88,7 @@ message ProjectContext { string projectPath = 2; string testDirPath = 3; string buildDirRelativePath = 4; + string clientProjectPath = 5; } enum ErrorMode { diff --git a/server/src/ProjectContext.cpp b/server/src/ProjectContext.cpp index 191cd945e..c906844fa 100644 --- a/server/src/ProjectContext.cpp +++ b/server/src/ProjectContext.cpp @@ -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; diff --git a/server/src/ProjectContext.h b/server/src/ProjectContext.h index 4c4981d97..f3d6233b4 100644 --- a/server/src/ProjectContext.h +++ b/server/src/ProjectContext.h @@ -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); @@ -28,6 +29,7 @@ class ProjectContext { const fs::path projectPath; const fs::path testDirPath; const fs::path buildDirRelativePath; + const fs::path clientProjectPath; }; } diff --git a/server/src/building/CompileCommand.cpp b/server/src/building/CompileCommand.cpp index 84482a256..6f08d2783 100644 --- a/server/src/building/CompileCommand.cpp +++ b/server/src/building/CompileCommand.cpp @@ -114,6 +114,5 @@ namespace utbot { auto path = Paths::getCCJsonFileFullPath(Paths::replaceExtension(*this->sourcePath, ".o"), this->directory); this->output = std::next(addFlagsToBegin({"-o", path})); } - } } diff --git a/server/src/utils/CompilationUtils.cpp b/server/src/utils/CompilationUtils.cpp index ee0d1d4d0..2ee86aea0 100644 --- a/server/src/utils/CompilationUtils.cpp +++ b/server/src/utils/CompilationUtils.cpp @@ -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"; @@ -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)) { @@ -96,7 +93,7 @@ namespace CompilationUtils { } } -// StringUtils::replaceAll(directoryField, userSystemProjectPath, projectPathStr); + StringUtils::replaceAll(directoryField, userSystemProjectPath, projectPathStr); cmd[directoryFieldName] = directoryField; if (cmd.contains(fileFieldName)) { @@ -106,25 +103,23 @@ namespace CompilationUtils { } else { for (auto ¤tFile : 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) { diff --git a/server/src/utils/StringUtils.cpp b/server/src/utils/StringUtils.cpp index 00bd70439..944beafe6 100644 --- a/server/src/utils/StringUtils.cpp +++ b/server/src/utils/StringUtils.cpp @@ -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); diff --git a/server/test/framework/BaseTest.h b/server/test/framework/BaseTest.h index 73d2477e0..33c888b1f 100644 --- a/server/test/framework/BaseTest.h +++ b/server/test/framework/BaseTest.h @@ -23,6 +23,7 @@ class BaseTest : public testing::Test { CompilerName compilerName = CompilerName::CLANG; std::string buildDirRelativePath; + std::string clientProjectPath; fs::path buildPath; std::vector srcPaths; diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index f4b3355df..10e9f0626 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -1204,7 +1204,7 @@ namespace { testDirPath = getTestFilePath(pregeneratedTestsRelativeDir); projectContext = std::make_unique( - 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"); @@ -1500,7 +1500,7 @@ namespace { testUtils::checkMinNumberOfTests(testGen.tests, 11); auto projectContext = std::make_unique(projectName, suitePath, suitePath / "tests", - buildDirRelativePath); + buildDirRelativePath, clientProjectPath); auto testFilter = GrpcUtils::createTestFilterForFile( Paths::sourcePathToTestPath(*projectContext, methods_with_asserts_cpp)); auto runRequest = createCoverageAndResultsRequest( @@ -1548,7 +1548,7 @@ namespace { testUtils::checkMinNumberOfTests(testGen.tests, 11); auto projectContext = std::make_unique(projectName, suitePath, suitePath / "tests", - buildDirRelativePath); + buildDirRelativePath, clientProjectPath); auto testFilter = GrpcUtils::createTestFilterForFile( Paths::sourcePathToTestPath(*projectContext, methods_with_asserts_cpp)); auto runRequest = createCoverageAndResultsRequest( @@ -1594,7 +1594,7 @@ namespace { testUtils::checkMinNumberOfTests(testGen.tests, 8); auto projectContext = std::make_unique(projectName, suitePath, suitePath / "tests", - buildDirRelativePath); + buildDirRelativePath, clientProjectPath); auto testFilter = GrpcUtils::createTestFilterForFile( Paths::sourcePathToTestPath(*projectContext, methods_with_exceptions_cpp)); auto runRequest = createCoverageAndResultsRequest( @@ -1640,7 +1640,7 @@ namespace { testUtils::checkMinNumberOfTests(testGen.tests, 8); auto projectContext = std::make_unique(projectName, suitePath, suitePath / "tests", - buildDirRelativePath); + buildDirRelativePath, clientProjectPath); auto testFilter = GrpcUtils::createTestFilterForFile( Paths::sourcePathToTestPath(*projectContext, methods_with_exceptions_cpp)); auto runRequest = createCoverageAndResultsRequest( diff --git a/server/test/framework/Stub_Tests.cpp b/server/test/framework/Stub_Tests.cpp index 6d12901d1..06f2330b1 100644 --- a/server/test/framework/Stub_Tests.cpp +++ b/server/test/framework/Stub_Tests.cpp @@ -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); @@ -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)); diff --git a/server/test/framework/Syntax_Tests.cpp b/server/test/framework/Syntax_Tests.cpp index d26617e6d..98a03bf0f 100644 --- a/server/test/framework/Syntax_Tests.cpp +++ b/server/test/framework/Syntax_Tests.cpp @@ -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)); @@ -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)); @@ -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( @@ -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( diff --git a/vscode-plugin/src/client/client.ts b/vscode-plugin/src/client/client.ts index 98ba760fc..04148e1c0 100644 --- a/vscode-plugin/src/client/client.ts +++ b/vscode-plugin/src/client/client.ts @@ -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'; @@ -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); @@ -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()); @@ -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); diff --git a/vscode-plugin/src/requests/protos.ts b/vscode-plugin/src/requests/protos.ts index d43de8723..f0665b53d 100644 --- a/vscode-plugin/src/requests/protos.ts +++ b/vscode-plugin/src/requests/protos.ts @@ -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 { @@ -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);