From 1047f6b2128da6fdd1cee47bc3d21a7f695e9183 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Sat, 15 May 2021 13:32:47 +0100 Subject: [PATCH] Rename get_yaml_files to discover_lintables As ansible-lint can lint not only yaml files we update internal method that detects lintable files. This should help avoiding confusions as this function does not always return only yaml files. --- src/ansiblelint/file_utils.py | 11 +++++++---- src/ansiblelint/utils.py | 4 ++-- test/TestUtils.py | 20 +++++++++++--------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index a9adb7bdd73..b8b34200713 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -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)) @@ -216,7 +216,7 @@ 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: @@ -224,6 +224,9 @@ def get_yaml_files(options: Namespace) -> Dict[str, Any]: _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('.') @@ -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(): diff --git a/src/ansiblelint/utils.py b/src/ansiblelint/utils.py index 70aea18a240..e70c9ddef33 100644 --- a/src/ansiblelint/utils.py +++ b/src/ansiblelint/utils.py @@ -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 @@ -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 diff --git a/test/TestUtils.py b/test/TestUtils.py index 252bc37a43f..d0c61edaf0a 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -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", @@ -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) @@ -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 ( @@ -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 @@ -383,8 +385,8 @@ def mockreturn(options): 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