diff mbox series

t-progress.c : unit tests for progress.c

Message ID 20231011082716.901048-1-siddhartth@google.com (mailing list archive)
State New, archived
Headers show
Series t-progress.c : unit tests for progress.c | expand

Commit Message

Siddharth Singh Oct. 11, 2023, 8:27 a.m. UTC
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>
---

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.

As written, a unit test for throughput progress printing would have to capture stdout from the library and compare it to the expected output, which is difficult to implement and unusual for unit tests anyway.

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){
…
}

Another way is to capture the output from the library into a file instead of stdout and then read it from the file and compare it to the expected output, However, this is a difficult task, and I recommend against it.

We may need to make some changes to the way these libraries are implemented in order to make them unit testable in the future.

Therefore, i want to ask if it's worth investing time in developing a solution?


I look forward to hearing your thoughts on this.
[1] https://lore.kernel.org/git/cover.1692297001.git.steadmon@google.com/



 Makefile                  |   1 +
 t/unit-tests/.gitignore   |   1 +
 t/unit-tests/t-progress.c | 229 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 231 insertions(+)
 create mode 100644 t/unit-tests/t-progress.c

Comments

Junio C Hamano Oct. 11, 2023, 5:29 p.m. UTC | #1
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.
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 4016da6e39..eabbfe32cf 100644
--- a/Makefile
+++ b/Makefile
@@ -1335,6 +1335,7 @@  THIRD_PARTY_SOURCES += sha1dc/%
 
 UNIT_TEST_PROGRAMS += t-basic
 UNIT_TEST_PROGRAMS += t-strbuf
+UNIT_TEST_PROGRAMS += t-progress
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_DIR)/%$X,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
diff --git a/t/unit-tests/.gitignore b/t/unit-tests/.gitignore
index e292d58348..cf5eb4803e 100644
--- a/t/unit-tests/.gitignore
+++ b/t/unit-tests/.gitignore
@@ -1,2 +1,3 @@ 
 /t-basic
 /t-strbuf
