diff --git a/.appveyor.yml b/.appveyor.yml index b83e81a..1ef8015 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -21,35 +21,34 @@ environment: - TOXENV: lint PYTHON_VERSION: 3.10 APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004 - - TOXENV: py39 + - TOXENV: py39-windows PYTHON_VERSION: 3.9 APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2019" - - TOXENV: py37 - PYTHON_VERSION: 3.7 + - TOXENV: py311-macos + PYTHON_VERSION: 3.11 APPVEYOR_BUILD_WORKER_IMAGE: macOS - - TOXENV: py38 + - TOXENV: py38-linux PYTHON_VERSION: 3.8 APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004 - - TOXENV: py39 + - TOXENV: py39-linux PYTHON_VERSION: 3.9 APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004 - - TOXENV: py310 + - TOXENV: py310-linux PYTHON_VERSION: 3.10 APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004 - - TOXENV: py311 + - TOXENV: py311-linux PYTHON_VERSION: 3.11 APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004 build: false install: - # AppVeyor requires us to manually source a virtualenv for non-Windows tests. This line is allowed to fail (and should - # fail) on Windows images. - - source ~/venv${PYTHON_VERSION}/bin/activate || (exit 0) - - pip install -U tox + - sh: source ~/venv${PYTHON_VERSION}/bin/activate + - cmd: set PATH=C:\Python39;C:\Python39\Scripts;%PATH% + - python -m pip install -U tox test_script: - - tox -vv + - python -m tox -vv branches: only: diff --git a/README.md b/README.md index 4b24aac..34eec07 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,25 @@ repos: Normally, after flake8-executable is installed, invoking flake8 will also run this plugin. For more details, check out the [Flake8 plugin page][]. +### Nix-shell scripts + +EXE003 also accepts a `nix-shell` shebang when its option lines select a Python +interpreter with `-i`, as described in the [Nix reference manual][nix-shell]: + +```python +#!/usr/bin/env nix-shell +#! nix-shell -i python3 +#! nix-shell --packages python3 +print("Hello world") +``` + +The first line selects `nix-shell`; the subsequent directives provide its interpreter +and dependencies. Merely including a Python package without selecting a Python +interpreter does not suppress EXE003. The file must still have executable permission +to satisfy EXE001. + +[nix-shell]: https://nix.dev/manual/nix/stable/command-ref/nix-shell.html#use-as-a--interpreter + ## Copyright and License Copyright (c) 2019 Hong Xu diff --git a/flake8_executable/__init__.py b/flake8_executable/__init__.py index 026abe0..6608e2d 100644 --- a/flake8_executable/__init__.py +++ b/flake8_executable/__init__.py @@ -18,6 +18,7 @@ from abc import ABC import os import re +import shlex from typing import Any, Iterable, List, Tuple, Optional, Union from ._version import version as __version__ # type: ignore @@ -113,6 +114,28 @@ def __init__(self, self.filename = filename self.lines = lines + @staticmethod + def _nix_shell_uses_python(shebang: str, lines: List[str]) -> bool: + if not re.match(r'^\s*#!\s*(?:\S*/)?(?:env\s+)?nix-shell(?:\s|$)', shebang): + return False + + # nix-shell combines options from all of its directive lines, with the last -i taking effect. + args: List[str] = [] + for line in lines: + match = re.match(r'^#!\s*nix-shell\s+(.*)$', line) + if match: + try: + args.extend(shlex.split(match.group(1))) + except ValueError: + return False + + interpreter = '' + arguments = iter(args) + for argument in arguments: + if argument == '-i': + interpreter = next(arguments, '') + return 'python' in interpreter + def run(self) -> Optional[Iterable[Error]]: # Get lines if its not already read if self.lines is None: @@ -139,7 +162,7 @@ def run(self) -> Optional[Iterable[Error]]: if not is_executable: # pragma: no cover windows. No execution of this branch on Windows if EXE001.should_check(filename=self.filename): yield EXE001(line_number=shebang_lineno)() - if 'python' not in shebang_line: + if 'python' not in shebang_line and not self._nix_shell_uses_python(shebang_line, self.lines): if EXE003.should_check(): yield EXE003(line_number=shebang_lineno, shebang=shebang_line.strip())() if shebang_lineno > 1: diff --git a/requirements/lint.txt b/requirements/lint.txt index cec51cf..39bfd05 100644 --- a/requirements/lint.txt +++ b/requirements/lint.txt @@ -1,4 +1,6 @@ bandit==1.7.4 +# Bandit 1.7.4 imports pbr but does not declare it as a dependency. +pbr==5.11.1 flake8==6.0.0 flake8-bugbear==21.11.29 flake8-comprehensions==3.10.1 diff --git a/tests/test_flake8_executable.py b/tests/test_flake8_executable.py index effb06e..fe1c750 100644 --- a/tests/test_flake8_executable.py +++ b/tests/test_flake8_executable.py @@ -123,6 +123,57 @@ def test_stdin_negative_empty(self, filename): ec = ExecutableChecker(filename=filename, lines=[]) assert len(tuple(ec.run())) == 0 + @pytest.mark.parametrize('shebang', [ + '#!/usr/bin/env nix-shell', + '#! /usr/bin/env nix-shell', + '#!/nix/var/nix/profiles/default/bin/nix-shell', + ]) + @pytest.mark.parametrize('options', [ + '#! nix-shell -i python3 --packages python3', + '#!nix-shell -i python3\n#!nix-shell --packages python3', + '#! nix-shell --packages python3\n#! nix-shell -i "python3"', + "#! nix-shell -i python3 -p 'python3.withPackages (ps: [ ps.requests ])'", + ]) + def test_nix_shell_python(self, shebang, options): + lines = (shebang + '\n' + options + '\nprint("hello")\n').splitlines(True) + assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == () + + @pytest.mark.parametrize('options', [ + '', + '#! nix-shell --packages python3', + '#! nix-shell -i bash --packages python3', + '#! nix-shell -i python3\n#! nix-shell -i bash', + '#! nix-shell -i', + '#! nix-shell -i "python3', + '# ordinary comment mentioning -i python3', + ]) + def test_nix_shell_without_python_interpreter(self, options): + shebang = '#!/usr/bin/env nix-shell' + lines = (shebang + '\n' + options + '\n').splitlines(True) + assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == ( + EXE003(line_number=1, shebang=shebang)(), + ) + + @pytest.mark.parametrize('shebang', ['#!/bin/bash', '#!/usr/bin/env nix-shell-wrapper']) + def test_nix_shell_options_require_nix_shell_shebang(self, shebang): + lines = [shebang + '\n', '#! nix-shell -i python3\n'] + assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == ( + EXE003(line_number=1, shebang=shebang)(), + ) + + def test_nix_shell_misplaced_shebang(self): + lines = ['# comment\n', '#!/usr/bin/env nix-shell\n', '#! nix-shell -i python3\n'] + assert tuple(ExecutableChecker(filename='-', lines=lines).run()) == (EXE005(line_number=2)(),) + + @pytest.mark.skipif(WIN32, reason="Windows doesn't support EXE001") + @pytest.mark.parametrize('executable', [True, False]) + def test_nix_shell_permissions(self, tmp_path, executable): + script = tmp_path / 'script.py' + script.write_text('#!/usr/bin/env nix-shell\n#! nix-shell -i python3\n') + script.chmod(0o755 if executable else 0o644) + errors = tuple(ExecutableChecker(filename=str(script)).run()) + assert errors == (() if executable else (EXE001(line_number=1)(),)) + def test_cli(self): "Test the flake8 CLI interface and ensure there's no crash." import flake8.main.application diff --git a/tox.ini b/tox.ini index 9b259d0..e645d55 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,6 @@ [tox] minversion = 3.20 envlist = py{37,38,39,310,311}-{linux,macos,windows} -skipsdist = True [testenv] download = True