2020-11-02 14:11:14 +01:00
|
|
|
#!/usr/bin/env python
|
2024-12-06 13:33:07 +01:00
|
|
|
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
2022-06-15 16:46:55 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2023-11-06 13:22:35 +01:00
|
|
|
import collections
|
2020-11-02 14:11:14 +01:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
|
2021-01-26 10:49:01 +08:00
|
|
|
import pexpect
|
|
|
|
|
2024-12-06 13:33:07 +01:00
|
|
|
Test = collections.namedtuple('Test', ['name', 'shell', 'term', 'prefix', 'pattern', 'ext'])
|
2020-11-02 14:11:14 +01:00
|
|
|
|
2024-12-06 13:33:07 +01:00
|
|
|
IDF_PATH = os.environ['IDF_PATH']
|
2023-11-06 13:22:35 +01:00
|
|
|
|
2024-12-06 13:33:07 +01:00
|
|
|
TESTS = (
|
|
|
|
Test('base',
|
|
|
|
'fish',
|
|
|
|
'vt100',
|
|
|
|
'',
|
|
|
|
'all.*app.*app-flash.*bootloader.*',
|
|
|
|
'fish'),
|
|
|
|
Test('base',
|
|
|
|
'bash',
|
|
|
|
'xterm-256color',
|
|
|
|
'',
|
|
|
|
'all.*app.*app-flash.*bootloader.*bootloader-flash.*build-system-targets.*clean.*',
|
|
|
|
'sh'),
|
|
|
|
Test('base',
|
|
|
|
'zsh',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
'all.*app.*app-flash.*bootloader.*bootloader-flash.*build-system-targets.*clean.*',
|
|
|
|
'sh'),
|
|
|
|
# Tests for file autocompletion
|
|
|
|
Test('file',
|
|
|
|
'fish',
|
|
|
|
'vt100',
|
|
|
|
f'@{IDF_PATH}/README',
|
|
|
|
'.*README.*md.*',
|
|
|
|
'fish'),
|
|
|
|
Test('file',
|
|
|
|
'bash',
|
|
|
|
'xterm-256color',
|
|
|
|
f'@{IDF_PATH}/README',
|
|
|
|
'.*README.*md.*',
|
|
|
|
'sh'),
|
|
|
|
Test('file',
|
|
|
|
'zsh',
|
|
|
|
'',
|
|
|
|
f'@{IDF_PATH}/README',
|
|
|
|
'.*README.*md.*',
|
|
|
|
'sh')
|
|
|
|
)
|
2023-11-06 13:22:35 +01:00
|
|
|
|
|
|
|
# Additional positional arguments for all child.expect() calls are constant so we can rely on the order and print message
|
|
|
|
# about which pattern was matched
|
|
|
|
pargs = (pexpect.EOF, pexpect.TIMEOUT)
|
|
|
|
|
|
|
|
|
2023-11-29 10:43:19 +01:00
|
|
|
def get_fail_msg(pproc: pexpect.spawn, msg: str, index: int) -> str:
|
2023-11-06 13:22:35 +01:00
|
|
|
try:
|
|
|
|
buf = pproc._buffer.getvalue()
|
|
|
|
except AttributeError:
|
|
|
|
# _buffer is an internal of pexpect.spawn and is not part of the API.
|
|
|
|
# Either there is no _buffer or it is not io.BytesIO() anymore.
|
|
|
|
buf = '<UNKNOWN>'
|
|
|
|
|
|
|
|
return '{} ({}) buffer: "{}"'.format(msg, 'EOF - child has exited' if index == 1 else 'TIMEOUT', buf)
|
|
|
|
|
|
|
|
|
|
|
|
class UTTest(unittest.TestCase):
|
|
|
|
|
2023-11-29 10:43:19 +01:00
|
|
|
def test_shell(self) -> None:
|
2023-11-06 13:22:35 +01:00
|
|
|
env = os.environ.copy()
|
|
|
|
for test in TESTS:
|
|
|
|
with self.subTest():
|
2024-12-06 13:33:07 +01:00
|
|
|
with open(os.path.join(IDF_PATH, f'{test.shell}.{test.name}.out'), 'wb') as o:
|
2023-11-06 13:22:35 +01:00
|
|
|
env['TERM'] = test.term
|
2024-12-06 13:33:07 +01:00
|
|
|
with pexpect.spawn(f'{test.shell} -i', env=env, logfile=o, timeout=200) as pproc:
|
|
|
|
if test.shell == 'bash':
|
|
|
|
# The @-argument autocomplete works reliably in bash only
|
|
|
|
# if @ is not included in COMP_WORDBREAKS.
|
|
|
|
pproc.sendline('COMP_WORDBREAKS=${COMP_WORDBREAKS//@/}')
|
|
|
|
pproc.sendline(f'. {IDF_PATH}/export.{test.ext}')
|
2023-11-06 13:22:35 +01:00
|
|
|
i = pproc.expect(['Go to the project directory and run.*idf\\.py build', *pargs])
|
|
|
|
self.assertEqual(i, 0, get_fail_msg(pproc, 'Export was not successful!', i))
|
2024-12-06 13:33:07 +01:00
|
|
|
pproc.send(f'idf.py {test.prefix}\t\t')
|
2023-11-06 13:22:35 +01:00
|
|
|
i = pproc.expect([test.pattern, *pargs], timeout=100)
|
2024-12-06 13:33:07 +01:00
|
|
|
self.assertEqual(i, 0, get_fail_msg(pproc, f'Autocompletion for idf.py failed in {test.shell}!', i))
|
2020-11-02 14:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|