Max Rozentsveyg | 5195ba3 | 2020-05-16 23:59:08 -0400 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | |
Chris Kuehl | 13991f0 | 2017-07-02 21:00:28 -0700 | [diff] [blame] | 4 | import pytest |
| 5 | |
Max Rozentsveyg | 5195ba3 | 2020-05-16 23:59:08 -0400 | [diff] [blame] | 6 | from pre_commit_hooks import check_executables_have_shebangs |
Chris Kuehl | 13991f0 | 2017-07-02 21:00:28 -0700 | [diff] [blame] | 7 | from pre_commit_hooks.check_executables_have_shebangs import main |
Max Rozentsveyg | 5195ba3 | 2020-05-16 23:59:08 -0400 | [diff] [blame] | 8 | from pre_commit_hooks.util import cmd_output |
| 9 | |
| 10 | skip_win32 = pytest.mark.skipif( |
| 11 | sys.platform == 'win32', |
| 12 | reason="non-git checks aren't relevant on windows", |
| 13 | ) |
Chris Kuehl | 13991f0 | 2017-07-02 21:00:28 -0700 | [diff] [blame] | 14 | |
| 15 | |
Max Rozentsveyg | 5195ba3 | 2020-05-16 23:59:08 -0400 | [diff] [blame] | 16 | @skip_win32 # pragma: win32 no cover |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 17 | @pytest.mark.parametrize( |
| 18 | 'content', ( |
| 19 | b'#!/bin/bash\nhello world\n', |
| 20 | b'#!/usr/bin/env python3.6', |
| 21 | b'#!python', |
Anthony Sottile | f5c42a0 | 2020-02-05 11:10:42 -0800 | [diff] [blame] | 22 | '#!☃'.encode(), |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 23 | ), |
| 24 | ) |
Chris Kuehl | 13991f0 | 2017-07-02 21:00:28 -0700 | [diff] [blame] | 25 | def test_has_shebang(content, tmpdir): |
| 26 | path = tmpdir.join('path') |
| 27 | path.write(content, 'wb') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame^] | 28 | assert main((str(path),)) == 0 |
Chris Kuehl | 13991f0 | 2017-07-02 21:00:28 -0700 | [diff] [blame] | 29 | |
| 30 | |
Max Rozentsveyg | 5195ba3 | 2020-05-16 23:59:08 -0400 | [diff] [blame] | 31 | @skip_win32 # pragma: win32 no cover |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 32 | @pytest.mark.parametrize( |
| 33 | 'content', ( |
| 34 | b'', |
| 35 | b' #!python\n', |
| 36 | b'\n#!python\n', |
| 37 | b'python\n', |
Anthony Sottile | f5c42a0 | 2020-02-05 11:10:42 -0800 | [diff] [blame] | 38 | '☃'.encode(), |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 39 | ), |
| 40 | ) |
Chris Kuehl | 13991f0 | 2017-07-02 21:00:28 -0700 | [diff] [blame] | 41 | def test_bad_shebang(content, tmpdir, capsys): |
| 42 | path = tmpdir.join('path') |
| 43 | path.write(content, 'wb') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame^] | 44 | assert main((str(path),)) == 1 |
Chris Kuehl | 13991f0 | 2017-07-02 21:00:28 -0700 | [diff] [blame] | 45 | _, stderr = capsys.readouterr() |
Anthony Sottile | f5c42a0 | 2020-02-05 11:10:42 -0800 | [diff] [blame] | 46 | assert stderr.startswith(f'{path}: marked executable but') |
Max Rozentsveyg | 5195ba3 | 2020-05-16 23:59:08 -0400 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def test_check_git_filemode_passing(tmpdir): |
| 50 | with tmpdir.as_cwd(): |
| 51 | cmd_output('git', 'init', '.') |
| 52 | |
| 53 | f = tmpdir.join('f') |
| 54 | f.write('#!/usr/bin/env bash') |
| 55 | f_path = str(f) |
| 56 | cmd_output('chmod', '+x', f_path) |
| 57 | cmd_output('git', 'add', f_path) |
| 58 | cmd_output('git', 'update-index', '--chmod=+x', f_path) |
| 59 | |
| 60 | g = tmpdir.join('g').ensure() |
| 61 | g_path = str(g) |
| 62 | cmd_output('git', 'add', g_path) |
| 63 | |
| 64 | # this is potentially a problem, but not something the script intends |
| 65 | # to check for -- we're only making sure that things that are |
| 66 | # executable have shebangs |
| 67 | h = tmpdir.join('h') |
| 68 | h.write('#!/usr/bin/env bash') |
| 69 | h_path = str(h) |
| 70 | cmd_output('git', 'add', h_path) |
| 71 | |
| 72 | files = (f_path, g_path, h_path) |
| 73 | assert check_executables_have_shebangs._check_git_filemode(files) == 0 |
| 74 | |
| 75 | |
| 76 | def test_check_git_filemode_failing(tmpdir): |
| 77 | with tmpdir.as_cwd(): |
| 78 | cmd_output('git', 'init', '.') |
| 79 | |
| 80 | f = tmpdir.join('f').ensure() |
| 81 | f_path = str(f) |
| 82 | cmd_output('chmod', '+x', f_path) |
| 83 | cmd_output('git', 'add', f_path) |
| 84 | cmd_output('git', 'update-index', '--chmod=+x', f_path) |
| 85 | |
| 86 | files = (f_path,) |
| 87 | assert check_executables_have_shebangs._check_git_filemode(files) == 1 |
| 88 | |
| 89 | |
| 90 | @pytest.mark.parametrize( |
| 91 | ('content', 'mode', 'expected'), |
| 92 | ( |
| 93 | pytest.param('#!python', '+x', 0, id='shebang with executable'), |
| 94 | pytest.param('#!python', '-x', 0, id='shebang without executable'), |
| 95 | pytest.param('', '+x', 1, id='no shebang with executable'), |
| 96 | pytest.param('', '-x', 0, id='no shebang without executable'), |
| 97 | ), |
| 98 | ) |
| 99 | def test_git_executable_shebang(temp_git_dir, content, mode, expected): |
| 100 | with temp_git_dir.as_cwd(): |
| 101 | path = temp_git_dir.join('path') |
| 102 | path.write(content) |
| 103 | cmd_output('git', 'add', str(path)) |
| 104 | cmd_output('chmod', mode, str(path)) |
| 105 | cmd_output('git', 'update-index', f'--chmod={mode}', str(path)) |
| 106 | |
Max R | 7848fcf | 2020-05-18 21:46:53 -0400 | [diff] [blame] | 107 | # simulate how identify chooses that something is executable |
Max Rozentsveyg | 5195ba3 | 2020-05-16 23:59:08 -0400 | [diff] [blame] | 108 | filenames = [path for path in [str(path)] if os.access(path, os.X_OK)] |
| 109 | |
| 110 | assert main(filenames) == expected |