Skip to content

Commit fa8b851

Browse files
committed
Merge branch 'install_permission' of https://github.com/django-commons/django-typer into install_permission
2 parents 4e9ebca + 5f0376c commit fa8b851

File tree

3 files changed

+110
-87
lines changed

3 files changed

+110
-87
lines changed

justfile

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,41 +264,55 @@ list-missed-tests: install log-tests test-all
264264
[script("bash")]
265265
test-bash:
266266
uv sync --all-extras
267-
source .venv/bin/activate && pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_bash tests/test_parser_completers.py tests/shellcompletion/test_bash.py
267+
source .venv/bin/activate
268+
pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_bash tests/test_parser_completers.py tests/shellcompletion/test_bash.py || exit
268269
uv sync --no-extra rich
269-
source .venv/bin/activate && pytest --cov-append tests/shellcompletion/test_bash.py::BashExeTests::test_prompt_install
270+
source .venv/bin/activate
271+
pytest --cov-append tests/shellcompletion/test_bash.py::BashExeTests::test_prompt_install || exit
270272

271273
# test zsh shell completions
272274
[script("zsh")]
273275
test-zsh:
274276
uv sync --all-extras
275-
source .venv/bin/activate && pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_zsh tests/test_parser_completers.py tests/shellcompletion/test_zsh.py
277+
source .venv/bin/activate
278+
pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_zsh tests/test_parser_completers.py tests/shellcompletion/test_zsh.py || exit
276279
uv sync --no-extra rich
277-
source .venv/bin/activate && pytest --cov-append tests/shellcompletion/test_zsh.py::ZshExeTests::test_prompt_install
280+
source .venv/bin/activate
281+
pytest --cov-append tests/shellcompletion/test_zsh.py::ZshExeTests::test_prompt_install || exit
278282

279283
# test powershell shell completions
280284
[script("powershell")]
281285
test-powershell:
282286
uv sync --no-extra rich
283-
. .venv/Scripts/activate.ps1; pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_powershell tests/test_parser_completers.py tests/test_parser_completers.py tests/shellcompletion/test_powershell.py::PowerShellTests tests/shellcompletion/test_powershell.py::PowerShellExeTests; if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
287+
. .venv/Scripts/activate.ps1
288+
pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_powershell tests/test_parser_completers.py tests/test_parser_completers.py tests/shellcompletion/test_powershell.py::PowerShellTests tests/shellcompletion/test_powershell.py::PowerShellExeTests
289+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
284290
uv sync --no-extra rich
285-
. .venv/Scripts/activate.ps1; pytest --cov-append tests/shellcompletion/test_powershell.py::PowerShellExeTests::test_prompt_install; if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
291+
. .venv/Scripts/activate.ps1
292+
pytest --cov-append tests/shellcompletion/test_powershell.py::PowerShellExeTests::test_prompt_install
293+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
286294

287295
# test pwsh shell completions
288296
[script("pwsh")]
289297
test-pwsh:
290298
uv sync --all-extras
291-
. .venv/Scripts/activate.ps1; pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_pwsh tests/test_parser_completers.py tests/shellcompletion/test_powershell.py::PWSHTests tests/shellcompletion/test_powershell.py::PWSHExeTests; if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
299+
. .venv/Scripts/activate.ps1
300+
pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_pwsh tests/test_parser_completers.py tests/shellcompletion/test_powershell.py::PWSHTests tests/shellcompletion/test_powershell.py::PWSHExeTests
301+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
292302
uv sync --no-extra rich
293-
. .venv/Scripts/activate.ps1; pytest --cov-append tests/shellcompletion/test_powershell.py::PWSHExeTests::test_prompt_install; if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
303+
. .venv/Scripts/activate.ps1
304+
pytest --cov-append tests/shellcompletion/test_powershell.py::PWSHExeTests::test_prompt_install
305+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
294306

