Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions src/coreComponents/dataRepository/unitTests/testXmlWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "dataRepository/xmlWrapper.hpp"
#include "common/format/EnumStrings.hpp"

#include <filesystem>
#include <fstream>

using namespace geos;

TEST( testXmlWrapper, array3d_errors )
Expand Down Expand Up @@ -501,6 +504,214 @@ TEST( testXmlWrapper, testGroupNamesArrayFormats )
}
}

class CollectIncludedTest : public ::testing::Test
{
protected:
std::filesystem::path m_tempDir;

void SetUp() override
{
m_tempDir = std::filesystem::temp_directory_path() / "geos_collectIncluded_test";
std::filesystem::create_directories( m_tempDir );
}

void TearDown() override
{
std::filesystem::remove_all( m_tempDir );
}

string filePath( string const & filename )
{
return ( m_tempDir / filename ).string();
}

void writeXML( string const & filename, string const & content )
{
std::ofstream f( filePath( filename ) );
f << content;
}
};


TEST_F( CollectIncludedTest, collectIncluded_noIncludes )
{
writeXML( "base.xml", "<Problem>"
"</Problem>" );

auto result = xmlWrapper::collectIncluded( filePath( "base.xml" ) );

EXPECT_TRUE( result.empty() );
}

TEST_F( CollectIncludedTest, collectIncluded_singleInclude )
{
writeXML( "child.xml", "<Problem>"
"</Problem>" );
writeXML( "base.xml", "<Problem>"
" <Included>"
" <File name=\"child.xml\"/>"
" </Included>"
"</Problem>" );

auto result = xmlWrapper::collectIncluded( filePath( "base.xml" ) );

EXPECT_NE( result.find( filePath( "child.xml" ) ), result.end() );
}

TEST_F( CollectIncludedTest, collectIncluded_multipleIncludes )
{
writeXML( "child1.xml", "<Problem>"
"</Problem>" );
writeXML( "child2.xml", "<Problem>"
"</Problem>" );
writeXML( "base.xml", "<Problem>"
" <Included>"
" <File name=\"child1.xml\"/>"
" <File name=\"child2.xml\"/>"
" </Included>"
"</Problem>" );

auto result = xmlWrapper::collectIncluded( filePath( "base.xml" ) );

EXPECT_NE( result.find( filePath( "child1.xml" ) ), result.end() );
EXPECT_NE( result.find( filePath( "child2.xml" ) ), result.end() );
}

TEST_F( CollectIncludedTest, collectIncluded_emptyNameAttribute )
{
writeXML( "base.xml", "<Problem>"
" <Included>"
" <File name=\"\"/>"
" </Included>"
"</Problem>" );

std::set< string > result;

EXPECT_ANY_THROW( xmlWrapper::collectIncluded( filePath( "base.xml" ), result ) );

EXPECT_TRUE( result.empty() );
}

TEST_F( CollectIncludedTest, collectIncluded_existingEntriesKept )
{
std::set< string > existingCollection;
existingCollection.insert( "/somewhere/thereisanalreadyexistingxmlfile.xml" );
writeXML( "base.xml", "<Problem>"
"</Problem>" );

xmlWrapper::collectIncluded( filePath( "base.xml" ), existingCollection );

EXPECT_NE( existingCollection.find( "/somewhere/thereisanalreadyexistingxmlfile.xml" ),
existingCollection.end() );
}


TEST_F( CollectIncludedTest, collectIncludedRecursive_noIncludes )
{
writeXML( "base.xml", "<Problem>"
"</Problem>" );

auto result = xmlWrapper::collectIncludedRecursive( filePath( "base.xml" ) );

EXPECT_EQ( result.size(), 1 ); // size 1 because collectIncludedRecursive collects the base file
}

TEST_F( CollectIncludedTest, collectIncludedRecursive_singleInclude )
{
writeXML( "child.xml", "<Problem>"
"</Problem>" );
writeXML( "base.xml", "<Problem>"
" <Included>"
" <File name=\"child.xml\"/>"
" </Included>"
"</Problem>" );

auto result = xmlWrapper::collectIncludedRecursive( filePath( "base.xml" ) );

EXPECT_NE( result.find( filePath( "child.xml" ) ), result.end() );
}

