Skip to content

Commit 95def3c

Browse files
committed
add fish completion tests
1 parent 949e65f commit 95def3c

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

Diff for: .github/workflows/test.yml

+7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ jobs:
9696
poetry run pip install --upgrade pip
9797
poetry install
9898
poetry run pip install -U "Django~=${{ matrix.django-version }}"
99+
- name: Install Fish shell
100+
run: |
101+
sudo apt-get update
102+
sudo apt-get install -y fish
99103
- name: Run Unit Tests
100104
run: |
101105
poetry run pytest django_typer/tests/completion_tests.py
@@ -154,6 +158,9 @@ jobs:
154158
touch ~/.bashrc
155159
echo "\n[[ -r "$(brew --prefix)/etc/profile.d/bash_completion.sh" ]] && . "$(brew --prefix)/etc/profile.d/bash_completion.sh" || true" >> ~/.bash_profile
156160
source ~/.bashrc
161+
- name: Install Fish shell
162+
run: |
163+
brew install fish
157164
- name: Install Poetry
158165
uses: snok/install-poetry@v1
159166
with:

Diff for: django_typer/management/commands/shellcompletion.py

+17-19
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,23 @@ def remove(
366366
Shells.bash: Path("~/.bashrc").expanduser(),
367367
Shells.zsh: Path("~/.zshrc").expanduser(),
368368
}.get(self.shell, None)
369-
assert (
370-
rc_file and rc_file.is_file()
371-
), f"No rc file {rc_file} found for shell {self.shell}"
372-
edited = []
373-
with open(rc_file, "rt", encoding="utf-8") as rc:
374-
for line in rc.readlines():
375-
if (
376-
self.shell is Shells.bash
377-
and line.strip() == f"source {installed_path}"
378-
):
379-
continue
380-
edited.append(line)
381-
# remove empty lines from the end of the file, the typer install scripts add
382-
# extra newlines
383-
while edited and not edited[-1].strip():
384-
edited.pop()
385-
edited.append("") # add one back on
386-
with open(rc_file, "wt", encoding="utf-8") as rc:
387-
rc.writelines(edited)
369+
if rc_file and rc_file.is_file():
370+
edited = []
371+
with open(rc_file, "rt", encoding="utf-8") as rc:
372+
for line in rc.readlines():
373+
if (
374+
self.shell is Shells.bash
375+
and line.strip() == f"source {installed_path}"
376+
):
377+
continue
378+
edited.append(line)
379+
# remove empty lines from the end of the file, the typer install scripts add
380+
# extra newlines
381+
while edited and not edited[-1].strip():
382+
edited.pop()
383+
edited.append("") # add one back on
384+
with open(rc_file, "wt", encoding="utf-8") as rc:
385+
rc.writelines(edited)
388386

389387
self.stdout = stdout
390388
self.stdout.write(

Diff for: django_typer/tests/completion_tests.py

+35
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,41 @@ class BashExeShellTests(_InstalledScriptTestCase, TestCase):
267267
pass
268268

269269

270+
@pytest.mark.skipif(shutil.which("fish") is None, reason="Fish not available")
271+
class _FishShellTests(_DefaultCompleteTestCase, TestCase):
272+
"""
273+
TODO this test is currently disabled because fish completion installation does
274+
not seem to work for scripts not on the path.
275+
"""
276+
277+
shell = "fish"
278+
directory = Path("~/.config/fish/completions").expanduser()
279+
280+
def set_environment(self, fd):
281+
# super().set_environment(fd)
282+
os.write(fd, f"export PATH={Path(sys.executable).parent}:$PATH\n".encode())
283+
os.write(
284+
fd,
285+
f'export DJANGO_SETTINGS_MODULE={os.environ["DJANGO_SETTINGS_MODULE"]}\n'.encode(),
286+
)
287+
os.write(fd, f"source .venv/bin/activate\n".encode())
288+
289+
def verify_install(self, script=None):
290+
if not script:
291+
script = self.manage_script
292+
self.assertTrue((self.directory / f"{script}.fish").exists())
293+
294+
def verify_remove(self, script=None):
295+
if not script:
296+
script = self.manage_script
297+
self.assertFalse((self.directory / f"{script}.fish").exists())
298+
299+
300+
@pytest.mark.skipif(shutil.which("fish") is None, reason="Fish not available")
301+
class FishExeShellTests(_InstalledScriptTestCase, TestCase):
302+
pass
303+
304+
270305
@pytest.mark.skipif(shutil.which("pwsh") is None, reason="Powershell not available")
271306
class PowerShellTests(_DefaultCompleteTestCase, TestCase):
272307

0 commit comments

Comments
 (0)