File: sendmail.t

package info (click to toggle)
libemail-sender-perl 1.300016-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 416 kB
  • ctags: 107
  • sloc: perl: 2,070; makefile: 2
file content (144 lines) | stat: -rw-r--r-- 3,351 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use Test::More tests => 5;
use strict;
$^W = 1;

use Capture::Tiny 0.08 'capture';
use Cwd;
use Config;
use Email::Sender::Transport::Sendmail;
use File::Spec;

my $IS_WIN32 = $^O eq 'MSWin32';

my $email = <<'EOF';
To:   Casey West <casey@example.com>
From: Casey West <casey@example.net>
Subject: This should never show up in my inbox

blah blah blah
EOF

my @to_unlink;
END { unlink @to_unlink }

sub get_bin_name {
  return 'sendmail.bat' if $IS_WIN32;

  my ($bin_path) = @_;
  my $input_file = File::Spec->catfile( $bin_path, 'sendmail' );
  my $fn = "sendmail-$$-$^T.tmp";
  my $output_file = File::Spec->catfile( $bin_path, $fn );

  open my $in_fh,  '<', $input_file  or die "can't read input sendmail: $!";
  open my $out_fh, '>', $output_file or die "can't write temp sendmail: $!";

  while (<$in_fh>) {
    s/\A#!perl$/#!$^X/;
    print $out_fh $_;
  }

  push @to_unlink, $output_file;

  return $fn;
}

my $bin_path = File::Spec->rel2abs('util');
my $bin_name = get_bin_name($bin_path);
my $sendmail_bin = File::Spec->catfile( $bin_path, $bin_name );
local $ENV{PATH} = join( $Config{path_sep}, $bin_path, $ENV{PATH});

SKIP:
{
  chmod 0755, $sendmail_bin;
  skip "Cannot run unless '$sendmail_bin' is executable", 1
    unless -x $sendmail_bin;

  my $path = eval { 
    Email::Sender::Transport::Sendmail->_find_sendmail($bin_name) 
  };
  is( $path, $sendmail_bin, "found (fake) sendmail at '$sendmail_bin'" );
}

{
  my $sender = Email::Sender::Transport::Sendmail->new({
    sendmail => File::Spec->catfile(qw/. util not-executable/)
  });

  my $error;
  capture { # hide errors from cmd.exe on Win32
    eval {
      no warnings;
      $sender->send(
        $email,
        {
          to   => [ 'devnull@example.com' ],
          from => 'devnull@example.biz',
        }
      );
    };
    $error = $@;
  };

  my $error_re = $IS_WIN32 ? qr/closing pipe/ : qr/open pipe/;
  like(
    $error->message,
    $error_re,
    'error message says what we expect',
  );
}

my $has_FileTemp = eval { require File::Temp; };

SKIP:
{
  skip 'Cannot run this test unless current perl is -x', 1 unless -x $^X;

  my $sender = Email::Sender::Transport::Sendmail->new({
    sendmail => $sendmail_bin
  });

  my $return = $sender->send(
    $email,
    {
      to   => [ 'devnull@example.com' ],
      from => 'devnull@example.biz',
    }
  );

  ok( $return, 'send() succeeded with executable $SENDMAIL' );
}

SKIP:
{
  skip 'Cannot run this test unless current perl is -x', 2 unless -x $^X;

  skip 'Cannot run this test without File::Temp', 2 unless $has_FileTemp;
  my $tempdir = File::Temp::tempdir(CLEANUP => 1);
  my $logfile  = File::Spec->catfile($tempdir, 'sendmail.log');

  local $ENV{EMAIL_SENDER_TRANSPORT_SENDMAIL_TEST_LOGDIR} = $tempdir;
  my $sender = Email::Sender::Transport::Sendmail->new({
    sendmail => $sendmail_bin
  });

  my $return = eval {
    $sender->send(
      $email,
      {
        to   => [ 'devnull@example.com' ],
        from => 'devnull@example.biz',
      }
    );
  };

  ok( $return, 'send() succeeded with executable sendmail in path' );

  if (-f $logfile) {
    open my $fh, '<', $logfile
        or die "Cannot read $logfile: $!";
    my $log = join '', <$fh>;
    like($log, qr/From: Casey West/, 'log contains From header');
  } else {
    fail('cannot check sendmail log contents');
  }
}