Skip to content

Commit 8882873

Browse files
Alexey Utkinalexey-utkin
authored andcommitted
UTBotCpp-235 Ambiguous restoration of the source file name from the test file #235
Encoding the file extension into the test file name as `_dot_<ext-without-dot>` suffix. Related with #173 (Collection of coverage takes too long)
1 parent 1648d32 commit 8882873

File tree

17 files changed

+50
-11
lines changed

17 files changed

+50
-11
lines changed

server/src/Paths.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,50 @@ namespace Paths {
281281
static const std::string MAKEFILE_EXTENSION = ".mk";
282282
static const std::string TEST_SUFFIX = "_test";
283283
static const std::string STUB_SUFFIX = "_stub";
284+
static const std::string DOT_SEP = "_dot_";
285+
static const char dot = '.';
284286

285287
fs::path sourcePathToTestPath(const utbot::ProjectContext &projectContext,
286288
const fs::path &sourceFilePath) {
287289
return projectContext.testDirPath / getRelativeDirPath(projectContext, sourceFilePath) /
288290
sourcePathToTestName(sourceFilePath);
289291
}
292+
293+
static inline fs::path addOrigExtensionAsSuffixAndAddNew(const fs::path &path,
294+
const std::string &newExt) {
295+
std::string extensionAsSuffix = path.extension().string();
296+
if (!extensionAsSuffix.empty()) {
297+
std::string fnWithNewExt =
298+
path.stem().string() + DOT_SEP + extensionAsSuffix.substr(1) + newExt;
299+
return path.parent_path() / fnWithNewExt;
300+
}
301+
return replaceExtension(path, newExt);
302+
}
303+
304+
static inline fs::path restoreExtensionFromSuffix(const fs::path &path,
305+
const std::string &defaultExt) {
306+
std::string fnWithoutExt = path.stem();
307+
fs::path fnWithExt;
308+
std::size_t posEncodedExtension = fnWithoutExt.rfind(DOT_SEP);
309+
if (posEncodedExtension == std::string::npos) {
310+
// In `sample_class_test.cpp` the `class` is not an extension
311+
fnWithExt = fnWithoutExt + defaultExt;
312+
}
313+
else {
314+
// In `sample_class_dot_cpp.cpp` the `cpp` is an extension
315+
fnWithExt = fnWithoutExt.substr(0, posEncodedExtension)
316+
+ dot
317+
+ fnWithoutExt.substr(posEncodedExtension + DOT_SEP.length());
318+
}
319+
return path.parent_path() / fs::path(fnWithExt);
320+
}
321+
290322
fs::path sourcePathToTestName(const fs::path &source) {
291-
return replaceExtension(addSuffix(source, TEST_SUFFIX), ".cpp").filename();
323+
return addSuffix(addOrigExtensionAsSuffixAndAddNew(source, ".cpp"),
324+
TEST_SUFFIX).filename();
292325
}
293326
fs::path testPathToSourceName(const fs::path &testFilePath) {
294-
return replaceExtension(removeSuffix(testFilePath, TEST_SUFFIX), ".c").filename();
327+
return restoreExtensionFromSuffix(removeSuffix(testFilePath, TEST_SUFFIX), ".c").filename();
295328
}
296329
fs::path sourcePathToStubName(const fs::path &source) {
297330
return addSuffix(source, STUB_SUFFIX).filename();

server/test/framework/BaseTest.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,18 @@ fs::path BaseTest::getTestDirectory() {
6060

6161
fs::path BaseTest::getPathToGeneratedTestFileByTestedFile(const string &fileName) {
6262
return getTestDirectory() /
63-
(Paths::addExtension(Paths::addTestSuffix(Paths::removeExtension(fileName)), ".cpp"));
63+
(Paths::addExtension(
64+
Paths::addTestSuffix(
65+
Paths::addSuffix(Paths::removeExtension(fileName), "_dot_c")),
66+
".cpp"));
6467
}
6568

6669
fs::path BaseTest::getPathToGeneratedTestHeaderFileByTestedFile(const string &fileName) {
6770
return getTestDirectory() /
68-
(Paths::addExtension(Paths::addTestSuffix(Paths::removeExtension(fileName)), ".h"));
71+
(Paths::addExtension(
72+
Paths::addTestSuffix(
73+
Paths::addSuffix(Paths::removeExtension(fileName), "_dot_c")),
74+
".h"));
6975
}
7076

7177
fs::path BaseTest::getStubsDirectory() {

server/test/suites/coverage/pregenerated_tests/basic_functions_test.cpp renamed to server/test/suites/coverage/pregenerated_tests/basic_functions_dot_c_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
33
*/
44

5-
#include "basic_functions_test.h"
5+
#include "basic_functions_dot_c_test.h"
66

77
#include "gtest/gtest.h"
88

server/test/suites/coverage/pregenerated_tests/dependent_functions_test.cpp renamed to server/test/suites/coverage/pregenerated_tests/dependent_functions_dot_c_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
33
*/
44

5-
#include "dependent_functions_test.h"
5+
#include "dependent_functions_dot_c_test.h"
66

77
#include "gtest/gtest.h"
88
namespace UTBot {

server/test/suites/coverage/pregenerated_tests/simple_class_test.cpp renamed to server/test/suites/coverage/pregenerated_tests/simple_class_dot_cpp_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
33
*/
44

5-
#include "simple_class_test.h"
5+
#include "simple_class_dot_cpp_test.h"
66

77
#include "gtest/gtest.h"
88

server/test/suites/coverage/pregenerated_tests/simple_loop_uncovered_test.cpp renamed to server/test/suites/coverage/pregenerated_tests/simple_loop_uncovered_dot_c_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
33
*/
44

5-
#include "simple_loop_uncovered_test.h"
5+
#include "simple_loop_uncovered_dot_c_test.h"
66

77
#include "gtest/gtest.h"
88

0 commit comments

Comments
 (0)