+/t-progress
diff --git a/t/unit-tests/t-progress.c b/t/unit-tests/t-progress.c
new file mode 100644
index 0000000000..437d6acf16
--- /dev/null
+++ b/t/unit-tests/t-progress.c
@@ -0,0 +1,229 @@ 
+#include "test-lib.h"
+#include "progress.c"
+
+
+static void t_simple_progress()
+{
+	int total = 4;
+	struct progress *progress = NULL;
+	int i;
+	progress = start_progress("Working hard", total);
+	for (i = 1; i <= total; i++) {
+		display_progress(progress, i);
+		check_uint(i, ==, progress->last_value);
+		check_str(progress->title, "Working hard");
+		check_int(progress->last_percent, ==, i * 100 / total);
+	}
+	return;
+}
+
+static void t_simple_progress_percent_text()
+{
+	int total = 4;
+	struct progress *progress = NULL;
+	int i;
+	char *expected[] = {
+		"  0% (0/4)",
+		" 25% (1/4)",
+		" 50% (2/4)",
+		" 75% (3/4)",
+		"100% (4/4)"
+		};
+	char *instructions[] = {
+		"progress",
+		"progress",
+		"progress",
+		"progress",
+		"progress"
+	};
+	int value[] = {
+		0,
+		1,
+		2,
+		3,
+		4
+	};
+	progress = start_progress("Working hard", total);
+	for (i = 0; i < 5; i++) {
+		if(strcmp(instructions[i], "progress")==0){
+			display_progress(progress, value[i]);
+			check_str(progress->title, "Working hard");
+			check_str(progress->counters_sb.buf, expected[i]);
+			check_uint(i * (100 / total), ==, progress->last_percent);
+		}
+	}
+	return;
+}
+
+static void t_progress_display_breaks_long_lines_1()
+{
+	int total = 100000;
+	struct progress *progress = NULL;
+	int i;
+	char *expected[4] = {
+		"  0% (100/100000)",
+		"  1% (1000/100000)",
+		" 10% (10000/100000)",
+		"100% (100000/100000)"
+	};
+	char *instructions[] = {
+		"progress",
+		"progress",
+		"progress",
+		"progress"
+	};
+	int value[] = {
+		100,
+		1000,
+		10000,
+		100000
+	};
+	progress = start_progress(
+		"Working hard.......2.........3.........4.........5.........6",
+		total);
+	for (i = 0; i < 4; i++) {
+		if(strcmp(instructions[i], "progress")==0){
+			display_progress(progress, value[i]);
+		}
+		check_str(progress->title, "Working hard.......2.........3.........4.........5.........6");
+		check_str(progress->counters_sb.buf, expected[i]);
+	}
+	return;
+}
+
+static void t_progress_display_breaks_long_lines_2()
+{
+	int total = 100000;
+	struct progress *progress = NULL;
+	int i;
+	char *expected[] = {
+		"",
+		"  0% (1/100000)",
+		"",
+		"  0% (2/100000)",
+		" 10% (10000/100000)",
+		"100% (100000/100000)"
+	};
+	char *instructions[] = {
+		"update",
+		"progress",
+		"update",
+		"progress",
+		"progress",
+		"progress"
+	};
+	int value[] = {
+		-1,
+		1,
+		-1,
+		2,
+		10000,
+		100000
+	};
+	progress = start_progress(
+		"Working hard.......2.........3.........4.........5.........6",
+		total);
+	for (i = 0; i < 5; i++) {
+		if(strcmp(instructions[i], "progress")==0){
+			display_progress(progress, value[i]);
+			check_str(progress->title, "Working hard.......2.........3.........4.........5.........6");
+			check_str(progress->counters_sb.buf, expected[i]);
+		}else if(strcmp(instructions[i], "update")==0){
+			progress_test_force_update();
+		}
+	}
+	return;
+}
+
+static void t_progress_display_breaks_long_lines_3()
+{
+	int total = 100000;
+	struct progress *progress = NULL;
+	int i;
+	char *expected[4] = {
+		" 25% (25000/100000)",
+		" 50% (50000/100000)",
+		" 75% (75000/100000)",
+		"100% (100000/100000)"
+	};
+	char *instructions[] = {
+		"progress",
+		"progress",
+		"progress",
+		"progress"
+	};
+	int value[] = {
+		25000,
+		50000,
+		75000,
+		100000
+	};
+	progress = start_progress(
+		"Working hard.......2.........3.........4.........5.........6",
+		total);
+	for (i = 0; i < 4; i++) {
+		if(strcmp(instructions[i], "progress")==0){
+			display_progress(progress, value[i]);
+			check_str(progress->title, "Working hard.......2.........3.........4.........5.........6");
+			check_str(progress->counters_sb.buf, expected[i]);
+		}else if(strcmp(instructions[i], "update")==0){
+			progress_test_force_update();
+		}
+	}
+	return;
+}
+
+
+static void t_progress_shortens_crazy_caller()
+{
+	int total = 1000;
+	struct progress *progress = NULL;
+	int i;
+	char *expected[4] = {
+		" 10% (100/1000)",
+		" 20% (200/1000)",
+		"  0% (1/1000)",
+		"100% (1000/1000)"
+	};
+	char *instructions[] = {
+		"progress",
+		"progress",
+		"progress",
+		"progress"
+	};
+	int value[] = {
+		100,
+		200,
+		1,
+		1000
+	};
+	progress = start_progress(
+		"Working hard.......2.........3.........4.........5.........6",
+		total);
+	for (i = 0; i < 4; i++) {
+		if(strcmp(instructions[i], "progress")==0){
+			display_progress(progress, value[i]);
+			check_str(progress->title, "Working hard.......2.........3.........4.........5.........6");
+			check_str(progress->counters_sb.buf, expected[i]);
+		}else if(strcmp(instructions[i], "update")==0){
+			progress_test_force_update();
+		}
+	}
+	return;
+}
+
+int cmd_main(int argc, const char **argv)
+{
+	TEST(t_simple_progress(), "Simple progress upto 3 units");
+	TEST(t_simple_progress_percent_text(),
+	     "Simple progress with percent output");
+	TEST(t_progress_display_breaks_long_lines_1(),
+	     "progress display breaks long lines #1");
+	TEST(t_progress_display_breaks_long_lines_2(),
+	     "progress display breaks long lines #2");
+	TEST(t_progress_display_breaks_long_lines_3(),
+	     "progress display breaks long lines #3");
+	TEST(t_progress_shortens_crazy_caller(),
+	     "progress shortens - crazy caller");
+	return test_done();
+}