From 77bac227418d23a75beb0c9dc40932f30e62813e Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 24 Dec 2021 16:08:08 +0100 Subject: [PATCH] test: Also print proper caret for too many arguments This case is handled specially for unknown reasons and was missed in #6030. --- src/builtins/test.cpp | 18 +++++++++++------- tests/checks/test.fish | 8 ++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/builtins/test.cpp b/src/builtins/test.cpp index cde90430e..dfd92da7c 100644 --- a/src/builtins/test.cpp +++ b/src/builtins/test.cpp @@ -560,9 +560,12 @@ unique_ptr test_parser::parse_args(const wcstring_list_t &args, wcst // Handle errors. // For now we only show the first error. - if (!parser.errors.empty()) { + if (!parser.errors.empty() || result->range.end < args.size()) { int narg = 0; int len_to_err = 0; + if (parser.errors.empty()) { + parser.error_idx = result->range.end; + } wcstring commandline; for (const wcstring &arg : args) { if (narg > 0) { @@ -576,7 +579,13 @@ unique_ptr test_parser::parse_args(const wcstring_list_t &args, wcst } err.append(program_name); err.append(L": "); - err.append(parser.errors.at(0)); + if (!parser.errors.empty()) { + err.append(parser.errors.at(0)); + } else { + append_format(err, L"unexpected argument at index %lu: '%ls'", + static_cast(result->range.end) + 1, + args.at(result->range.end).c_str()); + } err.push_back(L'\n'); err.append(commandline); err.push_back(L'\n'); @@ -588,11 +597,6 @@ unique_ptr test_parser::parse_args(const wcstring_list_t &args, wcst // parse_expression(). assert(result->range.end <= args.size()); if (result->range.end < args.size()) { - if (err.empty()) { - append_format(err, L"%ls: unexpected argument at index %lu: '%ls'\n", program_name, - static_cast(result->range.end) + 1, - args.at(result->range.end).c_str()); - } result.reset(nullptr); } } diff --git a/tests/checks/test.fish b/tests/checks/test.fish index 6372c73f3..75219a76d 100644 --- a/tests/checks/test.fish +++ b/tests/checks/test.fish @@ -51,3 +51,11 @@ t 5,2 # CHECKERR: ^ # CHECKERR: in function 't' with arguments '5,2' # CHECKERR: called on line {{\d+}} of file {{.*}}test.fish + +test -x /usr/bin/go /usr/local/bin/go +# CHECKERR: test: unexpected argument at index 3: '/usr/local/bin/go' +# CHECKERR: -x /usr/bin/go /usr/local/bin/go +# CHECKERR: {{ \^}} +# CHECKERR: {{.*}}test.fish (line {{\d+}}): +# CHECKERR: test -x /usr/bin/go /usr/local/bin/go +# CHECKERR: ^