Merge pull request #727 from OMEGARAZER/development
This commit is contained in:
@@ -17,6 +17,9 @@ class Completion:
|
|||||||
def install(self):
|
def install(self):
|
||||||
if self.shell in ("all", "bash"):
|
if self.shell in ("all", "bash"):
|
||||||
comp_dir = self.share_dir + "/bash-completion/completions/"
|
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:
|
for point in self.entry_points:
|
||||||
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "bash_source"
|
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "bash_source"
|
||||||
with open(comp_dir + point, "w") as file:
|
with open(comp_dir + point, "w") as file:
|
||||||
@@ -24,18 +27,24 @@ class Completion:
|
|||||||
print(f"Bash completion for {point} written to {comp_dir}{point}")
|
print(f"Bash completion for {point} written to {comp_dir}{point}")
|
||||||
if self.shell in ("all", "fish"):
|
if self.shell in ("all", "fish"):
|
||||||
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
|
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:
|
for point in self.entry_points:
|
||||||
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "fish_source"
|
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "fish_source"
|
||||||
with open(comp_dir + point, "w") as file:
|
with open(comp_dir + point + ".fish", "w") as file:
|
||||||
file.write(subprocess.run([point], env=self.env, capture_output=True, text=True).stdout)
|
file.write(subprocess.run([point], env=self.env, capture_output=True, text=True).stdout)
|
||||||
print(f"Fish completion for {point} written to {comp_dir}{point}")
|
print(f"Fish completion for {point} written to {comp_dir}{point}.fish")
|
||||||
if self.shell in ("all", "zsh"):
|
if self.shell in ("all", "zsh"):
|
||||||
comp_dir = self.share_dir + "/zsh/site-functions/"
|
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:
|
for point in self.entry_points:
|
||||||
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "zsh_source"
|
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "zsh_source"
|
||||||
with open(comp_dir + point, "w") as file:
|
with open(comp_dir + "_" + point, "w") as file:
|
||||||
file.write(subprocess.run([point], env=self.env, capture_output=True, text=True).stdout)
|
file.write(subprocess.run([point], env=self.env, capture_output=True, text=True).stdout)
|
||||||
print(f"Zsh completion for {point} written to {comp_dir}{point}")
|
print(f"Zsh completion for {point} written to {comp_dir}_{point}")
|
||||||
|
|
||||||
def uninstall(self):
|
def uninstall(self):
|
||||||
if self.shell in ("all", "bash"):
|
if self.shell in ("all", "bash"):
|
||||||
@@ -47,12 +56,12 @@ class Completion:
|
|||||||
if self.shell in ("all", "fish"):
|
if self.shell in ("all", "fish"):
|
||||||
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
|
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
|
||||||
for point in self.entry_points:
|
for point in self.entry_points:
|
||||||
if os.path.exists(comp_dir + point):
|
if os.path.exists(comp_dir + point + ".fish"):
|
||||||
os.remove(comp_dir + point)
|
os.remove(comp_dir + point + ".fish")
|
||||||
print(f"Fish completion for {point} removed from {comp_dir}{point}")
|
print(f"Fish completion for {point} removed from {comp_dir}{point}.fish")
|
||||||
if self.shell in ("all", "zsh"):
|
if self.shell in ("all", "zsh"):
|
||||||
comp_dir = self.share_dir + "/zsh/site-functions/"
|
comp_dir = self.share_dir + "/zsh/site-functions/"
|
||||||
for point in self.entry_points:
|
for point in self.entry_points:
|
||||||
if os.path.exists(comp_dir + point):
|
if os.path.exists(comp_dir + "_" + point):
|
||||||
os.remove(comp_dir + point)
|
os.remove(comp_dir + "_" + point)
|
||||||
print(f"Zsh completion for {point} removed from {comp_dir}{point}")
|
print(f"Zsh completion for {point} removed from {comp_dir}_{point}")
|
||||||
|
|||||||
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
|
||||||
Reference in New Issue
Block a user