diff mbox series

[v9,3/8] cat-file: split test utility functions into a separate library file

Message ID 20250108183740.67022-4-eric.peijian@gmail.com (mailing list archive)
State Superseded
Headers show
Series cat-file: add remote-object-info to batch-command | expand

Commit Message

Eric Ju Jan. 8, 2025, 6:37 p.m. UTC
This refactor extracts utility functions from the cat-file's test
t1006-cat-file.sh into a dedicated library file. The goal is to improve
code reuse and readability, enabling future tests to leverage these
utilities without duplicating code
---
 t/lib-cat-file.sh   | 16 ++++++++++++++++
 t/t1006-cat-file.sh | 13 +------------
 2 files changed, 17 insertions(+), 12 deletions(-)
 create mode 100644 t/lib-cat-file.sh

Comments

Christian Couder Jan. 10, 2025, 2:26 p.m. UTC | #1
About the commit subject, maybe something like the following would be
a bit shorter:

t1006: split test utility functions into new "lib-cat-file.sh"

On Wed, Jan 8, 2025 at 7:38 PM Eric Ju <eric.peijian@gmail.com> wrote:
>
> This refactor extracts utility functions from the cat-file's test

s/test/test script/

> t1006-cat-file.sh into a dedicated library file. The goal is to improve

s/a dedicated library file/a new "lib-cat-file.sh" dedicated library file/

> code reuse and readability, enabling future tests to leverage these
> utilities without duplicating code

s/code/code./

> diff --git a/t/lib-cat-file.sh b/t/lib-cat-file.sh
> new file mode 100644
> index 0000000000..9fb20be308
> --- /dev/null
> +++ b/t/lib-cat-file.sh
> @@ -0,0 +1,16 @@
> +# Library of git-cat-file related tests.

s/tests/test functions/

> +
> +# Print a string without a trailing newline

s/newline/newline./

> +echo_without_newline () {
> +       printf '%s' "$*"
> +}
> +
> +# Print a string without newlines and replaces them with a NULL character (\0).

s/replaces/replace/

> +echo_without_newline_nul () {
> +       echo_without_newline "$@" | tr '\n' '\0'
> +}
> +
> +# Calculate the length of a string removing any leading spaces.

This might be a bit misleading as leading spaces are removed from the
output from `wc -c`, not from the string.

> +strlen () {
> +       echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
> +}
Eric Ju Jan. 14, 2025, 1:33 a.m. UTC | #2
Thank you Christian. They are all fixed in v10

On Fri, Jan 10, 2025 at 9:26 AM Christian Couder
<christian.couder@gmail.com> wrote:
>
> About the commit subject, maybe something like the following would be
> a bit shorter:
>
> t1006: split test utility functions into new "lib-cat-file.sh"
>
> On Wed, Jan 8, 2025 at 7:38 PM Eric Ju <eric.peijian@gmail.com> wrote:
> >
> > This refactor extracts utility functions from the cat-file's test
>
> s/test/test script/
>
> > t1006-cat-file.sh into a dedicated library file. The goal is to improve
>
> s/a dedicated library file/a new "lib-cat-file.sh" dedicated library file/
>
> > code reuse and readability, enabling future tests to leverage these
> > utilities without duplicating code
>
> s/code/code./
>
> > diff --git a/t/lib-cat-file.sh b/t/lib-cat-file.sh
> > new file mode 100644
> > index 0000000000..9fb20be308
> > --- /dev/null
> > +++ b/t/lib-cat-file.sh
> > @@ -0,0 +1,16 @@
> > +# Library of git-cat-file related tests.
>
> s/tests/test functions/
>
> > +
> > +# Print a string without a trailing newline
>
> s/newline/newline./
>
> > +echo_without_newline () {
> > +       printf '%s' "$*"
> > +}
> > +
> > +# Print a string without newlines and replaces them with a NULL character (\0).
>
> s/replaces/replace/
>
> > +echo_without_newline_nul () {
> > +       echo_without_newline "$@" | tr '\n' '\0'
> > +}
> > +
> > +# Calculate the length of a string removing any leading spaces.
>
> This might be a bit misleading as leading spaces are removed from the
> output from `wc -c`, not from the string.
>

Yes, I will just change it to "Calculate the length of a string. "

> > +strlen () {
> > +       echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
> > +}
diff mbox series

Patch

diff --git a/t/lib-cat-file.sh b/t/lib-cat-file.sh
new file mode 100644
index 0000000000..9fb20be308
--- /dev/null
+++ b/t/lib-cat-file.sh
@@ -0,0 +1,16 @@ 
+# Library of git-cat-file related tests.
+
+# Print a string without a trailing newline
+echo_without_newline () {
+	printf '%s' "$*"
+}
+
+# Print a string without newlines and replaces them with a NULL character (\0).
+echo_without_newline_nul () {
+	echo_without_newline "$@" | tr '\n' '\0'
+}
+
+# Calculate the length of a string removing any leading spaces.
+strlen () {
+	echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
+}
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index ff9bf213aa..5c7d581ea2 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -3,6 +3,7 @@ 
 test_description='git cat-file'
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-cat-file.sh
 
 test_cmdmode_usage () {
 	test_expect_code 129 "$@" 2>err &&
@@ -98,18 +99,6 @@  do
 	'
 done
 
-echo_without_newline () {
-    printf '%s' "$*"
-}
-
-echo_without_newline_nul () {
-	echo_without_newline "$@" | tr '\n' '\0'
-}
-
-strlen () {
-    echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
-}
-
 run_tests () {
     type=$1
     oid=$2