This commit is contained in:
Kurt von Laven 2022-10-31 17:48:43 -07:00 committed by GitHub
commit e6285b9867
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 9 deletions

View file

@ -1,6 +1,6 @@
from __future__ import annotations
import os
import sys
import pytest
@ -9,6 +9,62 @@ from pre_commit_hooks.check_shebang_scripts_are_executable import \
from pre_commit_hooks.check_shebang_scripts_are_executable import main
from pre_commit_hooks.util import cmd_output
skip_win32 = pytest.mark.xfail(
sys.platform == 'win32',
reason="non-git checks aren't relevant on windows",
)
@skip_win32 # pragma: win32 no cover
@pytest.mark.parametrize(
'content', (
b'#!/bin/bash\nhello world\n',
b'#!/usr/bin/env python3.10',
b'#!python',
'#!☃'.encode(),
),
)
def test_executable_shebang(content, tmpdir):
path = tmpdir.join('path')
path.write(content, 'wb')
cmd_output('chmod', '+x', path)
assert main((str(path),)) == 0
@skip_win32 # pragma: win32 no cover
@pytest.mark.parametrize(
'content', (
b'#!/bin/bash\nhello world\n',
b'#!/usr/bin/env python3.10',
b'#!python',
'#!☃'.encode(),
),
)
def test_not_executable_shebang(content, tmpdir, capsys):
path = tmpdir.join('path')
path.write(content, 'wb')
assert main((str(path),)) == 1
_, stderr = capsys.readouterr()
assert stderr.startswith(
f'{path}: has a shebang but is not marked executable!',
)
@skip_win32 # pragma: win32 no cover
@pytest.mark.parametrize(
'content', (
b'',
b' #!python\n',
b'\n#!python\n',
b'python\n',
''.encode(),
),
)
def test_not_executable_no_shebang(content, tmpdir, capsys):
path = tmpdir.join('path')
path.write(content, 'wb')
assert main((str(path),)) == 0
def test_check_git_filemode_passing(tmpdir):
with tmpdir.as_cwd():
@ -83,7 +139,5 @@ def test_git_executable_shebang(temp_git_dir, content, mode, expected):
cmd_output('chmod', mode, str(path))
cmd_output('git', 'update-index', f'--chmod={mode}', str(path))
# simulate how identify chooses that something is executable
filenames = [path for path in [str(path)] if os.access(path, os.X_OK)]
assert main(filenames) == expected
files = (str(path),)
assert main(files) == expected