diff --git "a/examples/playbooks/with-umlaut-\303\244.yml" "b/examples/playbooks/with-umlaut-\303\244.yml" new file mode 100644 index 0000000000..3a18e9422f --- /dev/null +++ "b/examples/playbooks/with-umlaut-\303\244.yml" @@ -0,0 +1,5 @@ +--- +- hosts: + - localhost + roles: + - name: node diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index dbc22084b4..dd2e24aef0 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -166,7 +166,7 @@ def __repr__(self) -> str: def get_yaml_files(options: Namespace) -> Dict[str, Any]: """Find all yaml files.""" # git is preferred as it also considers .gitignore - git_command = ['git', 'ls-files', '*.yaml', '*.yml'] + git_command = ['git', 'ls-files', '-z', '*.yaml', '*.yml'] _logger.info("Discovering files to lint: %s", ' '.join(git_command)) out = None @@ -174,7 +174,7 @@ def get_yaml_files(options: Namespace) -> Dict[str, Any]: try: out = subprocess.check_output( git_command, stderr=subprocess.STDOUT, universal_newlines=True - ).splitlines() + ).split("\x00")[:-1] except subprocess.CalledProcessError as exc: _logger.warning( "Failed to discover yaml files to lint using git: %s", diff --git a/src/ansiblelint/testing/__init__.py b/src/ansiblelint/testing/__init__.py index 39984f0a07..4566a4a3df 100644 --- a/src/ansiblelint/testing/__init__.py +++ b/src/ansiblelint/testing/__init__.py @@ -98,12 +98,11 @@ def run_ansible_lint( if env is None: _env = {} - for v in safe_list: - if v in os.environ: - _env[v] = os.environ[v] - else: _env = env + for v in safe_list: + if v in os.environ and v not in _env: + _env[v] = os.environ[v] return subprocess.run( args, diff --git a/test/TestUtils.py b/test/TestUtils.py index 966281e056..bf4102a677 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -227,7 +227,7 @@ def test_get_yaml_files_git_verbose(reset_env_var, message_prefix, monkeypatch, expected_info = ( "ansiblelint", logging.INFO, - 'Discovering files to lint: git ls-files *.yaml *.yml', + 'Discovering files to lint: git ls-files -z *.yaml *.yml', ) assert expected_info in caplog.record_tuples @@ -267,6 +267,18 @@ 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.""" + 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 test_logger_debug(caplog): """Test that the double verbosity arg causes logger to be DEBUG.""" options = cli.get_config(['-vv']) @@ -300,7 +312,7 @@ def test_cli_auto_detect(capfd): out, err = capfd.readouterr() # Confirmation that it runs in auto-detect mode - assert "Discovering files to lint: git ls-files *.yaml *.yml" in err + assert "Discovering files to lint: git ls-files -z *.yaml *.yml" in err # An expected rule match from our examples assert ( "examples/playbooks/empty_playbook.yml:0: "