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
10 changes: 10 additions & 0 deletions lib/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ def get_yaml_files(options):
)
)

out = None

try:
out = subprocess.check_output(
git_command,
Expand All @@ -792,7 +794,15 @@ def get_yaml_files(options):
error_msg=exc.output.rstrip('\n')
)
)
except FileNotFoundError as exc:
if options.verbosity:
print(
"Warning: Failed to locate command: {error_msg!s}".format(
error_msg=exc
)
)

if out is None:
out = [
os.path.join(root, name)
for root, dirs, files in os.walk('.')
Expand Down
50 changes: 50 additions & 0 deletions test/TestUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pathlib import Path

import ansiblelint.utils as utils
from ansiblelint import cli

from importlib_metadata import version as get_dist_version
from packaging.version import Version
Expand Down Expand Up @@ -185,3 +186,52 @@ def test_expand_paths_vars(monkeypatch):
monkeypatch.setenv('TEST_PATH', test_path)
assert utils.expand_paths_vars(['~']) == [os.path.expanduser('~')]
assert utils.expand_paths_vars(['$TEST_PATH']) == [test_path]


@pytest.mark.parametrize(
('reset_env_var', 'message_prefix'),
(
('PATH', 'Warning: Failed to locate command:'),
('GIT_DIR', 'Warning: Failed to discover yaml files to lint using git:')
),
ids=('no Git installed', 'outside Git repository'),
)
def test_get_yaml_files_git_verbose(
reset_env_var,
message_prefix,
monkeypatch,
capsys
):
options, _ = cli.get_config(['-v'])
monkeypatch.setenv(reset_env_var, '')
utils.get_yaml_files(options)
msg1, msg2 = capsys.readouterr().out.splitlines()[:2]
assert msg1 == 'Discovering files to lint: git ls-files *.yaml *.yml'
assert msg2.startswith(message_prefix)


@pytest.mark.parametrize(
'is_in_git',
(True, False),
ids=('in Git', 'outside Git'),
)
def test_get_yaml_files_silent(is_in_git, monkeypatch, capsys):
options, _ = cli.get_config([])
test_dir = Path(__file__).resolve().parent
lint_path = test_dir / 'roles' / 'test-role'
if not is_in_git:
monkeypatch.setenv('GIT_DIR', '')

yaml_count = (
len(list(lint_path.glob('**/*.yml'))) + len(list(lint_path.glob('**/*.yaml')))
)

monkeypatch.chdir(str(lint_path))
files = utils.get_yaml_files(options)
stdout = capsys.readouterr().out
assert not stdout, 'No output is expected when the verbosity is off'
assert len(files) == yaml_count, (
"Expected to find {yaml_count} yaml files in {lint_path}".format_map(
locals(),
)
)