]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: (tests) add test for continuous json output
authorThomas Weißschuh <thomas@t-8ch.de>
Tue, 12 Dec 2023 18:40:31 +0000 (19:40 +0100)
committerThomas Weißschuh <thomas@t-8ch.de>
Wed, 13 Dec 2023 18:29:33 +0000 (19:29 +0100)
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
(cherry picked from commit be59729281c63eb8f226bdbdc9a356276be5eeae)

libsmartcols/samples/Makemodule.am
libsmartcols/samples/continuous-json.c [new file with mode: 0644]
meson.build
tests/commands.sh
tests/expected/libsmartcols/continuous-json [new file with mode: 0644]
tests/ts/libsmartcols/continuous-json [new file with mode: 0755]

index c0130b9e0a0fab8591390ead275649638c3ea3d6..1cbbf907d5b2cca57fd04c1e73fb395753c2b7ee 100644 (file)
@@ -4,6 +4,7 @@ check_PROGRAMS += \
        sample-scols-title \
        sample-scols-wrap \
        sample-scols-continuous \
+       sample-scols-continuous-json \
        sample-scols-fromfile \
        sample-scols-grouping-simple \
        sample-scols-grouping-overlay \
@@ -36,6 +37,10 @@ sample_scols_continuous_SOURCES = libsmartcols/samples/continuous.c
 sample_scols_continuous_LDADD = $(sample_scols_ldadd) libcommon.la
 sample_scols_continuous_CFLAGS = $(sample_scols_cflags)
 
+sample_scols_continuous_json_SOURCES = libsmartcols/samples/continuous-json.c
+sample_scols_continuous_json_LDADD = $(sample_scols_ldadd) libcommon.la
+sample_scols_continuous_json_CFLAGS = $(sample_scols_cflags)
+
 sample_scols_maxout_SOURCES = libsmartcols/samples/maxout.c
 sample_scols_maxout_LDADD = $(sample_scols_ldadd)
 sample_scols_maxout_CFLAGS = $(sample_scols_cflags)
diff --git a/libsmartcols/samples/continuous-json.c b/libsmartcols/samples/continuous-json.c
new file mode 100644 (file)
index 0000000..ae1f405
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
+ * Copyright (C) 2023 Thomas Weißschuh <thomas@t-8ch.de>
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "c.h"
+#include "xalloc.h"
+
+#include "libsmartcols.h"
+
+/* add columns to the @tb */
+static void setup_columns(struct libscols_table *tb)
+{
+       scols_table_enable_maxout(tb, 1);
+       if (!scols_table_new_column(tb, "COUNT", 0.1, SCOLS_FL_RIGHT))
+               goto fail;
+       if (!scols_table_new_column(tb, "TEXT", 0.9, 0))
+               goto fail;
+       return;
+fail:
+       scols_unref_table(tb);
+       err(EXIT_FAILURE, "failed to create output columns");
+}
+
+static struct libscols_line *add_line(struct libscols_table *tb, int i)
+{
+       char *p;
+       struct libscols_line *ln = scols_table_new_line(tb, NULL);
+
+       if (!ln)
+               err(EXIT_FAILURE, "failed to create output line");
+
+       xasprintf(&p, "%d", i);
+       if (scols_line_refer_data(ln, 0, p))
+               goto fail;
+
+       xasprintf(&p, "text%d", i);
+       if (scols_line_refer_data(ln, 1, p))
+               goto fail;
+
+       return ln;
+fail:
+       scols_unref_table(tb);
+       err(EXIT_FAILURE, "failed to create output line");
+}
+
+int main(void)
+{
+       struct libscols_table *tb;
+       size_t i;
+
+       scols_init_debug(0);
+
+       tb = scols_new_table();
+       if (!tb)
+               err(EXIT_FAILURE, "failed to create output table");
+       scols_table_enable_json(tb, 1);
+
+       setup_columns(tb);
+
+       for (i = 0; i < 10; i++) {
+               struct libscols_line *line;
+
+               line = add_line(tb, i);
+
+               /* print the line */
+               scols_table_print_range(tb, line, NULL);
+
+               fflush(scols_table_get_stream(tb));
+       }
+
+       scols_unref_table(tb);
+       return EXIT_SUCCESS;
+}
index b10028f1fd63c0ff998364701b30838d761af134..3aa0c95da4304abac4d2d414d47c4af1be02c012 100644 (file)
@@ -3334,6 +3334,15 @@ if not is_disabler(exe)
   exes += exe
 endif
 
+exe = executable(
+  'sample-scols-continuous-json',
+  'libsmartcols/samples/continuous-json.c',
+  include_directories : includes,
+  link_with : [lib_smartcols, lib_common])
+if not is_disabler(exe)
+  exes += exe
+endif
+
 exe = executable(
   'sample-scols-maxout',
   'libsmartcols/samples/maxout.c',
index 8dfb135a8e726e78d1269ca1fa9a2fa57e2a1777..1caa927096949c91261395bc62fad894c6c04996 100644 (file)
@@ -21,6 +21,7 @@ TS_HELPER_LIBMOUNT_UPDATE="${ts_helpersdir}test_mount_tab_update"
 TS_HELPER_LIBMOUNT_UTILS="${ts_helpersdir}test_mount_utils"
 TS_HELPER_LIBMOUNT_DEBUG="${ts_helpersdir}test_mount_debug"
 TS_HELPER_LIBMOUNT_FUZZ="${ts_helpersdir}test_mount_fuzz"
+TS_HELPER_LIBSMARTCOLS_CONTINUOUS_JSON="${ts_helpersdir}sample-scols-continuous-json"
 TS_HELPER_LIBSMARTCOLS_FROMFILE="${ts_helpersdir}sample-scols-fromfile"
 TS_HELPER_LIBSMARTCOLS_TITLE="${ts_helpersdir}sample-scols-title"
 TS_HELPER_PYLIBMOUNT_CONTEXT="$top_srcdir/libmount/python/test_mount_context.py"
diff --git a/tests/expected/libsmartcols/continuous-json b/tests/expected/libsmartcols/continuous-json
new file mode 100644 (file)
index 0000000..e8ba584
--- /dev/null
@@ -0,0 +1,40 @@
+{
+   "count": "0",
+   "text": "text0"
+}
+{
+   "count": "1",
+   "text": "text1"
+}
+{
+   "count": "2",
+   "text": "text2"
+}
+{
+   "count": "3",
+   "text": "text3"
+}
+{
+   "count": "4",
+   "text": "text4"
+}
+{
+   "count": "5",
+   "text": "text5"
+}
+{
+   "count": "6",
+   "text": "text6"
+}
+{
+   "count": "7",
+   "text": "text7"
+}
+{
+   "count": "8",
+   "text": "text8"
+}
+{
+   "count": "9",
+   "text": "text9"
+}
diff --git a/tests/ts/libsmartcols/continuous-json b/tests/ts/libsmartcols/continuous-json
new file mode 100755 (executable)
index 0000000..8456d6b
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/bash
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+#
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="title"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+TESTPROG="$TS_HELPER_LIBSMARTCOLS_CONTINUOUS_JSON"
+ts_check_test_command "$TESTPROG"
+
+ts_run $TESTPROG --width 80 >> $TS_OUTPUT 2>> $TS_ERRLOG
+
+ts_finalize