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