Skip to content

Commit

Permalink
feat: added a method in ConfigurationParser to validate if a file is …
Browse files Browse the repository at this point in the history
…a valid yaml file
  • Loading branch information
Nicogp committed Nov 12, 2024
1 parent 4fb8806 commit a1b61a3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,14 @@ namespace configuration
throw;
}
}

/// @brief Checks if the specified YAML file is valid.
///
/// This function attempts to load the YAML file located at the given path.
/// If the file can be loaded without throwing an exception, it is considered valid.
///
/// @param configFile The path to the YAML file to be validated.
/// @return `true` if the file is a valid YAML file; `false` otherwise.
bool isValidYamlFile(const std::filesystem::path& configFile) const;
};
} // namespace configuration
13 changes: 13 additions & 0 deletions src/agent/configuration_parser/src/configuration_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,17 @@ namespace configuration
}
}

bool ConfigurationParser::isValidYamlFile(const std::filesystem::path& configFile) const
{
try
{
YAML::LoadFile(configFile.string());
return true;
}
catch (const std::exception&)
{
return false;
}
}

} // namespace configuration
30 changes: 30 additions & 0 deletions src/agent/configuration_parser/tests/configuration_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,36 @@ TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault)
}
}

TEST_F(ConfigurationParserInvalidYamlFileTest, isValidYamlFileInvalid)
{
try
{
const auto parser = std::make_unique<configuration::ConfigurationParser>();

EXPECT_FALSE(parser->isValidYamlFile(m_tempConfigFilePath));
}
catch (const std::exception&)
{
std::filesystem::remove(m_tempConfigFilePath);
throw;
}
}

TEST_F(ConfigurationParserFileTest, isValidYamlFileValid)
{
try
{
const auto parser = std::make_unique<configuration::ConfigurationParser>();

EXPECT_TRUE(parser->isValidYamlFile(m_tempConfigFilePath));
}
catch (const std::exception&)
{
std::filesystem::remove(m_tempConfigFilePath);
throw;
}
}

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit a1b61a3

Please sign in to comment.