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(IPC::Run DBI DBD::SQLite);
16 foreach my $mod (@mods) {
18 plan skip_all => "missing $mod for $0" if $@;
20 IPC::Run->import(qw(run));
22 my $cmd_pfx = 'blib/script/public-inbox';
23 my $tmpdir = tempdir('pi-edit-XXXXXX', TMPDIR => 1, CLEANUP => 1);
24 my $mainrepo = "$tmpdir/v2";
25 my $ibx = PublicInbox::Inbox->new({
26 mainrepo => $mainrepo,
27 name => 'test-v2edit',
29 -primary_address => 'test@example.com',
30 indexlevel => 'basic',
32 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
33 my $cfgfile = "$tmpdir/config";
34 local $ENV{PI_CONFIG} = $cfgfile;
35 my $file = 't/data/0001.patch';
36 open my $fh, '<', $file or die "open: $!";
37 my $raw = do { local $/; <$fh> };
38 my $im = $ibx->importer(0);
39 my $mime = PublicInbox::MIME->new($raw);
40 my $mid = mid_clean($mime->header('Message-Id'));
41 ok($im->add($mime), 'add message to be edited');
43 my ($in, $out, $err, $cmd, $cur, $t);
44 my $__git_dir = "--git-dir=$ibx->{mainrepo}/git/0.git";
47 $in = $out = $err = '';
48 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean prefix/bool pfx/'";
49 $cmd = [ "$cmd_pfx-edit", "-F$file", $mainrepo ];
50 ok(run($cmd, \$in, \$out, \$err), "$t edit OK");
51 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
52 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
53 like($out, qr/[a-f0-9]{40}/, "$t shows commit on success");
56 $t = '-m MESSAGE_ID'; {
57 $in = $out = $err = '';
58 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
59 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
60 ok(run($cmd, \$in, \$out, \$err), "$t edit OK");
61 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
62 like($cur->header('Subject'), qr/boolean prefix/, "$t message edited");
63 like($out, qr/[a-f0-9]{40}/, "$t shows commit on success");
66 $t = 'no-op -m MESSAGE_ID'; {
67 $in = $out = $err = '';
68 my $before = `git $__git_dir rev-parse HEAD`;
69 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
70 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
71 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
73 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
74 is_deeply($cur, $prev, "$t makes no change");
75 like($cur->header('Subject'), qr/boolean prefix/,
76 "$t does not change message");
77 like($out, qr/NONE/, 'noop shows NONE');
78 my $after = `git $__git_dir rev-parse HEAD`;
79 is($after, $before, 'git head unchanged');
82 $t = 'no-op -m MESSAGE_ID w/Status: header'; { # because mutt does it
83 $in = $out = $err = '';
84 my $before = `git $__git_dir rev-parse HEAD`;
85 local $ENV{MAIL_EDITOR} =
86 "$^X -i -p -e 's/^Subject:.*/Status: RO\\n\$&/'";
87 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
88 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
90 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
91 is_deeply($cur, $prev, "$t makes no change");
92 like($cur->header('Subject'), qr/boolean prefix/,
93 "$t does not change message");
94 is($cur->header('Status'), undef, 'Status header not added');
95 like($out, qr/NONE/, 'noop shows NONE');
96 my $after = `git $__git_dir rev-parse HEAD`;
97 is($after, $before, 'git head unchanged');
100 $t = '-m MESSAGE_ID can change Received: headers'; {
101 $in = $out = $err = '';
102 my $before = `git $__git_dir rev-parse HEAD`;
103 local $ENV{MAIL_EDITOR} =
104 "$^X -i -p -e 's/^Subject:.*/Received: x\\n\$&/'";
105 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
106 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
107 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
108 like($cur->header('Subject'), qr/boolean prefix/,
109 "$t does not change Subject");
110 is($cur->header('Received'), 'x', 'added Received header');
114 $in = $out = $err = '';
115 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean/FAIL/'";
116 $cmd = [ "$cmd_pfx-edit", "-m$mid-miss", $mainrepo ];
117 ok(!run($cmd, \$in, \$out, \$err), "$t fails on invalid MID");
118 like($err, qr/No message found/, "$t shows error");
121 $t = 'non-interactive editor failure'; {
122 $in = $out = $err = '';
123 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 'END { exit 1 }'";
124 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
125 ok(!run($cmd, \$in, \$out, \$err), "$t detected");
126 like($err, qr/END \{ exit 1 \}' failed:/, "$t shows error");
129 $t = 'mailEditor set in config'; {
130 $in = $out = $err = '';
131 my $rc = system(qw(git config), "--file=$cfgfile",
132 'publicinbox.maileditor',
133 "$^X -i -p -e 's/boolean prefix/bool pfx/'");
134 is($rc, 0, 'set publicinbox.mailEditor');
135 local $ENV{MAIL_EDITOR};
136 local $ENV{GIT_EDITOR} = 'echo should not run';
137 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
138 ok(run($cmd, \$in, \$out, \$err), "$t edited message");
139 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
140 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
141 unlike($out, qr/should not run/, 'did not run GIT_EDITOR');
144 $t = '--raw and mbox escaping'; {
145 $in = $out = $err = '';
146 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^\$/\\nFrom not mbox\\n/'";
147 $cmd = [ "$cmd_pfx-edit", "-m$mid", '--raw', $mainrepo ];
148 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
149 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
150 like($cur->body, qr/^From not mbox/sm, 'put "From " line into body');
152 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^>From not/\$& an/'";
153 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
154 ok(run($cmd, \$in, \$out, \$err), "$t succeeds with mbox escaping");
155 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
156 like($cur->body, qr/^From not an mbox/sm,
157 'changed "From " line unescaped');
159 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^From not an mbox\\n//s'";
160 $cmd = [ "$cmd_pfx-edit", "-m$mid", '--raw', $mainrepo ];
161 ok(run($cmd, \$in, \$out, \$err), "$t succeeds again");
162 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
163 unlike($cur->body, qr/^From not an mbox/sm, "$t restored body");
166 $t = 'reuse Message-ID'; {
168 local $SIG{__WARN__} = sub { push @warn, @_ };
169 ok($im->add($mime), "$t and re-add");
171 like($warn[0], qr/reused for mismatched content/, "$t got warning");
174 $t = 'edit ambiguous Message-ID with -m'; {
175 $in = $out = $err = '';
176 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
177 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
178 ok(!run($cmd, \$in, \$out, \$err), "$t fails w/o --force");
179 like($err, qr/Multiple messages with different content found matching/,
181 like($err, qr/GIT_DIR=.*git show/is, "$t shows git commands");
184 $t .= ' and --force'; {
185 $in = $out = $err = '';
186 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Subject:x/i'";
187 $cmd = [ "$cmd_pfx-edit", "-m$mid", '--force', $mainrepo ];
188 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
189 like($err, qr/Will edit all of them/, "$t notes all will be edited");
190 my @dump = `git $__git_dir cat-file --batch --batch-all-objects`;
192 is_deeply([grep(/^Subject:/i, @dump)], [qw(Subject:x Subject:x)],
193 "$t edited both messages");