Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions lib/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


_PATH_VARS = ['exclude_paths', 'rulesdir', ]
INVALID_CONFIG_RC = 2


def abspath(path, base_dir):
Expand Down Expand Up @@ -46,17 +47,33 @@ def expand_to_normalized_paths(config, base_dir=None):


def load_config(config_file):
config_path = config_file if config_file else ".ansible-lint"
config_path = os.path.abspath(config_path)

if not os.path.exists(config_path):
config_path = os.path.abspath(config_file or '.ansible-lint')

if config_file:
if not os.path.exists(config_path):
print(
"Config file not found '{cfg!s}'.".format(cfg=config_path),
file=sys.stderr,
)
sys.exit(INVALID_CONFIG_RC)
elif not os.path.exists(config_path):
# a missing default config file should not trigger an error
return

try:
with open(config_path, "r") as stream:
config = yaml.safe_load(stream)
except yaml.YAMLError:
return None
except yaml.YAMLError as e:
print(e, file=sys.stderr)
sys.exit(INVALID_CONFIG_RC)
# TODO(ssbarnea): implement schema validation for config file
if isinstance(config, list):
print(
"Invalid configuration '{cfg!s}', expected YAML mapping in the config file.".
format(cfg=config_path),
file=sys.stderr,
)
sys.exit(INVALID_CONFIG_RC)

config_dir = os.path.dirname(config_path)
expand_to_normalized_paths(config, config_dir)
Expand Down
14 changes: 14 additions & 0 deletions test/TestCommandLineInvocationSameAsConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ def test_path_from_cli_depend_on_cwd(base_arguments, monkeypatch): # Issue 572

assert 'test/test' not in options1.exclude_paths[0]
assert 'test/test' in options2.exclude_paths[0]


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

Choose a reason for hiding this comment

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

👍

]
)
def test_config_failure(base_arguments, config_file):
"""Ensures specific config files produce error code 2."""
with pytest.raises(SystemExit, match="^2$"):
cli.get_config(base_arguments +
["-c", config_file])
Comment on lines +89 to +90
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.

3 changes: 3 additions & 0 deletions test/fixtures/ansible-config-invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# invalid .ansible-lint config file
- foo
- bar