in reply to STDERR in Test Results

G'day Bod,

The way I normally deal with this is along these lines:

my $expected_error = 1; my $got_error = 0; eval { # code expected to die with error here 1; } or do { $got_error = 1; }; is $got_error, $expected_error, 'test name';

— Ken

Replies are listed 'Best First'.
Re^2: STDERR in Test Results
by Bod (Parson) on Jun 19, 2023 at 15:40 UTC

    Thanks kcott but that still generates the rogue output to STDERR

    I've tried this:

    my $got_warn = 0; eval { $webhook_fail->process(); } or do { $got_warn = 1; }; ok( $got_warn, "Signature warning generated"); ok( !$webhook_fail->success, "Signature error" );
    Both tests pass but I still get the output to STDERR when the process method is called.

    I guess I'm just looking for a way to suppress the output...

      "... expected error ... Webhook Error ... Signature error ... an error displayed ..."

      Originally, you repeatedly referred to an error; now you seem to have changed that to a warning. My solution traps errors, which you asked for; it doesn't trap warnings.

      $ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +' $ perle ' my $expected_error = 1; my $got_error = 0; eval { my @rgb = qw{#ff0000 #00ff00 #0000ff}; open my $fh, "<", "nonexistent_file"; 1; } or do { $got_error = 1; }; say "\$expected_error[$expected_error] \$got_error[$got_error]"; ' Possible attempt to put comments in qw() list at -e line 6. $expected_error[1] $got_error[1]

      Perhaps "warnings: Fatal Warnings" would help you.

      — Ken

        Originally, you repeatedly referred to an error; now you seem to have changed that to a warning

        Sorry for the confusion.

        My module throws warnings and errors - it generates both by printing to STDERR

        The question wasn't so much about dealing with warnings and errors but more about suppressing the warnings during the output from the tests. Although, this is one of those situations where I didn't really know exactly what I wanted to know until I had asked the question and got lots of helpful comments. The original question asked about suppressing this line

        Stripe Webhook Error: Invalid Stripe Signature
        Hence why I referred to errors

      Update: Problem already found in Re^4: STDERR in Test Results.


      Both tests pass but I still get the output to STDERR when the process method is called. I guess I'm just looking for a way to suppress the output...

      Or perhaps for a place where someone prints to STDERR or warns.

      Looking at B::S::W 1.11 at CPAN, B::S::W::process() calls $self->_error() with the error message shown. And now look at that method:

      sub _error { my ($self, $message) = @_; $self->{'error'} = $message; if (defined &{$self->{'error'}}) { &{$self->{'error'}}($message); } else { STDERR->print("Stripe Webhook Error: $message\n"); } }

      That's why you shall use warn and die instead of printing to STDERR. sub _warning has the same problem.


      Oh, by the way: eval catches errors, not warnings, so your variable would be better named $got_error instead of $got_warn.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)