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

Hello there,

I would like to include some automated tests for a distribution that I release to CPAN. One of the modules included uses IPC to execute a specific program, expecting a defined output from it.

Having this program available everywhere is really hard because actually it connects to a server, execute commands and return the output generated. I thought creating a "mock" of this same program that expects an input and just print some output. That is easy to do.

What I would like to know is how is the best way to include that in the distribution to release to CPAN. The mock that I created is a very simple C program that would need to be compiled before running the tests using a Makefile already available. Considering that I'm using ExtUtils::MakeMaker to generate the distribution Makefile, how can I do that?

More important than that, is this the best way to do automated IPC testing for CPAN?

Thanks,

Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

Replies are listed 'Best First'.
Re: CPAN automated tests for IPC
by Eliya (Vicar) on Mar 16, 2012 at 17:46 UTC
    The mock that I created is a very simple C program that would need to be compiled before running the tests using a Makefile already available.

    There might be an easier way... but if all else fails, you can always add an extra target (which builds you C program) to the generated Makefile and add it as a prerequisite to the test target, so that your program would be built right before testing.

    Something like this (assuming you have your C stuff in a subdirectory mymock, and that mymock/Makefile would build the target executable mymock/mymock):

    ### Makefile.PL WriteMakefile( ... ); package MY; # override / modify test target sub test { my $t = shift->SUPER::test(@_); $t =~ s|^test ::\K| mymock/mymock |m; # inserts your prereq return $t; } # add your own target sub postamble { return <<'MAKE_MOCK'; mymock/mymock : mymock/Makefile cd mymock && $(MAKE) all MAKE_MOCK }

    (untested)

    See also Overriding MakeMaker Methods.

    Portability might well be an issue here, so you might ultimately be better off implementing your mock program in pure Perl... (if that's possible)

Re: CPAN automated tests for IPC
by BrowserUk (Patriarch) on Mar 16, 2012 at 19:05 UTC
    I thought creating a "mock" of this same program that expects an input and just print some output....

    The mock that I created is a very simple C program that would need to be compiled before running the tests using a Makefile already available.

    If all the mock program needs to do is accept some input and produce some output, why woudl you use C to write that?

    I use Perl one-liners for similar purposes al the time:

    my $mockedReturn = `perl -E"say 'fred' if $ARGV[0] eq 'bill'`;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      The C code is interactive, so my mock test should "interact" with the program, giving commands to it and expecting output. Again, I'm not sure if using another Perl script would have any issue (only thing that comes to my mind is that I need print buffering).

      Here is the C code anyway:

      I'm sure I could write something better in pure Perl, but I'm confident that it wouldn't compromisse the testing results.

      Alceu Rodrigues de Freitas Junior
      ---------------------------------
      "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

        This is a pretty literal translation of your C file, that should perform exactly the same way (barring typos):

        #! perl -slw use strict; use constant { COMP_FILE => "list_comp.txt", COMP_TYPES_FILE => "list_comp_types.txt", }; while( 1 ) { printf "svrmgr> "; chomp( my $command = <STDIN> ); if( $command eq COMP_FILE ) { open my $in, '<', COMP_FILE or printf("Error reading %s\n", COMP_FILE ) and next; print while <$in>; } elsif( $command eq COMP_TYPES_FILE ) { open my $in, '<', COMP_TYPES_FILE or printf("Error reading %s\n", COMP_TYPES_FILE ) and next; print while <$in>; } else if( $command eq 'EXIT' ) { print "Disconnecting...\n"; last; } else { print "Invalid command\n"; } } exit 0;

        Easier to maintain?

        (Also, just an aside. Are the blank lines in your C code an artifact of posting, or your preferred style?)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: CPAN automated tests for IPC
by Anonymous Monk on Mar 16, 2012 at 21:21 UTC