git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Altmanninger <aclopte@gmail.com>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: git@vger.kernel.org, "Junio C Hamano" <gitster@pobox.com>,
	"SZEDER Gábor" <szeder.dev@gmail.com>,
	"René Scharfe" <l.s.r@web.de>
Subject: Re: [PATCH v7 3/7] progress.c tests: make start/stop commands on stdin
Date: Mon, 27 Dec 2021 02:10:13 +0100	[thread overview]
Message-ID: <20211227011013.3ngeh57llxnknphf@gmail.com> (raw)
In-Reply-To: <patch-v7-3.7-d685c248686-20211217T041945Z-avarab@gmail.com>

On Fri, Dec 17, 2021 at 05:24:58AM +0100, Ævar Arnfjörð Bjarmason wrote:
> Change the usage of the "test-tool progress" introduced in
> 2bb74b53a49 (Test the progress display, 2019-09-16) to take command
> like "start" and "stop" on stdin, instead of running them implicitly.
> 
> This makes for tests that are easier to read, since the recipe will
> mirror the API usage, and allows for easily testing invalid usage that

(Of course invalid API usage wasn't really a problem before, but it's good
that you mention the upcoming tests, to calm any concerns)

> would yield (or should yield) a BUG(), e.g. providing two "start"
> calls in a row. A subsequent commit will add such tests.
> 
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  t/helper/test-progress.c    | 46 ++++++++++++++++++++++-------
>  t/t0500-progress-display.sh | 58 +++++++++++++++++++++++--------------
>  2 files changed, 72 insertions(+), 32 deletions(-)
> 
> diff --git a/t/helper/test-progress.c b/t/helper/test-progress.c
> index 50fd3be3dad..1435c28e950 100644
> --- a/t/helper/test-progress.c
> +++ b/t/helper/test-progress.c
> @@ -3,6 +3,9 @@
>   *
>   * Reads instructions from standard input, one instruction per line:
>   *
> + *   "start <total>[ <title>]" - Call start_progress(title, total),
> + *                               Uses the default title of "Working hard"
> + *                               if the " <title>" is omitted.
>   *   "progress <items>" - Call display_progress() with the given item count
>   *                        as parameter.
>   *   "throughput <bytes> <millis> - Call display_throughput() with the given
> @@ -10,6 +13,7 @@
>   *                                  specify the time elapsed since the
>   *                                  start_progress() call.
>   *   "update" - Set the 'progress_update' flag.
> + *   "stop" - Call stop_progress().
>   *
>   * See 't0500-progress-display.sh' for examples.
>   */
> @@ -19,34 +23,52 @@
>  #include "parse-options.h"
>  #include "progress.h"
>  #include "strbuf.h"
> +#include "string-list.h"
> +
> +/*
> + * We can't use "end + 1" as an argument to start_progress() below, it
> + * doesn't xstrdup() its "title" argument. We need to hold onto a
> + * valid "char *" for it until the end.
> + */
> +static char *dup_title(struct string_list *titles, const char *title)
> +{
> +	return string_list_insert(titles, title)->string;
> +}

