Fix install of completion if dirs missing
Fixes situations if completion directories are missing and adds tests for installer.
This commit is contained in:
parent
2e2dfe671b
commit
5d3a539eda
|
@ -17,6 +17,9 @@ class Completion:
|
|||
def install(self):
|
||||
if self.shell in ("all", "bash"):
|
||||
comp_dir = self.share_dir + "/bash-completion/completions/"
|
||||
if not os.path.exists(comp_dir):
|
||||
print("Creating Bash completion directory.")
|
||||
os.makedirs(comp_dir, exist_ok=True)
|
||||
for point in self.entry_points:
|
||||
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "bash_source"
|
||||
with open(comp_dir + point, "w") as file:
|
||||
|
@ -24,6 +27,9 @@ class Completion:
|
|||
print(f"Bash completion for {point} written to {comp_dir}{point}")
|
||||
if self.shell in ("all", "fish"):
|
||||
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
|
||||
if not os.path.exists(comp_dir):
|
||||
print("Creating Fish completion directory.")
|
||||
os.makedirs(comp_dir, exist_ok=True)
|
||||
for point in self.entry_points:
|
||||
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "fish_source"
|
||||
with open(comp_dir + point + ".fish", "w") as file:
|
||||
|
@ -31,6 +37,9 @@ class Completion:
|
|||
print(f"Fish completion for {point} written to {comp_dir}{point}.fish")
|
||||
if self.shell in ("all", "zsh"):
|
||||
comp_dir = self.share_dir + "/zsh/site-functions/"
|
||||
if not os.path.exists(comp_dir):
|
||||
print("Creating Zsh completion directory.")
|
||||
os.makedirs(comp_dir, exist_ok=True)
|
||||
for point in self.entry_points:
|
||||
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "zsh_source"
|
||||
with open(comp_dir + "_" + point, "w") as file:
|
||||
|
|
50
tests/test_completion.py
Normal file
50
tests/test_completion.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from bdfr.completion import Completion
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="Completions are not currently supported on Windows.")
|
||||
def test_cli_completion_all(tmp_path: Path):
|
||||
with patch("appdirs.user_data_dir", return_value=str(tmp_path)):
|
||||
Completion("all").install()
|
||||
assert Path.exists(Path(str(tmp_path) + "/bash-completion/completions/bdfr")) == 1
|
||||
assert Path.exists(Path(str(tmp_path) + "/fish/vendor_completions.d/bdfr.fish")) == 1
|
||||
assert Path.exists(Path(str(tmp_path) + "/zsh/site-functions/_bdfr")) == 1
|
||||
Completion("all").uninstall()
|
||||
assert Path.exists(Path(str(tmp_path) + "/bash-completion/completions/bdfr")) == 0
|
||||
assert Path.exists(Path(str(tmp_path) + "/fish/vendor_completions.d/bdfr.fish")) == 0
|
||||
assert Path.exists(Path(str(tmp_path) + "/zsh/site-functions/_bdfr")) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="Completions are not currently supported on Windows.")
|
||||
def test_cli_completion_bash(tmp_path: Path):
|
||||
with patch("appdirs.user_data_dir", return_value=str(tmp_path)):
|
||||
Completion("bash").install()
|
||||
assert Path.exists(Path(str(tmp_path) + "/bash-completion/completions/bdfr")) == 1
|
||||
Completion("bash").uninstall()
|
||||
assert Path.exists(Path(str(tmp_path) + "/bash-completion/completions/bdfr")) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="Completions are not currently supported on Windows.")
|
||||
def test_cli_completion_fish(tmp_path: Path):
|
||||
with patch("appdirs.user_data_dir", return_value=str(tmp_path)):
|
||||
Completion("fish").install()
|
||||
assert Path.exists(Path(str(tmp_path) + "/fish/vendor_completions.d/bdfr.fish")) == 1
|
||||
Completion("fish").uninstall()
|
||||
assert Path.exists(Path(str(tmp_path) + "/fish/vendor_completions.d/bdfr.fish")) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="Completions are not currently supported on Windows.")
|
||||
def test_cli_completion_zsh(tmp_path: Path):
|
||||
with patch("appdirs.user_data_dir", return_value=str(tmp_path)):
|
||||
Completion("zsh").install()
|
||||
assert Path.exists(Path(str(tmp_path) + "/zsh/site-functions/_bdfr")) == 1
|
||||
Completion("zsh").uninstall()
|
||||
assert Path.exists(Path(str(tmp_path) + "/zsh/site-functions/_bdfr")) == 0
|
Loading…
Reference in a new issue