[LTP] [RFC PATCH 2/5] lib: Allow test to have positional args

Petr Vorel pvorel@suse.cz
Fri Feb 28 18:24:35 CET 2025


Similar to $TST_POS_ARGS in shell API. This will be used in
testcases/lib/tst_run_shell.c.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
I'm not really sure if this is a good approach.
But bounding the +1 value to .runs_script does not look to me good
either.

 doc/developers/writing_tests.rst | 2 +-
 include/tst_test.h               | 5 ++++-
 lib/tst_test.c                   | 9 +++++++--
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/doc/developers/writing_tests.rst b/doc/developers/writing_tests.rst
index 9b18ec059c..f5796ddc49 100644
--- a/doc/developers/writing_tests.rst
+++ b/doc/developers/writing_tests.rst
@@ -521,7 +521,7 @@ LTP C And Shell Test API Comparison
     * - not applicable
       - TST_NEEDS_MODULE
 
-    * - not applicable
+    * - .pos_args (internal use for tst_run_shell.c)
       - TST_POS_ARGS
 
     * - not applicable
diff --git a/include/tst_test.h b/include/tst_test.h
index eb73cd593c..b249f833ab 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -292,8 +292,11 @@ struct tst_fs {
  *
  * @tcnt: A number of tests. If set the test() callback is called tcnt times
  *        and each time passed an increasing counter value.
+ *
  * @options: An NULL optstr terminated array of struct tst_option.
  *
+ * @pos_args: An number of positional parameters passed to tst_run_shell.c.
+ *
  * @min_kver: A minimal kernel version the test can run on. e.g. "3.10".
  *
  * @supported_archs: A NULL terminated array of architectures the test runs on
@@ -528,6 +531,7 @@ struct tst_fs {
 	unsigned int tcnt;
 
 	struct tst_option *options;
+	int pos_args;
 
 	const char *min_kver;
 
@@ -555,7 +559,6 @@ struct tst_fs {
 	unsigned int skip_in_secureboot:1;
 	unsigned int skip_in_compat:1;
 
-
 	int needs_abi_bits;
 
 	unsigned int needs_hugetlbfs:1;
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 3823ea109e..1c2cc5e3b2 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -711,6 +711,9 @@ static void parse_opts(int argc, char *argv[])
 
 	check_option_collision();
 
+	if (tst_test->pos_args < 0)
+		tst_brk(TBROK, ".pos_args must be >= 0");
+
 	optstr[0] = 0;
 
 	for (i = 0; i < ARRAY_SIZE(options); i++)
@@ -751,8 +754,10 @@ static void parse_opts(int argc, char *argv[])
 		}
 	}
 
-	if (optind < argc)
-		tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
+	if (optind + tst_test->pos_args < argc) {
+		tst_brk(TBROK, "Unexpected argument(s) '%s' (%d + %d < %d)",
+			argv[optind], optind, tst_test->pos_args, argc);
+	}
 }
 
 int tst_parse_int(const char *str, int *val, int min, int max)
-- 
2.47.2



More information about the ltp mailing list