1111
1212ProjectTestGen::ProjectTestGen (const testsgen::ProjectRequest &request,
1313 ProgressWriter *progressWriter,
14- bool testMode)
14+ bool testMode,
15+ bool autoDetect)
1516 : BaseTestGen(request.projectcontext(),
1617 request.settingscontext(),
1718 progressWriter,
@@ -22,20 +23,12 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request,
2223 buildDatabase =
2324 std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
2425 compilationDatabase = CompilationUtils::getCompilationDatabase (compileCommandsJsonPath);
25- vector<fs::path> sourcePathsCandidates;
26- for (const auto &compileCommand : compilationDatabase->getAllCompileCommands ()) {
27- try {
28- fs::path path = Paths::getCCJsonFileFullPath (compileCommand.Filename , compileCommand.Directory );
29- sourcePathsCandidates.push_back (path);
30- } catch (...) {
31- throw CompilationDatabaseException (" Cannot detect file: " + compileCommand.Filename +
32- " . Maybe you need to rebuild the project." );
33- }
26+ if (autoDetect) {
27+ autoDetectSourcePathsIfNotEmpty ();
28+ } else {
29+ vector<fs::path> sourcePathsCandidates = getSourcePathCandidates ();
30+ sourcePaths = sourcePathsCandidates;
3431 }
35- auto requestSourcePaths = CollectionUtils::transformTo<vector<fs::path>>(
36- request.sourcepaths (), [](std::string const &sourcePath) { return fs::path (sourcePath); });
37- sourcePaths =
38- Paths::filterPathsByDirNames (sourcePathsCandidates, requestSourcePaths, { " .c" , " .cpp" , " .cc" });
3932 testingMethodsSourcePaths = sourcePaths;
4033 setInitializedTestsMap ();
4134}
@@ -55,3 +48,36 @@ void ProjectTestGen::setTargetForSource(const fs::path &sourcePath) {
5548const testsgen::ProjectRequest *ProjectTestGen::getRequest () const {
5649 return request;
5750}
51+
52+ vector<fs::path> ProjectTestGen::getRequestSourcePaths () const {
53+ return CollectionUtils::transformTo<vector<fs::path>>(
54+ request->sourcepaths (), [](std::string const &sourcePath) { return fs::path (sourcePath); });
55+ }
56+
57+ vector<fs::path> ProjectTestGen::getSourcePathCandidates () const {
58+ vector<fs::path> sourcePathsCandidates;
59+ for (const auto &compileCommand : compilationDatabase->getAllCompileCommands ()) {
60+ try {
61+ fs::path path = Paths::getCCJsonFileFullPath (compileCommand.Filename , compileCommand.Directory );
62+ sourcePathsCandidates.push_back (path);
63+ } catch (...) {
64+ throw CompilationDatabaseException (" Cannot detect file: " + compileCommand.Filename +
65+ " . Maybe you need to rebuild the project." );
66+ }
67+ }
68+ return sourcePathsCandidates;
69+ }
70+ void ProjectTestGen::autoDetectSourcePathsIfNotEmpty () {
71+ // requestSourcePaths are from settings.json
72+ auto requestSourcePaths = getRequestSourcePaths ();
73+ // sourcePathsCandidates are from compile_commands.json
74+ auto sourcePathsCandidates = getSourcePathCandidates ();
75+ if (!requestSourcePaths.empty ()) {
76+ sourcePaths =
77+ Paths::filterPathsByDirNames (sourcePathsCandidates, requestSourcePaths, Paths::isSourceFile);
78+ } else {
79+ sourcePaths = sourcePathsCandidates;
80+ }
81+ }
82+
83+
0 commit comments