Skip to content

Conversation

@ssbarnea
Copy link
Member

@ssbarnea ssbarnea commented Apr 7, 2020

Corrects implementation of config file loading in order to return an error code in case of failure, with a meaningful error message.

@webknjaz
Copy link
Member

webknjaz commented Apr 7, 2020

Please add regression tests for this


def test_invalid_config_file(base_arguments):
"""Ensures invalid config_file produces error code 2."""
with pytest.raises(SystemExit) as excinfo:
Copy link
Member

Choose a reason for hiding this comment

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

Have you tried

Suggested change
with pytest.raises(SystemExit) as excinfo:
with pytest.raises(SystemExit, match='^2$'):

(not sure if it'll work, but it'd be cleaner if yes)

Comment on lines 79 to 90
def test_invalid_config_file(base_arguments):
"""Ensures invalid config_file produces error code 2."""
with pytest.raises(SystemExit, match="^2$"):
cli.get_config(base_arguments +
["-c", "test/fixtures/ansible-config-invalid.yml"])


def test_missing_config_file(base_arguments):
"""Ensures missing config_file produces error code 2."""
with pytest.raises(SystemExit, match="^2$"):
cli.get_config(base_arguments +
["-c", "/dev/null/ansible-config-missing.yml"])
Copy link
Member

Choose a reason for hiding this comment

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

We should parametrize this, you're copy-pasting too many lines. Also, I'd check for the message in stderr using the capsys fixture.

Copy link
Member Author

Choose a reason for hiding this comment

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

For 3 tests maybe it would have made sense, but for only two is going to add complexity. Still, I will do it.

Not testing stderr/stdout output was on purpose because the only contract I want to offer is the result code. The content of the error message and where it is sent may change in time, especially as we are likely to do some refactoring and adopt python logging.

"Config file not found '{cfg!s}'.".format(cfg=config_path),
file=sys.stderr,
)
sys.exit(2)
Copy link
Member

Choose a reason for hiding this comment

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

Could you pls move 2 to a global constant like INVALID_CONFIG_RC = 2?

Suggested change
sys.exit(2)
sys.exit(INVALID_CONFIG_RC)

I think it'd communicate the intent of the magic literal better. And would improve the consistency too.

Comment on lines +82 to +83
pytest.param("test/fixtures/ansible-config-invalid.yml", id="invalid"),
pytest.param("/dev/null/ansible-config-missing.yml", id="missing")
Copy link
Member

Choose a reason for hiding this comment

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

👍

Corrects implementation of config file loading in order to return
an error code in case of failure, with a meaningful error message.
Comment on lines +89 to +90
cli.get_config(base_arguments +
["-c", config_file])
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: this doesn't need to be multilined anymore. But won't block the PR if you decide to keep it.

@webknjaz webknjaz merged commit 38356bf into ansible:master Apr 8, 2020
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