Skip to content

Commit

Permalink
Fix env serialization for v1 configs (#1904)
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Walker <[email protected]>
  • Loading branch information
doug-walker authored Nov 17, 2023
1 parent 4d64b32 commit 4f4f30e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/OpenColorIO/OCIOYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4738,9 +4738,9 @@ inline void save(YAML::Emitter & out, const Config & config)
out << YAML::Newline;
out << YAML::Newline;

if (configMajorVersion >= 2)
if (configMajorVersion >= 2 || config.getNumEnvironmentVars() > 0)
{
// Print the environment even if empty.
// For v2 configs, write the environment section, even if empty.
out << YAML::Key << "environment";
out << YAML::Value << YAML::BeginMap;
for(int i = 0; i < config.getNumEnvironmentVars(); ++i)
Expand Down
49 changes: 49 additions & 0 deletions tests/cpu/Config_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,55 @@ OCIO_ADD_TEST(Config, serialize_searchpath)
}
}

OCIO_ADD_TEST(Config, serialize_environment)
{
{
OCIO::ConfigRcPtr config = OCIO::Config::Create();
config->setMajorVersion(1);
config->setMinorVersion(0);

std::ostringstream os;
config->serialize(os);
StringUtils::StringVec osvec = StringUtils::SplitByLines(os.str());

// A v1 config does not write the environment section if it's empty.
const std::string expected{ "search_path: \"\"" };
OCIO_CHECK_EQUAL(osvec[2], expected);
}
{
OCIO::ConfigRcPtr config = OCIO::Config::Create();
config->setMajorVersion(2);
config->setMinorVersion(0);

std::ostringstream os;
config->serialize(os);
StringUtils::StringVec osvec = StringUtils::SplitByLines(os.str());

// A v2 config does write the environment section, even if it's empty.
const std::string expected1{ "environment:" };
const std::string expected2{ " {}" };
OCIO_CHECK_EQUAL(osvec[2], expected1);
OCIO_CHECK_EQUAL(osvec[3], expected2);
}
{
OCIO::ConfigRcPtr config = OCIO::Config::Create();
config->setMajorVersion(1);
config->setMinorVersion(0);

config->addEnvironmentVar("SHOT", "0001");

std::ostringstream os;
config->serialize(os);
StringUtils::StringVec osvec = StringUtils::SplitByLines(os.str());

// A v1 config does write the environment section if it's not empty.
const std::string expected1{ "environment:" };
const std::string expected2{ " SHOT: 0001" };
OCIO_CHECK_EQUAL(osvec[2], expected1);
OCIO_CHECK_EQUAL(osvec[3], expected2);
}
}

OCIO_ADD_TEST(Config, validation)
{
{
Expand Down

0 comments on commit 4f4f30e

Please sign in to comment.