Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename get_yaml_files to discover_lintables #1550

Merged
merged 1 commit into from May 19, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/ansiblelint/file_utils.py
Expand Up @@ -201,8 +201,8 @@ def __repr__(self) -> str:
return f"{self.name} ({self.kind})"


def get_yaml_files(options: Namespace) -> Dict[str, Any]:
"""Find all yaml files."""
def discover_lintables(options: Namespace) -> Dict[str, Any]:
"""Find all files that we know how to lint."""
# git is preferred as it also considers .gitignore
git_command = ['git', 'ls-files', '-z']
_logger.info("Discovering files to lint: %s", ' '.join(git_command))
Expand All @@ -216,14 +216,17 @@ def get_yaml_files(options: Namespace) -> Dict[str, Any]:
except subprocess.CalledProcessError as exc:
if not (exc.returncode == 128 and 'fatal: not a git repository' in exc.output):
_logger.warning(
"Failed to discover yaml files to lint using git: %s",
"Failed to discover lintable files using git: %s",
exc.output.rstrip('\n'),
)
except FileNotFoundError as exc:
if options.verbosity:
_logger.warning("Failed to locate command: %s", exc)

if out is None:
# TODO(ssbarnea): avoid returning only yaml/yml files but be careful
# to avoid accidental return of too many files, especiall as some
# directories like .cache, .tox may bring undesireble noise.
out = [
os.path.join(root, name)
for root, dirs, files in os.walk('.')
Expand Down Expand Up @@ -265,7 +268,7 @@ def expand_dirs_in_lintables(lintables: Set[Lintable]) -> None:

if should_expand:
# this relies on git and we do not want to call unless needed
all_files = get_yaml_files(options)
all_files = discover_lintables(options)

for item in copy.copy(lintables):
if item.path.is_dir():
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/utils.py
Expand Up @@ -73,7 +73,7 @@
from ansiblelint.config import options
from ansiblelint.constants import FileType
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable, get_yaml_files
from ansiblelint.file_utils import Lintable, discover_lintables

# ansible-lint doesn't need/want to know about encrypted secrets, so we pass a
# string as the password to enable such yaml files to be opened and parsed
Expand Down Expand Up @@ -802,7 +802,7 @@ def get_lintables(
lintables.append(lintable)
else:

for filename in get_yaml_files(options):
for filename in discover_lintables(options):

p = Path(filename)
# skip exclusions
Expand Down
27 changes: 15 additions & 12 deletions test/TestUtils.py
Expand Up @@ -217,12 +217,14 @@ def test_expand_paths_vars(test_path, expected, monkeypatch):
),
ids=('no-git-cli', 'outside-git-repo'),
)
def test_get_yaml_files_git_verbose(reset_env_var, message_prefix, monkeypatch, caplog):
def test_discover_lintables_git_verbose(
reset_env_var, message_prefix, monkeypatch, caplog
):
"""Ensure that autodiscovery lookup failures are logged."""
options = cli.get_config(['-v'])
initialize_logger(options.verbosity)
monkeypatch.setenv(reset_env_var, '')
file_utils.get_yaml_files(options)
file_utils.discover_lintables(options)

expected_info = (
"ansiblelint",
Expand All @@ -239,7 +241,7 @@ def test_get_yaml_files_git_verbose(reset_env_var, message_prefix, monkeypatch,
(True, False),
ids=('in Git', 'outside Git'),
)
def test_get_yaml_files_silent(is_in_git, monkeypatch, capsys):
def test_discover_lintables_silent(is_in_git, monkeypatch, capsys):
"""Verify that no stderr output is displayed while discovering yaml files.

(when the verbosity is off, regardless of the Git or Git-repo presence)
Expand All @@ -257,7 +259,7 @@ def test_get_yaml_files_silent(is_in_git, monkeypatch, capsys):
)

monkeypatch.chdir(str(lint_path))
files = file_utils.get_yaml_files(options)
files = file_utils.discover_lintables(options)
stderr = capsys.readouterr().err
assert not stderr, 'No stderr output is expected when the verbosity is off'
assert (
Expand All @@ -267,14 +269,14 @@ def test_get_yaml_files_silent(is_in_git, monkeypatch, capsys):
)


def test_get_yaml_files_umlaut(monkeypatch):
"""Verify that filenames containing German umlauts are not garbled by the get_yaml_files."""
def test_discover_lintables_umlaut(monkeypatch):
"""Verify that filenames containing German umlauts are not garbled by the discover_lintables."""
options = cli.get_config([])
test_dir = Path(__file__).resolve().parent
lint_path = test_dir / '..' / 'examples' / 'playbooks'

monkeypatch.chdir(str(lint_path))
files = file_utils.get_yaml_files(options)
files = file_utils.discover_lintables(options)
assert '"with-umlaut-\\303\\244.yml"' not in files
assert 'with-umlaut-ä.yml' in files

Expand Down Expand Up @@ -376,18 +378,19 @@ def test_default_kinds(monkeypatch, path: str, kind: FileType) -> None:
options = cli.get_config([])

def mockreturn(options):
return [path]
return {path: kind}

# assert Lintable is able to determine file type
lintable_detected = Lintable(path)
lintable_expected = Lintable(path, kind=kind)
assert lintable_detected == lintable_expected

monkeypatch.setattr(utils, 'get_yaml_files', mockreturn)
result = utils.get_lintables(options)
monkeypatch.setattr(utils, 'discover_lintables', mockreturn)
result = utils.discover_lintables(options)
# get_lintable could return additional files and we only care to see
# that the given file is among the returned list.
assert lintable_expected in result
assert lintable_detected.name in result
assert lintable_detected.kind == result[lintable_expected.name]


def test_auto_detect_exclude(monkeypatch):
Expand All @@ -397,7 +400,7 @@ def test_auto_detect_exclude(monkeypatch):
def mockreturn(options):
return ['foo/playbook.yml', 'bar/playbook.yml']

monkeypatch.setattr(utils, 'get_yaml_files', mockreturn)
monkeypatch.setattr(utils, 'discover_lintables', mockreturn)
result = utils.get_lintables(options)
assert result == [Lintable('bar/playbook.yml', kind='playbook')]

Expand Down