开发者

How to test module functions which use hardcoded configuration file?

I want to make some tests on my modules.

Unfortunately, some functions in these modules use hardcoded configurations files.

package My::Module;

use strict;
use warnings;
use Readonly;

Readonly my $CONF_FILE => '/my/conf_file.xml';

=head1 FUNCTIONS

=head2 I开发者_高级运维nfo($appli)

Returns Application Information

=cut

sub Info
{
  my $appli = shift;

  my $conf = MyXML::Read($CONF_FILE);
  foreach my $a (ARRAY($conf->{application}))
  {
    return ($a) if ($a->{name} eq $appli);
  }

  return (undef);
}
[some others functions that use this config file...]

The solution that came to my mind is to create a new function in each module that will change this default config file when I need it.

Then I will use that function in my tests...

Do you have any other (better ?) ideas ?


Well, the proper thing for me to tell you would be "don't use hard coded paths". It'll come back and bite you at some point in the future, I promise.

But... assuming you're resolved to using them, there are a number of ways to allow an override. You're right you could add a function that would let you change it, or you could use an environmental variable:

  Readonly my $CONF_FILE => $ENV{'MY_CONF_FILE'} || '/foo/bar';

But the right thing to do is still to allow for other items to be passed in properly if you have a choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