updated Config::defaults() to behave as before

and added additional tests for it
This commit is contained in:
Matt Simerson 2014-09-16 09:52:05 -07:00
parent b0c3d715cc
commit a473c2732d
3 changed files with 49 additions and 37 deletions

View File

@ -1,5 +1,6 @@
package Qpsmtpd;
use strict;
#use warnings;
our $VERSION = "0.95";
@ -125,14 +126,14 @@ sub conf {
my $self = shift;
if (!$self->{_config}) {
$self->{_config} = Qpsmtpd::Config->new();
};
}
return $self->{_config};
}
sub config {
my $self = shift;
return $self->conf->config($self, @_);
};
}
sub config_dir {
my $self = shift;
@ -205,15 +206,17 @@ sub _load_plugin {
for my $dir (@plugin_dirs) {
next if !-e "$dir/$plugin";
Qpsmtpd::Plugin->compile($plugin_name, $package,
"$dir/$plugin", $self->{_test_mode}, $plugin);
"$dir/$plugin", $self->{_test_mode},
$plugin);
if ($safe_line !~ /logging/) {
$self->log(LOGDEBUG, "Loading $safe_line from $dir/$plugin");
};
}
last;
}
if (! defined &{"${package}::plugin_name"}) {
die "Plugin $plugin_name not found in our plugin dirs (", join(', ', @plugin_dirs), ")";
};
if (!defined &{"${package}::plugin_name"}) {
die "Plugin $plugin_name not found in our plugin dirs (",
join(', ', @plugin_dirs), ")";
}
}
my $plug = $package->new();
@ -234,9 +237,10 @@ sub _load_package_plugin {
$eval = $1;
eval $eval; ## no critic (Eval)
die "Failed loading $package - eval $@" if $@;
if ($plugin_line !~ /logging/) {
$self->log(LOGDEBUG, "Loading $package ($plugin_line)");
};
}
my $plug = $package->new();
$plug->_register($self, @$args);
@ -299,7 +303,8 @@ sub run_continuation {
my $tran = $self->transaction;
eval { (@r) = $code->{code}->($self, $tran, @$args); };
if ($@) {
$self->log(LOGCRIT, "FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@);
$self->log(LOGCRIT, "FATAL PLUGIN ERROR [" . $code->{name} . "]: ",
$@);
next;
}
@ -414,8 +419,8 @@ sub spool_dir {
# Make sure the spool dir has appropriate rights
if (((stat $Spool_dir)[2] & oct('07777')) != oct($Spool_perms)) {
$self->log(LOGWARN,
"Permissions on spool_dir $Spool_dir are not $Spool_perms")
};
"Permissions on spool_dir $Spool_dir are not $Spool_perms");
}
return $Spool_dir;
}
@ -433,10 +438,12 @@ sub temp_file {
sub temp_dir {
my ($self, $mask) = @_;
$mask ||= '0700';
my $dirname = $self->temp_file();
-d $dirname
or mkdir($dirname, $mask || '0700')
if (!-d $dirname) {
mkdir($dirname, $mask)
or die "Could not create temporary directory $dirname: $!";
}
return $dirname;
}

View File

@ -17,6 +17,7 @@ our %defaults = (
sub log {
my ($self, $trace, @log) = @_;
# logging methods attempt to read config files, this log() prevents that
# until after logging has fully loaded
return if $trace > LOGWARN;
@ -38,8 +39,8 @@ sub config {
($rc, @config) = $qp->run_hooks_no_respond('user_config', $type, $c);
if (defined $rc && $rc == OK) {
return wantarray ? @config : $config[0];
};
};
}
}
# then run the config hooks
($rc, @config) = $qp->run_hooks_no_respond('config', $c);
@ -50,7 +51,7 @@ sub config {
);
if (defined $rc && $rc == OK) {
return wantarray ? @config : $config[0];
};
}
# then qmail
@config = $self->get_qmail($c, $type);
@ -82,7 +83,7 @@ sub clear_cache {
sub default {
my ($self, $def) = @_;
return if ! $defaults{$def};
return if !exists $defaults{$def};
return wantarray ? ($defaults{$def}) : $defaults{$def};
}

View File

@ -79,7 +79,11 @@ sub __clear_cache {
sub __default {
is($config->default('me'), hostname, "default, my hostname");
is($config->default('timeout'), 1200, "default timeout is 1200");
ok(!$config->default('undefined-test'), "default, undefined");
is($config->default('undefined-test'), undef, "default, undefined");
$Qpsmtpd::Config::defaults{'zero-test'} = 0;
is($config->default('zero-test'), 0, "default, zero");
}
sub __get_qmail {