Skip to content

Commit

Permalink
Reinstate ProgramOptionEnv.parse_environment
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanGriffiths committed Oct 8, 2024
1 parent 101d1e0 commit 65ab963
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/unit-tests/options/test_program_option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,50 @@ TEST_F(ProgramOption, unparsed_command_line_returns_unprocessed_tokens)
EXPECT_THAT(po.unparsed_command_line(), ElementsAre("--hello", "world", "--answer", "42"));
}

TEST(ProgramOptionEnv, parse_environment)
{
// Env variables should be uppercase and "_" delimited
char const* name = "some-key";
char const* key = "SOME_KEY";
char const* value = "test_value";
auto const prefix = std::string{__PRETTY_FUNCTION__} + "_";

auto const mapper = [&prefix](std::string const& from) -> std::string
{
if (from.starts_with(prefix))
{
auto result = from.substr(prefix.size());

for(auto& ch : result)
{
if (ch == '_') ch = '-';
else ch = std::tolower(ch, std::locale::classic());
}

return result;
}
else
{
return std::string{};
}
};

auto const env = prefix + key;
setenv(env.c_str(), value, true);

bpo::options_description desc;
desc.add_options()
(name, bpo::value<std::string>());

mir::options::ProgramOption po;
po.parse_environment(desc, mapper);

EXPECT_EQ(value, po.get(name, "default"));
EXPECT_EQ("default", po.get("garbage", "default"));
EXPECT_TRUE(po.is_set(name));
EXPECT_FALSE(po.is_set("garbage"));
}

// TODO need to parse something
TEST(ProgramOptionFile, parse_files)
{
Expand Down

0 comments on commit 65ab963

Please sign in to comment.