Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions tests/test_cli.py
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
Comment on lines +42 to +47
Copy link
Contributor

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.

Copy link
Contributor Author

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.py module. The logic underneath would be tested in test_storage.py and presumably a test_shell_integrator.py.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that explains it.



@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
)
Loading