TEST_F( CollectIncludedTest, collectIncludedRecursive_multipleIncludes )
{
writeXML( "child1.xml", "<Problem>"
"</Problem>" );
writeXML( "child2.xml", "<Problem>"
"</Problem>" );
writeXML( "base.xml", "<Problem>"
" <Included>"
" <File name=\"child1.xml\"/>"
" <File name=\"child2.xml\"/>"
" </Included>"
"</Problem>" );

auto result = xmlWrapper::collectIncludedRecursive( filePath( "base.xml" ) );

EXPECT_NE( result.find( filePath( "child1.xml" ) ), result.end() );
EXPECT_NE( result.find( filePath( "child2.xml" ) ), result.end() );
}

TEST_F( CollectIncludedTest, collectIncludedRecursive_simpleRecursive )
{
writeXML( "child.xml", "<Problem>"
"</Problem>" );

writeXML( "middle.xml", "<Problem>"
" <Included>"
" <File name=\"child.xml\"/>"
" </Included>"
"</Problem>" );

writeXML( "base.xml", "<Problem>"
" <Included>"
" <File name=\"middle.xml\"/>"
" </Included>"
"</Problem>" );

auto result = xmlWrapper::collectIncludedRecursive( filePath( "base.xml" ) );

EXPECT_NE( result.find( filePath( "middle.xml" ) ), result.end() );
EXPECT_NE( result.find( filePath( "child.xml" ) ), result.end() );
}

TEST_F( CollectIncludedTest, collectIncludedRecursive_cyclePrevention )
{
writeXML( "cycle.xml", "<Problem>"
" <Included>"
" <File name=\"cycle.xml\"/>"
" </Included>"
"</Problem>" );

auto result = xmlWrapper::collectIncludedRecursive( filePath( "cycle.xml" ) );

EXPECT_NE( result.find( filePath( "cycle.xml" ) ), result.end() );
}

TEST_F( CollectIncludedTest, collectIncludedRecursive_noDuplicates )
{
writeXML( "base.xml", "<Problem>"
" <Included>"
" <File name=\"base.xml\"/>"
" </Included>"
"</Problem>" );

auto result = xmlWrapper::collectIncludedRecursive( filePath( "base.xml" ) );

EXPECT_EQ( result.size(), 1 ); // collectIncludedRecursive collects the base file
}

TEST_F( CollectIncludedTest, collectIncludedRecursive_existingEntriesKept )
{
std::set< string > existingCollection;
existingCollection.insert( "/somewhere/thereisanalreadyexistingxmlfile.xml" );
writeXML( "base.xml", "<Problem>"
"</Problem>" );

xmlWrapper::collectIncludedRecursive( filePath( "base.xml" ), existingCollection );

EXPECT_NE( existingCollection.find( "/somewhere/thereisanalreadyexistingxmlfile.xml" ),
existingCollection.end() );
}


