Is there a way to run Perl code during cpan install after the tests have passed?

I have my own cpan mirror which has some input modules for our internal use only. I would like to run some perl code while installing cpan. I am currently pasting the code directly into the Makefile.PL, but the problem is that it runs before "make test". I would like the code to run only after the distribution tests have passed.

One option would be to enter the code at the end of the last test file, but how do you know if all tests in all test files passed? Test :: More-> builder-> is_passing seems to only know about the current test file.

+3


source to share


2 answers


One parameter (if you are using ExtUtils::MakeMaker

) is to define a functionMY::test

in Makefile.PL

before (conditionally) add an additional task. Something like

 # Makefile.PL
 ...
 sub MY::test {
    my $text = shift->SUPER::test(@_);  # original 'test' action for this system
    if ($ENV{BOOM}) {
        $text =~ s/^(test ::.*)$/$1 GO-BOOM/m;
        $text .= q~
GO-BOOM ::
    $(PERL) -E 'say "BOOM!"'
~;
    # note: important to use tab, not spaces, before "$(PERL) ..."
    }
    return $text;
 }

      

The Makefile

action test

will now look like

test :: $(TEST_TYPE) subdirs-test GO-BOOM
...
GO-BOOM ::
        $(PERL) -E 'say "BOOM!"'

      

or



test :: $(TEST_TYPE) subdirs-test

      

depending on what happened c Makefile.PL

. (These are typical linux specifications in a Makefile. Your results may vary).

If you defined an environment variable BOOM

at startup Makefile.PL

, and if the previous actions associated with make test

are successful, then the action will test

also take the action GO-BOOM

. But if the test fails, it GO-BOOM

won't work.

$ BOOM=1 perl Makefile.PL
$ make test TEST_FILES=t/test-that-will-pass.t
PERL_DL_NONLAZY=1 "/usr/bin/perl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/test-that-will-pass.t
t/test-that-will-pass .. ok    
All tests successful.
Files=1, Tests=16,  4 wallclock secs ( 0.01 usr  0.01 sys +  0.06 cusr  0.01 csys =  0.09 CPU)
Result: PASS
"/usr/bin/perl" -E 'say "BOOM!"'
BOOM!

$ make test TEST_FILES=t/test-that-will-fail.t
PERL_DL_NONLAZY=1 "/usr/bin/perl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/test-that-will-fail.t
t/test-that-will-fail.t .. 1/? 
#   Failed test at t/test-that-will-fail.t line 2.
# Looks like you failed 1 test of 1.
t/test-that-will-fail.t .. Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests 

Test Summary Report
-------------------
t/test-that-will-fail.t (Wstat: 256 Tests: 1 Failed: 1)
  Failed test:  1
  Non-zero exit status: 1
Files=1, Tests=1,  0 wallclock secs ( 0.01 usr  0.01 sys +  0.03 cusr  0.00 csys =  0.05 CPU)
Result: FAIL
Failed 1/1 test programs. 1/1 subtests failed.
Makefile:938: recipe for target 'test_dynamic' failed
make: *** [test_dynamic] Error 1

      

(no arrow)

+2


source


Personally, I would use meta distribution. Let's say task :: MyCorp. This will then provide all the modules you need and have the added benefit of going through a test phase that won't work until all of the prerequisites have been successfully tested.

In fact, I often have a meta module like common :: sense where I can automatically load strict, warnings to the level I want, and import any other functionality I want everywhere (entry and translation are two large). In many cases it would be a convenient place for everyone.



Obviously without knowing how you have everything set up, I cannot be sure if this will work for you. There isn't a one-off solution, so if that's not enough, it might trigger some ideas that will work in your situation.

+3


source







All Articles