diff --git a/src/OpenColorIO/OCIOYaml.cpp b/src/OpenColorIO/OCIOYaml.cpp index e62964349..8681631a7 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 46efa9d33..abb003bdd 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) { {