int main( int argc, char * argv[] )
{
Expand Down
86 changes: 86 additions & 0 deletions src/coreComponents/dataRepository/xmlWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "common/format/StringUtilities.hpp"
#include "common/MpiWrapper.hpp"
#include "dataRepository/KeyNames.hpp"
#include "common/Path.hpp"

namespace geos
{
Expand Down Expand Up @@ -268,6 +269,91 @@ string buildMultipleInputXML( string_array const & inputFileList,
return inputFileName;
}

void collectIncluded( string const & filePath,
std::set< string > & collection )
{
xmlDocument doc;
xmlResult result = doc.loadFile( filePath );
GEOS_THROW_IF( !result,
GEOS_FMT( "Could not load XML file '{}': {}", filePath, result.description() ),
InputError );
xmlNode rootNode = doc.getFirstChild();

string const currentDir = splitPath( filePath ).first;

for( auto & includedNode : rootNode.children( includedListTag ) )
{
for( auto & fileNode : includedNode.children( includedFileTag ) )
{
string const fileName = fileNode.attribute( "name" ).value();

GEOS_THROW_IF( fileName.empty(),
GEOS_FMT( "An included file entry in '{}' has an empty or missing 'name' attribute.", filePath ),
InputError );

string absolutePath = isAbsolutePath( fileName )
? getAbsolutePath( fileName )
: getAbsolutePath( joinPath( currentDir, fileName ) );
collection.insert( absolutePath );
}
}
}

std::set< string > collectIncluded( string const & filePath )
{
std::set< string > collection;
collectIncluded( filePath, collection );
return collection;
}

void collectIncludedRecursive( string const & filePath,
std::set< string > & collection )
{
// We want absolute paths
string const absFilePath = getAbsolutePath( filePath );

if( collection.count( absFilePath ) > 0 )
{
return;
}
collection.insert( absFilePath );

xmlDocument doc;
xmlResult result = doc.loadFile( absFilePath );
GEOS_THROW_IF( !result,
GEOS_FMT( "Could not load XML file '{}': {}", filePath, result.description() ),
InputError );
xmlNode rootNode = doc.getFirstChild();

string const currentDir = splitPath( filePath ).first;

for( auto & includedNode : rootNode.children( includedListTag ) )
{
for( auto & fileNode : includedNode.children( includedFileTag ) )
{
string const includedFilePath = fileNode.attribute( "name" ).value();

if( includedFilePath.empty() )
{
continue;
}

string includedAbsPath = isAbsolutePath( includedFilePath )
? getAbsolutePath( includedFilePath )
: getAbsolutePath( joinPath( currentDir, includedFilePath ) );
collectIncludedRecursive( includedAbsPath,
collection );
}
}
}

std::set< string > collectIncludedRecursive( string const & filePath )
{
std::set< string > collection;
collectIncludedRecursive( filePath, collection );
return collection;
}

bool isFileMetadataAttribute( string const & name )
{
static const std::set< string > fileMetadataAttributes {
Expand Down
44 changes: 44 additions & 0 deletions src/coreComponents/dataRepository/xmlWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,50 @@ constexpr char const includedFileTag[] = "File";
string buildMultipleInputXML( string_array const & inputFileList,
string const & outputDir = {} );

/**
* @brief Collect the absolute paths of XML files directly included
* by a given xml file
* @param[in] filePath absolute path of the xml file to inspect
* @param[inout] collection collection to append with absolute file paths
*
* Only one level of inclusion is collected (files included by the included
* files are not added). See collectIncludedRecursive if you want this behavior.
* Duplicate entries are not inserted in @p collection
*/
void collectIncluded( string const & filePath,
std::set< string > & collection );

/**
* @brief Collect the absolute paths of XML files directly included
* by a given xml file
* @param[in] filePath absolute path of the xml file to inspect
* @return a collection of absolute paths
*
* Only one level of inclusion is collected (files included by the included
* files are not added). See collectIncludedRecursive if you want this behavior.
* Duplicate entries are not inserted in @p collection
*/
std::set< string > collectIncluded( string const & filePath );

/**
* @brief Recursively collect the absolute paths of an XML file and all XML
* files it includes
* @param[in] filePath absolute path of the root XML file
* @param[inout] collection collection to append with absolute file paths
* of every visited file (including @p filePath itself)
*/
void collectIncludedRecursive( string const & filePath,
std::set< string > & collection );

/**
* @brief Recursively collect the absolute paths of an XML file and all XML
* files it includes
* @param[in] filePath absolute path of the root XML file
* @return a collection of absolute paths of every visited file (including
* @p filePath itself)
*/
std::set< string > collectIncludedRecursive( string const & filePath );

/**
* @return true if the attribute with the specified name declares metadata relative to the xml
* @param name the name of an attribute
Expand Down
2 changes: 2 additions & 0 deletions src/coreComponents/fileIO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Contains:
#
set( fileIO_headers
LogLevelsInfo.hpp
Outputs/ArchiveInputDeck.hpp
Outputs/BlueprintOutput.hpp
Outputs/MemoryStatsOutput.hpp
Outputs/OutputBase.hpp
Expand All @@ -43,6 +44,7 @@ set( fileIO_headers
# Specify all sources
#
set( fileIO_sources
Outputs/ArchiveInputDeck.cpp
Outputs/BlueprintOutput.cpp
Outputs/MemoryStatsOutput.cpp
Outputs/OutputBase.cpp
Expand Down
Loading
Loading