Skip to content

Commit 9e480a9

Browse files
committed
fix
1 parent 88eaa23 commit 9e480a9

5 files changed

Lines changed: 52 additions & 23 deletions

File tree

gui/manualtest/projectfiledialog.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
Some manual testing in the project file dialog interface
55

66

7+
## Test: Relative paths
8+
9+
Ticket: #14983
10+
11+
1. Configure files/paths in project folder:
12+
* import a projectfile
13+
* add include paths in project folder
14+
* exclude file/folder
15+
16+
2. Save project
17+
18+
EXPECTED: Relative paths should be used in the XML
19+
20+
721
## Test: Platform file pic8.xml
822

923
Ticket: #14489

gui/projectfile.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,7 @@ void ProjectFile::setCheckPaths(const QStringList &paths)
751751

752752
void ProjectFile::setExcludedPaths(const QStringList &paths)
753753
{
754-
mExcludedPaths.clear();
755-
for (const QString &path : paths)
756-
mExcludedPaths << (QFileInfo(path).isAbsolute() ? getRelativePath(path) : path);
754+
mExcludedPaths = paths;
757755
}
758756

759757
void ProjectFile::setLibraries(const QStringList &libraries)
@@ -1199,7 +1197,7 @@ QString ProjectFile::getRelativePath(const QString &absolutePath) const
11991197
{
12001198
const QDir dir(QFileInfo(mFilename).absolutePath());
12011199
const QString relativePath(dir.relativeFilePath(absolutePath));
1202-
if (relativePath.startsWith("../.."))
1200+
if (relativePath.startsWith("../../..") || absolutePath.length() < relativePath.length())
12031201
return absolutePath;
12041202
return relativePath;
12051203
}

gui/projectfiledialog.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,9 @@ void ProjectFileDialog::addExcludeFile()
888888
QMap<QString,QString> filters;
889889
filters[tr("Source files")] = "*.c *.cpp";
890890
filters[tr("All files")] = "*.*";
891-
addExcludePath(QFileDialog::getOpenFileName(this, tr("Exclude file"), dir.canonicalPath(), toFilterString(filters)));
891+
QString fileName = QFileDialog::getOpenFileName(this, tr("Exclude file"), dir.canonicalPath(), toFilterString(filters));
892+
if (!fileName.isEmpty())
893+
addExcludePath(mProjectFile->getRelativePath(fileName));
892894
}
893895

894896
void ProjectFileDialog::editExcludePath()

gui/test/projectfile/testprojectfile.cpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,31 +214,44 @@ void TestProjectFile::emptyUserInclude() const
214214
QCOMPARE(settings.userIncludes.size(), 0);
215215
}
216216

217-
// Absolute path is made relative when it does not require walking up more than 2 parent folders
218-
void TestProjectFile::setExcludedPathsRelative() const
217+
// Absolute path is made relative when it does not require walking up more than 3 parent folders
218+
void TestProjectFile::getRelativePathRelative() const
219219
{
220220
ProjectFile projectFile;
221-
projectFile.setFilename("/some/path/123.cppcheck");
222-
projectFile.setExcludedPaths(QStringList() << "/some/externals/foo.cpp");
223-
QCOMPARE(projectFile.getExcludedPaths()[0], QString("../externals/foo.cpp"));
221+
projectFile.setFilename("/some/path/sub/123.cppcheck");
222+
QCOMPARE(projectFile.getRelativePath("/some/path/externals/foo.cpp"), QString("../externals/foo.cpp"));
224223
}
225224

226-
// Absolute path is kept as-is when making it relative would require walking up more than 2 parent folders
227-
void TestProjectFile::setExcludedPathsTooFarUp() const
225+
// Absolute path is made relative even when it requires walking up 2 parent folders
226+
void TestProjectFile::getRelativePathTwoUp() const
228227
{
229228
ProjectFile projectFile;
230-
projectFile.setFilename("/some/path/123.cppcheck");
231-
projectFile.setExcludedPaths(QStringList() << "/other/deep/foo.cpp");
232-
QCOMPARE(projectFile.getExcludedPaths()[0], QString("/other/deep/foo.cpp"));
229+
projectFile.setFilename("/some/path/sub/123.cppcheck");
230+
QCOMPARE(projectFile.getRelativePath("/some/externals/foo.cpp"), QString("../../externals/foo.cpp"));
233231
}
234232

235-
// Paths that are already relative are kept unchanged
236-
void TestProjectFile::setExcludedPathsAlreadyRelative() const
233+
// Absolute path is kept as-is when making it relative would require walking up more than 3 parent folders
234+
void TestProjectFile::getRelativePathTooFarUp() const
237235
{
238236
ProjectFile projectFile;
239-
projectFile.setFilename("/some/path/123.cppcheck");
240-
projectFile.setExcludedPaths(QStringList() << "gui/temp/");
241-
QCOMPARE(projectFile.getExcludedPaths()[0], QString("gui/temp/"));
237+
projectFile.setFilename("/some/path/sub/123.cppcheck");
238+
QCOMPARE(projectFile.getRelativePath("/other/deep/foo.cpp"), QString("/other/deep/foo.cpp"));
239+
}
240+
241+
// Absolute path in a subfolder of the project path is made relative without walking up at all
242+
void TestProjectFile::getRelativePathSubfolder() const
243+
{
244+
ProjectFile projectFile;
245+
projectFile.setFilename("/some/path/sub/123.cppcheck");
246+
QCOMPARE(projectFile.getRelativePath("/some/path/sub/src/file1.c"), QString("src/file1.c"));
247+
}
248+
249+
// Absolute path is kept as-is when it is shorter than the relative path, even if it does not require walking up 3 or more parent folders
250+
void TestProjectFile::getRelativePathAbsoluteShorter() const
251+
{
252+
ProjectFile projectFile;
253+
projectFile.setFilename("/ab/path/sub/123.cppcheck");
254+
QCOMPARE(projectFile.getRelativePath("/ab/foo.cpp"), QString("/ab/foo.cpp"));
242255
}
243256

244257
QTEST_MAIN(TestProjectFile)

gui/test/projectfile/testprojectfile.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ private slots:
3939

4040
void emptyUserInclude() const;
4141

42-
void setExcludedPathsRelative() const;
43-
void setExcludedPathsTooFarUp() const;
44-
void setExcludedPathsAlreadyRelative() const;
42+
void getRelativePathRelative() const;
43+
void getRelativePathTwoUp() const;
44+
void getRelativePathTooFarUp() const;
45+
void getRelativePathSubfolder() const;
46+
void getRelativePathAbsoluteShorter() const;
4547
};

0 commit comments

Comments
 (0)