1 # Copyright (C) 2019-2020 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 PublicInbox::TestCommon;
9 require PublicInbox::Inbox;
10 require PublicInbox::InboxWritable;
11 require PublicInbox::Config;
12 use PublicInbox::MID qw(mid_clean);
14 require_mods('DBD::SQLite');
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $inboxdir = "$tmpdir/v2";
17 my $ibx = PublicInbox::Inbox->new({
18 inboxdir => $inboxdir,
19 name => 'test-v2edit',
21 -primary_address => 'test@example.com',
22 indexlevel => 'basic',
24 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
25 my $cfgfile = "$tmpdir/config";
26 local $ENV{PI_CONFIG} = $cfgfile;
27 my $file = 't/data/0001.patch';
28 open my $fh, '<', $file or die "open: $!";
29 my $raw = do { local $/; <$fh> };
30 my $im = $ibx->importer(0);
31 my $mime = PublicInbox::MIME->new($raw);
32 my $mid = mid_clean($mime->header('Message-Id'));
33 ok($im->add($mime), 'add message to be edited');
35 my ($in, $out, $err, $cmd, $cur, $t);
36 my $git = PublicInbox::Git->new("$ibx->{inboxdir}/git/0.git");
37 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
40 $in = $out = $err = '';
41 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean prefix/bool pfx/'";
42 $cmd = [ '-edit', "-F$file", $inboxdir ];
43 ok(run_script($cmd, undef, $opt), "$t edit OK");
44 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
45 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
46 like($out, qr/[a-f0-9]{40}/, "$t shows commit on success");
49 $t = '-m MESSAGE_ID'; {
50 $in = $out = $err = '';
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 edit OK");
54 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
55 like($cur->header('Subject'), qr/boolean prefix/, "$t message edited");
56 like($out, qr/[a-f0-9]{40}/, "$t shows commit on success");
59 $t = 'no-op -m MESSAGE_ID'; {
60 $in = $out = $err = '';
61 my $before = $git->qx(qw(rev-parse HEAD));
62 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
63 $cmd = [ '-edit', "-m$mid", $inboxdir ];
64 ok(run_script($cmd, undef, $opt), "$t succeeds");
66 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
67 is_deeply($cur, $prev, "$t makes no change");
68 like($cur->header('Subject'), qr/boolean prefix/,
69 "$t does not change message");
70 like($out, qr/NONE/, 'noop shows NONE');
71 my $after = $git->qx(qw(rev-parse HEAD));
72 is($after, $before, 'git head unchanged');
75 $t = 'no-op -m MESSAGE_ID w/Status: header'; { # because mutt does it
76 $in = $out = $err = '';
77 my $before = $git->qx(qw(rev-parse HEAD));
78 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Status: RO\\n\$&/'";
79 $cmd = [ '-edit', "-m$mid", $inboxdir ];
80 ok(run_script($cmd, undef, $opt), "$t succeeds");
82 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
83 is_deeply($cur, $prev, "$t makes no change");
84 like($cur->header('Subject'), qr/boolean prefix/,
85 "$t does not change message");
86 is($cur->header('Status'), undef, 'Status header not added');
87 like($out, qr/NONE/, 'noop shows NONE');
88 my $after = $git->qx(qw(rev-parse HEAD));
89 is($after, $before, 'git head unchanged');
92 $t = '-m MESSAGE_ID can change Received: headers'; {
93 $in = $out = $err = '';
94 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Received: x\\n\$&/'";
95 $cmd = [ '-edit', "-m$mid", $inboxdir ];
96 ok(run_script($cmd, undef, $opt), "$t succeeds");
97 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
98 like($cur->header('Subject'), qr/boolean prefix/,
99 "$t does not change Subject");
100 is($cur->header('Received'), 'x', 'added Received header');
104 $in = $out = $err = '';
105 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean/FAIL/'";
106 $cmd = [ '-edit', "-m$mid-miss", $inboxdir ];
107 ok(!run_script($cmd, undef, $opt), "$t fails on invalid MID");
108 like($err, qr/No message found/, "$t shows error");
111 $t = 'non-interactive editor failure'; {
112 $in = $out = $err = '';
113 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 'END { exit 1 }'";
114 $cmd = [ '-edit', "-m$mid", $inboxdir ];
115 ok(!run_script($cmd, undef, $opt), "$t detected");
116 like($err, qr/END \{ exit 1 \}' failed:/, "$t shows error");
119 $t = 'mailEditor set in config'; {
120 $in = $out = $err = '';
121 my $rc = system(qw(git config), "--file=$cfgfile",
122 'publicinbox.maileditor',
123 "$^X -i -p -e 's/boolean prefix/bool pfx/'");
124 is($rc, 0, 'set publicinbox.mailEditor');
125 local $ENV{MAIL_EDITOR};
126 delete $ENV{MAIL_EDITOR};
127 local $ENV{GIT_EDITOR} = 'echo should not run';
128 $cmd = [ '-edit', "-m$mid", $inboxdir ];
129 ok(run_script($cmd, undef, $opt), "$t edited message");
130 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
131 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
132 unlike($out, qr/should not run/, 'did not run GIT_EDITOR');
135 $t = '--raw and mbox escaping'; {
136 $in = $out = $err = '';
137 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^\$/\\nFrom not mbox\\n/'";
138 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
139 ok(run_script($cmd, undef, $opt), "$t succeeds");
140 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
141 like($cur->body, qr/^From not mbox/sm, 'put "From " line into body');
143 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^>From not/\$& an/'";
144 $cmd = [ '-edit', "-m$mid", $inboxdir ];
145 ok(run_script($cmd, undef, $opt), "$t succeeds with mbox escaping");
146 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
147 like($cur->body, qr/^From not an mbox/sm,
148 'changed "From " line unescaped');
150 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^From not an mbox\\n//s'";
151 $cmd = [ '-edit', "-m$mid", '--raw', $inboxdir ];
152 ok(run_script($cmd, undef, $opt), "$t succeeds again");
153 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
154 unlike($cur->body, qr/^From not an mbox/sm, "$t restored body");
157 $t = 'reuse Message-ID'; {
159 local $SIG{__WARN__} = sub { push @warn, @_ };
160 ok($im->add($mime), "$t and re-add");
162 like($warn[0], qr/reused for mismatched content/, "$t got warning");
165 $t = 'edit ambiguous Message-ID with -m'; {
166 $in = $out = $err = '';
167 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
168 $cmd = [ '-edit', "-m$mid", $inboxdir ];
169 ok(!run_script($cmd, undef, $opt), "$t fails w/o --force");
170 like($err, qr/Multiple messages with different content found matching/,
172 like($err, qr/GIT_DIR=.*git show/is, "$t shows git commands");
175 $t .= ' and --force'; {
176 $in = $out = $err = '';
177 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Subject:x/i'";
178 $cmd = [ '-edit', "-m$mid", '--force', $inboxdir ];
179 ok(run_script($cmd, undef, $opt), "$t succeeds");
180 like($err, qr/Will edit all of them/, "$t notes all will be edited");
181 my @dump = $git->qx(qw(cat-file --batch --batch-all-objects));
183 is_deeply([grep(/^Subject:/i, @dump)], [qw(Subject:x Subject:x)],
184 "$t edited both messages");