Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Testing without linking to legacy codebase

by Anonymous Monk
on Jun 07, 2016 at 13:10 UTC ( [id://1165070]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

My situation: My business has a legacy codebase of hundreds of thousands of lines of poorly maintained Perl. We're writing new (Perl) code to take the place of this, bit by bit.

In one of my bits, I need to call a function from the original codebase; it's just too complicated to rewrite into the new system. I know that on all of our platforms--test, staging, production--I'll have access to the original, so I can say "use lib /path/to/orig; my $foo = Old::Codebase::complicated_function();". However, there's no way to do this in a test environment, because the old codebase is highly interdependent, and I can't import the entire thing into git just to have it around for the test.

How do I get around this? I don't need to test the old function; I can assume that it works. But I need to test the rest of the module, and it will fail if it can't find /path/to/orig. (That is, I can't just SKIP the test; the function will need to be called whenever the module runs.) I assume this is a regular problem, but haven't been able to find an answer.

Replies are listed 'Best First'.
Re: Testing without linking to legacy codebase
by stevieb (Canon) on Jun 07, 2016 at 13:36 UTC

    You can mock out the functions that are legacy (disclaimer: I'm the author of Mock::Sub. There are several other similar distributions. Unfortunately, it doesn't look like I included a SEE ALSO section in this cut, but searching meta for "mock" should list a few).

    Code untested. Mock::Sub can also return values, or perform side effects. Peruse the docs.

    use warnings; use strict; use Mock::Sub; use Test::More; my $mock = Mock::Sub->new; my $complicated_function = $mock->mock('Old::Codebase::complicated_function'); { my $new_obj = New::Codebase->new; $new_obj->do_something_with_old_code(); is ( $complicated_function->called, 1, "legacy method called ok" ); is ( $complicated_function->called_count, 3, "legacy method called proper num times" ); my @called_with = $complicated_function->called_with; is ( $called_with[0], 'param', "legacy method called with proper param" ); $complicated_function->reset; } done_testing();
Re: Testing without linking to legacy codebase
by MidLifeXis (Monsignor) on Jun 07, 2016 at 14:16 UTC

    Agreed with stevieb on mocking. My goto for this is Test::Mock::Guard. Basically you are replacing some method on a class or object with your own.

    my $guard = mock_guard( $original_class, { GetDataFromOldSystem => sub {...}, }, ); $obj = $original_class->new(...); $obj->SomeOtherFunctionThatCalls_GetDataFromOldSystem(...);

    Your provided sub would then return some pre-baked values instead of going to the original system.

    --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1165070]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found