Files
ResInsight/ApplicationLibCode/UnitTests/RiaFilePathTools-Test.cpp
T
Magne Sjaastad 5e791353b2 #14470 Do not truncate folder names containing a dot when removing file extension
The ensemble file set removed the file extension by searching for the last dot in the complete path. When the path pattern was already without extension, the last dot was found in a folder name, truncating the path pattern and causing the search for SMSPEC files to fail.

Add RiaFilePathTools::removeFileExtension based on std::filesystem::path::replace_extension, and use it both in RimEnsembleFileSet and in the grid and summary ensemble import, replacing a duplicated lambda doing the same operation.
2026-08-06 14:10:49 +02:00

196 lines
7.9 KiB
C++

#include "gtest/gtest.h"
#include "RiaFilePathTools.h"
#include <iostream>
std::ostream& operator<<( std::ostream& out, const QString& text )
{
out << text.toStdString();
return out;
}
//--------------------------------------------------------------------------------------------------
TEST( RiaFilePathTools, rootSearchPathFromSearchFilter )
{
{
QString testPath( "" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "" ), resultRootPath );
}
{
QString testPath( "D:/" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "D:/" ), resultRootPath );
}
{
QString testPath( "D:/A" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "D:/A" ), resultRootPath );
}
{
QString testPath( "D:/A/B[cd]/E" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "D:/A" ), resultRootPath );
}
{
QString testPath( "/A/B[cd]/E" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "/A" ), resultRootPath );
}
{
QString testPath( "/A/B?/E" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "/A" ), resultRootPath );
}
{
QString testPath( "//A/B/E*" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "//A/B" ), resultRootPath );
}
{
QString testPath( "//A/B/E" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "//A/B/E" ), resultRootPath );
}
{
QString testPath( "//A/B/E/" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "//A/B/E/" ), resultRootPath );
}
{
QString testPath( "//A/B[[]/E/" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "//A/B[[]/E/" ), resultRootPath );
}
{
QString testPath( "//A/B[]]/E/" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "//A/B[]]/E/" ), resultRootPath );
}
{
QString testPath( "//A/B[*]/E/" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "//A/B[*]/E/" ), resultRootPath );
}
{
QString testPath( "//A/B[?]/E/" );
QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter( testPath );
EXPECT_EQ( QString( "//A/B[?]/E/" ), resultRootPath );
}
}
//--------------------------------------------------------------------------------------------------
TEST( RiaFilePathTools, removeDuplicatePathSeparators )
{
{
QString testPath( "//myshare/folder-a/folder-b/" );
QString resultRootPath = RiaFilePathTools::removeDuplicatePathSeparators( testPath );
EXPECT_STRCASEEQ( testPath.toLatin1(), resultRootPath.toLatin1() );
}
{
QString testPath( "//myshare/folder-a//folder-b/" );
QString expectedPath( "//myshare/folder-a/folder-b/" );
QString resultRootPath = RiaFilePathTools::removeDuplicatePathSeparators( testPath );
EXPECT_STRCASEEQ( expectedPath.toLatin1(), resultRootPath.toLatin1() );
}
{
QString testPath( R"(\\myshare\folder-a\folder-b\)" );
QString resultRootPath = RiaFilePathTools::removeDuplicatePathSeparators( testPath );
EXPECT_STRCASEEQ( testPath.toLatin1(), resultRootPath.toLatin1() );
}
{
QString testPath( R"(\\myshare\folder-a\\folder-b\\)" );
QString expectedPath( R"(\\myshare\folder-a\folder-b\)" );
QString resultRootPath = RiaFilePathTools::removeDuplicatePathSeparators( testPath );
EXPECT_STRCASEEQ( expectedPath.toLatin1(), resultRootPath.toLatin1() );
}
}
//--------------------------------------------------------------------------------------------------
TEST( RiaFilePathTools, splitIntoComponets )
{
{
QString testPath( "e:/models/from_equinor_sftp/drogon3d_ahm/realization-0/iter-3/eclipse/model/DROGON-0.SMSPEC" );
auto words = RiaFilePathTools::splitPathIntoComponents( testPath );
EXPECT_EQ( 8, words.size() );
EXPECT_EQ( QString( "models" ), words[0] );
EXPECT_EQ( QString( "from_equinor_sftp" ), words[1] );
EXPECT_EQ( QString( "drogon3d_ahm" ), words[2] );
EXPECT_EQ( QString( "realization-0" ), words[3] );
EXPECT_EQ( QString( "iter-3" ), words[4] );
EXPECT_EQ( QString( "eclipse" ), words[5] );
EXPECT_EQ( QString( "model" ), words[6] );
EXPECT_EQ( QString( "DROGON-0.SMSPEC" ), words[7] );
}
{
QString testPath( "/home/builder/models/realization-0/iter-3/eclipse/model/DROGON-0.SMSPEC" );
auto words = RiaFilePathTools::splitPathIntoComponents( testPath );
EXPECT_EQ( 8, words.size() );
EXPECT_EQ( QString( "home" ), words[0] );
EXPECT_EQ( QString( "builder" ), words[1] );
EXPECT_EQ( QString( "models" ), words[2] );
EXPECT_EQ( QString( "realization-0" ), words[3] );
EXPECT_EQ( QString( "iter-3" ), words[4] );
EXPECT_EQ( QString( "eclipse" ), words[5] );
EXPECT_EQ( QString( "model" ), words[6] );
EXPECT_EQ( QString( "DROGON-0.SMSPEC" ), words[7] );
}
}
//--------------------------------------------------------------------------------------------------
TEST( RiaFilePathTools, keyPathComponentsForEachFilePath )
{
{
QString testPath0( "e:/models/from_equinor_sftp/drogon3d_ahm/realization-0/iter-3/eclipse/model/DROGON-0.SMSPEC" );
QString testPath1( "e:/models/from_equinor_sftp/drogon3d_ahm/realization-1/iter-3/eclipse/model/DROGON-1.SMSPEC" );
QStringList allPaths = { testPath0, testPath1 };
auto keyComponents = RiaFilePathTools::keyPathComponentsForEachFilePath( allPaths );
auto test0 = keyComponents[testPath0];
EXPECT_EQ( QString( "realization-0" ), test0.front() );
auto test1 = keyComponents[testPath1];
EXPECT_EQ( QString( "realization-1" ), test1.front() );
}
}
//--------------------------------------------------------------------------------------------------
TEST( RiaFilePathTools, removeFileExtension )
{
// A folder name can contain a dot. Only a file extension is removed, the folder name is left untouched.
const QString pathWithExtension = "/scratch/fmu/user/drogon.2024/realization-$(INDEX)/iter-0/eclipse/model/DROGON-$(INDEX).ESMRY";
EXPECT_EQ( QString( "/scratch/fmu/user/drogon.2024/realization-$(INDEX)/iter-0/eclipse/model/DROGON-$(INDEX)" ),
RiaFilePathTools::removeFileExtension( pathWithExtension ) );
// A path without a file extension must be returned unmodified, see https://github.com/OPM/ResInsight/issues/14470
const QString pathWithoutExtension = "/scratch/fmu/user/drogon.2024/realization-$(INDEX)/iter-0/eclipse/model/DROGON-$(INDEX)";
EXPECT_EQ( pathWithoutExtension, RiaFilePathTools::removeFileExtension( pathWithoutExtension ) );
#ifdef WIN32
// A backslash is only recognized as a path separator on Windows
const QString windowsPath = "d:\\scratch\\drogon.2024\\realization-0\\DROGON-0";
EXPECT_EQ( windowsPath, RiaFilePathTools::removeFileExtension( windowsPath ) );
#endif
// No folder part
EXPECT_EQ( QString( "DROGON-0" ), RiaFilePathTools::removeFileExtension( "DROGON-0.SMSPEC" ) );
// Empty path
EXPECT_EQ( QString( "" ), RiaFilePathTools::removeFileExtension( "" ) );
}