295307
# test fish shell completions
296308
[script("fish")]
297309
test-fish:
298310
uv sync --all-extras
299-
source .venv/bin/activate.fish && pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_fish tests/test_parser_completers.py tests/shellcompletion/test_fish.py
311+
source .venv/bin/activate.fish
312+
pytest --cov-append tests/shellcompletion/test_shell_resolution.py::TestShellResolution::test_fish tests/test_parser_completers.py tests/shellcompletion/test_fish.py || exit
300313
uv sync --no-extra rich
301-
source .venv/bin/activate.fish && pytest --cov-append tests/shellcompletion/test_fish.py::FishExeShellTests::test_prompt_install
314+
source .venv/bin/activate.fish
315+
pytest --cov-append tests/shellcompletion/test_fish.py::FishExeShellTests::test_prompt_install || exit
302316

303317
# run tests
304318
test *TESTS:

src/django_typer/shells/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def prompt(
303303
line_numbers=False,
304304
)
305305
self.console.print(syntax)
306-
return Confirm.ask(prompt_text)
306+
return Confirm.ask(prompt_text, console=self.console)
307307
else:
308308
print(source)
309309
return input(prompt_text + " [y/N] ").lower() in {"y", "yes"}

tests/shellcompletion/__init__.py

Lines changed: 85 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,21 @@ def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
498498
env = {
499499
**dict(os.environ),
500500
"DJANGO_SETTINGS_MODULE": "tests.settings.completion",
501+
"DJANGO_COLORS": "nocolor",
501502
**env,
502503
}
503504

504505
rex = re.compile
505506
expected = [
506-
rex(r"Append the above contents to (?P<file>.*)\?"), # 0
507-
rex(r"Create (?P<file>.*) with the above contents\?"), # 1
508-
rex(r"Aborted shell completion installation."), # 2
509-
rex(rf"Installed autocompletion for {self.shell}"), # 3
507+
rex(
508+
r"Append\s+the\s+above\s+contents\s+to\s+(?P<file>.*)\?", re.DOTALL
509+
), # 0
510+
rex(
511+
r"Create\s+(?P<file>.*)\s+with\s+the\s+above\s+contents\?",
512+
re.DOTALL,
513+
), # 1
514+
rex(r"Aborted\s+shell\s+completion\s+installation."), # 2
515+
rex(rf"Installed\s+autocompletion\s+for\s+{self.shell}"), # 3
510516
]
511517

