Skip to content
Open
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
9 changes: 0 additions & 9 deletions bin/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3023,15 +3023,6 @@ SIM_verif_eclipse_calculator:
compare:
- models/environment/eclipse_calculator/verif/SIM_verif/RUN_moon_shadow/log_test_data.csv
vs. models/environment/eclipse_calculator/verif/SIM_verif/verif_data/RUN_moon_shadow/log_test_data.csv
SIM_verif_env_utils:
model_dir: models/utilities/env_utils
path: models/utilities/env_utils/verif/SIM_verif
build_args: CML_OFFLINE_BUILD=1
runs:
RUN_verif/input.py:
compare:
- models/utilities/env_utils/verif/SIM_verif/RUN_verif/log_test_data.csv vs.
models/utilities/env_utils/verif/SIM_verif/verif_data/RUN_verif/log_test_data.csv
SIM_verif_events_manager:
model_dir: models/vehicle_management/events_manager
path: models/vehicle_management/events_manager/verif/SIM_verif
Expand Down
1 change: 1 addition & 0 deletions docs/sphinx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (TARGET Doxygen::doxygen AND Sphinx_FOUND)
models/fhw/index.rst
models/interactions/index.rst
models/tools/index.rst
models/utilities/env-utils.rst
models/utilities/index.rst
models/utilities/subscriptions.rst
models/vehicle_management/index.rst
Expand Down
403 changes: 403 additions & 0 deletions docs/sphinx/models/utilities/env-utils.rst

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/sphinx/models/utilities/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ really fit under other model categories.
:name: utilities-models
:titlesonly:

subscriptions.rst
env-utils.rst
subscriptions.rst
5 changes: 1 addition & 4 deletions models/tools/unit_test/models/include/unit_test.hh
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ class UnitTestFramework {

void update_sweeps();
void update_file();
private:
friend class UnitTestFrameworkTest;
std::string expand_env_variables(const std::string& input);
};

// Interface to make this look like the C-style unit-test framework:
Expand All @@ -135,4 +132,4 @@ inline void unit_test_init( UNIT_TEST * data) {data->initialize();}
inline void unit_test( UNIT_TEST * data) {data->update();}
inline void unit_test_dd( UNIT_TEST * data) {(void) data;}
inline void unit_test_shutdown( UNIT_TEST * data) {(void) data;}
#endif
#endif
59 changes: 4 additions & 55 deletions models/tools/unit_test/models/src/unit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ PURPOSE: (To provide a Trick-friendly unit-test framework)
REFERENCES: (../models-C by Jason Arnold)

LIBRARY DEPENDENCIES:
((cml/models/utilities/cml_message/src/cml_message.cc))
((cml/models/utilities/env_utils/src/env_utils.cc)
(cml/models/utilities/cml_message/src/cml_message.cc))

