Skip to content

Commit 53d46d7

Browse files
committed
fix --settings= and --pythonpath= passthrough for bash and zsh, start work on powershell test #156
1 parent 14db8e1 commit 53d46d7

File tree

6 files changed

+67
-21
lines changed

6 files changed

+67
-21
lines changed

django_typer/templates/shell_complete/bash.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99

1010
for ((i=0; i<COMP_CWORD; i++)); do
1111
case "${COMP_WORDS[i]}" in
12-
--settings)
12+
--settings|--settings=*)
1313
# Ensure the next word exists and is not another flag
14-
if [[ $((i + 1)) -lt $COMP_CWORD ]]; then
14+
if [[ "${COMP_WORDS[i]}" == --settings=* ]]; then
15+
settings_option="${COMP_WORDS[i]}"
16+
elif [[ $((i + 1)) -lt $COMP_CWORD ]]; then
1517
settings_option="--settings=${COMP_WORDS[i+1]}"
1618
fi
1719
;;
18-
--pythonpath)
20+
--pythonpath|--pythonpath=*)
1921
# Ensure the next word exists and is not another flag
20-
if [[ $((i + 1)) -lt $COMP_CWORD ]]; then
22+
if [[ "${COMP_WORDS[i]}" == --pythonpath=* ]]; then
23+
pythonpath_option="${COMP_WORDS[i]}"
24+
elif [[ $((i + 1)) -lt $COMP_CWORD ]]; then
2125
pythonpath_option="--pythonpath=${COMP_WORDS[i+1]}"
2226
fi
2327
;;

django_typer/templates/shell_complete/zsh.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020

2121
for ((i=1; i<$CURRENT; i++)); do
2222
case "${words[i]}" in
23-
--settings)
23+
--settings|--settings=*)
2424
# Only pass settings to completion script if we're sure it's value does not itself need completion!
25-
if (( i + 1 < CURRENT )) && [[ -n "${words[i+1]}" ]] && [[ "${words[i+1]}" != --* ]]; then
25+
if [[ "${words[i]}" == --settings=* ]]; then
26+
settings_option="${words[i]}"
27+
elif (( i + 1 < CURRENT )) && [[ -n "${words[i+1]}" ]] && [[ "${words[i+1]}" != --* ]]; then
2628
settings_option="--settings=${words[i+1]}"
2729
fi
2830
;;
29-
--pythonpath)
31+
--pythonpath|--pythonpath=*)
3032
# Only pass pythonpath to completion script if we're sure it's value does not itself need completion!
31-
if (( i + 1 < CURRENT )) && [[ -n "${words[i+1]}" ]] && [[ "${words[i+1]}" != --* ]]; then
33+
if [[ "${words[i]}" == --pythonpath=* ]]; then
34+
pythonpath_option="${words[i]}"
35+
elif (( i + 1 < CURRENT )) && [[ -n "${words[i+1]}" ]] && [[ "${words[i+1]}" != --* ]]; then
3236
pythonpath_option="--pythonpath=${words[i+1]}"
3337
fi
3438
;;

tests/shellcompletion/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
pass
3232

3333

34-
def read_all_from_fd_with_timeout(fd, timeout=1):
34+
def read_all_from_fd_with_timeout(fd, timeout=2):
3535
all_data = bytearray()
3636
start_time = time.time()
3737

@@ -277,6 +277,13 @@ def test_settings_pass_through(self):
277277
" ",
278278
)
279279
self.assertIn("django_typer", completions)
280+
completions = self.get_completions(
281+
self.launch_script,
282+
"app_labels",
283+
"--settings=tests.settings.examples",
284+
" ",
285+
)
286+
self.assertIn("django_typer", completions)
280287

281288
def test_pythonpath_pass_through(self):
282289
# https://github.com/django-commons/django-typer/issues/68
@@ -294,6 +301,14 @@ def test_pythonpath_pass_through(self):
294301
" ",
295302
)
296303
self.assertIn("working", completions)
304+
completions = self.get_completions(
305+
self.launch_script,
306+
"python_path",
307+
"--pythonpath=tests/off_path",
308+
"--option",
309+
" ",
310+
)
311+
self.assertIn("working", completions)
297312

298313

299314
class _InstalledScriptTestCase(_DefaultCompleteTestCase):

tests/shellcompletion/test_bash.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111

1212
@pytest.mark.skipif(shutil.which("bash") is None, reason="Bash not available")
13-
class BashShellTests(_DefaultCompleteTestCase, TestCase):
13+
class BashTests(_DefaultCompleteTestCase, TestCase):
1414
shell = "bash"
1515
directory = Path("~/.bash_completions").expanduser()
1616

1717
def set_environment(self, fd):
18-
# super().set_environment(fd)
19-
os.write(fd, f"export PATH={Path(sys.executable).parent}:$PATH\n".encode())
2018
os.write(
2119
fd,
2220
f"export DJANGO_SETTINGS_MODULE=tests.settings.completion\n".encode(),
@@ -43,5 +41,5 @@ def test_rich_output(self): ...
4341

4442

4543
@pytest.mark.skipif(shutil.which("bash") is None, reason="Bash not available")
46-
class BashExeShellTests(_InstalledScriptTestCase, BashShellTests):
44+
class BashExeTests(_InstalledScriptTestCase, BashTests):
4745
shell = "bash"

tests/shellcompletion/test_powershell.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@
55

66
import pytest
77
from django.test import TestCase
8+
from django_typer.management.commands.shells.powershell import PowerShellComplete
89

910
from tests.shellcompletion import (
1011
_DefaultCompleteTestCase,
1112
_InstalledScriptTestCase,
1213
)
1314

1415

15-
@pytest.mark.skipif(shutil.which("pwsh") is None, reason="Powershell not available")
16+
@pytest.mark.skipif(
17+
shutil.which("powershell") is None, reason="Powershell not available"
18+
)
1619
class PowerShellTests(_DefaultCompleteTestCase, TestCase):
17-
shell = "pwsh"
18-
directory = Path("~/.config/powershell").expanduser()
20+
shell = "powershell"
21+
profile: Path
22+
23+
@classmethod
24+
def setUpClass(cls) -> None:
25+
cls.profile = PowerShellComplete().get_user_profile()
26+
return super().setUpClass()
1927

2028
@property
2129
def interactive_opt(self):
@@ -30,6 +38,9 @@ def set_environment(self, fd):
3038
fd,
3139
f'$env:DJANGO_SETTINGS_MODULE="tests.settings.completion"\n'.encode(),
3240
)
41+
activate = Path(sys.executable).absolute().parent / "activate.ps1"
42+
if activate.is_file():
43+
os.write(fd, f"{activate}\n".encode())
3344

3445
def test_shell_complete(self):
3546
# just verify that install/remove works. The actual completion is not tested
@@ -42,25 +53,27 @@ def test_shell_complete(self):
4253
def verify_install(self, script=None):
4354
if not script:
4455
script = self.manage_script
45-
self.assertTrue((self.directory / "Microsoft.PowerShell_profile.ps1").exists())
56+
self.assertTrue(self.profile.exists())
4657
self.assertTrue(
4758
f"Register-ArgumentCompleter -Native -CommandName {script} -ScriptBlock $scriptblock"
48-
in (self.directory / "Microsoft.PowerShell_profile.ps1").read_text()
59+
in self.profile.read_text()
4960
)
5061

5162
def verify_remove(self, script=None):
5263
if not script:
5364
script = self.manage_script
54-
if (self.directory / "Microsoft.PowerShell_profile.ps1").exists():
55-
contents = (self.directory / "Microsoft.PowerShell_profile.ps1").read_text()
65+
if self.profile.exists():
66+
contents = self.profile.read_text()
5667
self.assertFalse(
5768
f"Register-ArgumentCompleter -Native -CommandName {script} -ScriptBlock $scriptblock"
5869
in contents
5970
)
6071
self.assertTrue(contents) # should have been deleted if it were empty
6172

6273

63-
@pytest.mark.skipif(shutil.which("pwsh") is None, reason="Powershell not available")
74+
@pytest.mark.skipif(
75+
shutil.which("powershell") is None, reason="Powershell not available"
76+
)
6477
class PowerShellInstallRemoveTests(_InstalledScriptTestCase, PowerShellTests):
6578
def test_shell_complete(self):
6679
# the power shell completion script registration is all in one file

tests/shellcompletion/test_zsh.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55
from django.test import TestCase
6+
import os
7+
import sys
68

79
from tests.shellcompletion import _DefaultCompleteTestCase, _InstalledScriptTestCase
810

@@ -22,6 +24,16 @@ def verify_remove(self, script=None):
2224
script = self.manage_script
2325
self.assertFalse((self.directory / f"_{script}").exists())
2426

27+
# def set_environment(self, fd):
28+
# os.write(
29+
# fd,
30+
# f"export DJANGO_SETTINGS_MODULE=tests.settings.completion\n".encode(),
31+
# )
32+
# os.write(fd, "source ~/.zshrc\n".encode())
33+
# activate = Path(sys.executable).absolute().parent / "activate"
34+
# if activate.is_file():
35+
# os.write(fd, f"source {activate}\n".encode())
36+
2537

2638
@pytest.mark.skipif(shutil.which("zsh") is None, reason="Z-Shell not available")
2739
class ZshExeTests(_InstalledScriptTestCase, ZshTests, TestCase):

0 commit comments

Comments
 (0)