Skip to content

Conversation

@kingbuzzman
Copy link
Contributor

@kingbuzzman kingbuzzman commented Jun 25, 2025

Adds tests for the current state of the application, no logic changes

Increases the coverage from 85% (master) to 91%

@kingbuzzman kingbuzzman marked this pull request as draft June 25, 2025 13:00
@kingbuzzman kingbuzzman marked this pull request as ready for review June 25, 2025 13:15
@bw2 bw2 requested review from bw2 and Copilot June 26, 2025 12:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds tests to improve the project's TOML configuration parsing while increasing overall test coverage. Key changes include:

  • New test classes for TOML and composite configuration parsing.
  • Additional tests to validate behavior with various TOML file conditions.
  • An update to setup.py to include the toml package in test dependencies.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_configargparse.py Adds new tests for TOML parsing and composite configuration error handling.
setup.py Updates test requirements to include the toml package.
Comments suppressed due to low confidence (1)

tests/test_configargparse.py:1895

  • [nitpick] Consider adding an inline comment to explain why appending extra keys in the TOML file is expected to trigger a SystemExit, which will help clarify the test intent.
    def test_toml_extra(self):

Comment on lines 1848 to 1858
def write_yaml_file(self, content="[section]\nkey1: yaml1\nkey2: yaml2"):
with open("config.yaml", "w") as f:
f.write(content)

def write_ini_file(self, content="[section]\nkey1=ini1\nkey2=ini2"):
with open("config.ini", "w") as f:
f.write(content)

def write_toml_file(self, content="[section]\nkey1 = 'toml1'\nkey2 = 'toml2'"):
with open("config.toml", "w") as f:
f.write(content)
Copy link

Copilot AI Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider refactoring the repeated file writing helper methods (for YAML, INI, and TOML) into a shared utility to reduce duplication across tests.

Suggested change
def write_yaml_file(self, content="[section]\nkey1: yaml1\nkey2: yaml2"):
with open("config.yaml", "w") as f:
f.write(content)
def write_ini_file(self, content="[section]\nkey1=ini1\nkey2=ini2"):
with open("config.ini", "w") as f:
f.write(content)
def write_toml_file(self, content="[section]\nkey1 = 'toml1'\nkey2 = 'toml2'"):
with open("config.toml", "w") as f:
f.write(content)
def write_config_file(self, filename, content):
"""Writes the given content to the specified file."""
with open(filename, "w") as f:
f.write(content)
def write_yaml_file(self, content="[section]\nkey1: yaml1\nkey2: yaml2"):
self.write_config_file("config.yaml", content)
def write_ini_file(self, content="[section]\nkey1=ini1\nkey2=ini2"):
self.write_config_file("config.ini", content)
def write_toml_file(self, content="[section]\nkey1 = 'toml1'\nkey2 = 'toml2'"):
self.write_config_file("config.toml", content)

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you not heard of StringIO, copilot?

Copy link
Owner

@bw2 bw2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! @tristanlatr @tbooth any objections to merging this?

default_config_files=['config.yaml', 'config.toml', 'config.ini'],
config_file_parser_class=configargparse.CompositeConfigParser(
[
configargparse.TomlConfigParser(['section']),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this line is removed the tests all still pass, so either remove the line or make it necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of. Issue is that IniConfigParser picks up the slack, since the two are structured very similarly. If you comment the other two out, and run the test for just pytest -s tests/test_configargparse.py::TestCompositeConfigParser::test_toml it passes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so just include something in the TOML snippet that would not be properly recognised by the INI parser, and that will make the test much more useful.

Copy link
Contributor Author

@kingbuzzman kingbuzzman Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For that i would need to touch the code, there is a bug that will cause a crash raising ConfigFileParserException.

This is the best i can do without touching anything else, and focusing the changes VERY narrowly.

I plan on fixing this issue after the other toml PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine IMO for these new tests you are adding to fail with the current code. Adding correct but failing tests is the motivator for accepting the patches to fix them!

I'd suggest adding a line like foo=0xDEADBEEF to the sample TOML snippet in your test. The INI parser will parse this incorrectly as "0xDEADBEEF". The old toml module will (it appears) raise an exception. The newer tomli module you want to introduce will correctly parse it as 3735928559, which should (but will currently not) be converted to a string :"3735928559"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I agree with what you say, but if its all the same, i'd like to do this in another PR. Can you approve it... if you deem it... worthy, so i can continue... pretty please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While youre at it, there are another 2 prs open as of 20mins ago, SUPER tiny. low risk. -- if you can take a quick look, id appreciate it immensely.

""")
parser = configargparse.TomlConfigParser(['tool.section'])
with open('config.toml', 'r') as f:
self.assertEqual(parser.parse(f), {'key1': "toml1", 'key2': [1, 2, 3]})
Copy link
Collaborator

@tbooth tbooth Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted by @tristanlatr this behaviour is incorrect so please change it to 'key2': ['1', '2', '3'] and then this test should (correctly) fail. If we want to force a module release before fixing it then it can be marked as an expected failure.

I fixed this in my own fork by having a unified config data cleanup function, rather then relying on each separate parser class to do its own cleanup. I would advocate adopting this change to resolve the failure, rather then writing a new quick fix to the TOML parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.



class TestTomlConfigParser(unittest.TestCase):
def setUp(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This temp file creation is a lot of work compared to using io.StringIO. Also, changing directory within a unit test is best avoided unless strictly necessary. Not a major problem - I can sort this out in a follow-up PR.

Copy link
Contributor Author

@kingbuzzman kingbuzzman Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted most..

@tbooth
Copy link
Collaborator

tbooth commented Jun 26, 2025

I added some comments but the one that really needs attention is the one on line 1834. The test purports to be testing composite parsing with the TOML parser but removing that parser from the list does not cause the tests to fail.

@kingbuzzman
Copy link
Contributor Author

@tbooth anything missing from your perspective?

@tbooth tbooth merged commit a7aaba3 into bw2:master Jul 1, 2025
21 checks passed
@kingbuzzman kingbuzzman deleted the dev/current-state branch July 1, 2025 22:35
@kingbuzzman
Copy link
Contributor Author

kingbuzzman commented Jul 3, 2025

@tbooth thanks for the merge. Next time please squash my commits, there is no need for so much noise in the commit history in the master branch:

image

The major issue of not squashing is that now all the PRs have conflicts now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants