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

Fix parsing of unicode filenames reported by git ls-files (#1339) #1340

Merged
merged 1 commit into from Feb 12, 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
5 changes: 5 additions & 0 deletions examples/playbooks/with-umlaut-ä.yml
@@ -0,0 +1,5 @@
---
- hosts:
- localhost
roles:
- name: node
4 changes: 2 additions & 2 deletions src/ansiblelint/file_utils.py
Expand Up @@ -166,15 +166,15 @@ 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

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",
Expand Down
7 changes: 3 additions & 4 deletions src/ansiblelint/testing/__init__.py
Expand Up @@ -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,
Expand Down
16 changes: 14 additions & 2 deletions test/TestUtils.py
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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: "
Expand Down