-
Notifications
You must be signed in to change notification settings - Fork 224
feat(compare_namelists): add TOML support for compare_namelists and mizuRoute baseline comparison #5024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat(compare_namelists): add TOML support for compare_namelists and mizuRoute baseline comparison #5024
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Unit tests for CIME.compare_namelists TOML comparison functions. | ||
| import unittest | ||
| import tempfile | ||
| import os | ||
|
|
||
| from CIME.compare_namelists import compare_namelist_files, is_namelist_file | ||
|
|
||
| class TestCompareNamelists(unittest.TestCase): | ||
| def test_toml_whitespace_handling(self): | ||
| """Verify that TOML comparison ignores arbitrary whitespace and comments when matching key-value pairs.""" | ||
| gold_toml = """ | ||
| [route_opt] | ||
| value = 5 | ||
| # a comment | ||
| """ | ||
| compare_toml = """ | ||
| [ route_opt ] | ||
| value = 5 | ||
| """ | ||
| with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_toml") as f1, \ | ||
| tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_toml") as f2: | ||
| f1.write(gold_toml) | ||
| f2.write(compare_toml) | ||
| f1_name = f1.name | ||
| f2_name = f2.name | ||
|
|
||
| try: | ||
| self.assertTrue(is_namelist_file(f1_name)) | ||
| match, comments = compare_namelist_files(f1_name, f2_name) | ||
| self.assertTrue(match) | ||
| self.assertEqual(comments, "") | ||
| finally: | ||
| os.remove(f1_name) | ||
| os.remove(f2_name) | ||
|
|
||
| def test_toml_compare_diff(self): | ||
| """Verify that TOML comparison correctly detects and reports mismatched key-value values.""" | ||
| gold_toml = """ | ||
| [route_opt] | ||
| value = 5 | ||
|
|
||
| [physics] | ||
| method = "IRF" | ||
| """ | ||
| compare_toml = """ | ||
| [route_opt] | ||
| value = 5 | ||
|
|
||
| [physics] | ||
| method = "MC" | ||
| """ | ||
| with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_toml") as f1, \ | ||
| tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_toml") as f2: | ||
| f1.write(gold_toml) | ||
| f2.write(compare_toml) | ||
| f1_name = f1.name | ||
| f2_name = f2.name | ||
|
|
||
| try: | ||
| match, comments = compare_namelist_files(f1_name, f2_name) | ||
| self.assertFalse(match) | ||
| self.assertIn("method had mismatched values", comments) | ||
| finally: | ||
| os.remove(f1_name) | ||
| os.remove(f2_name) | ||
|
|
||
| def test_underscore_toml_extension(self): | ||
| """Verify TOML files ending in _toml (such as mizuroute_toml) are correctly parsed as TOML namelists.""" | ||
| content = "[route_opt]\nvalue = 5\n" | ||
| with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_toml") as f1, \ | ||
| tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_toml") as f2: | ||
| f1.write(content) | ||
| f2.write(content) | ||
| f1_name = f1.name | ||
| f2_name = f2.name | ||
|
|
||
| try: | ||
| self.assertTrue(is_namelist_file(f1_name)) | ||
| match, comments = compare_namelist_files(f1_name, f2_name) | ||
| self.assertTrue(match) | ||
| finally: | ||
| os.remove(f1_name) | ||
| os.remove(f2_name) | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
| include_package_data=True, | ||
| name="CIME", | ||
| packages=find_packages(), | ||
| # Python 3.10 will end-of-life in Oct 2026; tomli is required for python_version < '3.11' | ||
| install_requires=["tomli; python_version < '3.11'"], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be fine, since python 3.9 is already at end of life. And 3.11 is only having security updates until 2028. So shouldn't be that contraversial to update to 3.11 from 3.9. There's also a python version checker in and that perhaps is where this should be set. But, there's a few different places this is done, so I'll defer to someone else who can suggest where this should be done. |
||
| test_suite="CIME.tests", | ||
| tests_requires=["pytest"], | ||
| url="https://github.com/ESMCI/cime", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We talked about a limitation of the standard tomllib library used here is that it removes comments. Which isn't great, but n this context for a comparison it's probably fine.