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

View File

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

View File

@ -79,7 +79,11 @@ sub __clear_cache {
sub __default { sub __default {
is($config->default('me'), hostname, "default, my hostname"); is($config->default('me'), hostname, "default, my hostname");
is($config->default('timeout'), 1200, "default timeout is 1200"); 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 { sub __get_qmail {