Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Generate config descriptions #1842

Merged
2 changes: 0 additions & 2 deletions src/app/CliArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ CliArgs::parse(int argc, char const* argv[])

if (parsed.count("config-description") != 0u) {
std::filesystem::path filePath = parsed["config-description"].as<std::string>();
if (!filePath.empty() && !filePath.string().ends_with(".md"))
filePath += ".md";

auto const res = util::config::ClioConfigDescription::generateConfigDescriptionToFile(filePath);
if (res.has_value())
Expand Down
1 change: 1 addition & 0 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_sources(
newconfig/ConfigDefinition.cpp
newconfig/ConfigFileJson.cpp
newconfig/ObjectView.cpp
newconfig/Types.cpp
newconfig/ValueView.cpp
)

Expand Down
13 changes: 10 additions & 3 deletions src/util/newconfig/ConfigDescription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <algorithm>
#include <array>
#include <cerrno>
#include <cstring>
#include <expected>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -86,11 +88,16 @@
// Validate the directory exists
auto const dir = fs::path(fullFilePath).parent_path();
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
if (!dir.empty() && !fs::exists(dir))
return std::unexpected<Error>{fmt::format("Error: File path {} does not exist", fullFilePath.string())};
return std::unexpected<Error>{

Check warning on line 91 in src/util/newconfig/ConfigDescription.hpp

View check run for this annotation

Codecov / codecov/patch

src/util/newconfig/ConfigDescription.hpp#L91

Added line #L91 was not covered by tests
fmt::format("Error: Directory {} does not exist or provided path is invalid", dir.string())
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
};

Check warning on line 93 in src/util/newconfig/ConfigDescription.hpp

View check run for this annotation

Codecov / codecov/patch

src/util/newconfig/ConfigDescription.hpp#L93

Added line #L93 was not covered by tests

std::ofstream file(fullFilePath);
if (!file.is_open())
return std::unexpected<Error>{fmt::format("Failed to create file: {}", fullFilePath.string())};
if (!file.is_open()) {
return std::unexpected{
fmt::format("Failed to create file '{}': {}", fullFilePath.string(), std::strerror(errno))
};
}

writeConfigDescriptionToFile(file);
file.close();
Expand Down
66 changes: 66 additions & 0 deletions src/util/newconfig/Types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2025, the clio developers.

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include "util/newconfig/Types.hpp"

#include <cstdint>
#include <ostream>
#include <string>
#include <variant>

namespace util::config {

std::ostream&
operator<<(std::ostream& stream, ConfigType type)
{
switch (type) {
case ConfigType::Integer:
stream << "int";
break;
case ConfigType::String:
stream << "string";
break;
case ConfigType::Double:
stream << "double";
break;
case ConfigType::Boolean:
stream << "boolean";
break;
default:
stream << "unsupported type";

Check warning on line 46 in src/util/newconfig/Types.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/newconfig/Types.cpp#L45-L46

Added lines #L45 - L46 were not covered by tests
}
return stream;
}

std::ostream&
operator<<(std::ostream& stream, Value value)
{
if (std::holds_alternative<std::string>(value)) {
stream << std::get<std::string>(value);
} else if (std::holds_alternative<bool>(value)) {
stream << (std::get<bool>(value) ? "False" : "True");
} else if (std::holds_alternative<double>(value)) {
stream << std::get<double>(value);
} else if (std::holds_alternative<int64_t>(value)) {
stream << std::get<int64_t>(value);
}
return stream;
}

} // namespace util::config
39 changes: 4 additions & 35 deletions src/util/newconfig/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,8 @@ enum class ConfigType { Integer, String, Double, Boolean };
* @param type The config type
* @return The same ostream we were given
*/
inline std::ostream&
operator<<(std::ostream& stream, ConfigType type)
{
switch (type) {
case ConfigType::Integer:
stream << "int";
break;
case ConfigType::String:
stream << "string";
break;
case ConfigType::Double:
stream << "double";
break;
case ConfigType::Boolean:
stream << "boolean";
break;
default:
stream << "unsupported type";
}
return stream;
}
std::ostream&
operator<<(std::ostream& stream, ConfigType type);

/** @brief Represents the supported Config Values */
using Value = std::variant<int64_t, std::string, bool, double>;
Expand All @@ -71,20 +52,8 @@ using Value = std::variant<int64_t, std::string, bool, double>;
* @param value The value type
* @return The same ostream we were given
*/
inline std::ostream&
operator<<(std::ostream& stream, Value value)
{
if (std::holds_alternative<std::string>(value)) {
stream << std::get<std::string>(value);
} else if (std::holds_alternative<bool>(value)) {
stream << (std::get<bool>(value) ? "False" : "True");
} else if (std::holds_alternative<double>(value)) {
stream << std::get<double>(value);
} else if (std::holds_alternative<int64_t>(value)) {
stream << std::get<int64_t>(value);
}
return stream;
}
std::ostream&
operator<<(std::ostream& stream, Value value);

/**
* @brief Get the corresponding clio config type
Expand Down
6 changes: 6 additions & 0 deletions tests/common/util/TmpFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ struct TmpFile {
ofs << content;
}

static TmpFile
empty()
{
return TmpFile{""};
}

TmpFile(TmpFile const&) = delete;
TmpFile(TmpFile&& other) : path{std::move(other.path)}
{
Expand Down
59 changes: 31 additions & 28 deletions tests/unit/app/CliArgsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include "app/CliArgs.hpp"
#include "util/TmpFile.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigDescription.hpp"

Expand All @@ -41,8 +42,6 @@ struct CliArgsTests : testing::Test {
testing::StrictMock<testing::MockFunction<int(CliArgs::Action::VerifyConfig)>> onVerifyMock;
};

static constexpr auto kCONFIG_DESCRIPTION_FILE_NAME = "../configDescription.md";

TEST_F(CliArgsTests, Parse_NoArgs)
{
std::array argv{"clio_server"};
Expand Down Expand Up @@ -154,15 +153,37 @@ TEST_F(CliArgsTests, Parse_VerifyConfig)
);
}

TEST_F(CliArgsTests, Parse_ConfigDescription)
TEST_F(CliArgsTests, Parse_ConfigDescriptionInvalidPath)
{
std::array argv{"clio_server", "--config-description", kCONFIG_DESCRIPTION_FILE_NAME};
using namespace util::config;
std::array argv{"clio_server", "--config-description", ""};
auto const action = CliArgs::parse(argv.size(), argv.data());
EXPECT_CALL(onExitMock, Call).WillOnce([](CliArgs::Action::Exit const& exit) { return exit.exitCode; });

EXPECT_EQ(
action.apply(
onRunMock.AsStdFunction(),
onExitMock.AsStdFunction(),
onMigrateMock.AsStdFunction(),
onVerifyMock.AsStdFunction()
),
EXIT_FAILURE
);
}

struct CliArgsTestsWithTmpFile : CliArgsTests {
TmpFile tmpFile = TmpFile::empty();
};

TEST_F(CliArgsTestsWithTmpFile, Parse_ConfigDescription)
{
std::array argv{"clio_server", "--config-description", tmpFile.path.c_str()};
auto const action = CliArgs::parse(argv.size(), argv.data());
EXPECT_CALL(onExitMock, Call).WillOnce([](CliArgs::Action::Exit const& exit) { return exit.exitCode; });

// user provide config markdown file name as well
ASSERT_TRUE(std::filesystem::exists(kCONFIG_DESCRIPTION_FILE_NAME));
std::filesystem::remove(kCONFIG_DESCRIPTION_FILE_NAME);
ASSERT_TRUE(std::filesystem::exists(tmpFile.path.c_str()));
std::filesystem::remove(tmpFile.path.c_str());
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved

EXPECT_EQ(
action.apply(
Expand All @@ -175,16 +196,16 @@ TEST_F(CliArgsTests, Parse_ConfigDescription)
);
}

TEST_F(CliArgsTests, Parse_ConfigDescriptionFileContent)
TEST_F(CliArgsTestsWithTmpFile, Parse_ConfigDescriptionFileContent)
{
using namespace util::config;

std::ofstream file(kCONFIG_DESCRIPTION_FILE_NAME);
std::ofstream file(tmpFile.path.c_str());
ASSERT_TRUE(file.is_open());
ClioConfigDescription::writeConfigDescriptionToFile(file);
file.close();

std::ifstream inFile(kCONFIG_DESCRIPTION_FILE_NAME);
std::ifstream inFile(tmpFile.path.c_str());
ASSERT_TRUE(inFile.is_open());

std::stringstream buffer;
Expand All @@ -200,23 +221,5 @@ TEST_F(CliArgsTests, Parse_ConfigDescriptionFileContent)
for (auto const& key : gClioConfig)
EXPECT_TRUE(fileContent.find(key.first));

std::filesystem::remove(kCONFIG_DESCRIPTION_FILE_NAME);
}

TEST_F(CliArgsTests, Parse_ConfigDescriptionInvalidPath)
{
using namespace util::config;
std::array argv{"clio_server", "--config-description", ""};
auto const action = CliArgs::parse(argv.size(), argv.data());
EXPECT_CALL(onExitMock, Call).WillOnce([](CliArgs::Action::Exit const& exit) { return exit.exitCode; });

EXPECT_EQ(
action.apply(
onRunMock.AsStdFunction(),
onExitMock.AsStdFunction(),
onMigrateMock.AsStdFunction(),
onVerifyMock.AsStdFunction()
),
EXIT_FAILURE
);
std::filesystem::remove(tmpFile.path.c_str());
}
Loading