From: Junio C Hamano <gitster@pobox.com>
To: Siddharth Singh <siddhartth@google.com>
Cc: git@vger.kernel.org, nasamuffin@google.com
Subject: Re: [PATCH] t-progress.c : unit tests for progress.c
Date: Wed, 11 Oct 2023 10:29:52 -0700 [thread overview]
Message-ID: <xmqq34yhm5in.fsf@gitster.g> (raw)
In-Reply-To: <20231011082716.901048-1-siddhartth@google.com> (Siddharth Singh's message of "Wed, 11 Oct 2023 10:27:08 +0200")
Siddharth Singh <siddhartth@google.com> writes:
> These tests are directly inspired from the tests inside
> t/t0500-progress-display.sh
>
> The existing shell tests for the Git progress library only test the output of the library, not the correctness of the progress struct fields. Unit tests can fill this gap and improve confidence that the library works as expected. For example, unit tests can verify that the progress struct fields are updated correctly when the library is used.
>
> Change-Id: I190522f29fdab9291af71b7788eeee2c0f26282d
> Signed-off-by: Siddharth Singh <siddhartth@google.com>
> ---
The contents is of course important, but please also pay attention
to the formatting to make what you write readable. Writing good
things does not help if it is not read. Wrap lines to appropriate
lengths.
Documentation/SubmittingPatches is your friend.
>
> Dear Git Community,
> As you may be aware, my colleague Josh is proposing a unit testing
> framework[1] on the mailing list. I attempted to use the latest
> version of that series to convert t/helper/test-progress.c to unit
> tests. However, while writing the tests, I realized that the way
> progress.c is implemented makes it very difficult to test it in
> units.
>
> Firstly, most unit tests are typically written as a method that
> takes the expected output and the actual output of the unit being
> tested, and compares them for equality. However, because it's
> intended to print user-facing output on an interactive terminal,
> progress.c prints everything out to stderr, which makes it
> difficult to unit test.
It sounds like you found where the test framework is lacking. If
making sure what we spew out to the standard error stream is worth
covering in the unit tests, the test framework needs to support it,
right?
> There are a few ways to work around this issue in my opinion. One
> way is to modify the library that does not print to output stream
> and returns the data to the caller:
>
> static void display(struct progress *progress, uint64_t n, char *done){
> …
> }
>
> becomes
>
> struct strbuf display(struct progress *progress,uint64_t n,char *done){
> …
> }
The approach adds a feature for outside callers to access this bit
of internal implementation detail of the progress code. If no real
callers want to exercise that feature and it is only useful for
testing, it smells like the tail wagging the dog, though.
It certainly is not worth butchering the real code for the sake of
working around the lack of current unit test framework to
contaminate the global namespace with way too generically named
function "display()".
Assuming that you *must* work around the lack of stderr support, it
probably would make much more sense to add a new member that points
at an output stream to "struct progress". Make it a thin wrapper
around "FILE *" that supports whatever display() and the helper
functions it calls needs, like write(2) or fprintf(3). And you can
mock that "output stream" in your unit tests to append to an in-core
buffer if you wanted to. The production code does not have to care
about your mock that way.
prev parent reply other threads:[~2023-10-11 17:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-11 8:27 [PATCH] t-progress.c : unit tests for progress.c Siddharth Singh
2023-10-11 17:29 ` Junio C Hamano [this message]
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=xmqq34yhm5in.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=nasamuffin@google.com \
--cc=siddhartth@google.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).