Skip to content

Commit

Permalink
add tests for #68
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Dec 19, 2024
1 parent fa39b0c commit 91510db
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 14 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
timeout-minutes: 60
- name: Run Unit Tests
run: |
just test
just test-all
mv .coverage py${{ matrix.python-version }}-dj${{ matrix.django-version }}.coverage
- name: Store coverage files
Expand Down Expand Up @@ -114,7 +114,7 @@ jobs:
timeout-minutes: 60
- name: Run Unit Tests
run: |
just test
just test-all
mv .coverage py${{ matrix.python-version }}-dj${{ matrix.django-version }}.coverage
- name: Store coverage files
Expand Down Expand Up @@ -167,7 +167,7 @@ jobs:
timeout-minutes: 60
- name: Run Unit Tests
run: |
just test
just test-all
mv .coverage py${{ matrix.python-version }}-dj${{ matrix.django-version }}.coverage
- name: Store coverage files
Expand Down Expand Up @@ -217,7 +217,7 @@ jobs:
timeout-minutes: 60
- name: Run Unit Tests
run: |
just test-cases tests/shellcompletion/*
just test tests/shellcompletion/*
mv .coverage linux-complete-py${{ matrix.python-version }}-dj${{ matrix.django-version }}.coverage
- name: Store coverage files
Expand Down Expand Up @@ -294,7 +294,7 @@ jobs:
- name: Run Unit Tests
shell: zsh {0}
run: |
just test-cases tests/shellcompletion/test_zsh.py || exit 1
just test tests/shellcompletion/test_zsh.py || exit 1
mv .coverage macos-complete-py${{ matrix.python-version }}-dj${{ matrix.django-version }}.coverage
- name: Store coverage files
Expand Down
1 change: 1 addition & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ v3.0.0 (202X-XX-XX)
* Implemented `Add a @finalize decorator for functions to collect/operate on subroutine results. <https://github.com/django-commons/django-typer/issues/140>`_
* Fixed `Remove management imports in django_typer/__init__.py <https://github.com/django-commons/django-typer/issues/95>`_
* Fixed `ParamSpec includes self for group methods <https://github.com/django-commons/django-typer/issues/73>`_
* Fixed `Installed shellcompletion scripts do not pass values of --settings or --pythonpath <https://github.com/django-commons/django-typer/issues/68>`_
* Fixed `shellcompletion complete should print to the command's stdout. <https://github.com/django-commons/django-typer/issues/19>`_

Migrating from 2.x to 3.x
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ test-rich:
poetry install -E rich
poetry run pytest -m rich --cov-append

test: test-rich test-no-rich install-colorama
test-all: test-rich test-no-rich install-colorama
poetry run pytest -m "not rich and not no_rich" --cov-append
poetry run pip uninstall -y colorama
poetry run pytest -k test_ctor_params --cov-append

test-cases +TESTS:
test *TESTS:
poetry run pytest {{ TESTS }}

precommit:
Expand Down
23 changes: 23 additions & 0 deletions tests/apps/test_app/management/commands/python_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import typing as t
from django_typer.management import TyperCommand
from typer import Option


def complete(ctx, param, incomplete):
from path_test import mod

return [mod.option]


class Command(TyperCommand):
def handle(
self,
option: t.Annotated[
str,
Option(
help="An option that requires --pythonpath to be passed for tab completion to work.",
shell_complete=complete,
),
],
):
return option
Empty file.
1 change: 1 addition & 0 deletions tests/off_path/path_test/mod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option = "working"
42 changes: 35 additions & 7 deletions tests/shellcompletion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def read_all_from_fd_with_timeout(fd, timeout):

def scrub(output: str) -> str:
"""Scrub control code characters and ansi escape sequences for terminal colors from output"""
return re.sub(r"[\x00-\x1F\x7F]|\x1B\[[0-?]*[ -/]*[@-~]", "", output).replace(
return re.sub(r"\x1B\[[0-?]*[ -/]*[@-~]", "", output, flags=re.IGNORECASE).replace(
"\t", ""
)

Expand All @@ -80,7 +80,7 @@ def setUp(self):
super().setUp()

def tearDown(self):
# self.remove()
self.remove()
super().tearDown()

def verify_install(self, script=None):
Expand Down Expand Up @@ -111,6 +111,7 @@ def remove(self, script=None):
if self.shell:
self.command.init(shell=self.shell)
self.command.uninstall(**kwargs)
self.get_completions("ping") # just to reinit shell
self.verify_remove(script=script)

def set_environment(self, fd):
Expand Down Expand Up @@ -202,9 +203,7 @@ def run_bad_command_completion(self):

def run_command_completion(self):
completions = self.get_completions(self.launch_script, "complet")
# annoyingly in CI there are some spaces inserted between the incomplete phrase
# and the completion on linux in bash specifically
self.assertTrue(re.match(r".*complet\s*ion.*", completions))
self.assertIn("completion", completions)
completions = self.get_completions(self.launch_script)
self.assertIn("adapted", completions)
self.assertIn("help_precedence", completions)
Expand Down Expand Up @@ -254,14 +253,43 @@ def test_shell_complete(self):
def test_rich_output(self):
self.install(force_color=True)
self.run_rich_option_completion(rich_output_expected=True)
self.remove()

@pytest.mark.rich
@pytest.mark.skipif(not rich_installed, reason="Rich not installed")
def test_no_rich_output(self):
self.install(no_color=True)
self.run_rich_option_completion(rich_output_expected=False)
# self.remove()

def test_settings_pass_through(self):
# https://github.com/django-commons/django-typer/issues/68
self.install()
completions = self.get_completions(self.launch_script, "app_labels", " ")
self.assertNotIn("django_typer", completions)
completions = self.get_completions(
self.launch_script,
"app_labels",
"--settings",
"tests.settings.examples",
" ",
)
self.assertIn("django_typer", completions)

def test_pythonpath_pass_through(self):
# https://github.com/django-commons/django-typer/issues/68
self.install()
completions = self.get_completions(
self.launch_script, "python_path", "--options", " "
)
self.assertNotIn("working", completions)
completions = self.get_completions(
self.launch_script,
"python_path",
"--pythonpath",
"tests/off_path",
"--option",
" ",
)
self.assertIn("working", completions)


class _InstalledScriptTestCase(_DefaultCompleteTestCase):
Expand Down

0 comments on commit 91510db

Please sign in to comment.