| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/integration/test_autocomplete.py |
| 4 |
# |
| 5 |
# Copyright 2023 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
|
| 11 |
import sys |
| 12 |
from test.helpers import skip_on_windows |
| 13 |
|
| 14 |
import pexpect |
| 15 |
import pytest |
| 16 |
|
| 17 |
TIMEOUT = 120 # CI can be slow at times when parallelization is extreme |
| 18 |
|
| 19 |
BASHRC_CONTENT = """\ |
| 20 |
# ~/.bashrc dummy file |
| 21 |
|
| 22 |
echo "Just testing if we don't replace existing script" > /dev/null |
| 23 |
# >>> just a test section >>> |
| 24 |
# regardless what is in there already |
| 25 |
# <<< just a test section <<< |
| 26 |
""" |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture(scope="session") |
| 30 |
def bashrc(homedir): |
| 31 |
bashrc_path = (homedir / '.bashrc') |
| 32 |
bashrc_path.write_text(BASHRC_CONTENT) |
| 33 |
yield bashrc_path |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture(scope="module") |
| 37 |
def cli_command(request) -> str: |
| 38 |
return request.config.getoption('--sut') |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture(scope="module") |
| 42 |
def autocomplete_installed(env, homedir, bashrc, cli_version, cli_command, is_running_on_docker): |
| 43 |
if is_running_on_docker: |
| 44 |
pytest.skip('Not supported on Docker') |
| 45 |
|
| 46 |
shell = pexpect.spawn( |
| 47 |
f'bash -i -c "{cli_command} install-autocomplete"', env=env, logfile=sys.stderr.buffer |
| 48 |
) |
| 49 |
try: |
| 50 |
shell.expect_exact('Autocomplete successfully installed for bash', timeout=TIMEOUT) |
| 51 |
finally: |
| 52 |
shell.close() |
| 53 |
shell.wait() |
| 54 |
assert (homedir / '.bash_completion.d' / cli_version).is_file() |
| 55 |
assert bashrc.read_text().startswith(BASHRC_CONTENT) |
| 56 |
|
| 57 |
|
| 58 |
@pytest.fixture |
| 59 |
def shell(env): |
| 60 |
shell = pexpect.spawn('bash -i', env=env, maxread=1000) |
| 61 |
shell.setwinsize(100, 100) # required to see all suggestions in tests |
| 62 |
yield shell |
| 63 |
shell.close() |
| 64 |
|
| 65 |
|
| 66 |
@skip_on_windows |
| 67 |
def test_autocomplete_b2_commands(autocomplete_installed, is_running_on_docker, shell, cli_version): |
| 68 |
if is_running_on_docker: |
| 69 |
pytest.skip('Not supported on Docker') |
| 70 |
shell.send(f'{cli_version} \t\t') |
| 71 |
shell.expect_exact(["authorize-account", "download-file", "get-bucket"], timeout=TIMEOUT) |
| 72 |
|
| 73 |
|
| 74 |
@skip_on_windows |
| 75 |
def test_autocomplete_b2_only_matching_commands( |
| 76 |
autocomplete_installed, is_running_on_docker, shell, cli_version |
| 77 |
): |
| 78 |
if is_running_on_docker: |
| 79 |
pytest.skip('Not supported on Docker') |
| 80 |
shell.send(f'{cli_version} delete-\t\t') |
| 81 |
|
| 82 |
shell.expect_exact("file", timeout=TIMEOUT) # common part of remaining cmds is autocompleted |
| 83 |
with pytest.raises(pexpect.exceptions.TIMEOUT): # no other commands are suggested |
| 84 |
shell.expect_exact("get-bucket", timeout=0.5) |
| 85 |
|
| 86 |
|
| 87 |
@skip_on_windows |
| 88 |
def test_autocomplete_b2__download_file__b2uri( |
| 89 |
autocomplete_installed, |
| 90 |
shell, |
| 91 |
b2_tool, |
| 92 |
bucket_name, |
| 93 |
file_name, |
| 94 |
is_running_on_docker, |
| 95 |
cli_version, |
| 96 |
): |
| 97 |
"""Test that autocomplete suggests bucket names and file names.""" |
| 98 |
if is_running_on_docker: |
| 99 |
pytest.skip('Not supported on Docker') |
| 100 |
shell.send(f'{cli_version} download_file \t\t') |
| 101 |
shell.expect_exact("b2://", timeout=TIMEOUT) |
| 102 |
shell.send('b2://\t\t') |
| 103 |
shell.expect_exact(bucket_name, timeout=TIMEOUT) |
| 104 |
shell.send(f'{bucket_name}/\t\t') |
| 105 |
shell.expect_exact(file_name, timeout=TIMEOUT) |