From 4f4f30e1d32b05fbbd5b498db6562584bac00dd4 Mon Sep 17 00:00:00 2001 From: Doug Walker Date: Fri, 17 Nov 2023 01:52:51 -0500 Subject: [PATCH] Fix env serialization for v1 configs (#1904) Signed-off-by: Doug Walker --- src/OpenColorIO/OCIOYaml.cpp | 4 +-- tests/cpu/Config_tests.cpp | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/OpenColorIO/OCIOYaml.cpp b/src/OpenColorIO/OCIOYaml.cpp index e62964349e..8681631a74 100644 --- a/src/OpenColorIO/OCIOYaml.cpp +++ b/src/OpenColorIO/OCIOYaml.cpp @@ -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) diff --git a/tests/cpu/Config_tests.cpp b/tests/cpu/Config_tests.cpp index 46efa9d333..abb003bdd7 100644 --- a/tests/cpu/Config_tests.cpp +++ b/tests/cpu/Config_tests.cpp @@ -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) { {