From 4e1607c8f60a5afbab1f5d4646ccf4079595aa83 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 8 Jul 2020 14:53:13 -0400 Subject: [PATCH 1/5] Enable autoflush on STDOUT and STDERR for tests This prevents STDOUT lines from appearing out of order relative to STDERR lines in the test log, which makes it much easier to understand what's happening. Devel::Autoflush is from https://metacpan.org/pod/Devel::Autoflush and is licensed under the terms of the Apache 2.0 license. --- Makefile.am | 4 +- t/lib/Devel/Autoflush.pm | 106 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 t/lib/Devel/Autoflush.pm diff --git a/Makefile.am b/Makefile.am index 6bf6498..a1cef03 100644 --- a/Makefile.am +++ b/Makefile.am @@ -67,7 +67,8 @@ PL_LOG_DRIVER = $(LOG_DRIVER) PL_LOG_COMPILER = $(PERL) AM_PL_LOG_FLAGS = -Mstrict -w \ -I'$(abs_top_builddir)' \ - -I'$(abs_top_srcdir)'/t/lib + -I'$(abs_top_srcdir)'/t/lib \ + -MDevel::Autoflush handwritten_tests = \ t/geturl_connectivity.pl \ t/geturl_ssl.pl \ @@ -77,6 +78,7 @@ generated_tests = \ t/version.pl TESTS = $(handwritten_tests) $(generated_tests) EXTRA_DIST += $(handwritten_tests) \ + t/lib/Devel/Autoflush.pm \ t/lib/Test/Builder.pm \ t/lib/Test/Builder/Formatter.pm \ t/lib/Test/Builder/IO/Scalar.pm \ diff --git a/t/lib/Devel/Autoflush.pm b/t/lib/Devel/Autoflush.pm new file mode 100644 index 0000000..855db0b --- /dev/null +++ b/t/lib/Devel/Autoflush.pm @@ -0,0 +1,106 @@ +package Devel::Autoflush; +# ABSTRACT: Set autoflush from the command line +our $VERSION = '0.06'; # VERSION + +my $kwalitee_nocritic = << 'END'; +# can't use strict as older stricts load Carp and we can't allow side effects +use strict; +END + +my $old = select STDOUT; +$|++; +select STDERR; +$|++; +select $old; + +1; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Devel::Autoflush - Set autoflush from the command line + +=head1 VERSION + +version 0.06 + +=head1 SYNOPSIS + + perl -MDevel::Autoflush Makefile.PL + +=head1 DESCRIPTION + +This module is a hack to set autoflush for STDOUT and STDERR from the command +line or from C for code that needs it but doesn't have it. + +This often happens when prompting: + + # guess.pl + print "Guess a number: "; + my $n = ; + +As long as the output is going to a terminal, the prompt is flushed when STDIN +is read. However, if the output is being piped, the print statement will +not automatically be flushed, no prompt will be seen and the program will +silently appear to hang while waiting for input. This might happen with 'tee': + + $ perl guess.pl | tee capture.out + +Use Devel::Autoflush to work around this: + + $ perl -MDevel::Autoflush guess.pl | tee capture.out + +Or set it in C: + + $ export PERL5OPT=-MDevel::Autoflush + $ perl guess.pl | tee capture.out + += SEE ALSO + +=over 4 + +=item * + +L -- same idea but STDOUT only and + +only available as part of the full CPANPLUS distribution + +=back + +=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan + +=head1 SUPPORT + +=head2 Bugs / Feature Requests + +Please report any bugs or feature requests through the issue tracker +at L. +You will be notified automatically of any progress on your issue. + +=head2 Source Code + +This is open source software. The code repository is available for +public review and contribution under the terms of the license. + +L + + git clone https://github.com/dagolden/Devel-Autoflush.git + +=head1 AUTHOR + +David Golden + +=head1 COPYRIGHT AND LICENSE + +This software is Copyright (c) 2014 by David Golden. + +This is free software, licensed under: + + The Apache License, Version 2.0, January 2004 + +=cut From 192a00ad64e9d5dddaa7821e82004b0036c3941c Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 8 Jul 2020 15:36:12 -0400 Subject: [PATCH 2/5] Don't match against locale-dependent `$!` --- t/write_cache.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/write_cache.pl b/t/write_cache.pl index de3b6f2..70aab5e 100644 --- a/t/write_cache.pl +++ b/t/write_cache.pl @@ -31,8 +31,8 @@ sub tc { my @test_cases = ( tc("create cache file", catfile($dir, 'a', 'b', 'cachefile'), undef), tc("overwrite cache file", catfile($dir, 'a', 'b', 'cachefile'), undef), - tc("bad directory", catfile($dir, 'a', 'b', 'cachefile', 'bad'), qr/File exists/), - tc("read-only directory", catfile($ro_dir, 'cachefile'), qr/Permission denied/), + tc("bad directory", catfile($dir, 'a', 'b', 'cachefile', 'bad'), qr/Failed to create/i), + tc("read-only directory", catfile($ro_dir, 'cachefile'), qr/Failed to create/i), ); for my $tc (@test_cases) { From 173865bd7595aff40310ca97301c3015730835b0 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 8 Jul 2020 15:36:45 -0400 Subject: [PATCH 3/5] Don't test writing to a read-only directory root can always write to a read-only directory so the test was failing on the CI runners (which run as root). --- t/write_cache.pl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/t/write_cache.pl b/t/write_cache.pl index 70aab5e..b92fd15 100644 --- a/t/write_cache.pl +++ b/t/write_cache.pl @@ -15,10 +15,6 @@ $module->redefine('warning', sub { my $tmpdir = File::Temp->newdir(); my $dir = $tmpdir->dirname(); diag("temporary directory: $dir"); -my $ro_tmpdir = File::Temp->newdir(); -my $ro_dir = $ro_tmpdir->dirname(); -chmod(0500, $ro_dir) or BAIL_OUT($!); -diag("temporary read-only directory: $ro_dir"); sub tc { return { @@ -32,7 +28,7 @@ my @test_cases = ( tc("create cache file", catfile($dir, 'a', 'b', 'cachefile'), undef), tc("overwrite cache file", catfile($dir, 'a', 'b', 'cachefile'), undef), tc("bad directory", catfile($dir, 'a', 'b', 'cachefile', 'bad'), qr/Failed to create/i), - tc("read-only directory", catfile($ro_dir, 'cachefile'), qr/Failed to create/i), + tc("bad file", catfile($dir, 'a', 'b'), qr/Failed to create/i), ); for my $tc (@test_cases) { From bba1ecfe2712f04fef0f676f16ad410bf617c024 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 8 Jul 2020 15:49:31 -0400 Subject: [PATCH 4/5] Switch from `redefine` to `mock` --- t/write_cache.pl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/t/write_cache.pl b/t/write_cache.pl index b92fd15..94e959b 100644 --- a/t/write_cache.pl +++ b/t/write_cache.pl @@ -8,7 +8,9 @@ eval { require 'ddclient'; } or BAIL_OUT($@); my $warning; my $module = Test::MockModule->new('ddclient'); -$module->redefine('warning', sub { +# Note: 'mock' is used instead of 'redefine' because 'redefine' is not available in the versions of +# Test::MockModule distributed with old Debian and Ubuntu releases. +$module->mock('warning', sub { BAIL_OUT("warning already logged") if defined($warning); $warning = sprintf(shift, @_); }); From d3097324806f5d3834b803e16dc5916fe29a434d Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 8 Jul 2020 13:36:06 -0400 Subject: [PATCH 5/5] Add missing test dependency on Test::MockModule This should have been done in 6ae5fe62d7a5d60c4a875258ea422d59a744cacc but I forgot. --- .github/workflows/ci.yml | 2 ++ configure.ac | 1 + 2 files changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ca6d200..4ad62c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,7 @@ jobs: libio-socket-inet6-perl \ libio-socket-ip-perl \ libplack-perl \ + libtest-mockmodule-perl \ libtest-tcp-perl \ libtest-warnings-perl \ liburi-perl \ @@ -113,6 +114,7 @@ jobs: perl-HTTP-Daemon-SSL \ perl-IO-Socket-INET6 \ perl-Plack \ + perl-Test-MockModule \ perl-Test-TCP \ perl-Test-Warnings \ ; diff --git a/configure.ac b/configure.ac index 33b88fd..e0dc09a 100644 --- a/configure.ac +++ b/configure.ac @@ -68,6 +68,7 @@ m4_foreach_w([_m], [ HTTP::Response IO::Socket::IP Scalar::Util + Test::MockModule Test::TCP Test::Warnings Time::HiRes