Skip to content

Commit d4ace94

Browse files
committed
abs
1 parent cada3a2 commit d4ace94

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

gui/projectfile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ QString ProjectFile::getRelativePath(const QString &absolutePath) const
11971197
{
11981198
const QDir dir(QFileInfo(mFilename).absolutePath());
11991199
const QString relativePath(dir.relativeFilePath(absolutePath));
1200-
if (relativePath.startsWith("../.."))
1200+
if (relativePath.startsWith("../../..") || absolutePath.length() < relativePath.length())
12011201
return absolutePath;
12021202
return relativePath;
12031203
}

gui/test/projectfile/testprojectfile.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,28 +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
217+
// Absolute path is made relative when it does not require walking up more than 3 parent folders
218218
void TestProjectFile::getRelativePathRelative() const
219219
{
220220
ProjectFile projectFile;
221-
projectFile.setFilename("/some/path/123.cppcheck");
222-
QCOMPARE(projectFile.getRelativePath("/some/externals/foo.cpp"), 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"));
223+
}
224+
225+
// Absolute path is made relative even when it requires walking up 2 parent folders
226+
void TestProjectFile::getRelativePathTwoUp() const
227+
{
228+
ProjectFile projectFile;
229+
projectFile.setFilename("/some/path/sub/123.cppcheck");
230+
QCOMPARE(projectFile.getRelativePath("/some/externals/foo.cpp"), QString("../../externals/foo.cpp"));
223231
}
224232

225-
// Absolute path is kept as-is when making it relative would require walking up more than 2 parent folders
233+
// Absolute path is kept as-is when making it relative would require walking up more than 3 parent folders
226234
void TestProjectFile::getRelativePathTooFarUp() const
227235
{
228236
ProjectFile projectFile;
229-
projectFile.setFilename("/some/path/123.cppcheck");
237+
projectFile.setFilename("/some/path/sub/123.cppcheck");
230238
QCOMPARE(projectFile.getRelativePath("/other/deep/foo.cpp"), QString("/other/deep/foo.cpp"));
231239
}
232240

233241
// Absolute path in a subfolder of the project path is made relative without walking up at all
234242
void TestProjectFile::getRelativePathSubfolder() const
235243
{
236244
ProjectFile projectFile;
237-
projectFile.setFilename("/some/path/123.cppcheck");
238-
QCOMPARE(projectFile.getRelativePath("/some/path/src/file1.c"), QString("src/file1.c"));
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"));
239255
}
240256

241257
QTEST_MAIN(TestProjectFile)

gui/test/projectfile/testprojectfile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ private slots:
4040
void emptyUserInclude() const;
4141

4242
void getRelativePathRelative() const;
43+
void getRelativePathTwoUp() const;
4344
void getRelativePathTooFarUp() const;
4445
void getRelativePathSubfolder() const;
46+
void getRelativePathAbsoluteShorter() const;
4547
};

0 commit comments

Comments
 (0)