def test_get_yaml_files_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) expected_info = ( "ansiblelint", logging.INFO, 'Discovering files to lint: git ls-files -z', ) assert expected_info in caplog.record_tuples assert any(m.startswith(message_prefix) for m in caplog.messages)
def test_get_yaml_files_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) Also checks expected number of files are detected. """ options = cli.get_config([]) test_dir = Path(__file__).resolve().parent lint_path = test_dir / '..' / 'examples' / '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 = file_utils.get_yaml_files(options) stderr = capsys.readouterr().err assert not stderr, 'No stderr 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(), )
def test_get_yaml_files_umlaut(monkeypatch): """Verify that filenames containing German umlauts are not garbled by the get_yaml_files.""" 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) assert '"with-umlaut-\\303\\244.yml"' not in files assert 'with-umlaut-ä.yml' in files
def get_lintables( options: Namespace = Namespace(), args: Optional[List[str]] = None ) -> List[Lintable]: """Detect files and directories that are lintable.""" lintables: List[Lintable] = [] # passing args bypass auto-detection mode if args: for arg in args: lintable = Lintable(arg) if lintable.kind in ("yaml", None): _logger.warning( "Overriding detected file kind '%s' with 'playbook' " "for given positional argument: %s", lintable.kind, arg, ) lintable = Lintable(arg, kind="playbook") lintables.append(lintable) else: for filename in get_yaml_files(options): p = Path(filename) # skip exclusions try: for file_path in options.exclude_paths: if str(p.resolve()).startswith(str(file_path)): raise FileNotFoundError( f'File {file_path} matched exclusion entry: {p}' ) except FileNotFoundError as e: _logger.debug('Ignored %s due to: %s', p, e) continue lintables.append(Lintable(p)) # stage 2: guess roles from current lintables, as there is no unique # file that must be present in any kind of role. for lintable in lintables: parts = lintable.path.parent.parts if 'roles' in parts: role = lintable.path while role.parent.name != "roles" and role.name: role = role.parent if role.exists: lintable = Lintable(role, kind="role") if lintable not in lintables: _logger.debug("Added role: %s", lintable) lintables.append(lintable) return lintables