From 1382eb3a19a949e721cca37bea16f28f3ae983fc Mon Sep 17 00:00:00 2001
From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de>
Date: Wed, 8 Sep 2021 13:32:46 +0200
Subject: [PATCH] Config: fixed job config corruption while formatting native
 variables in another job

---
 CHANGES.txt |   8 ++++
 mkexp       |   4 +-
 test.py     | 107 +++++++++++++++++++++++++++++++++-------------------
 3 files changed, 79 insertions(+), 40 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 178510c..03f59c9 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -14,6 +14,14 @@ Global
 
 * TODO: Porting to python3
 
+Version 1.1.1rc2
+================
+
+Configuration
+-------------
+
+* Fixed job config corruption while formatting native variables in another job
+
 Version 1.1.1rc1
 ================
 
diff --git a/mkexp b/mkexp
index 9b4d0a1..09f8082 100755
--- a/mkexp
+++ b/mkexp
@@ -534,8 +534,8 @@ for subjob, subconfig in jobs_config.iteritems():
         # Prepare namelists for inclusion in scripts
         var_format = job_config['JOB'].get('.var_format', '${%s}')
         var_list = set()
-        del job_config['jobs']
-        job_config['jobs'] = jobs_config
+        job_config['jobs'] = {}
+        job_config['jobs'].merge(jobs_config.dict())
         job_config.walk(format_vars, log=var_list, fmt=var_format)
         job_config['VARIABLES_'] = var_list
         for namelist, groups in job_config['namelists'].iteritems():
diff --git a/test.py b/test.py
index 4203e4e..58de4f1 100644
--- a/test.py
+++ b/test.py
@@ -210,44 +210,6 @@ class ContentTestCase(MkexpSimpleTestCase):
         self.assertIn("key1 = local", result)
         self.assertIn("key2 = global", result)
 
-    def test_var_statement(self):
-        exp_id = "test_var_statement"
-        job_id = "job"
-        writeconfig(exp_id, """
-            EXP_TYPE = 
-            GLOBAL1 = 123$${VAR1}456
-            GLOBAL2 = $${VAR2}$${VAR3}
-            GLOBAL3 = 1, $${VAR2}, 3
-            GLOBAL${FOUR} = 4
-            [namelists]
-              [[namelist]]
-                [[[group]]]
-                  key = abc$${var}def
-            [jobs]
-              [["""+job_id+"""]]
-                .var_format = <<<%s>>>
-        """)
-        writetemplate(exp_id, job_id, """
-            GLOBAL1=%{GLOBAL1}
-            GLOBAL2=%{GLOBAL2}
-            GLOBAL3='%{GLOBAL3|join(" ")}'
-            GLOBAL4=%{context("GLOBAL<<<FOUR>>>")}
-            %{NAMELIST}
-        """)
-        expected = align("""
-            GLOBAL1=123<<<VAR1>>>456
-            GLOBAL2=<<<VAR2>>><<<VAR3>>>
-            GLOBAL3='1 <<<VAR2>>> 3'
-            GLOBAL4=4
-            &group
-                key = 'abc<<<var>>>def'
-            /
-        """)
-        ignore = output(script("mkexp "+exp_id+".config"))
-        result = readfile(join("test", "experiments", exp_id, exp_id+"."+job_id))
-        result = align(result)
-        self.assertMultiLineEqual(expected, result)
-
     def test_var_list_in_context(self):
         exp_id = "test_var_list_in_context"
         job_id = "job"
@@ -1201,6 +1163,75 @@ class JobSiblingsTestCase(MkexpSimpleTestCase):
         result = align(result)
         self.assertMultiLineEqual(expected, result)
 
+class NativeVariableTestCase(MkexpSimpleTestCase):
+
+    def test_var_statement(self):
+        exp_id = "test_var_statement"
+        job_id = "job"
+        writeconfig(exp_id, """
+            EXP_TYPE = 
+            GLOBAL1 = 123$${VAR1}456
+            GLOBAL2 = $${VAR2}$${VAR3}
+            GLOBAL3 = 1, $${VAR2}, 3
+            GLOBAL${FOUR} = 4
+            [namelists]
+              [[namelist]]
+                [[[group]]]
+                  key = abc$${var}def
+            [jobs]
+              [["""+job_id+"""]]
+                .var_format = <<<%s>>>
+        """)
+        writetemplate(exp_id, job_id, """
+            GLOBAL1=%{GLOBAL1}
+            GLOBAL2=%{GLOBAL2}
+            GLOBAL3='%{GLOBAL3|join(" ")}'
+            GLOBAL4=%{context("GLOBAL<<<FOUR>>>")}
+            %{NAMELIST}
+        """)
+        expected = align("""
+            GLOBAL1=123<<<VAR1>>>456
+            GLOBAL2=<<<VAR2>>><<<VAR3>>>
+            GLOBAL3='1 <<<VAR2>>> 3'
+            GLOBAL4=4
+            &group
+                key = 'abc<<<var>>>def'
+            /
+        """)
+        ignore = output(script("mkexp "+exp_id+".config"))
+        result = readfile(join("test", "experiments", exp_id, exp_id+"."+job_id))
+        result = align(result)
+        self.assertMultiLineEqual(expected, result)
+
+    def test_var_separation(self):
+        exp_id = "test_var_separation"
+        writeconfig(exp_id, """
+            EXP_TYPE = 
+            [jobs]
+              use_native_var = $${native_var}
+              [[job1]]
+                .var_format = <<<%s>>>
+              [[job2]]
+        """)
+        writetemplate(exp_id, "job1", """
+            %{JOB.use_native_var}
+        """)
+        writetemplate(exp_id, "job2", """
+            %{JOB.use_native_var}
+        """)
+        expected1 = align("""
+            <<<native_var>>>
+        """)
+        expected2 = align("""
+            ${native_var}
+        """)
+        ignore = output(script("mkexp "+exp_id+".config"))
+        result1 = readfile(join("test", "experiments", exp_id, exp_id+".job1"))
+        result2 = readfile(join("test", "experiments", exp_id, exp_id+".job2"))
+        result1 = align(result1)
+        result2 = align(result2)
+        self.assertMultiLineEqual(expected1, result1)
+        self.assertMultiLineEqual(expected2, result2)
 
 if __name__ == '__main__':
     unittest.main()
-- 
GitLab