|
| 1 | +from invoke import run, Responder, exceptions |
| 2 | +import os |
| 3 | +import json |
| 4 | +import pytest |
| 5 | +import semver |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +this_test_path = os.path.dirname(os.path.realpath(__file__)) |
| 9 | +# Calculate absolute path of the CLI |
| 10 | +cli_path = os.path.join(this_test_path, '..', 'arduino-cli') |
| 11 | + |
| 12 | +# Useful reference: |
| 13 | +# http://docs.pyinvoke.org/en/1.2/api/runners.html#invoke.runners.Result |
| 14 | + |
| 15 | + |
| 16 | +def cli_line(*args): |
| 17 | + # Accept a list of arguments cli_line('lib list --format json') |
| 18 | + # Return a full command line string e.g. 'arduino-cli help --format json' |
| 19 | + cli_full_line = ' '.join([cli_path, ' '.join(str(arg) for arg in args)]) |
| 20 | + return cli_full_line |
| 21 | + |
| 22 | + |
| 23 | +def run_command(*args): |
| 24 | + result = run(cli_line(*args), echo=False, hide=True, warn=True) |
| 25 | + return result |
| 26 | + |
| 27 | + |
| 28 | +def test_command_help(): |
| 29 | + result = run_command('help') |
| 30 | + assert result.ok |
| 31 | + assert result.stderr == '' |
| 32 | + assert 'Usage' in result.stdout |
| 33 | + |
| 34 | + |
| 35 | +def test_command_lib_list(): |
| 36 | + result = run_command('lib list') |
| 37 | + assert result.ok |
| 38 | + assert result.stderr == '' |
| 39 | + result = run_command('lib list', '--format json') |
| 40 | + assert '{}' == result.stdout |
| 41 | + |
| 42 | + |
| 43 | +def test_command_lib_install(): |
| 44 | + libs = ['\"AzureIoTProtocol_MQTT\"', '\"CMMC MQTT Connector\"', '\"WiFiNINA\"'] |
| 45 | + # Should be safe to run install multiple times |
| 46 | + result_1 = run_command('lib install {}'.format(' '.join(libs))) |
| 47 | + assert result_1.ok |
| 48 | + result_2 = run_command('lib install {}'.format(' '.join(libs))) |
| 49 | + assert result_2.ok |
| 50 | + |
| 51 | +def test_command_lib_update_index(): |
| 52 | + result = run_command('lib update-index') |
| 53 | + assert result.ok |
| 54 | + assert 'Updating index: library_index.json downloaded' == result.stdout.splitlines()[-1].strip() |
| 55 | + |
| 56 | +def test_command_lib_remove(): |
| 57 | + libs = ['\"AzureIoTProtocol_MQTT\"', '\"CMMC MQTT Connector\"', '\"WiFiNINA\"'] |
| 58 | + result = run_command('lib uninstall {}'.format(' '.join(libs))) |
| 59 | + assert result.ok |
| 60 | + |
| 61 | +@pytest.mark.slow |
| 62 | +def test_command_lib_search(): |
| 63 | + result = run_command('lib search') |
| 64 | + assert result.ok |
| 65 | + out_lines = result.stdout.splitlines() |
| 66 | + libs = [] |
| 67 | + # Create an array with just the name of the vars |
| 68 | + for line in out_lines: |
| 69 | + if 'Name: ' in line: |
| 70 | + libs.append(line.split()[1].strip('\"')) |
| 71 | + number_of_libs = len(libs) |
| 72 | + assert sorted(libs) == libs |
| 73 | + assert ['WiFi101', 'WiFi101OTA'] == [lib for lib in libs if 'WiFi101' in lib] |
| 74 | + result = run_command('lib search --format json') |
| 75 | + assert result.ok |
| 76 | + libs_found_from_json = json.loads(result.stdout) |
| 77 | + number_of_libs_from_json = len(libs_found_from_json.get('libraries')) |
| 78 | + assert number_of_libs == number_of_libs_from_json |
| 79 | + |
| 80 | + |
| 81 | +def test_command_board_list(): |
| 82 | + result = run_command('board list --format json') |
| 83 | + assert result.ok |
| 84 | + # check is a valid json and contains a list of ports |
| 85 | + ports = json.loads(result.stdout).get('ports') |
| 86 | + assert isinstance(ports, list) |
| 87 | + for port in ports: |
| 88 | + assert 'protocol' in port |
| 89 | + assert 'protocol_label' in port |
| 90 | + |
| 91 | + |
| 92 | +def test_command_board_listall(): |
| 93 | + result = run_command('board listall') |
| 94 | + assert result.ok |
| 95 | + assert ['Board', 'Name', 'FQBN'] == result.stdout.splitlines()[0].strip().split() |
| 96 | + |
| 97 | + |
| 98 | +def test_command_version(): |
| 99 | + result = run_command('version --format json') |
| 100 | + assert result.ok |
| 101 | + parsed_out = json.loads(result.stdout) |
| 102 | + |
| 103 | + assert parsed_out.get('Application', False) == 'arduino-cli' |
| 104 | + assert isinstance(semver.parse(parsed_out.get('VersionString', False)), dict) |
| 105 | + assert isinstance(parsed_out.get('Commit', False), str) |
| 106 | + assert datetime.strptime(parsed_out.get('BuildDate')[:-2], '%Y-%m-%dT%H:%M:%S.%f') |
0 commit comments