Skip to content

Commit 5a52bcd

Browse files
author
Samat Gaynutdinov
committed
make tests portable
1 parent 6a86fef commit 5a52bcd

26 files changed

+498
-133
lines changed

server/src/Paths.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,20 @@ namespace Paths {
194194
return getArtifactsRootDir(projectContext) / "tests";
195195
}
196196
fs::path getMakefileDir(const utbot::ProjectContext &projectContext, const fs::path &sourceFilePath) {
197-
return getArtifactsRootDir(projectContext) / "make" / getRelativeDirPath(projectContext, sourceFilePath);
197+
// return getArtifactsRootDir(projectContext) / "make" / getRelativeDirPath(projectContext, sourceFilePath);
198+
return getGeneratedHeaderDir(projectContext, sourceFilePath);
198199
}
199200
fs::path getGeneratedHeaderDir(const utbot::ProjectContext &projectContext, const fs::path &sourceFilePath) {
200201
return projectContext.testDirPath / getRelativeDirPath(projectContext, sourceFilePath);
201202
}
202203
fs::path getRecompiledDir(const utbot::ProjectContext &projectContext) {
203-
return getTmpDir(projectContext.projectName) / "recompiled";
204+
return getTmpDir(projectContext) / "recompiled";
204205
}
205206
fs::path getTestObjectDir(const utbot::ProjectContext &projectContext) {
206-
return getTmpDir(projectContext.projectName) / "test_objects";
207+
return getTmpDir(projectContext) / "test_objects";
207208
}
208209
fs::path getCoverageDir(const utbot::ProjectContext &projectContext) {
209-
return getTmpDir(projectContext.projectName) / "coverage";
210+
return getTmpDir(projectContext) / "coverage";
210211
}
211212
fs::path getClangCoverageDir(const utbot::ProjectContext &projectContext) {
212213
return getCoverageDir(projectContext) / "lcov";
@@ -249,22 +250,22 @@ namespace Paths {
249250

250251
fs::path getBuildFilePath(const utbot::ProjectContext &projectContext,
251252
const fs::path &sourceFilePath) {
252-
fs::path path = getTmpDir(projectContext.projectName) /
253+
fs::path path = getTmpDir(projectContext) /
253254
getRelativeDirPath(projectContext, sourceFilePath) /
254255
sourceFilePath.filename();
255256
return addExtension(path, ".o");
256257
}
257258

258259
fs::path getStubBuildFilePath(const utbot::ProjectContext &projectContext,
259260
const fs::path &sourceFilePath) {
260-
fs::path path = getTmpDir(projectContext.projectName) /
261+
fs::path path = getTmpDir(projectContext) /
261262
getRelativeDirPath(projectContext, sourceFilePath) /
262263
sourcePathToStubName(sourceFilePath);
263264
return addExtension(path, ".o");
264265
}
265266

266267
fs::path getWrapperDirPath(const utbot::ProjectContext &projectContext) {
267-
return getArtifactsRootDir(projectContext) / "wrapper";
268+
return projectContext.testDirPath / "wrapper";
268269
}
269270

270271
fs::path getWrapperFilePath(const utbot::ProjectContext &projectContext,
@@ -392,6 +393,18 @@ namespace Paths {
392393
return fs::relative(source.parent_path(), projectContext.projectPath);
393394
}
394395

396+
std::optional<std::string> getRelativePathWithShellVariable(const string &shellVariableForBase,
397+
const fs::path &base,
398+
const fs::path &source) {
399+
400+
auto relative = fs::relative(source, base).string();
401+
if (relative.empty() || StringUtils::contains(relative, "..")) {
402+
return std::nullopt;
403+
}
404+
return std::make_optional(StringUtils::stringFormat("%s/%s", shellVariableForBase, fs::relative(source, base)));
405+
}
406+
407+
395408
fs::path stubPathToSourcePath(const utbot::ProjectContext &projectContext,
396409
const fs::path &stubPath) {
397410
fs::path sourceFilePath =
@@ -404,7 +417,13 @@ namespace Paths {
404417
return removeSuffix(srcPath, STUB_SUFFIX).stem() == headerPath.stem();
405418
}
406419
fs::path getBuildDir(const utbot::ProjectContext &projectContext) {
407-
return getTmpDir(projectContext.projectName) / "build";
420+
return projectContext.buildDir;
421+
}
422+
423+
fs::path getTmpDir(const utbot::ProjectContext &projectContext)
424+
{
425+
fs::path mountedCCJsonDirPath = getBuildDir(projectContext) / CompilationUtils::MOUNTED_CC_JSON_DIR_NAME;
426+
return mountedCCJsonDirPath / "tmp" / RequestEnvironment::getClientId();
408427
}
409428

410429
//endregion

server/src/Paths.h

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace Paths {
108108
return fs::path(path).replace_filename(newName);
109109
}
110110

111-
CollectionUtils::FileSet findFilesInFolder(const fs::path& folder);
111+
CollectionUtils::FileSet findFilesInFolder(const fs::path &folder);
112112

113113
std::vector<fs::path> findFilesInFolder(const fs::path &folder, const CollectionUtils::FileSet &sourcePaths);
114114

@@ -164,19 +164,32 @@ namespace Paths {
164164
if (projectName.empty()) {
165165
return tmpPath / "tmp" / RequestEnvironment::getClientId();
166166
}
167-
return tmpPath / "tmp" / RequestEnvironment::getClientId() / projectName;
167+
// return tmpPath / "tmp" / RequestEnvironment::getClientId() / projectName;
168+
// here projectName is actually buildPath
169+
return fs::path(projectName) / CompilationUtils::MOUNTED_CC_JSON_DIR_NAME / "tmp" /
170+
RequestEnvironment::getClientId();
168171
}
169172

173+
static inline fs::path getTmpPath() {
174+
return tmpPath;
175+
}
176+
177+
fs::path getTmpDir(const utbot::ProjectContext &projectContext);
178+
170179
fs::path getBuildDir(const utbot::ProjectContext &projectContext);
171180

181+
static inline fs::path getBuildDirRelative(const utbot::ProjectContext &projectContext) {
182+
return projectContext.buildDirRelativePath;
183+
}
184+
172185
//region json
173186
static inline fs::path getClientsJsonPath() {
174187
return tmpPath / "tmp" / "clients.json";
175188
}
176189

177-
fs::path getCCJsonFileFullPath(const std::string &filename, const fs::path &directory);
190+
fs::path getCCJsonFileFullPath(const string &filename, const fs::path &directory);
178191

179-
bool isPath(const std::string& possibleFilePath) noexcept;
192+
bool isPath(const std::string &possibleFilePath) noexcept;
180193
//endregion
181194

182195
//region klee
@@ -196,11 +209,11 @@ namespace Paths {
196209
return projectTmpPath / "klee_out";
197210
}
198211

199-
static inline bool isKtest(fs::path const& path) {
212+
static inline bool isKtest(fs::path const &path) {
200213
return path.extension() == ".ktest";
201214
}
202215

203-
static inline bool isKtestJson(fs::path const& path) {
216+
static inline bool isKtestJson(fs::path const &path) {
204217
return path.extension() == ".ktestjson";
205218
}
206219

@@ -217,21 +230,25 @@ namespace Paths {
217230

218231
//region extensions
219232
extern const std::vector<std::string> CFileHeaderExtensions;
233+
220234
static inline bool isHFile(const fs::path &path) {
221235
return CollectionUtils::contains(CFileHeaderExtensions, path.extension());
222236
}
223237

224238
extern const std::vector<std::string> CFileSourceExtensions;
239+
225240
static inline bool isCFile(const fs::path &path) {
226241
return CollectionUtils::contains(CFileSourceExtensions, path.extension());
227242
}
228243

229244
extern const std::vector<std::string> CXXFileExtensions;
245+
230246
static inline bool isCXXFile(const fs::path &path) {
231247
return CollectionUtils::contains(CXXFileExtensions, path.extension());
232248
}
233249

234250
extern const std::vector<std::string> HPPFileExtensions;
251+
235252
static inline bool isHppFile(const fs::path &path) {
236253
return CollectionUtils::contains(HPPFileExtensions, path.extension());
237254
}
@@ -283,7 +300,7 @@ namespace Paths {
283300

284301
fs::path getMakefileDir(const utbot::ProjectContext &projectContext, const fs::path &sourceFilePath);
285302

286-
fs::path getGeneratedHeaderDir(const utbot::ProjectContext &projectContext, const fs::path& sourceFilePath);
303+
fs::path getGeneratedHeaderDir(const utbot::ProjectContext &projectContext, const fs::path &sourceFilePath);
287304

288305
fs::path getRecompiledDir(const utbot::ProjectContext &projectContext);
289306

@@ -344,8 +361,13 @@ namespace Paths {
344361

345362
fs::path getRelativeDirPath(const utbot::ProjectContext &projectContext, const fs::path &source);
346363

347-
fs::path getMakefilePathFromSourceFilePath(const utbot::ProjectContext &projectContext, const fs::path &sourceFilePath,
348-
const std::string &suffix = "");
364+
std::optional<std::string> getRelativePathWithShellVariable(const std::string &shellVariableForBase,
365+
const fs::path &base,
366+
const fs::path &source);
367+
368+
fs::path
369+
getMakefilePathFromSourceFilePath(const utbot::ProjectContext &projectContext, const fs::path &sourceFilePath,
370+
const std::string &suffix = "");
349371

350372
fs::path getStubsMakefilePath(const utbot::ProjectContext &projectContext, const fs::path &sourceFilePath);
351373

@@ -363,7 +385,7 @@ namespace Paths {
363385

364386
//endregion
365387

366-
bool isHeadersEqual(const fs::path& srcPath, const fs::path& headerPath);
388+
bool isHeadersEqual(const fs::path &srcPath, const fs::path &headerPath);
367389
}
368390

369391
#endif //UNITTESTBOT_PATHS_H

server/src/Server.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void Server::run(uint16_t customPort) {
4747
LOG_S(INFO) << "UnitTestBot Server, build " << UTBOT_BUILD_NUMBER;
4848
LOG_S(INFO) << "Logs directory: " << Paths::logPath;
4949
LOG_S(INFO) << "Latest log path: " << Paths::getUtbotLogAllFilePath();
50-
LOG_S(INFO) << "Tmp directory path: " << Paths::tmpPath;
50+
LOG_S(INFO) << "Tmp directory path: " << Paths::getTmpPath();
5151
LOG_S(INFO) << "Executable path: " << fs::current_path();
5252

5353
host = "0.0.0.0";
@@ -574,8 +574,9 @@ Status Server::TestsGenServiceImpl::PrintModulesContent(ServerContext *context,
574574

575575
MEASURE_FUNCTION_EXECUTION_TIME
576576

577-
fs::path serverBuildDir = Paths::getTmpDir(request->projectname());
577+
// fs::path serverBuildDir = Paths::getTmpDir(request->projectname());
578578
utbot::ProjectContext projectContext{ *request };
579+
fs::path serverBuildDir = Paths::getTmpDir(projectContext);
579580
std::shared_ptr<BuildDatabase> buildDatabase = BuildDatabase::create(projectContext);
580581
StubSourcesFinder(buildDatabase).printAllModules();
581582
return Status::OK;
@@ -615,6 +616,7 @@ Server::TestsGenServiceImpl::ConfigureProject(ServerContext *context,
615616
MEASURE_FUNCTION_EXECUTION_TIME
616617

617618
const auto &projectContext = request->projectcontext();
619+
utbot::ProjectContext utbotProjectContext{ projectContext };
618620
fs::path buildDirPath =
619621
fs::path(projectContext.projectpath()) / projectContext.builddirrelativepath();
620622
switch (request->configmode()) {
@@ -625,13 +627,13 @@ Server::TestsGenServiceImpl::ConfigureProject(ServerContext *context,
625627
case ConfigMode::GENERATE_JSON_FILES: {
626628
std::vector<std::string> cmakeOptions(request->cmakeoptions().begin(), request->cmakeoptions().end());
627629
return UserProjectConfiguration::RunProjectConfigurationCommands(
628-
buildDirPath, projectContext.projectname(), cmakeOptions, writer);
630+
buildDirPath, utbotProjectContext, cmakeOptions, writer);
629631
}
630632
case ConfigMode::ALL: {
631633
std::vector<std::string> cmakeOptions(request->cmakeoptions().begin(), request->cmakeoptions().end());
632634
return UserProjectConfiguration::RunProjectReConfigurationCommands(
633635
buildDirPath, fs::path(projectContext.projectpath()),
634-
projectContext.projectname(), cmakeOptions, writer);
636+
utbotProjectContext, cmakeOptions, writer);
635637
}
636638
default:
637639
return {StatusCode::CANCELLED, "Invalid request type."};

server/src/Synchronizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs,
155155
true);
156156

157157
fs::path ccJsonStubDirPath =
158-
Paths::getTmpDir(testGen->projectContext.projectName) / "stubs_build_files";
158+
Paths::getTmpDir(testGen->projectContext) / "stubs_build_files";
159159
auto stubsCdb = createStubsCompilationDatabase(stubFiles, ccJsonStubDirPath);
160160

161161
auto sourceToHeaderRewriter =

server/src/building/BaseCommand.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,8 @@ namespace utbot {
124124
optimizationLevel = addFlagToBegin(flag);
125125
}
126126
}
127+
128+
void BaseCommand::setDirectory(const fs::path& directory) {
129+
this->directory = directory;
130+
}
127131
}

server/src/building/BaseCommand.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ namespace utbot {
9090
size_t erase_if(std::function<bool(std::string)> f);
9191

9292
void setOptimizationLevel(const std::string &flag);
93+
94+
void setDirectory(const fs::path& directory);
9395
};
9496
}
9597

server/src/building/BuildDatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ std::shared_ptr<BuildDatabase> BuildDatabase::create(const utbot::ProjectContext
5757
fs::path compileCommandsJsonPath =
5858
CompilationUtils::substituteRemotePathToCompileCommandsJsonPath(
5959
projectContext.projectPath, projectContext.buildDirRelativePath);
60-
fs::path serverBuildDir = Paths::getTmpDir(projectContext.projectName);
60+
fs::path serverBuildDir = Paths::getTmpDir(projectContext);
6161
std::shared_ptr<BuildDatabase> buildDatabase =
6262
std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
6363
return buildDatabase;

server/src/building/CompileCommand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ namespace utbot {
111111
return *sourcePath;
112112
}
113113

114-
void CompileCommand::setSourcePath(fs::path sourcePath) {
114+
void CompileCommand::setSourcePath(std::string sourcePath) {
115115
*(this->sourcePath) = std::move(sourcePath);
116116
}
117117

@@ -131,7 +131,7 @@ namespace utbot {
131131
return false;
132132
}
133133

134-
void CompileCommand::setOutput(fs::path output) {
134+
void CompileCommand::setOutput(std::string output) {
135135
*(this->output) = std::move(output);
136136
}
137137

server/src/building/CompileCommand.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace utbot {
3939

4040
[[nodiscard]] fs::path getSourcePath() const;
4141

42-
void setSourcePath(fs::path sourcePath);
42+
void setSourcePath(std::string sourcePath);
4343

4444
[[nodiscard]] fs::path getCompiler() const;
4545

@@ -49,7 +49,7 @@ namespace utbot {
4949

5050
[[nodiscard]] bool isArchiveCommand() const override;
5151

52-
void setOutput(fs::path output);
52+
void setOutput(std::string output);
5353

5454
void removeGccFlags();
5555

server/src/building/UserProjectConfiguration.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ Status UserProjectConfiguration::RunBuildDirectoryCreation(const fs::path &build
3939

4040
Status
4141
UserProjectConfiguration::RunProjectConfigurationCommands(const fs::path &buildDirPath,
42-
const std::string &projectName,
43-
std::vector<std::string> cmakeOptions,
42+
const utbot::ProjectContext& projectContext,
43+
vector<std::string> cmakeOptions,
4444
ProjectConfigWriter const &writer) {
4545
try {
4646
fs::path bearShPath = createBearShScript(buildDirPath);
@@ -53,13 +53,13 @@ UserProjectConfiguration::RunProjectConfigurationCommands(const fs::path &buildD
5353
fs::path cmakeListsPath = getCmakeListsPath(buildDirPath);
5454
if (fs::exists(cmakeListsPath)) {
5555
LOG_S(INFO) << "Configure cmake project";
56-
RunProjectConfigurationCommand(buildDirPath, cmakeParams, projectName, writer);
56+
RunProjectConfigurationCommand(buildDirPath, cmakeParams, projectContext, writer);
5757
} else {
5858
LOG_S(INFO) << "CMakeLists.txt not found in root project directory: " << cmakeListsPath
5959
<< ". Skipping cmake step.";
6060
}
6161
LOG_S(INFO) << "Configure make project";
62-
RunProjectConfigurationCommand(buildDirPath, bearMakeParams, projectName, writer);
62+
RunProjectConfigurationCommand(buildDirPath, bearMakeParams, projectContext, writer);
6363
writer.writeResponse(ProjectConfigStatus::IS_OK);
6464
} catch (const std::exception &e) {
6565
fs::remove(getCompileCommandsJsonPath(buildDirPath));
@@ -71,11 +71,11 @@ UserProjectConfiguration::RunProjectConfigurationCommands(const fs::path &buildD
7171

7272
void UserProjectConfiguration::RunProjectConfigurationCommand(const fs::path &buildDirPath,
7373
const ShellExecTask::ExecutionParameters &params,
74-
const std::string &projectName,
74+
const utbot::ProjectContext &projectContext,
7575
const ProjectConfigWriter &writer) {
76-
auto[out, status, _] = ShellExecTask::runShellCommandTask(params, buildDirPath, projectName, true, true);
76+
auto[out, status, _] = ShellExecTask::runShellCommandTask(params, buildDirPath, projectContext.projectName, true, true);
7777
if (status != 0) {
78-
auto logFilePath = LogUtils::writeLog(out, Paths::getTmpDir(projectName), "project-import");
78+
auto logFilePath = LogUtils::writeLog(out, Paths::getTmpDir(projectContext), "project-import");
7979
std::string message = StringUtils::stringFormat(
8080
"Running command \"%s\" failed. See more info in logs.", params.toString());
8181
throw std::runtime_error(message);
@@ -123,6 +123,8 @@ fs::path UserProjectConfiguration::createBearShScript(const fs::path &buildDirPa
123123

124124
Status UserProjectConfiguration::RunProjectReConfigurationCommands(const fs::path &buildDirPath,
125125
const fs::path &projectDirPath,
126+
const utbot::ProjectContext &projectContext,
127+
std::vector<std::string> cmakeOptions,
126128
const std::string &projectName,
127129
std::vector<std::string> cmakeOptions,
128130
ProjectConfigWriter const &writer) {
@@ -142,7 +144,7 @@ Status UserProjectConfiguration::RunProjectReConfigurationCommands(const fs::pat
142144
if (!createBuildDirectory(buildDirPath, writer)) {
143145
return Status::OK;
144146
}
145-
return UserProjectConfiguration::RunProjectConfigurationCommands(buildDirPath, projectName,
147+
return UserProjectConfiguration::RunProjectConfigurationCommands(buildDirPath, projectContext,
146148
cmakeOptions, writer);
147149
}
148150

0 commit comments

Comments
 (0)