Skip to content

Commit

Permalink
cli: remove formatting hint correclty when it's not the last element.
Browse files Browse the repository at this point in the history
I noticed that the 'lightning-cli summary' output was wrong.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Aug 13, 2019
1 parent 69b02e2 commit 0f5036d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ static enum format delete_format_hint(const char *resp,
format = HUMAN;

/* Don't let hint appear in the output! */
json_tok_remove(toks, result, hint, 1);
json_tok_remove(toks, result, hint-1, 1);
return format;
}

Expand Down
114 changes: 114 additions & 0 deletions cli/test/run-remove-hint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "config.h"
#include <assert.h>
#include <common/amount.h>
#include <common/bigsize.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int test_main(int argc, char *argv[]);
ssize_t test_read(int fd, void *buf, size_t len);
int test_socket(int domain, int type, int protocol);
int test_connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
int test_getpid(void);
int test_printf(const char *format, ...);
int test_fputc(int c, FILE *stream);

#define main test_main
#define read test_read
#define socket test_socket
#define connect test_connect
#define getpid test_getpid
#define printf test_printf
#define fputc test_fputc

#include "../lightning-cli.c"
#undef main

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for version_and_exit */
char *version_and_exit(const void *unused UNNEEDED)
{ fprintf(stderr, "version_and_exit called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

int test_socket(int domain UNUSED, int type UNUSED, int protocol UNUSED)
{
/* We give a real fd, as it writes to it */
return open("/dev/null", O_WRONLY);
}

int test_connect(int sockfd UNUSED, const struct sockaddr *addr UNUSED,
socklen_t addrlen UNUSED)
{
return 0;
}

int test_getpid(void)
{
return 9999;
}

static char *response = "{\"id\": \"lightning-cli-9999\", \"jsonrpc\": \"2.0\", \"result\": {\"channels\": [\"\n\", \"├477308sat OUT/OURS ┼ IN/THEIRS 477308sat┤ SCID FLAG ALIAS\", \"├──────────────────────┼──────────────────────┤ 580612x1826x0 [__] BLUEIRON-v0.7.2rc1\"], \"my_address\": \"02b78caed0f45120acc48efe867aa506e8ea60f0712a23303178471da0ca2213f5@hdco6sxkbisc7s5t.onion\", \"format-hint\": \"simple\", \"avail_in\": \"0.00477308500btc (USD $54.37)\", \"num_utxos\": 0, \"num_gossipers\": 1, \"channels_flags\": \"P:private O:offline\", \"avail_out\": \"0.00477308500btc (USD $54.37)\", \"utxo_amount\": \"0.00000000btc (USD $0.00)\", \"num_channels\": 1, \"num_connected\": 1}}\n\n";
static size_t response_off;

ssize_t test_read(int fd UNUSED, void *buf, size_t len)
{
if (len > strlen(response + response_off))
len = strlen(response + response_off);
memcpy(buf, response + response_off, len);
return len;
}

static char *output;

int test_printf(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
tal_append_vfmt(&output, fmt, ap);
va_end(ap);
return 1;
}

int test_fputc(int c, FILE *stream)
{
tal_append_fmt(&output, "%c", c);
return (unsigned)c;
}

int main(int argc UNUSED, char *argv[])
{
setup_locale();

char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "test", NULL };

output = tal_strdup(NULL, "");
assert(test_main(3, fake_argv) == 0);

assert(streq(output, "channels=\n"
"\n"
"├477308sat OUT/OURS ┼ IN/THEIRS 477308sat┤ SCID FLAG ALIAS\n"
"├──────────────────────┼──────────────────────┤ 580612x1826x0 [__] BLUEIRON-v0.7.2rc1\n"
"my_address=02b78caed0f45120acc48efe867aa506e8ea60f0712a23303178471da0ca2213f5@hdco6sxkbisc7s5t.onion\n"
"avail_in=0.00477308500btc (USD $54.37)\n"
"num_utxos=0\n"
"num_gossipers=1\n"
"channels_flags=P:private O:offline\n"
"avail_out=0.00477308500btc (USD $54.37)\n"
"utxo_amount=0.00000000btc (USD $0.00)\n"
"num_channels=1\n"
"num_connected=1\n"));
tal_free(output);
return 0;
}

0 comments on commit 0f5036d

Please sign in to comment.