PROGRAMMERS:
(((Jason Arnold) (Titan) (Jul 2005))
Expand All @@ -15,27 +16,20 @@ LIBRARY DEPENDENCIES:
#include <cmath>
#include <cfloat>
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <list>
#include <sstream>
#include <fstream>
#include <regex>
#include <stdexcept>
#include <string>

#include "../include/unit_test.hh"

#include "cml/models/utilities/env_utils/include/env_utils.hh"
#include "cml/models/utilities/math_utils/include/math_utils.hh"

#include "cml/models/utilities/cml_message/include/cml_message.hh"

#include "trick/input_processor_proto.h"
#include "trick/IPPython.hh"

extern Trick::IPPython* the_pip;


/*****************************************************************************
Constructor
Expand Down Expand Up @@ -252,51 +246,6 @@ UnitTestFramework::configure_file_combinations()
process_linked_variables();
}

/*****************************************************************************
expand_env_variables
Purpose: (Replaces environment variable placeholders in the form `${VAR_NAME}`
within the input string with their corresponding values from the
process environment. The search pattern matches variable names
beginning with a letter or underscore, followed by letters, digits,
or underscores. If an environment variable is found, its value is
inserted into the output string. If a variable is not set, the
placeholder is left unchanged, a warning is printed via `CMLMessage::error`,
and a runtime exception is thrown. The method preserves any text
outside of `${}` sequences unchanged.)
*****************************************************************************/
std::string UnitTestFramework::expand_env_variables(const std::string& input) {
static const std::regex pattern(R"(\$\{([A-Za-z_][A-Za-z0-9_]*)\})");

std::string result;
const std::sregex_iterator begin(input.begin(), input.end(), pattern);
const std::sregex_iterator end;

std::size_t last_pos = 0;

for (auto it = begin; it != end; ++it) {
const std::smatch& match = *it;
const std::size_t substr_len = static_cast<std::size_t>(match.position()) - last_pos;
result.append(input.substr(last_pos, substr_len)); // text before match

const std::string var_name = match[1].str();
const char* env_val = std::getenv(var_name.c_str());
if (env_val != nullptr)
{
result.append(env_val);
} else
{
CMLMessage::error(__FILE__, __LINE__,
"Warning: Environment variable '", var_name, "' is not set. Leaving placeholder unchanged.\n");
result.append(match[0].str()); // Keep the original "${VAR}"
throw std::runtime_error("Missing environment variable: " + var_name);
}
last_pos = static_cast<std::size_t>(match.position() + match.length());
}

result.append(input.substr(last_pos)); // remaining text
return result;
}

/*****************************************************************************
configure_from_definition_file
Purpose:(Used when the tests are specified in a single file with each row of
Expand Down Expand Up @@ -676,4 +625,4 @@ UnitTestFramework::update_file()
commands.push_back(commands.front());
}
commands.pop_front();
}
}
40 changes: 2 additions & 38 deletions models/tools/unit_test/models/tests/test_unit_framework.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
#include <cstdlib>
#include <exception>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "../include/unit_test.hh"
#include "mocks/cml/cml_message_mock.hh"
#include "../include/unit_test.hh"

// Create a test subclass to access protected methods (if needed)
class UnitTestFrameworkTest : public UnitTestFramework {
public:
using UnitTestFramework::expand_env_variables;
};

TEST(UnitTestFrameworkTest, DefaultConstructorInitializesState) {
TEST(UnitTestFramework, DefaultConstructorInitializesState) {
UnitTestFramework utf;

EXPECT_TRUE(utf.enabled);
Expand All @@ -21,29 +11,3 @@ TEST(UnitTestFrameworkTest, DefaultConstructorInitializesState) {
EXPECT_EQ(utf.linked_vars_file_name, "");
EXPECT_EQ(utf.cycle_overruns_limit, 2u);
}

TEST(UnitTestFrameworkTest, ExpandEnvVariableKnown) {
setenv("MY_TEST_PATH", "/tmp/testdir", 1);
UnitTestFrameworkTest utf;

std::string input = "${MY_TEST_PATH}/file.txt";
std::string expected = "/tmp/testdir/file.txt";

EXPECT_EQ(utf.expand_env_variables(input), expected);
}

TEST(UnitTestFrameworkTest, ExpandEnvVariableUnknownThrows) {
using testing::_;
using testing::HasSubstr;

CMLMessage::Mock cml_message_mock;

unsetenv("NON_EXISTENT_VAR");
UnitTestFrameworkTest utf;

EXPECT_CALL(
cml_message_mock,
publish(CMLMessage::Error, _, _, HasSubstr("'NON_EXISTENT_VAR' is not set")));
std::string input = "${NON_EXISTENT_VAR}/file.txt";
EXPECT_THROW(utf.expand_env_variables(input), std::runtime_error);
}
4 changes: 3 additions & 1 deletion models/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ add_cml_library(
constraint_check/include/valset_constraint.hh
convert_string/include/convert_string.hh
double_to_words/include/convert_double_to_words.hh
env_utils/include/env_utils.h
env_utils/include/env_utils.hh
fault_arch/include/sSensorFaults.hh
fault_management/include/fault.hh
fault_management/include/fault_bias.hh
Expand Down Expand Up @@ -74,6 +74,7 @@ add_cml_library(
constraint_check/src/constraint_test_templates.cc
constraint_check/src/constraint_test_timed_templates.cc
double_to_words/src/convert_double_to_uint_words.cc
env_utils/src/env_utils.cc
fault_arch/src/sSensorFaults.cc
fault_management/src/fault.cc
fault_management/src/fault_function.cc
Expand Down Expand Up @@ -109,5 +110,6 @@ add_cml_tests(

SOURCES
cml_message/test/cml_message_test.cc
env_utils/test/env_utils_test.cc
table_interp_cpp/test/table_independent_variable_test.cc
)
1 change: 1 addition & 0 deletions models/utilities/env_utils/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Read the documentation for the env utils model [here](https://nasa.github.io/cml/models/utilities/env-utils.html).
Binary file removed models/utilities/env_utils/docs/env_utils.pdf
Binary file not shown.
91 changes: 0 additions & 91 deletions models/utilities/env_utils/docs/env_utils.tex

This file was deleted.

40 changes: 0 additions & 40 deletions models/utilities/env_utils/include/env_utils.h

This file was deleted.

Loading
Loading