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 = '-m MESSAGE_ID can change Received: headers'; {
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:.*/Received: x\\n\$&/'";
87 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
88 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
89 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
90 like($cur->header('Subject'), qr/boolean prefix/,
91 "$t does not change Subject");
92 is($cur->header('Received'), 'x', 'added Received header');
96 $in = $out = $err = '';
97 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/boolean/FAIL/'";
98 $cmd = [ "$cmd_pfx-edit", "-m$mid-miss", $mainrepo ];
99 ok(!run($cmd, \$in, \$out, \$err), "$t fails on invalid MID");
100 like($err, qr/No message found/, "$t shows error");
103 $t = 'non-interactive editor failure'; {
104 $in = $out = $err = '';
105 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 'END { exit 1 }'";
106 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
107 ok(!run($cmd, \$in, \$out, \$err), "$t detected");
108 like($err, qr/END \{ exit 1 \}' failed:/, "$t shows error");
111 $t = 'mailEditor set in config'; {
112 $in = $out = $err = '';
113 my $rc = system(qw(git config), "--file=$cfgfile",
114 'publicinbox.maileditor',
115 "$^X -i -p -e 's/boolean prefix/bool pfx/'");
116 is($rc, 0, 'set publicinbox.mailEditor');
117 local $ENV{MAIL_EDITOR};
118 local $ENV{GIT_EDITOR} = 'echo should not run';
119 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
120 ok(run($cmd, \$in, \$out, \$err), "$t edited message");
121 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
122 like($cur->header('Subject'), qr/bool pfx/, "$t message edited");
123 unlike($out, qr/should not run/, 'did not run GIT_EDITOR');
126 $t = '--raw and mbox escaping'; {
127 $in = $out = $err = '';
128 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^\$/\\nFrom not mbox\\n/'";
129 $cmd = [ "$cmd_pfx-edit", "-m$mid", '--raw', $mainrepo ];
130 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
131 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
132 like($cur->body, qr/^From not mbox/sm, 'put "From " line into body');
134 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^>From not/\$& an/'";
135 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
136 ok(run($cmd, \$in, \$out, \$err), "$t succeeds with mbox escaping");
137 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
138 like($cur->body, qr/^From not an mbox/sm,
139 'changed "From " line unescaped');
141 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^From not an mbox\\n//s'";
142 $cmd = [ "$cmd_pfx-edit", "-m$mid", '--raw', $mainrepo ];
143 ok(run($cmd, \$in, \$out, \$err), "$t succeeds again");
144 $cur = PublicInbox::MIME->new($ibx->msg_by_mid($mid));
145 unlike($cur->body, qr/^From not an mbox/sm, "$t restored body");
148 $t = 'reuse Message-ID'; {
150 local $SIG{__WARN__} = sub { push @warn, @_ };
151 ok($im->add($mime), "$t and re-add");
153 like($warn[0], qr/reused for mismatched content/, "$t got warning");
156 $t = 'edit ambiguous Message-ID with -m'; {
157 $in = $out = $err = '';
158 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/bool pfx/boolean prefix/'";
159 $cmd = [ "$cmd_pfx-edit", "-m$mid", $mainrepo ];
160 ok(!run($cmd, \$in, \$out, \$err), "$t fails w/o --force");
161 like($err, qr/Multiple messages with different content found matching/,
163 like($err, qr/GIT_DIR=.*git show/is, "$t shows git commands");
166 $t .= ' and --force'; {
167 $in = $out = $err = '';
168 local $ENV{MAIL_EDITOR} = "$^X -i -p -e 's/^Subject:.*/Subject:x/i'";
169 $cmd = [ "$cmd_pfx-edit", "-m$mid", '--force', $mainrepo ];
170 ok(run($cmd, \$in, \$out, \$err), "$t succeeds");
171 like($err, qr/Will edit all of them/, "$t notes all will be edited");
172 my @dump = `git $__git_dir cat-file --batch --batch-all-objects`;
174 is_deeply([grep(/^Subject:/i, @dump)], [qw(Subject:x Subject:x)],
175 "$t edited both messages");