From 629d022fb4fe621025b3cdc6b17fc5c4ae460ac7 Mon Sep 17 00:00:00 2001 From: Yurii Kasper Date: Wed, 13 Aug 2025 11:13:07 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=85=20test:=20Implement=20tests=20for?= =?UTF-8?q?=20shell=20completions,=20test=20suggestions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_shell_completion.py | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/test_shell_completion.py diff --git a/tests/test_shell_completion.py b/tests/test_shell_completion.py new file mode 100644 index 0000000..b22e149 --- /dev/null +++ b/tests/test_shell_completion.py @@ -0,0 +1,42 @@ +from click.testing import CliRunner +from oks_cli.main import cli +from pathlib import Path + + +def test_install_completion_zsh(): + runner = CliRunner() + + result = runner.invoke(cli, ["install-completion", "--type", "zsh"]) + assert result.exit_code == 0 + assert "Autocompletion installed for zsh" in result.output + + completion_file = Path("~/.oks_cli/completions/oks-cli.sh").expanduser() + assert completion_file.exists() + + script_content = completion_file.read_text() + assert "compdef _oks_cli_completion oks-cli" in script_content + +def test_install_completion_bash(): + runner = CliRunner() + + result = runner.invoke(cli, ["install-completion", "--type", "bash"]) + assert result.exit_code == 0 + assert "Autocompletion installed for bash" in result.output + + completion_file = Path("~/.oks_cli/completions/oks-cli.sh").expanduser() + assert completion_file.exists() + + script_content = completion_file.read_text() + assert "_oks_cli_completion()" in script_content + +def test_bash_completion_suggestions(): + runner = CliRunner() + env = { + "_CLI_COMPLETE": "bash_complete", + "COMP_WORDS": "cli clu", + "COMP_CWORD": "1", + "COMP_LINE": "cli clu", + "COMP_POINT": str(len("cli clu")), + } + result = runner.invoke(cli, [], env=env) + assert "plain,cluster" in result.output From ab8bf5fdcd15deae9de1d38148e018d1908b39c5 Mon Sep 17 00:00:00 2001 From: Yurii Kasper Date: Thu, 14 Aug 2025 11:23:54 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=85=20test:=20Test=20dynamic=20shell?= =?UTF-8?q?=20completions=20for=20cluster=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_shell_completion.py | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/test_shell_completion.py b/tests/test_shell_completion.py index b22e149..ac16102 100644 --- a/tests/test_shell_completion.py +++ b/tests/test_shell_completion.py @@ -1,6 +1,7 @@ from click.testing import CliRunner from oks_cli.main import cli from pathlib import Path +from unittest.mock import patch, MagicMock def test_install_completion_zsh(): @@ -40,3 +41,51 @@ def test_bash_completion_suggestions(): } result = runner.invoke(cli, [], env=env) assert "plain,cluster" in result.output + +@patch("oks_cli.utils.requests.request") +def test_cluster_dynamic_shell_completion_suggestions(mock_request, add_default_profile): + def get_env(action: str, flag: str): + return { + "_CLI_COMPLETE": "bash_complete", + "COMP_WORDS": f"cli cluster {action} -p test -c test {flag}", + "COMP_CWORD": "8", + "COMP_LINE": f"cli cluster {action} -p test -c test {flag}", + "COMP_POINT": str(len(f"cli cluster {action} -p test -c test {flag}")), + } + + mock_request.side_effect = [ + MagicMock(status_code=200, headers = {}, json=lambda: {"ResponseContext": {}, "Versions": ["1.33"]}) + ] + runner = CliRunner() + + result = runner.invoke(cli, [], env=get_env("create", "--version")) + assert "plain,1.33" in result.output + + mock_request.side_effect = [ + MagicMock(status_code=200, headers = {}, json=lambda: {"ResponseContext": {}, "ControlPlanes": ["cp.3.masters.small", "cp.3.masters.medium"]}) + ] + + result = runner.invoke(cli, [], env=get_env("create", "--control-plane")) + assert "plain,cp.3.masters.small" in result.output + assert "plain,cp.3.masters.medium" in result.output + + mock_request.side_effect = [ + MagicMock(status_code=200, headers = {}, json=lambda: {"ResponseContext": {}, "CPSubregions": ["2a", "2b", "2c"]}) + ] + + result = runner.invoke(cli, [], env=get_env("create", '--zone')) + assert "plain,2a" in result.output + assert "plain,2b" in result.output + assert "plain,2c" in result.output + + + result = runner.invoke(cli, [], env=get_env("update", "--version")) + assert "plain,1.33" in result.output + + mock_request.side_effect = [ + MagicMock(status_code=200, headers = {}, json=lambda: {"ResponseContext": {}, "ControlPlanes": ["cp.3.masters.small", "cp.3.masters.medium"]}) + ] + + result = runner.invoke(cli, [], env=get_env("update", "--control-plane")) + assert "plain,cp.3.masters.small" in result.output + assert "plain,cp.3.masters.medium" in result.output From b687a293cc350b8a6458619f2822d3413d5ee4b0 Mon Sep 17 00:00:00 2001 From: Yurii Kasper Date: Thu, 14 Aug 2025 11:42:48 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=85=20test:=20Shell=20completion=20fo?= =?UTF-8?q?r=20profile=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_shell_completion.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_shell_completion.py b/tests/test_shell_completion.py index ac16102..3a34cbc 100644 --- a/tests/test_shell_completion.py +++ b/tests/test_shell_completion.py @@ -89,3 +89,16 @@ def get_env(action: str, flag: str): result = runner.invoke(cli, [], env=get_env("update", "--control-plane")) assert "plain,cp.3.masters.small" in result.output assert "plain,cp.3.masters.medium" in result.output + +def test_profile_dynamic_shell_completion_suggestions(add_default_profile): + env = { + "_CLI_COMPLETE": "bash_complete", + "COMP_WORDS": "cli --profile", + "COMP_CWORD": "3", + "COMP_LINE": "cli --profile", + "COMP_POINT": str(len("cli --profile")), + } + runner = CliRunner() + + result = runner.invoke(cli, [], env=env) + assert "plain,default" in result.output