Skip to content

Commit

Permalink
Build and test test/env.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyashton committed Jan 29, 2025
1 parent 8ab87d0 commit 35a0889
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@ if(BUILD_TESTS)
if(BUILD_UNIT_TESTS)
add_unit_test(map_test ${CMAKE_CURRENT_SOURCE_DIR}/src/ds/test/map_test.cpp)

add_unit_test(
env_test ${CMAKE_CURRENT_SOURCE_DIR}/src/host/test/env.cpp
${CCF_DIR}/src/host/env.cpp
)

add_unit_test(
json_schema ${CMAKE_CURRENT_SOURCE_DIR}/src/ds/test/json_schema.cpp
)
Expand Down
43 changes: 23 additions & 20 deletions src/host/test/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

#include "host/env.h"

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#define FMT_HEADER_ONLY
#include <fmt/format.h>

TEST_CASE("envvars" * doctest::test_suite("env"))
{
{
Expand All @@ -13,15 +17,14 @@ TEST_CASE("envvars" * doctest::test_suite("env"))
std::string test_value("test_value");
::setenv("TEST_ENV_VAR", test_value.c_str(), 1);

REQUIRE("" == ccf::nonstd::expand_envvar(""));
REQUIRE("not an env var" == ccf::nonstd::expand_envvar("not an env var"));
REQUIRE(
"$ENV_VAR_NOT_SET" == ccf::nonstd::expand_envvar("$ENV_VAR_NOT_SET"));
REQUIRE(test_value == ccf::nonstd::expand_envvar("$TEST_ENV_VAR"));
REQUIRE("" == ccf::env::expand_envvar(""));
REQUIRE("not an env var" == ccf::env::expand_envvar("not an env var"));
REQUIRE("$ENV_VAR_NOT_SET" == ccf::env::expand_envvar("$ENV_VAR_NOT_SET"));
REQUIRE(test_value == ccf::env::expand_envvar("$TEST_ENV_VAR"));

// ${} syntax is not supported
REQUIRE(
"${ENV_VAR_NOT_SET}" == ccf::nonstd::expand_envvar("${ENV_VAR_NOT_SET}"));
"${ENV_VAR_NOT_SET}" == ccf::env::expand_envvar("${ENV_VAR_NOT_SET}"));
}
{
INFO("Expand path");
Expand All @@ -31,33 +34,33 @@ TEST_CASE("envvars" * doctest::test_suite("env"))
std::string test_value2("test_value2");
::setenv("TEST_ENV_VAR2", test_value2.c_str(), 1);

REQUIRE("" == ccf::nonstd::expand_envvars_in_path(""));
REQUIRE("foo" == ccf::nonstd::expand_envvars_in_path("foo"));
REQUIRE("foo/" == ccf::nonstd::expand_envvars_in_path("foo/"));
REQUIRE("foo/bar" == ccf::nonstd::expand_envvars_in_path("foo/bar"));
REQUIRE("/" == ccf::nonstd::expand_envvars_in_path("/"));
REQUIRE("/foo" == ccf::nonstd::expand_envvars_in_path("/foo"));
REQUIRE("/foo/" == ccf::nonstd::expand_envvars_in_path("/foo/"));
REQUIRE("/foo/bar" == ccf::nonstd::expand_envvars_in_path("/foo/bar"));
REQUIRE("" == ccf::env::expand_envvars_in_path(""));
REQUIRE("foo" == ccf::env::expand_envvars_in_path("foo"));
REQUIRE("foo/" == ccf::env::expand_envvars_in_path("foo/"));
REQUIRE("foo/bar" == ccf::env::expand_envvars_in_path("foo/bar"));
REQUIRE("/" == ccf::env::expand_envvars_in_path("/"));
REQUIRE("/foo" == ccf::env::expand_envvars_in_path("/foo"));
REQUIRE("/foo/" == ccf::env::expand_envvars_in_path("/foo/"));
REQUIRE("/foo/bar" == ccf::env::expand_envvars_in_path("/foo/bar"));

REQUIRE(
fmt::format("{}", test_value1) ==
ccf::nonstd::expand_envvars_in_path("$TEST_ENV_VAR1"));
ccf::env::expand_envvars_in_path("$TEST_ENV_VAR1"));
REQUIRE(
fmt::format("{}/", test_value1) ==
ccf::nonstd::expand_envvars_in_path("$TEST_ENV_VAR1/"));
ccf::env::expand_envvars_in_path("$TEST_ENV_VAR1/"));
REQUIRE(
fmt::format("{}/{}", test_value1, test_value2) ==
ccf::nonstd::expand_envvars_in_path("$TEST_ENV_VAR1/$TEST_ENV_VAR2"));
ccf::env::expand_envvars_in_path("$TEST_ENV_VAR1/$TEST_ENV_VAR2"));

REQUIRE(
fmt::format("/{}", test_value1) ==
ccf::nonstd::expand_envvars_in_path("/$TEST_ENV_VAR1"));
ccf::env::expand_envvars_in_path("/$TEST_ENV_VAR1"));
REQUIRE(
fmt::format("/{}/", test_value1) ==
ccf::nonstd::expand_envvars_in_path("/$TEST_ENV_VAR1/"));
ccf::env::expand_envvars_in_path("/$TEST_ENV_VAR1/"));
REQUIRE(
fmt::format("/{}/{}", test_value1, test_value2) ==
ccf::nonstd::expand_envvars_in_path("/$TEST_ENV_VAR1/$TEST_ENV_VAR2"));
ccf::env::expand_envvars_in_path("/$TEST_ENV_VAR1/$TEST_ENV_VAR2"));
}
}

0 comments on commit 35a0889

Please sign in to comment.