From 5d3a539eda7edf77c6339b1ee664f674c3884920 Mon Sep 17 00:00:00 2001 From: OMEGARAZER <869111+OMEGARAZER@users.noreply.github.com> Date: Mon, 19 Dec 2022 17:54:34 -0500 Subject: [PATCH] Fix install of completion if dirs missing Fixes situations if completion directories are missing and adds tests for installer. --- bdfr/completion.py | 9 ++++++++ tests/test_completion.py | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/test_completion.py diff --git a/bdfr/completion.py b/bdfr/completion.py index fac944f..43a9743 100644 --- a/bdfr/completion.py +++ b/bdfr/completion.py @@ -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: diff --git a/tests/test_completion.py b/tests/test_completion.py new file mode 100644 index 0000000..91f9fd2 --- /dev/null +++ b/tests/test_completion.py @@ -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