test_autocomplete.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. # SPDX-License-Identifier: Apache-2.0
  4. import collections
  5. import os
  6. import unittest
  7. import pexpect
  8. Test = collections.namedtuple('Test', ['name', 'term', 'pattern', 'ext'])
  9. TESTS = (Test('fish', 'vt100', 'all.*app.*app-flash.*bootloader.*', 'fish'),
  10. Test('bash', 'xterm-256color', 'all.*app.*app-flash.*bootloader.*bootloader-flash.*build-system-targets.*clean.*', 'sh'),
  11. Test('zsh', '', 'all.*app.*app-flash.*bootloader.*bootloader-flash.*build-system-targets.*clean.*', 'sh'))
  12. # Additional positional arguments for all child.expect() calls are constant so we can rely on the order and print message
  13. # about which pattern was matched
  14. pargs = (pexpect.EOF, pexpect.TIMEOUT)
  15. def get_fail_msg(pproc, msg, index):
  16. try:
  17. buf = pproc._buffer.getvalue()
  18. except AttributeError:
  19. # _buffer is an internal of pexpect.spawn and is not part of the API.
  20. # Either there is no _buffer or it is not io.BytesIO() anymore.
  21. buf = '<UNKNOWN>'
  22. return '{} ({}) buffer: "{}"'.format(msg, 'EOF - child has exited' if index == 1 else 'TIMEOUT', buf)
  23. class UTTest(unittest.TestCase):
  24. def test_shell(self):
  25. idf_path = os.environ['IDF_PATH']
  26. env = os.environ.copy()
  27. for test in TESTS:
  28. with self.subTest():
  29. with open(os.path.join(idf_path, f'{test.name}.out'), 'wb') as o:
  30. env['TERM'] = test.term
  31. with pexpect.spawn(f'{test.name} -i', env=env, logfile=o, timeout=200) as pproc:
  32. pproc.sendline(f'. {idf_path}/export.{test.ext}')
  33. i = pproc.expect(['Go to the project directory and run.*idf\\.py build', *pargs])
  34. self.assertEqual(i, 0, get_fail_msg(pproc, 'Export was not successful!', i))
  35. pproc.send('idf.py \t\t')
  36. i = pproc.expect([test.pattern, *pargs], timeout=100)
  37. self.assertEqual(i, 0, get_fail_msg(pproc, f'Autocompletion for idf.py failed in {test.name}!', i))
  38. if __name__ == '__main__':
  39. unittest.main()