From 686aa9d6c567bd4252c6b02d98ba981ae78a7766 Mon Sep 17 00:00:00 2001 From: Andrew McIntosh Date: Sat, 4 Oct 2025 11:43:12 -0400 Subject: [PATCH 1/2] test: Add basic test for cli module Add a test for the cli module. This does not provide much cli coverage yet, but given the recent breakage I figured it was worth getting a start of cli tests. --- tests/test_cli.py | 129 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..8692a0d --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,129 @@ +from unittest.mock import patch + +from click.testing import CliRunner + +from alix.cli import main +from alix.shell_integrator import ShellIntegrator + + +@patch.object(ShellIntegrator, "apply_single_alias") +@patch("alix.cli.storage") +def test_cli_add(mock_storage, mock_apply): + mock_storage.add.return_value = True + mock_storage.get.return_value = None + mock_apply.return_value = (True, "✓ Applied alias 'alix-test-echo' to .zshrc") + + runner = CliRunner() + result = runner.invoke( + main, + [ + "add", + "-n", + "alix-test-echo", + "-c", + "alix test working!", + "-d", + "alix test shortcut", + ], + ) + + assert result.exit_code == 0 + + assert "✔ Added alias: alix-test-echo = 'alix test working!'" in result.output + assert ( + "💡 Alias 'alix-test-echo' is now available in new shell sessions" + in result.output + ) + assert "For current session, run: source ~/.zshrc" in result.output + + +@patch.object(ShellIntegrator, "apply_single_alias") +@patch("alix.cli.storage") +def test_cli_add__already_present(mock_storage, mock_apply): + mock_storage.add.return_value = False + + runner = CliRunner() + result = runner.invoke( + main, + [ + "add", + "-n", + "alix-test-echo", + "-c", + "alix test working!", + "-d", + "alix test shortcut", + ], + ) + + assert result.exit_code == 0 + mock_apply.assert_not_called() + + assert "Alias/Command/Function already exists. Add --force flag to override" in result.output + assert "Added alias: alix-test-echo = 'alix test working!'" not in result.output + assert "✓ Applied alias 'alix-test-echo' to .zshrc" not in result.output + + +@patch.object(ShellIntegrator, "apply_single_alias") +@patch("alix.cli.storage") +def test_cli_add__apply_failed(mock_storage, mock_apply): + mock_storage.add.return_value = True + mock_storage.get.return_value = None + mock_apply.return_value = (False, "No shell configuration file found") + + runner = CliRunner() + result = runner.invoke( + main, + [ + "add", + "-n", + "alix-test-echo", + "-c", + "alix test working!", + "-d", + "alix test shortcut", + ], + ) + + assert result.exit_code == 0 + + assert "Added alias: alix-test-echo = 'alix test working!'" in result.output + assert ( + "⚠ Alias saved but not applied: No shell configuration file found" + in result.output + ) + assert " Run 'alix apply' to apply all aliases to shell" in result.output + + assert "✓ Applied alias 'alix-test-echo' to .zshrc" not in result.output + + +@patch.object(ShellIntegrator, "apply_single_alias") +@patch("alix.cli.storage") +def test_cli_add__no_apply(mock_storage, mock_apply): + mock_storage.get.return_value = None + mock_storage.add.return_value = True + + runner = CliRunner() + result = runner.invoke( + main, + [ + "add", + "--no-apply", + "-n", + "alix-test-echo", + "-c", + "alix test working!", + "-d", + "alix test shortcut", + ], + ) + + assert result.exit_code == 0 + mock_apply.assert_not_called() + + assert "Added alias: alix-test-echo = 'alix test working!'" in result.output + assert "✓ Applied alias 'alix-test-echo' to .zshrc" not in result.output + assert ( + "⚠ Alias saved but not applied: alix-test-echo = 'alix test working!'" + not in result.output + ) From 63686a1faf47e2d41c92802f4b1ff0ea64aa3d30 Mon Sep 17 00:00:00 2001 From: Andrew McIntosh Date: Tue, 7 Oct 2025 12:56:04 -0400 Subject: [PATCH 2/2] Add logic checks to cli tests --- tests/test_cli.py | 66 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 8692a0d..e315b25 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,4 +1,4 @@ -from unittest.mock import patch +from unittest.mock import ANY, patch from click.testing import CliRunner @@ -7,11 +7,15 @@ @patch.object(ShellIntegrator, "apply_single_alias") +@patch("alix.cli.subprocess") @patch("alix.cli.storage") -def test_cli_add(mock_storage, mock_apply): +def test_cli_add(mock_storage, mock_subprocess, mock_apply, alias): mock_storage.add.return_value = True mock_storage.get.return_value = None + mock_subprocess.run.return_value.returncode = 1 mock_apply.return_value = (True, "✓ Applied alias 'alix-test-echo' to .zshrc") + alias.created_at = ANY + alias.shell = ANY runner = CliRunner() result = runner.invoke( @@ -24,11 +28,17 @@ def test_cli_add(mock_storage, mock_apply): "alix test working!", "-d", "alix test shortcut", + "--tags", + "a, b" ], ) assert result.exit_code == 0 + mock_storage.get.assert_called_with("alix-test-echo") + mock_subprocess.run.assert_called() + mock_storage.add.assert_called_with(alias) + assert "✔ Added alias: alix-test-echo = 'alix test working!'" in result.output assert ( "💡 Alias 'alix-test-echo' is now available in new shell sessions" @@ -39,8 +49,39 @@ def test_cli_add(mock_storage, mock_apply): @patch.object(ShellIntegrator, "apply_single_alias") @patch("alix.cli.storage") -def test_cli_add__already_present(mock_storage, mock_apply): - mock_storage.add.return_value = False +def test_cli_add__already_in_storage(mock_storage, mock_apply): + mock_storage.get.return_value = "alix-test-echo" + + runner = CliRunner() + result = runner.invoke( + main, + [ + "add", + "-n", + "alix-test-echo", + "-c", + "alix test working!", + "-d", + "alix test shortcut", + ], + ) + + assert result.exit_code == 0 + mock_storage.add.assert_not_called() + mock_apply.assert_not_called() + + assert "Alias/Command/Function already exists. Add --force flag to override" in result.output + assert "Added alias: alix-test-echo = 'alix test working!'" not in result.output + assert "✓ Applied alias 'alix-test-echo' to .zshrc" not in result.output + + +@patch.object(ShellIntegrator, "apply_single_alias") +@patch("alix.cli.subprocess") +@patch("alix.cli.storage") +def test_cli_add__already_an_alias(mock_storage, mock_subprocess, mock_apply): + mock_storage.get.return_value = None + mock_subprocess.run.return_value.returncode = 0 + mock_subprocess.run.return_value.stdout = "Edit the alias to override it" runner = CliRunner() result = runner.invoke( @@ -57,6 +98,7 @@ def test_cli_add__already_present(mock_storage, mock_apply): ) assert result.exit_code == 0 + mock_storage.add.assert_not_called() mock_apply.assert_not_called() assert "Alias/Command/Function already exists. Add --force flag to override" in result.output @@ -66,10 +108,12 @@ def test_cli_add__already_present(mock_storage, mock_apply): @patch.object(ShellIntegrator, "apply_single_alias") @patch("alix.cli.storage") -def test_cli_add__apply_failed(mock_storage, mock_apply): +def test_cli_add__apply_failed(mock_storage, mock_apply, alias): mock_storage.add.return_value = True mock_storage.get.return_value = None mock_apply.return_value = (False, "No shell configuration file found") + alias.created_at = ANY + alias.shell = ANY runner = CliRunner() result = runner.invoke( @@ -82,10 +126,14 @@ def test_cli_add__apply_failed(mock_storage, mock_apply): "alix test working!", "-d", "alix test shortcut", + "--tags", + "a, b" ], ) assert result.exit_code == 0 + mock_storage.add.assert_called_with(alias) + mock_apply.assert_called_with(alias) assert "Added alias: alix-test-echo = 'alix test working!'" in result.output assert ( @@ -99,9 +147,11 @@ def test_cli_add__apply_failed(mock_storage, mock_apply): @patch.object(ShellIntegrator, "apply_single_alias") @patch("alix.cli.storage") -def test_cli_add__no_apply(mock_storage, mock_apply): +def test_cli_add__no_apply(mock_storage, mock_apply, alias): mock_storage.get.return_value = None mock_storage.add.return_value = True + alias.created_at = ANY + alias.shell = ANY runner = CliRunner() result = runner.invoke( @@ -115,10 +165,14 @@ def test_cli_add__no_apply(mock_storage, mock_apply): "alix test working!", "-d", "alix test shortcut", + "--tags", + "a, b" ], ) assert result.exit_code == 0 + mock_storage.get.assert_called_with("alix-test-echo") + mock_storage.add.assert_called_with(alias) mock_apply.assert_not_called() assert "Added alias: alix-test-echo = 'alix test working!'" in result.output