diff mbox series

[1/3] kunit: Improve format of the NOT_ERR_OR_NULL assertion

Message ID 20240821191412.2031-2-michal.wajdeczko@intel.com (mailing list archive)
State New
Delegated to: Brendan Higgins
Headers show
Series kunit: Improve format of some assertion messages | expand

Commit Message

Michal Wajdeczko Aug. 21, 2024, 7:14 p.m. UTC
Diagnostic message for failed KUNIT_ASSERT|EXPECT_NOT_ERR_OR_NULL
shows only raw error code, like for this example:

  void *myptr = ERR_PTR(-ENOMEM);
  KUNIT_EXPECT_NOT_ERR_OR_NULL(test, myptr);

we will get:

  [ ] Expected myptr is not error, but is: -12

but we can improve it by using more friendly error pointer format:

  [ ] Expected myptr is not error, but is -ENOMEM

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
Cc: David Gow <davidgow@google.com>
Cc: Rae Moar <rmoar@google.com>
---
 lib/kunit/assert.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

David Gow Aug. 22, 2024, 6:37 a.m. UTC | #1
On Thu, 22 Aug 2024 at 03:15, Michal Wajdeczko
<michal.wajdeczko@intel.com> wrote:
>
> Diagnostic message for failed KUNIT_ASSERT|EXPECT_NOT_ERR_OR_NULL
> shows only raw error code, like for this example:
>
>   void *myptr = ERR_PTR(-ENOMEM);
>   KUNIT_EXPECT_NOT_ERR_OR_NULL(test, myptr);
>
> we will get:
>
>   [ ] Expected myptr is not error, but is: -12
>
> but we can improve it by using more friendly error pointer format:
>
>   [ ] Expected myptr is not error, but is -ENOMEM
>
> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> ---
> Cc: David Gow <davidgow@google.com>
> Cc: Rae Moar <rmoar@google.com>
> ---

I like this, though wouldn't mind also adding the numerical value
afterwards. Up to you, though, I can live without.

It does break the 'kunit_test_ptr_not_err_assert_format' test in
lib/kunit/assert_test.c: it should be fairly simple to update.

With the test fixed, this is:
Reviewed-by: David Gow <davidgow@google.com>

-- David



-- David

>  lib/kunit/assert.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
> index 867aa5c4bccf..6e4333d0c6a0 100644
> --- a/lib/kunit/assert.c
> +++ b/lib/kunit/assert.c
> @@ -82,9 +82,9 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
>                                   ptr_assert->text);
>         } else if (IS_ERR(ptr_assert->value)) {
>                 string_stream_add(stream,
> -                                 KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
> +                                 KUNIT_SUBTEST_INDENT "Expected %s is not error, but is %pe\n",
>                                   ptr_assert->text,
> -                                 PTR_ERR(ptr_assert->value));
> +                                 ptr_assert->value);
>         }
>         kunit_assert_print_msg(message, stream);
>  }
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
index 867aa5c4bccf..6e4333d0c6a0 100644
--- a/lib/kunit/assert.c
+++ b/lib/kunit/assert.c
@@ -82,9 +82,9 @@  void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
 				  ptr_assert->text);
 	} else if (IS_ERR(ptr_assert->value)) {
 		string_stream_add(stream,
-				  KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
+				  KUNIT_SUBTEST_INDENT "Expected %s is not error, but is %pe\n",
 				  ptr_assert->text,
-				  PTR_ERR(ptr_assert->value));
+				  ptr_assert->value);
 	}
 	kunit_assert_print_msg(message, stream);
 }