512518
install_command = [
@@ -520,6 +526,7 @@ def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
520526
self.verify_remove(directory=directory)
521527

522528
install = pexpect.spawn(self.manage_script, install_command, env=env)
529+
install.setwinsize(24, 800)
523530

524531
def wait_for_output(child) -> t.Tuple[int, t.Optional[str]]:
525532
index = child.expect(expected)
@@ -544,6 +551,7 @@ def wait_for_output(child) -> t.Tuple[int, t.Optional[str]]:
544551

545552
# test an install
546553
install = pexpect.spawn(self.manage_script, install_command, env=env)
554+
install.setwinsize(24, 800)
547555

548556
while True:
549557
idx, _ = wait_for_output(install)
@@ -555,75 +563,76 @@ def wait_for_output(child) -> t.Tuple[int, t.Optional[str]]:
555563

556564
self.verify_install(directory=directory)
557565

558-
else:
559-
560-
def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
561-
env = {
562-
**dict(os.environ),
563-
"DJANGO_SETTINGS_MODULE": "tests.settings.completion",
564-
**env,
565-
}
566-
567-
rex = re.compile
568-
expected_patterns = [
569-
rex(r"Append the above contents to (?P<file>.*)\?"), # 0
570-
rex(r"Create (?P<file>.*) with the above contents\?"), # 1
571-
rex(r"Aborted shell completion installation."), # 2
572-
rex(rf"Installed autocompletion for {self.shell}"), # 3
573-
]
574-
575-
install_command = [
576-
self.manage_script,
577-
"shellcompletion",
578-
"--no-color",
579-
"--shell",
580-
self.shell,
581-
"install",
582-
]
583-
self.remove()
584-
self.verify_remove(directory=directory)
585-
586-
def run_with_response(responses: t.List[str]):
587-
process = subprocess.Popen(
588-
install_command,
589-
env=env,
590-
cwd=directory,
591-
stdin=subprocess.PIPE,
592-
stdout=subprocess.PIPE,
593-
stderr=subprocess.STDOUT,
594-
text=True,
595-
)
596-
597-
output = ""
598-
for response in responses:
599-
while True:
600-
line = process.stdout.readline()
601-
if not line:
602-
break
603-
output += line
604-
605-
matched_index, matched_file = match_output(line)
606-
if matched_index is not None:
607-
process.stdin.write(response + "\n")
608-
process.stdin.flush()
609-
break
610-
611-
process.wait()
612-
return output
613-
614-
def match_output(line: str) -> t.Tuple[t.Optional[int], t.Optional[str]]:
615-
for i, pattern in enumerate(expected_patterns):
616-
match = pattern.search(line)
617-
if match:
618-
return i, match.groupdict().get("file")
619-
return None, None
620-
621-
# Test abort sequence
622-
abort_output = run_with_response(["N", "N"])
623-
self.assertIn("Aborted shell completion installation.", abort_output)
624-
self.verify_remove(directory=directory)
625-
626-
# Test install sequence
627-
install_output = run_with_response(["Y", "Y"])
628-
self.assertIn(f"Installed autocompletion for {self.shell}", install_output)
629-
self.verify_install(directory=directory)
566+
# else:
567+
568+
# def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
569+
# env = {
570+
# **dict(os.environ),
571+
# "DJANGO_SETTINGS_MODULE": "tests.settings.completion",
572+
# "DJANGO_COLORS": "nocolor",
573+
# **env,
574+
# }
575+
576+
# rex = re.compile
577+
# expected_patterns = [
578+
# rex(r"Append the above contents to (?P<file>.*)\?"), # 0
579+
# rex(r"Create (?P<file>.*) with the above contents\?"), # 1
580+
# rex(r"Aborted shell completion installation."), # 2
581+
# rex(rf"Installed autocompletion for {self.shell}"), # 3
582+
# ]
583+
584+
# install_command = [
585+
# self.manage_script,
586+
# "shellcompletion",
587+
# "--no-color",
588+
# "--shell",
589+
# self.shell,
590+
# "install",
591+
# ]
592+
# self.remove()
593+
# self.verify_remove(directory=directory)
594+
595+
# def run_with_response(responses: t.List[str]):
596+
# process = subprocess.Popen(
597+
# install_command,
598+
# env=env,
599+
# cwd=directory,
600+
# stdin=subprocess.PIPE,
601+
# stdout=subprocess.PIPE,
602+
# stderr=subprocess.STDOUT,
603+
# text=True,
604+
# )
605+
606+
# output = ""
607+
# for response in responses:
608+
# while True:
609+
# line = process.stdout.readline()
610+
# if not line:
611+
# break
612+
# output += line
613+
614+
# matched_index, matched_file = match_output(line)
615+
# if matched_index is not None:
616+
# process.stdin.write(response + "\n")
617+
# process.stdin.flush()
618+
# break
619+
620+
# process.wait()
621+
# return output
622+
623+
# def match_output(line: str) -> t.Tuple[t.Optional[int], t.Optional[str]]:
624+
# for i, pattern in enumerate(expected_patterns):
625+
# match = pattern.search(line)
626+
# if match:
627+
# return i, match.groupdict().get("file")
628+
# return None, None
629+
630+
# # Test abort sequence
631+
# abort_output = run_with_response(["N", "N"])
632+
# self.assertIn("Aborted shell completion installation.", abort_output)
633+
# self.verify_remove(directory=directory)
634+
635+
# # Test install sequence
636+
# install_output = run_with_response(["Y", "Y"])
637+
# self.assertIn(f"Installed autocompletion for {self.shell}", install_output)
638+
# self.verify_install(directory=directory)

0 commit comments

Comments
 (0)