Repository navigation
A malformed config file should be reported, not raised as a configparser traceback - #3473
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
…ser traceback
Pointing `--config-file` at a file that exists but is not a valid INI file escapes as a
raw exception:
```
$ gitlab --config-file broken.cfg project list
Traceback (most recent call last):
File ".../gitlab/config.py", line 123, in _parse_config
_config.read(self._files, encoding="utf-8")
File ".../configparser.py", line 696, in read
self._read(fp, filename)
configparser.MissingSectionHeaderError: File contains no section headers.
file: 'broken.cfg', line: 1
```
Every other problem with the config file the user names is reported as one line, through
`GitlabConfigMissingError` in `_get_config_files()`:
```
$ gitlab --config-file /nope.cfg project list
Cannot read config from file: [Errno 2] No such file or directory: '/nope.cfg'
```
`configparser.read()` deliberately swallows `OSError`, which is why the missing-file case
is already handled, but it lets `configparser.Error` (a syntax error, a duplicate option,
...) propagate.
## Change
`gitlab/config.py`, `GitlabConfigParser._parse_config()`:
```python
_config = configparser.ConfigParser()
- _config.read(self._files, encoding="utf-8")
+ try:
+ _config.read(self._files, encoding="utf-8")
+ except configparser.Error as e:
+ raise GitlabConfigMissingError(
+ f"Cannot read config from file: {e}"
+ ) from e
```
| invocation | before | after |
|---|---|---|
| `--config-file broken.cfg` (no section header) | `MissingSectionHeaderError` traceback, exit 1 | `Cannot read config from file: File contains no section headers. file: 'broken.cfg', line: 1`, exit 1 |
| `--config-file dup.cfg` (duplicate option) | `DuplicateOptionError` traceback | same one-line report |
| `--config-file /nope.cfg` | `Cannot read config from file: [Errno 2] ...` | unchanged |
| `--config-file good.cfg` | works | unchanged |
The error class and the message prefix are the ones the CLI already prints for an
unreadable config file (`tests/functional/cli/test_cli.py::test_arg_config_missing_file_raises`
asserts exactly that prefix), so both ways of getting the file wrong now look the same.
## Test
`tests/unit/test_config.py`:
```python
def test_broken_config_file_raises(tmp_path):
config_file = tmp_path / "config"
config_file.write_text("this is not an ini file\n")
with pytest.raises(config.GitlabConfigMissingError, match="Cannot read config from file"):
config.GitlabConfigParser(config_files=[str(config_file)])
```
`pytest tests/unit/test_config.py`: 19 passed. The new test fails on an unmodified checkout
with `configparser.MissingSectionHeaderError`.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3473 +/- ##
=======================================
Coverage 95.78% 95.78%
=======================================
Files 100 100
Lines 6172 6175 +3
=======================================
+ Hits 5912 5915 +3
Misses 260 260
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pointing
--config-fileat a file that exists but is not a valid INI file escapes as araw exception:
Every other problem with the config file the user names is reported as one line, through
GitlabConfigMissingErrorin_get_config_files():configparser.read()deliberately swallowsOSError, which is why the missing-file caseis already handled, but it lets
configparser.Error(a syntax error, a duplicate option,...) propagate.
Change
gitlab/config.py,GitlabConfigParser._parse_config():--config-file broken.cfg(no section header)MissingSectionHeaderErrortraceback, exit 1Cannot read config from file: File contains no section headers. file: 'broken.cfg', line: 1, exit 1--config-file dup.cfg(duplicate option)DuplicateOptionErrortraceback--config-file /nope.cfgCannot read config from file: [Errno 2] ...--config-file good.cfgThe error class and the message prefix are the ones the CLI already prints for an
unreadable config file (
tests/functional/cli/test_cli.py::test_arg_config_missing_file_raisesasserts exactly that prefix), so both ways of getting the file wrong now look the same.
Test
tests/unit/test_config.py:pytest tests/unit/test_config.py: 19 passed. The new test fails on an unmodified checkoutwith
configparser.MissingSectionHeaderError.