-
Notifications
You must be signed in to change notification settings - Fork 14
test: Add basic test for cli module #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Valentin-v-Todorov
merged 2 commits into
TheDevOpsBlueprint:main
from
amcintosh:feature/add-tests-cli-module
Oct 9, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| from unittest.mock import ANY, 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.subprocess") | ||
| @patch("alix.cli.storage") | ||
| 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( | ||
| main, | ||
| [ | ||
| "add", | ||
| "-n", | ||
| "alix-test-echo", | ||
| "-c", | ||
| "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" | ||
| 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_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( | ||
| 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.storage") | ||
| 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( | ||
| main, | ||
| [ | ||
| "add", | ||
| "-n", | ||
| "alix-test-echo", | ||
| "-c", | ||
| "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 ( | ||
| "⚠ 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, 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( | ||
| main, | ||
| [ | ||
| "add", | ||
| "--no-apply", | ||
| "-n", | ||
| "alix-test-echo", | ||
| "-c", | ||
| "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 | ||
| 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 | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that you are still asserting the output messages only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertions are on lines 38-41. These are unit tests so I'm confining the scope of the test to the code in the
cli.pymodule. The logic underneath would be tested intest_storage.pyand presumably atest_shell_integrator.py.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that explains it.