From: Rasmus Villemoes <ravi@prevas.dk>
To: u-boot@lists.denx.de
Cc: Tom Rini <trini@konsulko.com>,
Quentin Schulz <quentin.schulz@cherry.de>,
Rasmus Villemoes <ravi@prevas.dk>
Subject: [PATCH v2 4/4] cmd: test: fix handling of single-argument form of test
Date: Thu, 12 Mar 2026 11:01:06 +0100 [thread overview]
Message-ID: <20260312100106.702368-5-ravi@prevas.dk> (raw)
In-Reply-To: <20260312100106.702368-1-ravi@prevas.dk>
POSIX states that
0 arguments:
Exit false (1).
1 argument:
Exit true (0) if $1 is not null; otherwise, exit false.
and at least bash and busybox sh behave that way.
The current 'argc < 3' does the right thing for a non-existing or
empty argv[1], but not for a non-empty argv[1]. Fix that and add
corresponding test cases.
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
---
cmd/test.c | 11 +++++++++--
test/hush/if.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/cmd/test.c b/cmd/test.c
index f0645ef98f1..0d0f090386c 100644
--- a/cmd/test.c
+++ b/cmd/test.c
@@ -71,10 +71,17 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
argc--;
}
- /* args? */
- if (argc < 3)
+ /*
+ * Per POSIX, 'test' with 0 arguments should return 1, while
+ * 'test <arg>' should be equivalent to 'test -n <arg>',
+ * i.e. true if and only if <arg> is not empty.
+ */
+ if (argc < 2)
return 1;
+ if (argc == 2)
+ return !strcmp(argv[1], "");
+
#ifdef DEBUG
{
debug("test(%d):", argc);
diff --git a/test/hush/if.c b/test/hush/if.c
index 5f75ea68b32..6129e2c530c 100644
--- a/test/hush/if.c
+++ b/test/hush/if.c
@@ -32,6 +32,15 @@ static int hush_test_if_base(struct unit_test_state *uts)
sprintf(if_formatted, if_format, "false");
ut_asserteq(1, run_command(if_formatted, 0));
+ sprintf(if_formatted, if_format, "test");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "test ''");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "test 'abc'");
+ ut_assertok(run_command(if_formatted, 0));
+
return 0;
}
HUSH_TEST(hush_test_if_base, 0);
@@ -355,6 +364,27 @@ static int hush_test_lbracket_alias(struct unit_test_state *uts)
sprintf(if_formatted, if_format, "[ ! aaa != aaa -o ! bbb = bbb ]");
ut_assertok(run_command(if_formatted, 0));
+ sprintf(if_formatted, if_format, "[ ]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ '' ]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ ''");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ 'abc' ]");
+ ut_assertok(run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ 'abc'");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
return 0;
}
HUSH_TEST(hush_test_lbracket_alias, UTF_CONSOLE);
--
2.53.0
next prev parent reply other threads:[~2026-03-12 10:01 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 1/4] cmd: test: allow using [ as alias for test Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 2/4] doc: test: document [ ] spelling of test Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 3/4] test: add tests for left-bracket alias for 'test' command Rasmus Villemoes
2026-03-12 10:01 ` Rasmus Villemoes [this message]
2026-03-13 6:24 ` [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Anshul Dalal
2026-03-25 23:05 ` Tom Rini
2026-03-27 15:48 ` Franz Schnyder
2026-03-27 19:38 ` Rasmus Villemoes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260312100106.702368-5-ravi@prevas.dk \
--to=ravi@prevas.dk \
--cc=quentin.schulz@cherry.de \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox