2 # Copyright (C) 2019-2021 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 };
29 $in = $out = $err = '';
30 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean prefix/bool pfx/'";
31 $cmd = [ '-edit', "-F$file", $inboxdir ];
32 ok(run_script($cmd, undef, $opt), "$t edit OK");
33 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
34 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
35 like($out, qr/[a-f0-9]{40,}/, "$t shows commit on success");
38 $t = '-m MESSAGE_ID'; {
39 $in = $out = $err = '';
40 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
41 $cmd = [ '-edit', "-m$mid", $inboxdir ];
42 ok(run_script($cmd, undef, $opt), "$t edit OK");
43 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
44 like($cur->header('Subject'), qr/boolean prefix/, "$t message edited");
45 like($out, qr/[a-f0-9]{40,}/, "$t shows commit on success");
48 $t = 'no-op -m MESSAGE_ID'; {
49 $in = $out = $err = '';
50 my $before = $git->qx(qw(rev-parse HEAD));
51 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
52 $cmd = [ '-edit', "-m$mid", $inboxdir ];
53 ok(run_script($cmd, undef, $opt), "$t succeeds");
55 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
56 is_deeply($cur, $prev, "$t makes no change");
57 like($cur->header('Subject'), qr/boolean prefix/,
58 "$t does not change message");
59 like($out, qr/NONE/, 'noop shows NONE');
60 my $after = $git->qx(qw(rev-parse HEAD));
61 is($after, $before, 'git head unchanged');
64 $t = 'no-op -m MESSAGE_ID w/Status: header'; { # because mutt does it
65 $in = $out = $err = '';
66 my $before = $git->qx(qw(rev-parse HEAD));
67 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Status: RO\\n\$&/'";
68 $cmd = [ '-edit', "-m$mid", $inboxdir ];
69 ok(run_script($cmd, undef, $opt), "$t succeeds");
71 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
72 is_deeply($cur, $prev, "$t makes no change");
73 like($cur->header('Subject'), qr/boolean prefix/,
74 "$t does not change message");
75 is($cur->header('Status'), undef, 'Status header not added');
76 like($out, qr/NONE/, 'noop shows NONE');
77 my $after = $git->qx(qw(rev-parse HEAD));
78 is($after, $before, 'git head unchanged');
81 $t = '-m MESSAGE_ID can change Received: headers'; {
82 $in = $out = $err = '';
83 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Received: x\\n\$&/'";
84 $cmd = [ '-edit', "-m$mid", $inboxdir ];
85 ok(run_script($cmd, undef, $opt), "$t succeeds");
86 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
87 like($cur->header('Subject'), qr/boolean prefix/,
88 "$t does not change Subject");
89 is($cur->header('Received'), 'x', 'added Received header');
93 $in = $out = $err = '';
94 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean/FAIL/'";
95 $cmd = [ '-edit', "-m$mid-miss", $inboxdir ];
96 ok(!run_script($cmd, undef, $opt), "$t fails on invalid MID");
97 like($err, qr/No message found/, "$t shows error");
100 $t = 'non-interactive editor failure'; {
101 $in = $out = $err = '';
102 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 'END { exit 1 }'";
103 $cmd = [ '-edit', "-m$mid", $inboxdir ];
104 ok(!run_script($cmd, undef, $opt), "$t detected");
105 like($err, qr/END \{ exit 1 \}' failed:/, "$t shows error");
108 $t = 'mailEditor set in config'; {
109 $in = $out = $err = '';
110 my $rc = xsys(qw(git config), "--file=$cfgfile",
111 'publicinbox.maileditor',
112 "$^X -i -p -e 's/boolean prefix/bool pfx/'");
113 is($rc, 0, 'set publicinbox.mailEditor');
114 local $ENV{MAIL_EDITOR};
115 delete $ENV{MAIL_EDITOR};
116 local $ENV{GIT_EDITOR} = 'echo should not run';
117 $cmd = [ '-edit', "-m$mid", $inboxdir ];
118 ok(run_script($cmd, undef, $opt), "$t edited message");
119 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
120 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
121 unlike($out, qr/should not run/, 'did not run GIT_EDITOR');
124 $t = '--raw and mbox escaping'; {
125 $in = $out = $err = '';
126 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^\$/\\nFrom not mbox\\n/'";
127 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
128 ok(run_script($cmd, undef, $opt), "$t succeeds");
129 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
130 like($cur->body, qr/^From not mbox/sm, 'put "From " line into body');
132 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^>From not/\$& an/'";
133 $cmd = [ '-edit', "-m$mid", $inboxdir ];
134 ok(run_script($cmd, undef, $opt), "$t succeeds with mbox escaping");
135 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
136 like($cur->body, qr/^From not an mbox/sm,
137 'changed "From " line unescaped');
139 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^From not an mbox\\n//s'";
140 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
141 ok(run_script($cmd, undef, $opt), "$t succeeds again");
142 $cur = PublicInbox::Eml->new($ibx->msg_by_mid($mid));
143 unlike($cur->body, qr/^From not an mbox/sm, "$t restored body");
146 $t = 'reuse Message-ID'; {
148 local $SIG{__WARN__} = sub { push @warn, @_ };
149 my $im = $ibx->importer(0);
150 ok($im->add($eml), "$t and re-add");
152 like($warn[0], qr/reused for mismatched content/, "$t got warning");
155 $t = 'edit ambiguous Message-ID with -m'; {
156 $in = $out = $err = '';
157 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
158 $cmd = [ '-edit', "-m$mid", $inboxdir ];
159 ok(!run_script($cmd, undef, $opt), "$t fails w/o --force");
160 like($err, qr/Multiple messages with different content found matching/,
162 like($err, qr/GIT_DIR=.*git show/is, "$t shows git commands");
165 $t .= ' and --force'; {
166 $in = $out = $err = '';
167 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Subject:x/i'";
168 $cmd = [ '-edit', "-m$mid", '--force', $inboxdir ];
169 ok(run_script($cmd, undef, $opt), "$t succeeds");
170 like($err, qr/Will edit all of them/, "$t notes all will be edited");
171 my @dump = $git->qx(qw(cat-file --batch --batch-all-objects));
173 is_deeply([grep(/^Subject:/i, @dump)], [qw(Subject:x Subject:x)],
174 "$t edited both messages");