Skip to content

Commit

Permalink
Finish shell completion implementation
Browse files Browse the repository at this point in the history
A whole lot of busywork:

- Poetry
- Tests
- Documentation
- Change log

Signed-off-by: Carmen Bianca BAKKER <carmenbianca@fsfe.org>
  • Loading branch information
carmenbianca committed Sep 26, 2024
1 parent df31c88 commit b450c11
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Install dependencies
run: |
pip install poetry~=1.3.0
poetry install --no-interaction --only main,test
poetry install --no-interaction --only main,test --all-extras
- name: Run tests with pytest
run: |
poetry run pytest --cov=reuse
Expand All @@ -53,7 +53,6 @@ jobs:
- name: Install dependencies
run: |
pip install poetry~=1.3.0
pip install shtab
poetry install --no-interaction --only main,dev,test
- name: Lint with Pylint
run: |
Expand Down Expand Up @@ -87,7 +86,6 @@ jobs:
- name: Install dependencies
run: |
pip install poetry~=1.3.0
pip install shtab
poetry install --no-interaction --only main,dev,test
- name: Test typing with mypy
run: |
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ repos:
- id: reuse-lint-file
```

### Shell completion

You can generate a shell completion script with `reuse --print-completion bash`.
Replace 'bash' as needed. You must place the printed text in a file dictated by
your shell to benefit from completions.

This functionality depends on `shtab`, which is an optional dependency. To
benefit from this feature, install reuse with the `completion` extra, like
`pipx install reuse[completion]`.

## Maintainers

- Carmen Bianca Bakker <carmenbianca@fsfe.org>
Expand Down
1 change: 1 addition & 0 deletions changelog.d/added/shtab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added `--print-completion` using a new `shtab` optional dependency. (#1076)
11 changes: 8 additions & 3 deletions docs/man/reuse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ Options

.. option:: -s, --print-completion SHELL

Print a static shell completion file, for the given shell. This option depends
on python-shtab and as such defines which shells are supported. Presently this
includes bash, tcsh and zsh, with fish support being proposed.
Print a static shell completion script for the given shell and exit. You must
place the printed text in a file dictated by your shell before the completions
will function. For Bash, this file is
``${XDG_DATA_HOME}/bash-completion/reuse``.

This option depends on ``shtab``, which is an optional dependency of
:program:`reuse`. The supported shells depend on your installed version of
``shtab``.

.. option:: -h, --help

Expand Down
21 changes: 20 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ license-expression = ">=1.0"
python-debian = ">=0.1.34,!=0.1.45,!=0.1.46,!=0.1.47"
tomlkit = ">=0.8"
attrs = ">=21.3"
shtab = { version = ">=1.4.0", optional = true }

[tool.poetry.extras]
completion = ["shtab"]

[tool.poetry.group.test.dependencies]
pytest = ">=6.0.0"
Expand Down
10 changes: 6 additions & 4 deletions src/reuse/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"""Entry functions for reuse."""

import argparse
import contextlib
import logging
import os
import sys
import warnings
from gettext import gettext as _
from pathlib import Path
from types import ModuleType
from typing import IO, Callable, List, Optional, Type, cast

from . import (
Expand All @@ -35,10 +37,9 @@
from .project import GlobalLicensingConflict, GlobalLicensingFound, Project
from .vcs import find_root

try:
import shtab
except ImportError:
shtab = None
shtab: Optional[ModuleType] = None
with contextlib.suppress(ImportError):
import shtab # type: ignore[no-redef]

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -110,6 +111,7 @@ def parser() -> argparse.ArgumentParser:
help=_("define root of project"),
)
if shtab:
# This is magic. Running `reuse -s bash` now prints bash completions.
shtab.add_argument_to(parser, ["-s", "--print-completion"])
parser.add_argument(
"--version",
Expand Down
12 changes: 11 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from freezegun import freeze_time

from reuse import download
from reuse._main import main
from reuse._main import main, shtab
from reuse._util import GIT_EXE, HG_EXE, JUJUTSU_EXE, PIJUL_EXE, cleandoc_nl
from reuse.report import LINT_VERSION

Expand Down Expand Up @@ -88,6 +88,16 @@ def mock_put_license_in_file(monkeypatch):
return result


@pytest.mark.skipif(not shtab, reason="shtab required")
def test_print_completion(capsys):
"""shtab completions are printed."""
with pytest.raises(SystemExit) as error:
main(["--print-completion", "bash"])

assert error.value.code == 0
assert "AUTOMATICALLY GENERATED by `shtab`" in capsys.readouterr().out


def test_lint(fake_repository, stringio, optional_git_exe, optional_hg_exe):
"""Run a successful lint. The optional VCSs are there to make sure that the
test also works if these programs are not installed.
Expand Down

0 comments on commit b450c11

Please sign in to comment.