From a484c1ff5bc9957d8c804559bcce4668b2944669 Mon Sep 17 00:00:00 2001 From: grasci <86058054+grasci-arm@users.noreply.github.com> Date: Tue, 5 Apr 2022 16:56:00 +0200 Subject: [PATCH] [packchk] wrong ERROR M310 Fixes #110 Co-authored-by: Sourabh Mehta <73165318+soumeh01@users.noreply.github.com> --- tools/packchk/src/CheckFiles.cpp | 68 +++++++++---------- .../test/unittests/src/TestCheckFiles.cpp | 44 ++++++++++++ 2 files changed, 78 insertions(+), 34 deletions(-) diff --git a/tools/packchk/src/CheckFiles.cpp b/tools/packchk/src/CheckFiles.cpp index 411734b0e..6d87d3134 100644 --- a/tools/packchk/src/CheckFiles.cpp +++ b/tools/packchk/src/CheckFiles.cpp @@ -356,60 +356,60 @@ bool CheckFiles::FindGetExactFileSystemName(const std::string& path, const std:: * @brief check name as written in PDSC against it's counterpart on the filesystem, for case sensitivity * @param fileName filename as written in PDSC * @param lineNo line number for error reporting - * @return + * @return true/false */ bool CheckFiles::CheckCaseSense(const string& fileName, int lineNo) { - if(fileName.empty()) { + if (fileName.empty()) { return true; } LogMsg("M058", PATH(fileName)); - string pdscPath = RteUtils::BackSlashesToSlashes(RteUtils::RemoveTrailingBackslash(fileName)); - string path = pdscPath; - string systemPath; - vector pathVect; - - do { - string actualPath = path; - path = RteUtils::ExtractFilePath(actualPath, 0); - string checkPath = RteUtils::ExtractFileName(actualPath); - string testPath = GetPackagePath(); - testPath += "/"; - testPath += path; - string outPath; - - if(FindGetExactFileSystemName(testPath, checkPath, outPath)) { - pathVect.push_back(outPath); + string filePath, testPath, outPath, packPath; + vector sysPathSegments; + std::list filePathSegments; + + packPath = GetPackagePath(); + filePath = RteUtils::BackSlashesToSlashes(RteUtils::RemoveTrailingBackslash(fileName)); + RteUtils::SplitString(filePathSegments, filePath, '/'); + testPath = packPath; + + for (const auto& seg : filePathSegments) { + if (seg == ".." || seg == ".") { + sysPathSegments.push_back(seg); + testPath += "/" + seg; + continue; } - } while(!path.empty() && path != "."); - for(auto it2 = pathVect.rbegin(); it2 != pathVect.rend(); it2++) { - if(!systemPath.empty()) { - systemPath += "/"; + if (FindGetExactFileSystemName(testPath, seg, outPath)) { + sysPathSegments.push_back(outPath); + testPath += "/" + outPath; + } + else { + string errMsg = string("file/folder \"") + seg + "\" not found"; + LogMsg("M103", VAL("REF", errMsg)); + return false; } - systemPath += *it2; } - string::size_type pos; - string::size_type endPos = pdscPath.find_first_not_of("./"); - do { - pos = pdscPath.find_first_of("./"); - if(pos < endPos && pos != string::npos) - pdscPath.erase(pos, 1); - } while(pos < endPos && pos != string::npos); + string systemPath; + for (const auto& itrSeg : sysPathSegments) { + if (!systemPath.empty()) { + systemPath += "/"; + } + systemPath += itrSeg; + } bool ok = true; - if(pdscPath.compare(systemPath)) { - LogMsg("M310", VAL("PDSC", pdscPath), VAL("SYSTEM", systemPath), lineNo); + if (filePath.compare(systemPath)) { + LogMsg("M310", VAL("PDSC", filePath), VAL("SYSTEM", systemPath), lineNo); ok = false; } - if(ok) { + if (ok) { LogMsg("M010"); } - return ok; } diff --git a/tools/packchk/test/unittests/src/TestCheckFiles.cpp b/tools/packchk/test/unittests/src/TestCheckFiles.cpp index 11de877c2..61d4e60b2 100644 --- a/tools/packchk/test/unittests/src/TestCheckFiles.cpp +++ b/tools/packchk/test/unittests/src/TestCheckFiles.cpp @@ -37,6 +37,7 @@ class TestCheckFiles : public Test { void TearDown() override { errLog.ClearLogMessages(); + RteFsUtils::RemoveDir(string(BUILD_FOLDER) + "/testdata"); } private: @@ -145,3 +146,46 @@ TEST_F(TestCheckFiles, CheckFileExtension_NullItem) { // THEN the check is expected to pass EXPECT_TRUE(res); } + +TEST_F(TestCheckFiles, CheckCaseSense) +{ + // test setup + string packPath = checkFiles.GetPackagePath(); + string testDataFolder = packPath + "/testdata"; + const string& testApiFolder = testDataFolder + "/Api"; + if (RteFsUtils::Exists(testDataFolder)) { + RteFsUtils::RemoveDir(testDataFolder); + } + ASSERT_TRUE(RteFsUtils::CreateDirectories(testApiFolder)); + ASSERT_TRUE(RteFsUtils::CreateFile( + testApiFolder + "/Exclusive.h", RteUtils::EMPTY_STRING)); + ASSERT_TRUE(RteFsUtils::CreateDirectories(testDataFolder + "/.test1")); + ASSERT_TRUE(RteFsUtils::CreateFile( + testDataFolder + "/.test1/NonExclusive.h", RteUtils::EMPTY_STRING)); + checkFiles.SetPackagePath(testDataFolder); + + // test + map testInputs = { + // FilePath, expectedResults + { RteUtils::EMPTY_STRING, true}, + { "Api\\Exclusive.h", true}, + { "Api/Exclusive.h", true}, + { "./Api/Exclusive.h", true}, + { "././././Api/Exclusive.h", true}, + { ".test1/NonExclusive.h", true}, + { ".test1/../Api/Exclusive.h", true}, + { "../testdata/Api/Exclusive.h", true}, + { "../Invalid/Path/Exclusive.h", false}, + { "api\\exclusive.h", false}, + { "api/exclusive.h", false} + }; + + for (const auto& [filePath, result] : testInputs) { + EXPECT_EQ(result, checkFiles.CheckCaseSense(filePath, 1)) << + "error: failed for input \"" << filePath << "\"" << endl; + } + + // cleanup + RteFsUtils::RemoveDir(testDataFolder); + checkFiles.SetPackagePath(packPath); +}