It seems weird to reference someone else's local variables in "end + 1" here.
How about inlining this function instead?

			} else if (*end == ' ') {
				char *title_duped = string_list_insert(&titles, end + 1)->string;
				progress = start_progress(title_duped, total);
			} else {

and maybe add a comment there, but I'm not sure if we still need it.

>  
>  int cmd__progress(int argc, const char **argv)
>  {
> -	int total = 0;
> -	const char *title;
> +	const char *const default_title = "Working hard";
> +	struct string_list titles = STRING_LIST_INIT_DUP;
>  	struct strbuf line = STRBUF_INIT;
> -	struct progress *progress;
> +	struct progress *progress = NULL;
>  
>  	const char *usage[] = {
> -		"test-tool progress [--total=<n>] <progress-title>",
> +		"test-tool progress <stdin",
>  		NULL

(unrelated: I'd always add a trailing comma if I can, even though in this case it won't ever matter)

>  	};
>  	struct option options[] = {
> -		OPT_INTEGER(0, "total", &total, "total number of items"),
>  		OPT_END(),
>  	};
>  
>  	argc = parse_options(argc, argv, NULL, options, usage, 0);
> -	if (argc != 1)
> -		die("need a title for the progress output");
> -	title = argv[0];
> +	if (argc)
> +		usage_with_options(usage, options);
>  
>  	progress_testing = 1;
> -	progress = start_progress(title, total);
>  	while (strbuf_getline(&line, stdin) != EOF) {
>  		char *end;
>  
> -		if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
> +		if (skip_prefix(line.buf, "start ", (const char **) &end)) {
> +			uint64_t total = strtoull(end, &end, 10);
> +			if (*end == '\0')
> +				progress = start_progress(default_title, total);
> +			else if (*end == ' ')
> +				progress = start_progress(dup_title(&titles,
> +								    end + 1),
> +							  total);
> +			else
> +				die("invalid input: '%s'\n", line.buf);
> +		} else if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
>  			uint64_t item_count = strtoull(end, &end, 10);
>  			if (*end != '\0')
>  				die("invalid input: '%s'\n", line.buf);
> @@ -65,12 +87,14 @@ int cmd__progress(int argc, const char **argv)
>  			display_throughput(progress, byte_count);
>  		} else if (!strcmp(line.buf, "update")) {
>  			progress_test_force_update();
> +		} else if (!strcmp(line.buf, "stop")) {
> +			stop_progress(&progress);
>  		} else {
>  			die("invalid input: '%s'\n", line.buf);
>  		}
>  	}
> -	stop_progress(&progress);
>  	strbuf_release(&line);
> +	string_list_clear(&titles, 0);
>  
>  	return 0;
>  }
> diff --git a/t/t0500-progress-display.sh b/t/t0500-progress-display.sh
> index f37cf2eb9c9..27ab4218b01 100755
> --- a/t/t0500-progress-display.sh
> +++ b/t/t0500-progress-display.sh
> @@ -18,6 +18,7 @@ test_expect_success 'simple progress display' '
>  	EOF
>  
>  	cat >in <<-\EOF &&
> +	start 0
>  	update
>  	progress 1
>  	update
> @@ -26,8 +27,9 @@ test_expect_success 'simple progress display' '
>  	progress 4
>  	update
>  	progress 5
> +	stop
>  	EOF
> -	test-tool progress "Working hard" <in 2>stderr &&
> +	test-tool progress <in 2>stderr &&
>  
>  	show_cr <stderr >out &&
>  	test_cmp expect out
> @@ -42,11 +44,13 @@ test_expect_success 'progress display with total' '
>  	EOF
>  
>  	cat >in <<-\EOF &&
> +	start 3
>  	progress 1
>  	progress 2
>  	progress 3
> +	stop
>  	EOF
> -	test-tool progress --total=3 "Working hard" <in 2>stderr &&
> +	test-tool progress <in 2>stderr &&
>  
>  	show_cr <stderr >out &&
>  	test_cmp expect out
> @@ -63,14 +67,14 @@ Working hard.......2.........3.........4.........5.........6:
>  EOF
>  
>  	cat >in <<-\EOF &&
> +	start 100000 Working hard.......2.........3.........4.........5.........6
>  	progress 100
>  	progress 1000
>  	progress 10000
>  	progress 100000
> +	stop
>  	EOF
> -	test-tool progress --total=100000 \
> -		"Working hard.......2.........3.........4.........5.........6" \

I don't know enough about progress tests to judge if this is better.
The start invocation does look nicer, but it might feel weird to always
include "stop". We could do that automatically but then we're no longer
mirroring the API...

> -		<in 2>stderr &&
> +	test-tool progress <in 2>stderr &&

  reply	other threads:[~2021-12-27  1:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-17  4:24 [PATCH v7 0/7] progress: test fixes / cleanup Ævar Arnfjörð Bjarmason
2021-12-17  4:24 ` [PATCH v7 1/7] leak tests: fix a memory leaks in "test-progress" helper Ævar Arnfjörð Bjarmason
2021-12-27  1:07   ` Johannes Altmanninger
2021-12-17  4:24 ` [PATCH v7 2/7] progress.c test helper: add missing braces Ævar Arnfjörð Bjarmason
2021-12-17  4:24 ` [PATCH v7 3/7] progress.c tests: make start/stop commands on stdin Ævar Arnfjörð Bjarmason
2021-12-27  1:10   ` Johannes Altmanninger [this message]
2021-12-27  1:31     ` Ævar Arnfjörð Bjarmason
2021-12-17  4:24 ` [PATCH v7 4/7] progress.c tests: test some invalid usage Ævar Arnfjörð Bjarmason
2021-12-27  1:11   ` Johannes Altmanninger
2022-01-03 23:48     ` Junio C Hamano
2021-12-17  4:25 ` [PATCH v7 5/7] progress.c: add temporary variable from progress struct Ævar Arnfjörð Bjarmason
2021-12-27  1:11   ` Johannes Altmanninger
2021-12-17  4:25 ` [PATCH v7 6/7] pack-bitmap-write.c: don't return without stop_progress() Ævar Arnfjörð Bjarmason
2021-12-27  1:11   ` Johannes Altmanninger
2021-12-17  4:25 ` [PATCH v7 7/7] various *.c: use isatty(0|2), not isatty(STDIN_FILENO|STDERR_FILENO) Ævar Arnfjörð Bjarmason
2021-12-27  1:17   ` Johannes Altmanninger

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=20211227011013.3ngeh57llxnknphf@gmail.com \
    --to=aclopte@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=szeder.dev@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).