Skip to content

Commit 4f4f30e

Browse files
authored
Fix env serialization for v1 configs (#1904)
Signed-off-by: Doug Walker <doug.walker@autodesk.com>
1 parent 4d64b32 commit 4f4f30e

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/OpenColorIO/OCIOYaml.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,9 +4738,9 @@ inline void save(YAML::Emitter & out, const Config & config)
47384738
out << YAML::Newline;
47394739
out << YAML::Newline;
47404740

4741-
if (configMajorVersion >= 2)
4741+
if (configMajorVersion >= 2 || config.getNumEnvironmentVars() > 0)
47424742
{
4743-
// Print the environment even if empty.
4743+
// For v2 configs, write the environment section, even if empty.
47444744
out << YAML::Key << "environment";
47454745
out << YAML::Value << YAML::BeginMap;
47464746
for(int i = 0; i < config.getNumEnvironmentVars(); ++i)

tests/cpu/Config_tests.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,55 @@ OCIO_ADD_TEST(Config, serialize_searchpath)
768768
}
769769
}
770770

771+
OCIO_ADD_TEST(Config, serialize_environment)
772+
{
773+
{
774+
OCIO::ConfigRcPtr config = OCIO::Config::Create();
775+
config->setMajorVersion(1);
776+
config->setMinorVersion(0);
777+
778+
std::ostringstream os;
779+
config->serialize(os);
780+
StringUtils::StringVec osvec = StringUtils::SplitByLines(os.str());
781+
782+
// A v1 config does not write the environment section if it's empty.
783+
const std::string expected{ "search_path: \"\"" };
784+
OCIO_CHECK_EQUAL(osvec[2], expected);
785+
}
786+
{
787+
OCIO::ConfigRcPtr config = OCIO::Config::Create();
788+
config->setMajorVersion(2);
789+
config->setMinorVersion(0);
790+
791+
std::ostringstream os;
792+
config->serialize(os);
793+
StringUtils::StringVec osvec = StringUtils::SplitByLines(os.str());
794+
795+
// A v2 config does write the environment section, even if it's empty.
796+
const std::string expected1{ "environment:" };
797+
const std::string expected2{ " {}" };
798+
OCIO_CHECK_EQUAL(osvec[2], expected1);
799+
OCIO_CHECK_EQUAL(osvec[3], expected2);
800+
}
801+
{
802+
OCIO::ConfigRcPtr config = OCIO::Config::Create();
803+
config->setMajorVersion(1);
804+
config->setMinorVersion(0);
805+
806+
config->addEnvironmentVar("SHOT", "0001");
807+
808+
std::ostringstream os;
809+
config->serialize(os);
810+
StringUtils::StringVec osvec = StringUtils::SplitByLines(os.str());
811+
812+
// A v1 config does write the environment section if it's not empty.
813+
const std::string expected1{ "environment:" };
814+
const std::string expected2{ " SHOT: 0001" };
815+
OCIO_CHECK_EQUAL(osvec[2], expected1);
816+
OCIO_CHECK_EQUAL(osvec[3], expected2);
817+
}
818+
}
819+
771820
OCIO_ADD_TEST(Config, validation)
772821
{
773822
{

0 commit comments

Comments
 (0)