2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # edit frontend behavior test (t/replace.t for backend)
7 use PublicInbox::TestCommon;
8 use PublicInbox::MID qw(mid_clean);
11 require_mods('DBD::SQLite');
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $inboxdir = "$tmpdir/v2";
14 my $file = 't/data/0001.patch';
15 my $eml = eml_load($file);
16 my $mid = mid_clean($eml->header('Message-ID'));
17 my $ibx = create_inbox 'v2edit', indexlevel => 'basic', version => 2,
18 tmpdir => $inboxdir, sub {
20 $im->add($eml) or BAIL_OUT;
22 my $cfgfile = "$tmpdir/config";
23 local $ENV{PI_CONFIG} = $cfgfile;
24 my ($in, $out, $err, $cmd, $cur, $t);
25 my $git = PublicInbox::Git->new("$ibx->{inboxdir}/git/0.git");
26 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
27 my $ipe = "$^X -w -i -p -e";
30 $in = $out = $err = '';
31 local $ENV{MAIL_EDITOR} = "$ipe 's/boolean prefix/bool pfx/'";
32 $cmd = [ '-edit', "-F$file", $inboxdir ];
33 ok(run_script($cmd, undef, $opt), "$t edit OK");
34 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
35 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
36 like($out, qr/[a-f0-9]{40,}/, "$t shows commit on success");
39 $t = '-m MESSAGE_ID'; {
40 $in = $out = $err = '';
41 local $ENV{MAIL_EDITOR} = "$ipe 's/bool pfx/boolean prefix/'";
42 $cmd = [ '-edit', "-m$mid", $inboxdir ];
43 ok(run_script($cmd, undef, $opt), "$t edit OK");
44 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
45 like($cur->header('Subject'), qr/boolean prefix/, "$t message edited");
46 like($out, qr/[a-f0-9]{40,}/, "$t shows commit on success");
49 $t = 'no-op -m MESSAGE_ID'; {
50 $in = $out = $err = '';
51 my $before = $git->qx(qw(rev-parse HEAD));
52 local $ENV{MAIL_EDITOR} = "$ipe 's/bool pfx/boolean prefix/'";
53 $cmd = [ '-edit', "-m$mid", $inboxdir ];
54 ok(run_script($cmd, undef, $opt), "$t succeeds");
56 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
57 is_deeply($cur, $prev, "$t makes no change");
58 like($cur->header('Subject'), qr/boolean prefix/,
59 "$t does not change message");
60 like($out, qr/NONE/, 'noop shows NONE');
61 my $after = $git->qx(qw(rev-parse HEAD));
62 is($after, $before, 'git head unchanged');
65 $t = 'no-op -m MESSAGE_ID w/Status: header'; { # because mutt does it
66 $in = $out = $err = '';
67 my $before = $git->qx(qw(rev-parse HEAD));
68 local $ENV{MAIL_EDITOR} = "$ipe 's/^Subject:.*/Status: RO\\n\$&/'";
69 $cmd = [ '-edit', "-m$mid", $inboxdir ];
70 ok(run_script($cmd, undef, $opt), "$t succeeds");
72 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
73 is_deeply($cur, $prev, "$t makes no change");
74 like($cur->header('Subject'), qr/boolean prefix/,
75 "$t does not change message");
76 is($cur->header('Status'), undef, 'Status header not added');
77 like($out, qr/NONE/, 'noop shows NONE');
78 my $after = $git->qx(qw(rev-parse HEAD));
79 is($after, $before, 'git head unchanged');
82 $t = '-m MESSAGE_ID can change Received: headers'; {
83 $in = $out = $err = '';
84 local $ENV{MAIL_EDITOR} = "$ipe 's/^Subject:.*/Received: x\\n\$&/'";
85 $cmd = [ '-edit', "-m$mid", $inboxdir ];
86 ok(run_script($cmd, undef, $opt), "$t succeeds");
87 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
88 like($cur->header('Subject'), qr/boolean prefix/,
89 "$t does not change Subject");
90 is($cur->header('Received'), 'x', 'added Received header');
94 $in = $out = $err = '';
95 local $ENV{MAIL_EDITOR} = "$ipe 's/boolean/FAIL/'";
96 $cmd = [ '-edit', "-m$mid-miss", $inboxdir ];
97 ok(!run_script($cmd, undef, $opt), "$t fails on invalid MID");
98 like($err, qr/No message found/, "$t shows error");
101 $t = 'non-interactive editor failure'; {
102 $in = $out = $err = '';
103 local $ENV{MAIL_EDITOR} = "$ipe 'END { exit 1 }'";
104 $cmd = [ '-edit', "-m$mid", $inboxdir ];
105 ok(!run_script($cmd, undef, $opt), "$t detected");
106 like($err, qr/END \{ exit 1 \}' failed:/, "$t shows error");
109 $t = 'mailEditor set in config'; {
110 $in = $out = $err = '';
111 my $rc = xsys(qw(git config), "--file=$cfgfile",
112 'publicinbox.maileditor',
113 "$ipe 's/boolean prefix/bool pfx/'");
114 is($rc, 0, 'set publicinbox.mailEditor');
115 local $ENV{MAIL_EDITOR};
116 delete $ENV{MAIL_EDITOR};
117 local $ENV{GIT_EDITOR} = 'echo should not run';
118 $cmd = [ '-edit', "-m$mid", $inboxdir ];
119 ok(run_script($cmd, undef, $opt), "$t edited message");
120 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
121 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
122 unlike($out, qr/should not run/, 'did not run GIT_EDITOR');
125 $t = '--raw and mbox escaping'; {
126 $in = $out = $err = '';
127 local $ENV{MAIL_EDITOR} = "$ipe 's/^\$/\\nFrom not mbox\\n/'";
128 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
129 ok(run_script($cmd, undef, $opt), "$t succeeds");
130 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
131 like($cur->body, qr/^From not mbox/sm, 'put "From " line into body');
133 local $ENV{MAIL_EDITOR} = "$ipe 's/^>From not/\$& an/'";
134 $cmd = [ '-edit', "-m$mid", $inboxdir ];
135 ok(run_script($cmd, undef, $opt), "$t succeeds with mbox escaping");
136 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
137 like($cur->body, qr/^From not an mbox/sm,
138 'changed "From " line unescaped');
140 local $ENV{MAIL_EDITOR} = "$ipe 's/^From not an mbox\\n//s'";
141 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
142 ok(run_script($cmd, undef, $opt), "$t succeeds again");
143 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
144 unlike($cur->body, qr/^From not an mbox/sm, "$t restored body");
147 $t = 'reuse Message-ID'; {
149 local $SIG{__WARN__} = sub { push @warn, @_ };
150 my $im = $ibx->importer(0);
151 ok($im->add($eml), "$t and re-add");
153 like($warn[0], qr/reused for mismatched content/, "$t got warning");
156 $t = 'edit ambiguous Message-ID with -m'; {
157 $in = $out = $err = '';
158 local $ENV{MAIL_EDITOR} = "$ipe 's/bool pfx/boolean prefix/'";
159 $cmd = [ '-edit', "-m$mid", $inboxdir ];
160 ok(!run_script($cmd, undef, $opt), "$t fails w/o --force");
161 like($err, qr/Multiple messages with different content found matching/,
163 like($err, qr/GIT_DIR=.*git show/is, "$t shows git commands");
166 $t .= ' and --force'; {
167 $in = $out = $err = '';
168 local $ENV{MAIL_EDITOR} = "$ipe 's/^Subject:.*/Subject:x/i'";
169 $cmd = [ '-edit', "-m$mid", '--force', $inboxdir ];
170 ok(run_script($cmd, undef, $opt), "$t succeeds");
171 like($err, qr/Will edit all of them/, "$t notes all will be edited");
172 my @dump = $git->qx(qw(cat-file --batch --batch-all-objects));
174 is_deeply([grep(/^Subject:/i, @dump)], [qw(Subject:x Subject:x)],
175 "$t edited both messages");