blib/lib/Finance/Tiller2QIF/Interactive.pm

filter: Statementstmt 98.8% 261 / 264 Branchbran 72.8% 86 / 118 Conditioncond 77.7% 35 / 45 MC/DCmcdc 37.5% 9 / 24 Subroutinesub 100% 34 / 34 Podpod 100% 1 / 1 totaltotal 87.6% 426 / 486 CC 86 SCAR 27.2
CC
86
Cov
89%
Run
37.5
_recover
13.7
_publish
10.0
CRAP 15.2
« blib/lib/Finance/Tiller2QIF/Map.pm ↑ Summary blib/lib/Finance/Tiller2QIF/ReadCSV.pm »
1 package Finance::Tiller2QIF::Interactive;
2 # ABSTRACT: Review and correct an isolated import before publishing it
3 2 $Finance::Tiller2QIF::Interactive::VERSION = '1.23';
4 2 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use open ':std', ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Temp qw(tempdir tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use DBD::SQLite::Constants qw(SQLITE_OK SQLITE_DENY SQLITE_ATTACH SQLITE_DETACH
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 SQLITE_PRAGMA SQLITE_TRANSACTION SQLITE_SAVEPOINT);
16 2 ▶use Finance::Tiller2QIF::DB qw(connect_db);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Finance::Tiller2QIF::ReadCSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Finance::Tiller2QIF::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Finance::Tiller2QIF::WriteQIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 DESCRIPTION
22  
23 The C<interactive> CLI command rebuilds an isolated database from a fixed
24 checkpoint and input CSV, then applies current mapping, hooks, and session SQL.
25 Only explicit export publishes the database and QIF. Session artifacts are
26 retained. A publication journal permits rollback after an interrupted install.
27  
28 =head1 FUNCTIONS
29  
30 =head2 Run
31  
32 Finance::Tiller2QIF::Interactive::Run(\%options);
33  
34 Run the interactive CLI session with resolved CLI options. Returns 1 after
35 publication, 0 on cancellation, and dies on an unrecovered error. Refresh errors
36 are reported within the session so the user can repair files and retry.
37  
38 =cut
39  
40 {
41 package Finance::Tiller2QIF::Interactive::_DatabaseGuard;
42 2 $Finance::Tiller2QIF::Interactive::_DatabaseGuard::VERSION = '1.23';
43 21 ▶ sub DESTROY ($self) {
Subroutine DESTROY: called CC=2 SCAR=6.9
44 21 local $@;
45 21 eval {
46 21 ▶ $self->[0]->rollback unless $self->[0]{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[0]{"AutoCommit"} 20 1 21
47 21 $self->[0]->disconnect;
48 };
49 }
50 }
51  
52 # Database/QIF copies and hashes are binary file transport, not text decoding.
53 2 ▶sub _digest ($file) {
Subroutine _digest: called CC=2 SCAR=6.9
54 319 ▶ return undef unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 2 317 319
55 317 my $fh = path($file)->openr_raw;
56 317 return Digest::SHA->new(256)->addfile($fh)->hexdigest;
57 }
58  
59 112 ▶sub _same ($a, $b) { return ($a // '') eq ($b // '') }
Subroutine _same: called CC=3 SCAR=11.0
Condition100%4/4
$a // ""
l!l
1111
$b // ""
l!l
1093
MC/DC100%1/1
$a // ""
$a
MC/DC100%1/1
$b // ""
$b
Truth table100%2/2
A
A= $a
Aresult
11
00
Truth table100%2/2
A
A= $b
Aresult
11
00
60  
61 135 ▶sub _sync ($file) {
Subroutine _sync: called CC=4 SCAR=14.9
62 # Windows uses normal OS buffering; power-loss durability is not required.
63 # This also bypasses directory fsync, which Windows does not support here.
64 135 ▶ return if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 135 135
65 135 my $fh = path($file)->openr_raw;
66 135 ▶ $fh->sync or die "Cannot sync $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->sync or die "Cannot sync ${file}: $!\n" 135 0 135
67 135 ▶ close $fh or die "Cannot close $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file}: $!\n" 135 0 135
68 }
69  
70 72 ▶sub _sync_directory ($file) {
Subroutine _sync_directory: called CC=1 SCAR=0.0
71 72 _sync(path($file)->parent);
72 }
73  
74 # Stage in the destination directory so rename cannot cross filesystems.
75 17 ▶sub _install ($source, $target) {
Subroutine _install: called CC=4 SCAR=14.3
76 17 my ($fh, $stage) = tempfile('.tiller2qif-install-XXXXXXXX',
77 DIR => path($target)->parent->stringify, UNLINK => 0);
78 17 ▶ close $fh or die "Cannot close $stage: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${stage}: $!\n" 17 0 17
79 17 path($source)->copy($stage);
80 17 ▶ chmod((stat($source))[2] & 0777, $stage) or die "Cannot chmod $stage: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod +(stat $source)[2] & 511, $stage or die "Cannot chmod ${stage}: $!\n" 17 0 17
81 17 _sync($stage);
82 17 ▶ rename $stage, $target or die "Cannot install $target: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $stage, $target or die "Cannot install ${target}: $!\n" 17 0 17
83 17 _sync_directory($target);
84 }
85  
86 13 ▶sub _journal ($file, $record) {
Subroutine _journal: called CC=3 SCAR=11.2
87 13 my ($fh, $stage) = tempfile('.tiller2qif-journal-XXXXXXXX',
88 DIR => path($file)->parent->stringify, UNLINK => 0);
89 13 ▶ close $fh or die "Cannot close $stage: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${stage}: $!\n" 13 0 13
90 13 path($stage)->spew_utf8(Cpanel::JSON::XS->new->canonical->encode($record));
91 13 _sync($stage);
92 13 ▶ rename $stage, $file or die "Cannot install journal $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $stage, $file or die "Cannot install journal ${file}: $!\n" 13 0 13
93 13 _sync_directory($file);
94 }
95  
96 27 ▶sub _recover ($journal) {
Subroutine _recover: called CC=13 SCAR=26.1
97 27 ▶ return unless -e $journal;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $journal 19 8 27
98 8 my $record = Cpanel::JSON::XS->new->decode(path($journal)->slurp_utf8);
99 8 ▶ if ($record->{state} ne 'committed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"state"} ne "committed") 3 5 8
100 # Refuse to overwrite an unrelated edit made after the interrupted session.
101 3 for my $entry ($record->{files}->@*) {
102 6 my $now = _digest($entry->{target});
103 die "Recovery conflict at $entry->{target}; inspect $journal\n"
104 6 ▶ unless _same($now, $entry->{old}) || _same($now, $entry->{new});
Branch: 1 branch
BranchTrueFalseTotal
unless _same($now, $entry->{"old"}) or _same($now, $entry->{"new"}) 0 6 6
Condition66%2/3
_same($now, $entry->{"old"}) or _same($now, $entry->{"new"})
l!l&&r!l&&!r
240
MC/DC0%0/2
_same($now, $entry->{"old"}) or _same($now, $entry->{"new"})
_same($now, $entry->{"old"}) _same($now, $entry->{"new"})
Truth table66%2/3
A or B
A= _same($now, $entry->{"old"})B= _same($now, $entry->{"new"})
ABresult
1X1
011
000
105 die "Recovery backup changed: $entry->{backup}\n"
106 6 ▶ if defined $entry->{old} && !_same(_digest($entry->{backup}), $entry->{old});
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"old"} and not _same(_digest($entry->{"backup"}), $entry->{"old"}) 0 6 6
Condition66%2/3
defined $entry->{"old"} and not _same(_digest($entry->{"backup"}), $entry->{"old"})
!ll&&!rl&&r
150
MC/DC0%0/2
defined $entry->{"old"} and not _same(_digest($entry->{"backup"}), $entry->{"old"})
defined $entry->{"old"} not _same(_digest($entry->{"backup"}), $entry->{"old"})
Truth table66%2/3
A and not(B)
A= defined $entry->{"old"}B= not _same(_digest($entry->{"backup"}), $entry->{"old"})
ABresult
0X0
100
111
107 }
108 3 for my $entry (reverse $record->{files}->@*) {
109 6 ▶ next if _same(_digest($entry->{target}), $entry->{old});
Branch: 1 branch
BranchTrueFalseTotal
if _same(_digest($entry->{"target"}), $entry->{"old"}) 2 4 6
110 4 ▶ if (defined $entry->{old}) { _install($entry->{backup}, $entry->{target}) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->{'old'}) { } 3 1 4
111 else {
112 1 ▶ unlink $entry->{target} or die "Cannot restore absent $entry->{target}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $entry->{"target"} or die qq[Cannot restore absent $entry->{"target"}: $!\n] 1 0 1
113 1 _sync_directory($entry->{target});
114 }
115 }
116 3 say 'Rolled back interrupted publication; originals restored.';
117 }
118 8 ▶ unlink $journal or die "Cannot remove publication journal $journal: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $journal or die "Cannot remove publication journal ${journal}: $!\n" 8 0 8
119 8 _sync_directory($journal);
120 }
121  
122 9 ▶sub _publish ($session, $db, $qif) {
Subroutine _publish: called CC=10 SCAR=23.0
123 9 my @files;
124 9 for my $pair ([$db, $session->{db}, 'database'], [$qif, $session->{output}, 'output']) {
125 18 my ($source, $target, $name) = @$pair;
126 18 my $old = _digest($target);
127 die "$target changed outside the session; export refused\n"
128 18 ▶ unless _same($old, $session->{original}{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless _same($old, $session->{"original"}{$name}) 1 17 18
129 17 my $backup = "$session->{dir}/original-$name";
130 17 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 16 1 17
131 16 path($target)->copy($backup);
132 16 ▶ chmod((stat($target))[2] & 0777, $backup) or die "Cannot chmod $backup: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod +(stat $target)[2] & 511, $backup or die "Cannot chmod ${backup}: $!\n" 16 0 16
133 16 _sync($backup);
134 16 _sync_directory($backup);
135 }
136 17 _sync($source);
137 17 _sync_directory($source);
138 17 push @files, { target => $target, source => $source, backup => $backup,
139 old => $old, new => _digest($source) };
140 }
141 8 my $record = { state => 'pending', files => \@files };
142 8 _journal($session->{journal}, $record);
143 8 my $ok = eval {
144 # Keep the recoverable interval small: all generation has already finished.
145 8 for my $entry (@files) { _install($entry->{source}, $entry->{target}) }
146 5 $record->{state} = 'committed';
147 5 _journal($session->{journal}, $record);
148 4 1;
149 };
150 8 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 4 4 8
151 4 my $error = $@;
152 # The committed marker is the commit point. A signal or sync error after
153 # that rename must not report an unsuccessful session with published data.
154 4 my $installed = eval {
155 Cpanel::JSON::XS->new->decode(path($session->{journal})->slurp_utf8)->{state}
156 4 eq 'committed';
157 };
158 4 ▶ if ($installed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($installed) 1 3 4
159 1 warn "Publication committed; journal cleanup deferred: $error";
160 1 return;
161 }
162 3 ▶ eval { _recover($session->{journal}); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _recover($session->{"journal"}); 1 } } or die qq[$error\nRollback requires recovery using $session->{"journal"}: $@] 2 1 3
163 or die "$error\nRollback requires recovery using $session->{journal}: $@";
164 2 die $error;
165 }
166 # A committed journal is harmless if cleanup is interrupted.
167 4 ▶ eval { _recover($session->{journal}); 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _recover($session->{"journal"}); 1 } } or warn $@ 4 0 4
168 }
169  
170 49 ▶sub _prepare_sql ($dbh) {
Subroutine _prepare_sql: called CC=1 SCAR=0.0
171 # Corrections and hooks operate only on the working database. Transaction
172 # boundaries are owned by the session, and PRAGMA cannot enable WAL sidecars.
173 49 my %denied = map { $_ => 1 } (SQLITE_ATTACH, SQLITE_DETACH, SQLITE_PRAGMA,
174 SQLITE_TRANSACTION, SQLITE_SAVEPOINT);
175 49 ▶ $dbh->sqlite_set_authorizer(sub { $denied{$_[0]} ? SQLITE_DENY : SQLITE_OK });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$denied{$_[0]} ? : 1 400 401
176 }
177  
178 33 ▶sub _sql ($dbh, $file, $verbose=0) {
Subroutine _sql: called CC=3 SCAR=11.1
179 33 local $dbh->{sqlite_allow_multiple_statements} = 1;
180 33 $dbh->do(path($file)->slurp_utf8);
181 30 ▶ say "Applied SQL: $file" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 30 30
182 }
183  
184 40 ▶sub _sources ($session) {
Subroutine _sources: called CC=1 SCAR=0.0
185 91 return { map { $_ => _digest($session->{options}{$_}) }
186 40 grep { defined $session->{options}{$_} } qw(mapfile beforemap aftermap corrections) };
187 }
188  
189 33 ▶sub _signature ($sources) {
Subroutine _signature: called CC=1 SCAR=0.0
190 33 return Cpanel::JSON::XS->new->canonical->encode($sources);
191 }
192  
193 28 ▶sub _refresh ($session) {
Subroutine _refresh: called CC=7 SCAR=19.5
194 28 my $generation = ++$session->{generation};
195 28 my $dir = "$session->{dir}/review-$generation";
196 28 path($dir)->mkpath;
197 28 my %options = $session->{options}->%*;
198 28 my $sources = _sources($session);
199 28 for my $key (keys %$sources) {
200 63 ▶ die "Missing $key file: $options{$key}\n" unless defined $sources->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sources->{$key} 0 63 63
201 63 my $copy = "$dir/$key";
202 63 path($options{$key})->copy($copy);
203 die "$options{$key} changed while reading; refresh again\n"
204 63 ▶ unless _same(_digest($copy), $sources->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless _same(_digest($copy), $sources->{$key}) 0 63 63
205 63 $options{$key} = $copy;
206 }
207 28 my $db = "$dir/working.sqlite3";
208 28 path($session->{baseline})->copy($db);
209 28 Finance::Tiller2QIF::ReadCSV::Ingest($session->{input}, $db, $options{verbose}, 1);
210 26 $options{db_path} = $db;
211 26 $options{strict} = 1;
212 26 $options{prepare_db} = \&_prepare_sql;
213 26 $options{sql_runner} = \&_sql;
214 26 ▶ if ($options{mapfile}) { Finance::Tiller2QIF::Map::Map(\%options) }
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'mapfile'}) { } 23 3 26
215 else {
216 3 my $dbh = connect_db($db);
217 3 _prepare_sql($dbh);
218 _sql($dbh, $options{$_}, $options{verbose})
219 3 for grep { $options{$_} } qw(beforemap aftermap);
220 3 $dbh->disconnect;
221 }
222 24 my $dbh = connect_db($db);
223 24 _prepare_sql($dbh);
224 24 _sql($dbh, $options{corrections}, $options{verbose});
225 21 $dbh->disconnect;
226 21 my ($count, $text) = Finance::Tiller2QIF::WriteQIF::_preview_text($db);
227 21 my $preview = "$dir/preview.t2qpv";
228 21 path($preview)->spew_utf8($text);
229 21 ▶ chmod 0400, $preview or die "Cannot make preview read-only: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod 0400, $preview or die "Cannot make preview read-only: $!\n" 21 0 21
230 21 $session->{review} = { db => $db, preview => $preview, count => $count,
231 signature => _signature($sources), digest => _digest($db) };
232 21 $session->{valid} = 1;
233 }
234  
235 28 ▶sub _view ($session) {
Subroutine _view: called CC=8 SCAR=21.4
236 28 my $review = $session->{review};
237 28 my $viewer = $session->{options}{viewer};
238 28 ▶ if (lc $viewer eq 'console') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $viewer eq 'console') { } 26 2 28
239 26 ▶ print path($review->{preview})->slurp_utf8 if $review;
Branch: 1 branch
BranchTrueFalseTotal
if $review 22 4 26
240 }
241 else {
242 my @files = ($review ? ($review->{preview}) : (),
243 $session->{options}{mapfile} ? ($session->{options}{mapfile}) : (),
244 2 ▶ $session->{options}{corrections});
Branch: 2 branches
BranchTrueFalseTotal
$review ? : 2 0 2
$session->{'options'}{'mapfile'} ? : 2 0 2
245 2 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 2 2
246 0 Finance::Tiller2QIF::WriteQIF::_launch_windows_viewer($viewer, @files);
247 }
248 else {
249 my $cmd = join ' ', $viewer,
250 2 map { Finance::Tiller2QIF::WriteQIF::_shell_quote($_) } @files;
251 2 ▶ die "preview viewer '$viewer' failed\n" if system($cmd);
Branch: 1 branch
BranchTrueFalseTotal
if system $cmd 0 2 2
252 }
253 }
254 28 ▶ say "$review->{count} transaction(s) in last successful preview." if $review;
Branch: 1 branch
BranchTrueFalseTotal
if $review 24 4 28
255 }
256  
257 22 ▶sub Run ($options) {
Subroutine Run: called CC=35 SCAR=36.3
Pod documented
258 22 my %options = %$options;
259 22 for my $key (qw(db_path output input mapfile beforemap aftermap)) {
260 132 ▶ next unless defined $options{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{$key} 41 91 132
261 91 my $file = path($options{$key})->absolute;
262 91 ▶ die "Interactive paths must not be symlinks: $file\n" if -l $file;
Branch: 1 branch
BranchTrueFalseTotal
if -l $file 0 91 91
263 91 $options{$key} = $file->parent->realpath->child($file->basename)->stringify;
264 91 ▶ die "Not a regular file: $file\n" if -e $file && !-f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file and not -f $file 0 91 91
Condition66%2/3
-e $file and not -f $file
!ll&&!rl&&r
1900
MC/DC0%0/2
-e $file and not -f $file
-e $file not -f $file
Truth table66%2/3
A and not(B)
A= -e $fileB= not -f $file
ABresult
0X0
100
111
265 }
266 22 ▶ die "interactive requires an existing database; use newdb first\n" unless -f $options{db_path};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $options{"db_path"} 0 22 22
267 22 for my $target_key (qw(db_path output)) {
268 43 my $target = $options{$target_key};
269 43 for my $key (qw(db_path output input mapfile beforemap aftermap)) {
270 254 ▶ next if $key eq $target_key || !defined $options{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $target_key or not defined $options{$key} 121 133 254
Condition100%3/3
$key eq $target_key or not defined $options{$key}
l!l&&r!l&&!r
4378133
MC/DC100%2/2
$key eq $target_key or not defined $options{$key}
$key eq $target_key not defined $options{$key}
Truth table100%3/3
A or not(B)
A= $key eq $target_keyB= not defined $options{$key}
ABresult
1X1
011
000
271 133 my $file = $options{$key};
272 133 my @a = stat($target);
273 133 my @b = stat($file);
274 133 ▶ die "Database and output must be distinct from all input files\n"
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $target or @a and @b and $a[0] == $b[0] and $a[1] == $b[1] 1 132 133
Condition75%9/12
$file eq $target or @a and @b and $a[0] == $b[0] and $a[1] == $b[1]
l!l&&r!l&&!r
10132
@a and @b and $a[0] == $b[0] and $a[1] == $b[1]
!ll&&!rl&&r
41280
@a and @b and $a[0] == $b[0]
!ll&&!rl&&r
40128
@a and @b
!ll&&!rl&&r
31128
MC/DC20%1/5
$file eq $target or @a and @b and $a[0] == $b[0] and $a[1] == $b[1]
$file eq $target @a @b $a[0] == $b[0] $a[1] == $b[1]
Truth table66%4/6
A or B and C and D and E
A= $file eq $targetB= @aC= @bD= $a[0] == $b[0]E= $a[1] == $b[1]
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
275 if $file eq $target || (@a && @b && $a[0] == $b[0] && $a[1] == $b[1]);
276 }
277 }
278 21 my $db = $options{db_path};
279 21 for my $reserved ("$db.interactive.lock", "$db.interactive.json") {
280 42 ▶ die "Interactive metadata must not be a symlink: $reserved\n" if -l $reserved;
Branch: 1 branch
BranchTrueFalseTotal
if -l $reserved 0 42 42
281 die "Interactive metadata path is reserved: $reserved\n"
282 42 ▶ if grep { defined $_ && $_ eq $reserved } @options{qw(output input mapfile beforemap aftermap)};
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_ and $_ eq $reserved;} @options{"output", "input", "mapfile", "beforemap", "aftermap"} 0 42 42
Condition66%2/3
defined $_ && $_ eq $reserved
!ll&&!rl&&r
781320
MC/DC0%0/2
defined $_ && $_ eq $reserved
defined $_ $_ eq $reserved
Truth table66%2/3
A && B
A= defined $_B= $_ eq $reserved
ABresult
0X0
100
111
283 }
284 21 my $lock = path("$db.interactive.lock")->opena_utf8;
285 21 ▶ flock($lock, LOCK_EX | LOCK_NB) or die "Another interactive session is using $db\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $lock, 6 or die "Another interactive session is using $db\n" 21 0 21
286 21 my $journal = "$db.interactive.json";
287 21 _recover($journal);
288 21 my $guard = connect_db($db);
289 21 my $release = bless [$guard], 'Finance::Tiller2QIF::Interactive::_DatabaseGuard';
290 21 ▶ die "Interactive sessions require a rollback-journal database, not WAL\n"
Branch: 1 branch
BranchTrueFalseTotal
if lc $guard->selectrow_array("PRAGMA journal_mode") eq "wal" 1 20 21
291 if lc($guard->selectrow_array('PRAGMA journal_mode')) eq 'wal';
292 20 $guard->do('BEGIN EXCLUSIVE');
293 20 my $dir = tempdir('.tiller2qif-session-XXXXXXXX', DIR => path($db)->parent->stringify, CLEANUP => 0);
294 20 say "Interactive session artifacts: $dir";
295 my $session = { dir => $dir, db => $db, output => $options{output}, journal => $journal,
296 baseline => "$dir/checkpoint.sqlite3", input => "$dir/input.csv", generation => 0,
297 20 original => { database => _digest($db), output => _digest($options{output}) } };
298 20 path($db)->copy($session->{baseline});
299 chmod((stat($db))[2] & 0777, $session->{baseline})
300 20 ▶ or die "Cannot preserve database permissions: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod +(stat $db)[2] & 511, $session->{"baseline"} or die "Cannot preserve database permissions: $!\n" 20 0 20
301 20 path($options{input})->copy($session->{input});
302 20 $options{corrections} = "$dir/corrections.t2qsql";
303 20 path($options{corrections})->spew_utf8(<<'SQL');
304 -- Session corrections run AFTER mapping and aftermap on every fresh rebuild.
305 -- Use full transaction IDs from the preview. Save, then choose Apply/refresh.
306 -- UPDATE transactions SET mapped_category = 'Expenses:Food' WHERE id = 'ID';
307 -- UPDATE transactions SET payee = 'Corrected name' WHERE id = 'ID';
308 -- UPDATE transactions SET skipped = 0, exported = 0 WHERE id = 'ID';
309 -- Removing a statement removes its effect on the next refresh.
310 -- ATTACH, DETACH, PRAGMA and explicit transactions are not supported.
311 SQL
312 20 $session->{options} = \%options;
313 20 say "Corrections SQL: $options{corrections}";
314 20 my $interrupted = 0;
315 1 ▶ local $SIG{INT} = local $SIG{TERM} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
316 1 $interrupted = 1;
317 1 die "Interactive session interrupted; artifacts: $dir\n";
318 20 };
319 28 ▶ my $refresh = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
320 28 $session->{valid} = 0;
321 28 ▶ if (!eval { _refresh($session); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { _refresh($session); 1 } }) 7 21 28
322 7 ▶ die $@ if $interrupted;
Branch: 1 branch
BranchTrueFalseTotal
if $interrupted 1 6 7
323 6 warn "Refresh failed: $@";
324 }
325 27 _view($session);
326 20 };
327 20 $refresh->();
328 19 while (1) {
329 35 say 'A=Apply and refresh, V=View/edit, E=Export, C=Cancel';
330 35 my $answer = <STDIN>;
331 35 ▶ if (!defined $answer || $answer =~ /^\s*[cn]\s*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $answer or $answer =~ /^\s*[cn]\s*$/iu) 9 26 35
Condition100%3/3
not defined $answer or $answer =~ /^\s*[cn]\s*$/iu
l!l&&r!l&&!r
1826
MC/DC100%2/2
not defined $answer or $answer =~ /^\s*[cn]\s*$/iu
not defined $answer $answer =~ /^\s*[cn]\s*$/iu
Truth table100%3/3
not(A) or B
A= not defined $answerB= $answer =~ /^\s*[cn]\s*$/iu
ABresult
1X1
011
000
332 9 say "Session cancelled; original database and QIF unchanged. Artifacts: $dir";
333 9 return 0;
334 }
335 26 ▶ if ($answer =~ /^\s*a\s*$/i) { $refresh->(); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($answer =~ /^\s*a\s*$/iu) 8 18 26
336 18 ▶ if ($answer =~ /^\s*v\s*$/i) { _view($session); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($answer =~ /^\s*v\s*$/iu) 1 17 18
337 0 ▶ if ($answer !~ /^\s*[ey]\s*$/i) { say 'Choose A, V, E, or C.'; next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($answer =~ /^\s*[ey]\s*$/iu) 0 17 17
338 17 ▶ if (!$session->{valid}
Branch: 1 branch
BranchTrueFalseTotal
if (not $session->{"valid"} or $session->{"review"}{"signature"} ne _signature(_sources($session)) or not _same($session->{"review"}{"digest"}, _digest($session->{"review"}{"db"}))) 7 10 17
Condition83%5/6
not $session->{"valid"} or $session->{"review"}{"signature"} ne _signature(_sources($session)) or not _same($session->{"review"}{"digest"}, _digest($session->{"review"}{"db"}))
l!l&&r!l&&!r
7010
not $session->{"valid"} or $session->{"review"}{"signature"} ne _signature(_sources($session))
l!l&&r!l&&!r
5210
MC/DC66%2/3
not $session->{"valid"} or $session->{"review"}{"signature"} ne _signature(_sources($session)) or not _same($session->{"review"}{"digest"}, _digest($session->{"review"}{"db"}))
not $session->{"valid"} $session->{"review"}{"signature"} ne _signature(_sources($session)) not _same($session->{"review"}{"digest"}, _digest($session->{"review"}{"db"}))
Truth table75%3/4
not(A) or B or not(C)
A= not $session->{"valid"}B= $session->{"review"}{"signature"} ne _signature(_sources($session))C= not _same($session->{"review"}{"digest"}, _digest($session->{"review"}{"db"}))
ABCresult
1XX1
01X1
0011
0000
339 || $session->{review}{signature} ne _signature(_sources($session))
340 || !_same($session->{review}{digest}, _digest($session->{review}{db}))) {
341 7 say 'Apply and refresh successfully before exporting saved changes.';
342 7 next;
343 }
344 10 my $export_db = "$dir/export.sqlite3";
345 10 my $export_qif = "$dir/export.qif";
346 10 path($session->{review}{db})->copy($export_db);
347 10 ▶ chmod((stat($session->{baseline}))[2] & 0777, $export_db)
Branch: 1 branch
BranchTrueFalseTotal
chmod +(stat $session->{"baseline"})[2] & 511, $export_db or die "Cannot preserve database permissions: $!\n" 10 0 10
348 or die "Cannot preserve database permissions: $!\n";
349 my $count = Finance::Tiller2QIF::WriteQIF::Emit($export_db, $export_qif,
350 10 $options{verbose}, $options{qifdate});
351 chmod((stat($options{output}))[2] & 0777, $export_qif)
352 9 ▶ or die "Cannot preserve output permissions: $!\n" if -e $options{output};
Branch: 1 branch
BranchTrueFalseTotal
if -e $options{"output"} 8 1 9
Condition60%3/5
-e $options{'output'} && (chmod((stat $options{'output'})[2] & 511, $export_qif) || die("Cannot preserve output permissions: $!\n"))
!ll
180
chmod +(stat $options{"output"})[2] & 511, $export_qif or die "Cannot preserve output permissions: $!\n"
l!l
80
MC/DC0%0/2
-e $options{'output'} && (chmod((stat $options{'output'})[2] & 511, $export_qif) || die("Cannot preserve output permissions: $!\n"))
-e $options{'output'} chmod +(stat $options{"output"})[2] & 511, $export_qif
Truth table33%1/3
A && B
A= -e $options{'output'}B= chmod +(stat $options{"output"})[2] & 511, $export_qif
ABresult
0X0
100
111
353 # Windows cannot replace a database while SQLite has it open. The session
354 # lock remains held, and _publish rechecks the original file digests.
355 9 ▶ $guard->disconnect if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 9 9
356 9 _publish($session, $export_db, $export_qif);
357 5 say "QIF written: $options{output}, $count records emitted! Session committed.";
358 5 return 1;
359 }
360 }
361  
362 2 1;
« blib/lib/Finance/Tiller2QIF/Map.pm ↑ Summary blib/lib/Finance/Tiller2QIF/ReadCSV.pm »