[02/21] tests: Add bash lib

Message ID 20240520090611.10406-3-jonatan.schlag@ipfire.org
State New
Headers
Series [01/21] test: Add bash lib for colors |

Commit Message

Jonatan Schlag May 20, 2024, 9:05 a.m. UTC
  This allows use to write test with less effort as we can reuse functions

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 tests/lib.sh | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 tests/lib.sh
  

Comments

Michael Tremer May 31, 2024, 9:47 a.m. UTC | #1
Hello,

> On 20 May 2024, at 10:05, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote:
> 
> This allows use to write test with less effort as we can reuse functions
> 
> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> ---
> tests/lib.sh | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
> create mode 100644 tests/lib.sh
> 
> diff --git a/tests/lib.sh b/tests/lib.sh
> new file mode 100644
> index 000000000..7749d5158
> --- /dev/null
> +++ b/tests/lib.sh
> @@ -0,0 +1,20 @@
> +#!/usr/bin/bash
> +
> +LIB_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"

???

I don’t think this is very intuitive.

> +. ${LIB_DIR}/lib_color.sh
> +
> +test_that() {
> +
> + if ! "$@" ; then
> + echo -e "${CLR_RED_BG} Test failed: ${*} ${CLR_RESET}"
> + return 1
> + else
> + echo -e "${CLR_GREEN_BG} Test succeded: ${*} ${CLR_RESET}"
> + return 0
> + fi
> +}

We have had so many discussions about how to name functions. “That” is quite generic in my opinion.

Why not “test_command” or something like that?

> +
> +var_has_value() {
> + [[ "${!1}" == "${2}" ]]
> +}
> -- 
> 2.39.2
>
  
Jonatan Schlag June 2, 2024, 5:49 p.m. UTC | #2
Hi,


Am Freitag, dem 31.05.2024 um 10:47 +0100 schrieb Michael Tremer:
> Hello,
> 
> > On 20 May 2024, at 10:05, Jonatan Schlag
> > <jonatan.schlag@ipfire.org> wrote:
> > 
> > This allows use to write test with less effort as we can reuse
> > functions
> > 
> > Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> > ---
> > tests/lib.sh | 20 ++++++++++++++++++++
> > 1 file changed, 20 insertions(+)
> > create mode 100644 tests/lib.sh
> > 
> > diff --git a/tests/lib.sh b/tests/lib.sh
> > new file mode 100644
> > index 000000000..7749d5158
> > --- /dev/null
> > +++ b/tests/lib.sh
> > @@ -0,0 +1,20 @@
> > +#!/usr/bin/bash
> > +
> > +LIB_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
> 
> ???
> 
> I don’t think this is very intuitive.

Ok, what am I supposed to do about it? I need the path where this file
is located, and I cannot hard-code it. Should I add a comment?

Jonatan

> 
> > +. ${LIB_DIR}/lib_color.sh
> > +
> > +test_that() {
> > +
> > + if ! "$@" ; then
> > + echo -e "${CLR_RED_BG} Test failed: ${*} ${CLR_RESET}"
> > + return 1
> > + else
> > + echo -e "${CLR_GREEN_BG} Test succeded: ${*} ${CLR_RESET}"
> > + return 0
> > + fi
> > +}
> 
> We have had so many discussions about how to name functions. “That”
> is quite generic in my opinion.
> 
> Why not “test_command” or something like that?
> 
> > +
> > +var_has_value() {
> > + [[ "${!1}" == "${2}" ]]
> > +}
> > -- 
> > 2.39.2
> > 
>
  
Michael Tremer June 3, 2024, 9:19 a.m. UTC | #3
Hello,

> On 2 Jun 2024, at 18:49, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote:
> 
> 
> Hi,
> 
> 
> Am Freitag, dem 31.05.2024 um 10:47 +0100 schrieb Michael Tremer:
>> Hello,
>> 
>>> On 20 May 2024, at 10:05, Jonatan Schlag
>>> <jonatan.schlag@ipfire.org> wrote:
>>> 
>>> This allows use to write test with less effort as we can reuse
>>> functions
>>> 
>>> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
>>> ---
>>> tests/lib.sh | 20 ++++++++++++++++++++
>>> 1 file changed, 20 insertions(+)
>>> create mode 100644 tests/lib.sh
>>> 
>>> diff --git a/tests/lib.sh b/tests/lib.sh
>>> new file mode 100644
>>> index 000000000..7749d5158
>>> --- /dev/null
>>> +++ b/tests/lib.sh
>>> @@ -0,0 +1,20 @@
>>> +#!/usr/bin/bash
>>> +
>>> +LIB_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
>> 
>> ???
>> 
>> I don’t think this is very intuitive.
> 
> Ok, what am I supposed to do about it? I need the path where this file
> is located, and I cannot hard-code it. Should I add a comment?

Generally speaking, I like to split things into one line per task. So that it is very easy to see what is happening when reading the code. This approach also has some more advantages. For example, you can easily put a print statement between lines to see if you are getting the output that you are expecting. That makes debugging nice and easy.

When I say generally, there are of course exceptions like very trivial things that can often be put into the same line.

Commenting is also a lot easier when you only have to comment one thing at once. You need to add way more comments, please.

-Michael

> Jonatan
> 
>> 
>>> +. ${LIB_DIR}/lib_color.sh
>>> +
>>> +test_that() {
>>> +
>>> + if ! "$@" ; then
>>> + echo -e "${CLR_RED_BG} Test failed: ${*} ${CLR_RESET}"
>>> + return 1
>>> + else
>>> + echo -e "${CLR_GREEN_BG} Test succeded: ${*} ${CLR_RESET}"
>>> + return 0
>>> + fi
>>> +}
>> 
>> We have had so many discussions about how to name functions. “That”
>> is quite generic in my opinion.
>> 
>> Why not “test_command” or something like that?
>> 
>>> +
>>> +var_has_value() {
>>> + [[ "${!1}" == "${2}" ]]
>>> +}
>>> -- 
>>> 2.39.2
  

Patch

diff --git a/tests/lib.sh b/tests/lib.sh
new file mode 100644
index 000000000..7749d5158
--- /dev/null
+++ b/tests/lib.sh
@@ -0,0 +1,20 @@ 
+#!/usr/bin/bash
+
+LIB_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
+
+. ${LIB_DIR}/lib_color.sh
+
+test_that() {
+
+	if ! "$@" ; then
+		echo -e "${CLR_RED_BG} Test failed: ${*} ${CLR_RESET}"
+		return 1
+	else
+		echo -e "${CLR_GREEN_BG} Test succeded: ${*} ${CLR_RESET}"
+		return 0
+	fi
+}
+
+var_has_value() {
+	[[ "${!1}" == "${2}" ]]
+}