1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # edit frontend behavior test (t/replace.t for backend)
7 use File::Temp qw/tempdir/;
8 require './t/common.perl';
10 require PublicInbox::Inbox;
11 require PublicInbox::InboxWritable;
12 require PublicInbox::Config;
13 use PublicInbox::MID qw(mid_clean);
15 my @mods = qw(DBI DBD::SQLite);
16 foreach my $mod (@mods) {
18 plan skip_all => "missing $mod for $0" if $@;
21 my $tmpdir = tempdir('pi-edit-XXXXXX', TMPDIR => 1, CLEANUP => 1);
22 my $inboxdir = "$tmpdir/v2";
23 my $ibx = PublicInbox::Inbox->new({
24 inboxdir => $inboxdir,
25 name => 'test-v2edit',
27 -primary_address => 'test@example.com',
28 indexlevel => 'basic',
30 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
31 my $cfgfile = "$tmpdir/config";
32 local $ENV{PI_CONFIG} = $cfgfile;
33 my $file = 't/data/0001.patch';
34 open my $fh, '<', $file or die "open: $!";
35 my $raw = do { local $/; <$fh> };
36 my $im = $ibx->importer(0);
37 my $mime = PublicInbox::MIME->new($raw);
38 my $mid = mid_clean($mime->header('Message-Id'));
39 ok($im->add($mime), 'add message to be edited');
41 my ($in, $out, $err, $cmd, $cur, $t);
42 my $git = PublicInbox::Git->new("$ibx->{inboxdir}/git/0.git");
43 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
46 $in = $out = $err = '';
47 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean prefix/bool pfx/'";
48 $cmd = [ '-edit', "-F$file", $inboxdir ];
49 ok(run_script($cmd, undef, $opt), "$t edit OK");
50 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
51 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
52 like($out, qr/[a-f0-9]{40}/, "$t shows commit on success");
55 $t = '-m MESSAGE_ID'; {
56 $in = $out = $err = '';
57 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
58 $cmd = [ '-edit', "-m$mid", $inboxdir ];
59 ok(run_script($cmd, undef, $opt), "$t edit OK");
60 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
61 like($cur->header('Subject'), qr/boolean prefix/, "$t message edited");
62 like($out, qr/[a-f0-9]{40}/, "$t shows commit on success");
65 $t = 'no-op -m MESSAGE_ID'; {
66 $in = $out = $err = '';
67 my $before = $git->qx(qw(rev-parse HEAD));
68 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
69 $cmd = [ '-edit', "-m$mid", $inboxdir ];
70 ok(run_script($cmd, undef, $opt), "$t succeeds");
72 $cur = PublicInbox::MIME->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 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 = 'no-op -m MESSAGE_ID w/Status: header'; { # because mutt does it
82 $in = $out = $err = '';
83 my $before = $git->qx(qw(rev-parse HEAD));
84 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Status: RO\\n\$&/'";
85 $cmd = [ '-edit', "-m$mid", $inboxdir ];
86 ok(run_script($cmd, undef, $opt), "$t succeeds");
88 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
89 is_deeply($cur, $prev, "$t makes no change");
90 like($cur->header('Subject'), qr/boolean prefix/,
91 "$t does not change message");
92 is($cur->header('Status'), undef, 'Status header not added');
93 like($out, qr/NONE/, 'noop shows NONE');
94 my $after = $git->qx(qw(rev-parse HEAD));
95 is($after, $before, 'git head unchanged');
98 $t = '-m MESSAGE_ID can change Received: headers'; {
99 $in = $out = $err = '';
100 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Received: x\\n\$&/'";
101 $cmd = [ '-edit', "-m$mid", $inboxdir ];
102 ok(run_script($cmd, undef, $opt), "$t succeeds");
103 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
104 like($cur->header('Subject'), qr/boolean prefix/,
105 "$t does not change Subject");
106 is($cur->header('Received'), 'x', 'added Received header');
110 $in = $out = $err = '';
111 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean/FAIL/'";
112 $cmd = [ '-edit', "-m$mid-miss", $inboxdir ];
113 ok(!run_script($cmd, undef, $opt), "$t fails on invalid MID");
114 like($err, qr/No message found/, "$t shows error");
117 $t = 'non-interactive editor failure'; {
118 $in = $out = $err = '';
119 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 'END { exit 1 }'";
120 $cmd = [ '-edit', "-m$mid", $inboxdir ];
121 ok(!run_script($cmd, undef, $opt), "$t detected");
122 like($err, qr/END \{ exit 1 \}' failed:/, "$t shows error");
125 $t = 'mailEditor set in config'; {
126 $in = $out = $err = '';
127 my $rc = system(qw(git config), "--file=$cfgfile",
128 'publicinbox.maileditor',
129 "$^X -i -p -e 's/boolean prefix/bool pfx/'");
130 is($rc, 0, 'set publicinbox.mailEditor');
131 local $ENV{MAIL_EDITOR};
132 delete $ENV{MAIL_EDITOR};
133 delete local $ENV{MAIL_EDITOR};
134 local $ENV{GIT_EDITOR} = 'echo should not run';
135 $cmd = [ '-edit', "-m$mid", $inboxdir ];
136 ok(run_script($cmd, undef, $opt), "$t edited message");
137 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
138 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
139 unlike($out, qr/should not run/, 'did not run GIT_EDITOR');
142 $t = '--raw and mbox escaping'; {
143 $in = $out = $err = '';
144 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^\$/\\nFrom not mbox\\n/'";
145 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
146 ok(run_script($cmd, undef, $opt), "$t succeeds");
147 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
148 like($cur->body, qr/^From not mbox/sm, 'put "From " line into body');
150 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^>From not/\$& an/'";
151 $cmd = [ '-edit', "-m$mid", $inboxdir ];
152 ok(run_script($cmd, undef, $opt), "$t succeeds with mbox escaping");
153 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
154 like($cur->body, qr/^From not an mbox/sm,
155 'changed "From " line unescaped');
157 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^From not an mbox\\n//s'";
158 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
159 ok(run_script($cmd, undef, $opt), "$t succeeds again");
160 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
161 unlike($cur->body, qr/^From not an mbox/sm, "$t restored body");
164 $t = 'reuse Message-ID'; {
166 local $SIG{__WARN__} = sub { push @warn, @_ };
167 ok($im->add($mime), "$t and re-add");
169 like($warn[0], qr/reused for mismatched content/, "$t got warning");
172 $t = 'edit ambiguous Message-ID with -m'; {
173 $in = $out = $err = '';
174 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
175 $cmd = [ '-edit', "-m$mid", $inboxdir ];
176 ok(!run_script($cmd, undef, $opt), "$t fails w/o --force");
177 like($err, qr/Multiple messages with different content found matching/,
179 like($err, qr/GIT_DIR=.*git show/is, "$t shows git commands");
182 $t .= ' and --force'; {
183 $in = $out = $err = '';
184 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Subject:x/i'";
185 $cmd = [ '-edit', "-m$mid", '--force', $inboxdir ];
186 ok(run_script($cmd, undef, $opt), "$t succeeds");
187 like($err, qr/Will edit all of them/, "$t notes all will be edited");
188 my @dump = $git->qx(qw(cat-file --batch --batch-all-objects));
190 is_deeply([grep(/^Subject:/i, @dump)], [qw(Subject:x Subject:x)],
191 "$t edited both messages");