shell: Extend dummy backend to record for testing
It is useful to run tests which generate shell output and check that it is correct. Update the existing 'dummy' backend to support this. It works by retaining the output in a small buffer so that it can be read and checked by the test. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
95b8355dd4
commit
2a8463e2b8
2 changed files with 44 additions and 0 deletions
|
@ -1,4 +1,6 @@
|
|||
/*
|
||||
* Shell backend used for testing
|
||||
*
|
||||
* Copyright (c) 2018 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
|
@ -17,6 +19,12 @@ extern const struct shell_transport_api shell_dummy_transport_api;
|
|||
|
||||
struct shell_dummy {
|
||||
bool initialized;
|
||||
|
||||
/** current number of bytes in buffer (0 if no output) */
|
||||
size_t len;
|
||||
|
||||
/** output buffer to collect shell output */
|
||||
char buf[100];
|
||||
};
|
||||
|
||||
#define SHELL_DUMMY_DEFINE(_name) \
|
||||
|
@ -37,6 +45,18 @@ struct shell_dummy {
|
|||
*/
|
||||
const struct shell *shell_backend_dummy_get_ptr(void);
|
||||
|
||||
/**
|
||||
* @brief Returns the buffered output in the shell and resets the pointer
|
||||
*
|
||||
* The returned data is always followed by a nul character at position *sizep
|
||||
*
|
||||
* @param shell Shell pointer
|
||||
* @param sizep Returns size of data in shell buffer
|
||||
* @returns pointer to buffer containing shell output
|
||||
*/
|
||||
const char *shell_backend_dummy_get_output(const struct shell *shell,
|
||||
size_t *sizep);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
/*
|
||||
* Shell backend used for testing
|
||||
*
|
||||
* Copyright (c) 2018 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
|
@ -55,13 +57,22 @@ static int write(const struct shell_transport *transport,
|
|||
const void *data, size_t length, size_t *cnt)
|
||||
{
|
||||
struct shell_dummy *sh_dummy = (struct shell_dummy *)transport->ctx;
|
||||
size_t store_cnt;
|
||||
|
||||
if (!sh_dummy->initialized) {
|
||||
*cnt = 0;
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
store_cnt = length;
|
||||
if (sh_dummy->len + store_cnt >= sizeof(sh_dummy->buf)) {
|
||||
store_cnt = sizeof(sh_dummy->buf) - sh_dummy->len - 1;
|
||||
}
|
||||
memcpy(sh_dummy->buf + sh_dummy->len, data, store_cnt);
|
||||
sh_dummy->len += store_cnt;
|
||||
|
||||
*cnt = length;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -99,3 +110,16 @@ const struct shell *shell_backend_dummy_get_ptr(void)
|
|||
{
|
||||
return &shell_dummy;
|
||||
}
|
||||
|
||||
const char *shell_backend_dummy_get_output(const struct shell *shell,
|
||||
size_t *sizep)
|
||||
{
|
||||
struct shell_dummy *sh_dummy;
|
||||
|
||||
sh_dummy = (struct shell_dummy *)shell->iface->ctx;
|
||||
sh_dummy->buf[sh_dummy->len] = '\0';
|
||||
*sizep = sh_dummy->len;
|
||||
sh_dummy->len = 0;
|
||||
|
||||
return sh_dummy->buf;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue