| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/_cli/test_autocomplete_install.py |
| 4 |
# |
| 5 |
# Copyright 2023 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
import pathlib |
| 11 |
import shutil |
| 12 |
from test.helpers import skip_on_windows |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from b2._internal._cli.autocomplete_install import ( |
| 17 |
SHELL_REGISTRY, |
| 18 |
add_or_update_shell_section, |
| 19 |
) |
| 20 |
|
| 21 |
section = "test_section" |
| 22 |
managed_by = "pytest" |
| 23 |
content = "test content" |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture |
| 27 |
def test_file(tmp_path): |
| 28 |
yield tmp_path / "test_file.sh" |
| 29 |
|
| 30 |
|
| 31 |
def test_add_or_update_shell_section_new_section(test_file): |
| 32 |
test_file.write_text("# preexisting content\n\n") |
| 33 |
|
| 34 |
add_or_update_shell_section(test_file, section, managed_by, content) |
| 35 |
|
| 36 |
assert test_file.read_text() == f"""# preexisting content |
| 37 |
|
| 38 |
|
| 39 |
# >>> {section} >>> |
| 40 |
# This section is managed by {managed_by} . Manual edit may break automated updates. |
| 41 |
{content} |
| 42 |
# <<< {section} <<< |
| 43 |
""" |
| 44 |
|
| 45 |
|
| 46 |
def test_add_or_update_shell_section_existing_section(test_file): |
| 47 |
old_content = "old content" |
| 48 |
new_content = "new content" |
| 49 |
|
| 50 |
# Write the initial file with an existing section |
| 51 |
test_file.write_text( |
| 52 |
f"""# preexisting content |
| 53 |
|
| 54 |
# >>> {section} >>> |
| 55 |
# This section is managed by {managed_by} . Manual edit may break automated updates. |
| 56 |
{old_content} |
| 57 |
# <<< {section} <<< |
| 58 |
""" |
| 59 |
) |
| 60 |
|
| 61 |
# Add the new content to the section |
| 62 |
add_or_update_shell_section(test_file, section, managed_by, new_content) |
| 63 |
|
| 64 |
assert test_file.read_text() == f"""# preexisting content |
| 65 |
|
| 66 |
# >>> {section} >>> |
| 67 |
# This section is managed by {managed_by} . Manual edit may break automated updates. |
| 68 |
{new_content} |
| 69 |
# <<< {section} <<< |
| 70 |
""" |
| 71 |
|
| 72 |
|
| 73 |
def test_add_or_update_shell_section_no_file(test_file): |
| 74 |
# Add the new content to the section, which should create the file |
| 75 |
add_or_update_shell_section(test_file, section, managed_by, content) |
| 76 |
|
| 77 |
assert test_file.read_text() == f""" |
| 78 |
# >>> {section} >>> |
| 79 |
# This section is managed by {managed_by} . Manual edit may break automated updates. |
| 80 |
{content} |
| 81 |
# <<< {section} <<< |
| 82 |
""" |
| 83 |
|
| 84 |
|
| 85 |
@pytest.fixture |
| 86 |
def dummy_command(homedir, monkeypatch, env): |
| 87 |
name = "dummy_command" |
| 88 |
bin_path = homedir / "bin" / name |
| 89 |
bin_path.parent.mkdir(parents=True, exist_ok=True) |
| 90 |
bin_path.symlink_to(pathlib.Path(__file__).parent / "fixtures" / f"{name}.py") |
| 91 |
monkeypatch.setenv("PATH", f"{homedir}/bin:{env['PATH']}") |
| 92 |
yield name |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.parametrize("shell", ["bash", "zsh", "fish"]) |
| 96 |
@skip_on_windows |
| 97 |
def test_autocomplete_installer(homedir, env, shell, caplog, dummy_command): |
| 98 |
caplog.set_level(10) |
| 99 |
shell_installer = SHELL_REGISTRY.get(shell, prog=dummy_command) |
| 100 |
|
| 101 |
shell_bin = shutil.which(shell) |
| 102 |
if shell_bin is None: |
| 103 |
pytest.skip(f"{shell} is not installed") |
| 104 |
|
| 105 |
assert shell_installer.is_enabled() is False |
| 106 |
shell_installer.install() |
| 107 |
assert shell_installer.is_enabled() is True |