blib/lib/App/karr/Task.pm

filter: Statementstmt 77.0% 154 / 200 Branchbran 44.4% 40 / 90 Conditioncond 16.6% 7 / 42 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 86.1% 31 / 36 Podpod 92.8% 13 / 14 totaltotal 60.7% 246 / 405 CC 64 SCAR 32.7
CC
64
Cov
61%
update_timestamps
156.0
_normalize_lists
27.7
append_body
20.0
CRAP 26.4
« blib/lib/App/karr/Encoding.pm ↑ Summary blib/lib/App/karr/Cmd/Materialize.pm »
1 # ABSTRACT: Task object representing a single kanban card
2  
3 package App::karr::Task;
4 9 our $VERSION = '0.602';
5 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use App::karr::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use App::karr::Encoding qw( yaml_dump yaml_load repair_mojibake );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # Imported empty on purpose: App::karr::Encoding owns every JSON crossing, so
12 # the encode_json/decode_json this module would otherwise pull in must not be
13 # reachable here. Only the boolean singletons are wanted -- see L</to_json_hash>.
14 9 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 9 has id => ( is => 'ro', required => 1 );
18 9 has title => ( is => 'rw', required => 1 );
19 5 ▶has status => ( is => 'rw', default => sub { 'backlog' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
20 10 ▶has priority => ( is => 'rw', default => sub { 'medium' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
21 9 has assignee => ( is => 'rw', predicate => 1, clearer => 1 );
22 22 ▶has tags => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23 9 has due => ( is => 'rw', predicate => 1, clearer => 1 );
24 9 has estimate => ( is => 'rw', predicate => 1, clearer => 1 );
25 11 ▶has class => ( is => 'rw', default => sub { 'standard' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
26 9 has parent => ( is => 'rw', predicate => 1, clearer => 1 );
27 24 ▶has depends_on => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
28 9 ▶has body => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 11 ▶has created => ( is => 'ro', default => sub { gmtime->datetime . 'Z' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
30 11 ▶has updated => ( is => 'rw', default => sub { gmtime->datetime . 'Z' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
31 9 has claimed_by => ( is => 'rw', predicate => 1, clearer => 1 );
32 9 has claimed_at => ( is => 'rw', predicate => 1, clearer => 1 );
33 9 has blocked => ( is => 'rw', predicate => 1, clearer => 1 );
34 9 has block_reason => ( is => 'rw', predicate => 1, clearer => 1 );
35 9 has started => ( is => 'rw', predicate => 1, clearer => 1 );
36 9 has completed => ( is => 'rw', predicate => 1, clearer => 1 );
37 11 ▶has extra => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
38 9 has file_path => ( is => 'rw', predicate => 1 );
39  
40  
41 # Every frontmatter key karr models itself. Anything else read from a document
42 # goes to L</extra> instead of being silently dropped.
43 9 my @FRONTMATTER_FIELDS = qw(
44 id title status priority created updated started completed
45 assignee tags due estimate parent depends_on
46 blocked block_reason claimed_by claimed_at class
47 );
48 9 my %IS_FRONTMATTER_FIELD = map { $_ => 1 } @FRONTMATTER_FIELDS;
49  
50 9 ▶use constant MAX_SLUG_LENGTH => 50;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # Optional fields are addressed through their predicate everywhere (pick,
53 # board, list, show, handoff all treat has_X as "is this set"). Clearing one
54 # by assigning undef would leave the predicate true, so callers must use the
55 # generated clear_X. This guards the load path: a document that carries an
56 # explicit null, or a value with no length at all, is normalized back to
57 # "unset" instead of lingering as has_X-true-but-empty.
58 #
59 # The empty case is the interop one. Every optional field here is `omitempty`
60 # in kanban-md's Go struct, so "absent" and "present but empty" are the same
61 # state on that side of the boundary -- while on this side Moo's predicate
62 # calls the second one set. A hand-written or third-party card carrying
63 # `claimed_by: ""` therefore looked claimed to `board`, blocked a
64 # require_claim move in `move`/`edit`, printed "Claimed:" with nothing after
65 # it in `show`, and counted as an engaged card toward karr-foundation's
66 # auto-block. #59 patched three of those readers one at a time; normalizing
67 # once here is the same fix for all of them, including the ones nobody has
68 # written yet (ticket #98).
69 #
70 # Emptiness is length, never truth: `0` and `"0"` are one character long and
71 # have to survive, which is the trap that gave ticket #78 its "body 0" row.
72 24 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.4
Pod undocumented
73 24 my ($self) = @_;
74 24 for my $attr (qw( assignee due estimate parent claimed_by claimed_at blocked block_reason started completed )) {
75 240 my $clearer = "clear_$attr";
76 240 my $has = "has_$attr";
77 240 ▶ next unless $self->$has;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$has 233 7 240
78 7 my $value = $self->$attr;
79 7 ▶ $self->$clearer if !defined $value || !length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 7 7
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
007
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
80 }
81 24 $self->_normalize_lists;
82 24 $self->_normalize_blocked;
83 }
84  
85 # The two list-valued fields, guarded here for the same reason the optional
86 # scalars are cleared above: the parse gate is the only gate. A scalar where
87 # the list belongs -- `tags: urgent`, `depends_on: 1`, both only writable by
88 # hand or by a third tool, since every karr write goes through to_frontmatter
89 # -- used to pass construction and die mid-write at the dereference in
90 # to_frontmatter, as a raw Perl error carrying a source location (the #77
91 # class), and on the import path after refs had already started moving, which
92 # broke serialize_from's all-or-nothing promise (#70). A field whose whole
93 # meaning is "a list" refuses a single value as a usage error at parse time,
94 # where from_file can still name the file (ticket #125).
95 #
96 # An empty or null value is not a scalar value. Every list field is `omitempty`
97 # in kanban-md's Go struct, so "present but empty" is the same state as
98 # "absent" (the #98 rule) and loads as the empty list.
99 24 ▶sub _normalize_lists {
Subroutine _normalize_lists: called CC=7 SCAR=33.2
100 24 my ($self) = @_;
101 24 for my $attr (qw( tags depends_on )) {
102 48 my $value = $self->$attr;
103 48 ▶ next if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 48 0 48
104 0 ▶ if ( !defined $value || ( !ref $value && !length $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and ref $value || length $value) 0 0 0
Condition0%0/6
defined $value and ref $value || length $value
!ll&&!rl&&r
000
ref $value or length $value
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $value and ref $value || length $value
defined $value ref $value length $value
Truth table0%0/4
A and B or C
A= defined $valueB= ref $valueC= length $value
ABCresult
0XX0
1000
11X1
1011
105 0 $self->$attr([]);
106 0 next;
107 }
108 0 ▶ die "Frontmatter field '$attr' must be a list"
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 0 0
109 . ( ref $value ? '' : ', not a single value' ) . "\n";
110 }
111 }
112  
113 # karr up to 0.402 stored the blocking *reason* in `blocked` as free text;
114 # kanban-md has always had `blocked: bool` plus `block_reason: string`, and its
115 # YAML decoder refuses a string there outright ("cannot unmarshal !!str into
116 # bool" -- the task then vanishes from its board). This pulls a legacy document
117 # into the kanban-md shape on read, which is why no migration command is needed:
118 # the next write of that task emits the new shape (ticket #58).
119 #
120 # The invariant everything downstream relies on: has_blocked is true if and only
121 # if the task is blocked. "Blocked but false" is not representable, matching the
122 # `omitempty` on kanban-md's Blocked field.
123 24 ▶sub _normalize_blocked {
Subroutine _normalize_blocked: called CC=6 SCAR=20.8
124 24 my ($self) = @_;
125 24 ▶ return unless $self->has_blocked;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_blocked 23 1 24
126 1 my $raw = $self->blocked;
127  
128 # A boolean object from some other YAML loader.
129 1 ▶ return $self->_set_blocked_flag($raw) if ref $raw;
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw 0 1 1
130  
131 # YAML::XS loads `blocked: false` as the empty string, so this covers the
132 # honest boolean false as well as an explicitly empty value.
133 1 ▶ return $self->_set_blocked_flag(0) if !length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw 0 1 1
134  
135 1 my $bool = eval { App::karr::Config->parse_bool($raw) };
136 1 ▶ return $self->_set_blocked_flag($bool) if defined $bool;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bool 1 0 1
137  
138 # Not a boolean spelling, so it is a legacy reason string.
139 0 $self->_set_blocked_flag(1);
140 0 ▶ $self->block_reason($raw) unless $self->has_block_reason;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_block_reason 0 0 0
141 0 return;
142 }
143  
144 # Note the asymmetry with L</unblock>: a document that says `blocked: false`
145 # while still carrying a `block_reason` keeps that reason, because dropping it
146 # would be exactly the silent frontmatter deletion of ticket #69. Only an
147 # explicit unblock throws the reason away.
148 1 ▶sub _set_blocked_flag {
Subroutine _set_blocked_flag: called CC=2 SCAR=7.2
149 1 my ( $self, $value ) = @_;
150 1 ▶ return $value ? $self->blocked(!!1) : $self->clear_blocked;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 0 1
151 }
152  
153 1 ▶sub block {
Subroutine block: called CC=3 SCAR=12.3
Pod documented
154 1 my ( $self, $reason ) = @_;
155 1 $self->blocked(!!1);
156 1 ▶ if ( defined $reason && length $reason ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason and length $reason) { } 1 0 1
Condition33%1/3
defined $reason and length $reason
!ll&&!rl&&r
001
MC/DC0%0/2
defined $reason and length $reason
defined $reason length $reason
Truth table33%1/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
157 1 $self->block_reason($reason);
158 } else {
159 0 $self->clear_block_reason;
160 }
161 1 return $self;
162 }
163  
164  
165 0 ▶sub unblock {
Subroutine unblock: not called CC=1 SCAR=6.9
Pod documented
166 0 my ($self) = @_;
167 0 $self->clear_blocked;
168 0 $self->clear_block_reason;
169 0 return $self;
170 }
171  
172  
173 # kanban-md's AppendBody (internal/board/mutate.go:571): the trailing newlines
174 # come off the existing body and the two halves are joined with a blank line.
175 # karr joined with a single "\n" until ticket #238, and Markdown folds a single
176 # newline into a space -- so every note appended by `karr edit -a` or
177 # `karr handoff --note` was rendered into the paragraph above it, and a card
178 # that collected notes over a session ended up as one block.
179 #
180 # Emptiness is measured *after* the trim, which is where this parts company with
181 # the reference: Go tests `existing != ""` first, so a body that is nothing but
182 # newlines still gets the separator and comes back with a leading blank line.
183 # That body cannot arrive from the board -- _parse_content strips trailing
184 # newlines, the normal form ticket #78 settled -- but it can arrive from a
185 # caller in process, and "an empty body gains no separator" is the whole point
186 # of the branch. Only newlines are trimmed, never other whitespace: an indented
187 # last line is content.
188 #
189 # length, not truth, on the trimmed body: appending to a body of "0" must
190 # preserve it (ticket #78).
191 0 ▶sub append_body {
Subroutine append_body: not called CC=4 SCAR=30.0
Pod documented
192 0 my ( $self, $text, $timestamp ) = @_;
193 # The handoff form, deliberately not kanban-md's: karr writes every stamp in
194 # UTC (App::karr::Role::ClaimTimeout says why), and `[[2006-01-02]]` is
195 # wiki-link syntax that means nothing here. Opt-in at both call sites -- an
196 # inline prefix would turn `-a "## Findings"` into a line that is no longer a
197 # heading, so it cannot be forced on every append.
198 0 ▶ $text = gmtime->strftime('%Y-%m-%d %H:%M') . ' ' . $text if $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp 0 0 0
199 0 ▶ my $body = defined $self->body ? $self->body : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->body ? : 0 0 0
200 0 $body =~ s/\n+\z//;
201 0 ▶ $self->body( length $body ? $body . "\n\n" . $text : $text );
Branch: 1 branch
BranchTrueFalseTotal
length $body ? : 0 0 0
202 0 return $self;
203 }
204  
205  
206 0 ▶sub update_timestamps {
Subroutine update_timestamps: not called CC=12 SCAR=50.5
Pod documented
207 0 my ( $self, $old_status, $new_status, $first_status, $config ) = @_;
208 0 my $now = gmtime->datetime . 'Z';
209  
210 # Called on the class, is_terminal_status answers for the default board --
211 # the literal `done` and `archived`. That is all this method could ever ask
212 # before the last #67 leftover fell, so a board whose final column is named
213 # anything else recorded no completion at all: `karr move 1 shipped` stamped
214 # `started` and left `completed` unset for ever, and every reader built on
215 # it (metrics, context's recently-completed) saw an empty set. Hand the
216 # board's own App::karr::Config in and its statuses decide instead.
217 0 ▶ $config //= 'App::karr::Config';
Condition0%0/2
$config //= "App::karr::Config"
l!l
00
MC/DC0%0/1
$config //= "App::karr::Config"
$config
Truth table0%0/2
A
A= $config
Aresult
11
00
218  
219 # First move out of the board's first status starts the clock, and never
220 # restarts it.
221 0 ▶ if ( !$self->has_started
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_started and defined $first_status and defined $old_status and $old_status eq $first_status and $new_status ne $first_status) 0 0 0
Condition0%0/12
not $self->has_started and defined $first_status and defined $old_status and $old_status eq $first_status and $new_status ne $first_status
!ll&&!rl&&r
000
not $self->has_started and defined $first_status and defined $old_status and $old_status eq $first_status
!ll&&!rl&&r
000
not $self->has_started and defined $first_status and defined $old_status
!ll&&!rl&&r
000
not $self->has_started and defined $first_status
!ll&&!rl&&r
000
MC/DC0%0/5
not $self->has_started and defined $first_status and defined $old_status and $old_status eq $first_status and $new_status ne $first_status
not $self->has_started defined $first_status defined $old_status $old_status eq $first_status $new_status ne $first_status
Truth table0%0/6
not(A) and B and C and D and E
A= not $self->has_startedB= defined $first_statusC= defined $old_statusD= $old_status eq $first_statusE= $new_status ne $first_status
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
222 && defined $first_status
223 && defined $old_status
224 && $old_status eq $first_status
225 && $new_status ne $first_status )
226 {
227 0 $self->started($now);
228 }
229  
230 0 ▶ if ( $config->is_terminal_status($new_status) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->is_terminal_status($new_status)) { } 0 0 0
elsif (defined $old_status and $config->is_terminal_status($old_status)) { } 0 0 0
231 0 ▶ $self->completed($now) unless $self->has_completed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_completed 0 0 0
232 # A task dragged straight to done never passed through in-progress, so it
233 # has no start; without this its cycle time would be unmeasurable.
234 0 ▶ $self->started($now) unless $self->has_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_started 0 0 0
235 ▶ } elsif ( defined $old_status
Condition0%0/3
defined $old_status and $config->is_terminal_status($old_status)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $old_status and $config->is_terminal_status($old_status)
defined $old_status $config->is_terminal_status($old_status)
Truth table0%0/3
A and B
A= defined $old_statusB= $config->is_terminal_status($old_status)
ABresult
0X0
100
111
236 && $config->is_terminal_status($old_status) )
237 {
238 # Reopening. `started` is deliberately kept: the work did begin then.
239 0 $self->clear_completed;
240 }
241  
242 0 return $self;
243 }
244  
245  
246 9 ▶sub slug {
Subroutine slug: called CC=4 SCAR=21.2
Pod documented
247 9 my ($self) = @_;
248 9 my $slug = lc($self->title);
249 9 $slug =~ s/[^a-z0-9]+/-/g;
250 9 $slug =~ s/^-|-$//g;
251 9 ▶ return $slug if length($slug) <= MAX_SLUG_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
if length $slug <= 50 9 0 9
252  
253 # Truncate on a word boundary the way kanban-md's GenerateSlug does
254 # (internal/task/slug.go): cutting mid-word backs up to the last dash, and a
255 # cut that already landed on one keeps the whole final word. A hard cut at 50
256 # gave the same task two different filenames in a shared tasks/ directory.
257 0 my $truncated = substr( $slug, 0, MAX_SLUG_LENGTH );
258 0 ▶ if ( substr( $slug, MAX_SLUG_LENGTH, 1 ) ne '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($slug, 50, 1) ne "-") 0 0 0
259 0 my $idx = rindex( $truncated, '-' );
260 0 ▶ $truncated = substr( $truncated, 0, $idx ) if $idx > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx > 0 0 0 0
261 }
262 0 $truncated =~ s/-+\z//;
263 0 return $truncated;
264 }
265  
266  
267 7 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
268 7 my ($self) = @_;
269 7 return sprintf('%03d-%s.md', $self->id, $self->slug);
270 }
271  
272  
273 11 ▶sub to_frontmatter {
Subroutine to_frontmatter: called CC=13 SCAR=26.0
Pod documented
274 11 my ($self) = @_;
275 # Passthrough keys form the base so a modelled field always wins the slot it
276 # owns, and so a field that has since been cleared cannot be resurrected by a
277 # stale copy in extra.
278 11 my %fm = %{ $self->extra };
279 11 delete @fm{@FRONTMATTER_FIELDS};
280  
281 11 %fm = (
282 %fm,
283 id => $self->id,
284 title => $self->title,
285 status => $self->status,
286 priority => $self->priority,
287 created => $self->created,
288 updated => $self->updated,
289 class => $self->class,
290 );
291 11 ▶ $fm{assignee} = $self->assignee if $self->has_assignee;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_assignee 1 10 11
292 11 ▶ $fm{tags} = $self->tags if @{$self->tags};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->tags;} 1 10 11
293 11 ▶ $fm{due} = $self->due if $self->has_due;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_due 0 11 11
294 11 ▶ $fm{estimate} = $self->estimate if $self->has_estimate;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_estimate 0 11 11
295 11 ▶ $fm{parent} = $self->parent if $self->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_parent 0 11 11
296 11 ▶ $fm{depends_on} = $self->depends_on if @{$self->depends_on};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->depends_on;} 0 11 11
297 11 ▶ $fm{claimed_by} = $self->claimed_by if $self->has_claimed_by;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_claimed_by 2 9 11
298 11 ▶ $fm{claimed_at} = $self->claimed_at if $self->has_claimed_at;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_claimed_at 1 10 11
299 11 ▶ $fm{blocked} = $self->blocked if $self->has_blocked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_blocked 1 10 11
300 11 ▶ $fm{block_reason} = $self->block_reason if $self->has_block_reason;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_block_reason 1 10 11
301 11 ▶ $fm{started} = $self->started if $self->has_started;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_started 0 11 11
302 11 ▶ $fm{completed} = $self->completed if $self->has_completed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_completed 0 11 11
303 11 return \%fm;
304 }
305  
306  
307  
308 0 ▶sub to_json_hash {
Subroutine to_json_hash: not called CC=4 SCAR=30.0
Pod documented
309 0 my ($self) = @_;
310 0 my $data = $self->to_frontmatter;
311 # to_frontmatter only ever puts a true value here, and it has to become a
312 # real JSON boolean rather than a Perl one: an older JSON backend would
313 # encode Perl's !!1 as the number 1.
314 0 ▶ $data->{blocked} = JSON::MaybeXS::true() if exists $data->{blocked};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"blocked"} 0 0 0
315 0 ▶ $data->{body} = $self->body if defined $self->body && length $self->body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->body and length $self->body 0 0 0
Condition0%0/3
defined $self->body and length $self->body
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->body and length $self->body
defined $self->body length $self->body
Truth table0%0/3
A and B
A= defined $self->bodyB= length $self->body
ABresult
0X0
100
111
316 0 return $data;
317 }
318  
319 10 ▶sub to_markdown {
Subroutine to_markdown: called CC=4 SCAR=13.9
Pod documented
320 10 my ($self) = @_;
321 10 my $yaml = yaml_dump($self->to_frontmatter);
322 10 $yaml =~ s/\A---\n//;
323 10 my $md = "---\n${yaml}---\n";
324 10 my $body = $self->body;
325 10 ▶ if ( defined $body && length $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and length $body) 3 7 10
Condition66%2/3
defined $body and length $body
!ll&&!rl&&r
073
MC/DC50%1/2
defined $body and length $body
defined $body length $body
Truth table66%2/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
326 3 $md .= "\n" . $body;
327 # kanban-md's Write terminates the body only when it is not already
328 # terminated (internal/task/file.go); matching it keeps a document karr
329 # rewrites byte-identical to the one kanban-md would have written.
330 3 ▶ $md .= "\n" unless $body =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /\n\z/ 3 0 3
331 }
332 10 return $md;
333 }
334  
335  
336 # One card, one line. The format is `list --compact`'s, and it lives here
337 # rather than in that command because `show --compact` prints the same line
338 # (#254) and two commands spelling one printf out separately is how two
339 # renderings of the same thing start to drift. Callers add the newline.
340 #
341 # Fixed-width on the two fields whose width is bounded, free on the one whose
342 # width is not: the id column pads to four so short and long ids stay aligned
343 # and a five-digit board simply widens the row, and the status is
344 # right-aligned to the width of the longest default status name. The title is
345 # printed as it stands, never truncated -- what a compact rendering saves is
346 # lines, not the content of a line.
347 0 ▶sub compact_line {
Subroutine compact_line: not called CC=1 SCAR=6.9
Pod documented
348 0 my ($self) = @_;
349 0 return sprintf '#%-4u %10s %s', $self->id, $self->status, $self->title;
350 }
351  
352  
353 13 ▶sub _parse_content {
Subroutine _parse_content: called CC=3 SCAR=11.2
354 13 my ($class, $content) = @_;
355 # The closing delimiter is anchored to the start of a line (/m), the way
356 # kanban-md's splitFrontmatter scans for a literal "\n---\n". Without the
357 # anchor a frontmatter value that merely *ends* in "---" -- `blocked:
358 # waiting ---`, which YAML::XS dumps unquoted -- terminated the frontmatter
359 # mid-line, and the truncated document then failed Task->new with "Missing
360 # required arguments: id, title". Every command that loads the board hit it,
361 # `delete` included, so the board could not be repaired with karr at all
362 # (ticket #52).
363 13 ▶ my ($yaml, $body) = $content =~ m{\A---\n(.+?)^---[ \t]*(?:\n(.*))?\z}ms
Branch: 1 branch
BranchTrueFalseTotal
() = $content =~ /\A---\n(.+?)^---[ \t]*(?:\n(.*))?\z/ms or die "Invalid task format\n" 13 0 13
364 or die "Invalid task format\n";
365 13 ▶ $body //= '';
Condition50%1/2
$body //= ""
l!l
130
MC/DC0%0/1
$body //= ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
366 13 $body =~ s/^\n//;
367 # Every trailing newline, not one. The file path and the ref path disagreed
368 # otherwise: App::karr::Git::read_ref_with_oid chomps the blob before the
369 # document reaches us, so a single strip here left the ref round trip one
370 # newline shorter than the file round trip, and a body ending in blank lines
371 # lost one of them per save (ticket #78). Stripping greedily makes both paths
372 # agree on the same normal form -- a karr body never ends in a newline.
373 13 $body =~ s/\n+\z//;
374 13 return (yaml_load($yaml), $body);
375 }
376  
377 # Split a parsed frontmatter hash into constructor arguments and passthrough
378 # keys. Moo drops unknown constructor arguments without a word, so anything not
379 # separated out here is deleted from the board on the next write (ticket #69).
380 13 ▶sub _split_frontmatter {
Subroutine _split_frontmatter: called CC=3 SCAR=11.3
381 13 my ($class, $fm) = @_;
382 13 my ( %args, %extra );
383 13 for my $key ( keys %$fm ) {
384 97 ▶ if ( $IS_FRONTMATTER_FIELD{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_FRONTMATTER_FIELD{$key}) { } 97 0 97
385 97 $args{$key} = $fm->{$key};
386 } else {
387 0 $extra{$key} = $fm->{$key};
388 }
389 }
390 13 return ( \%args, \%extra );
391 }
392  
393 2 ▶sub from_string {
Subroutine from_string: called CC=2 SCAR=7.0
Pod documented
394 2 my ($class, $content, %opt) = @_;
395 2 my ($fm, $body) = $class->_parse_content($content);
396 # repair_frontmatter is set by App::karr::Git::load_task_ref for a board
397 # written before refs/karr/meta/encoding existed. Only the frontmatter is
398 # repaired: it went through YAML::XS::Dump, which encoded the already-encoded
399 # octets a second time. The body was concatenated onto the document verbatim
400 # and is single-encoded, so touching it would corrupt it (ticket #53).
401 2 ▶ $fm = repair_mojibake($fm) if $opt{repair_frontmatter};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"repair_frontmatter"} 0 2 2
402 2 my ( $args, $extra ) = $class->_split_frontmatter($fm);
403 2 return $class->new(%$args, extra => $extra, body => $body);
404 }
405  
406  
407 11 ▶sub from_file {
Subroutine from_file: called CC=3 SCAR=13.6
Pod documented
408 11 my ($class, $file) = @_;
409 11 $file = path($file);
410 # Every failure names the file. `karr import` parses a whole directory in one
411 # go, and a bare "Invalid task format" -- which is also what a CRLF card gets,
412 # since _parse_content requires a literal "\A---\n" for kanban-md parity --
413 # left the user to guess which card it came from (ticket #70).
414 11 my $task = eval {
415 11 my ($fm, $body) = $class->_parse_content($file->slurp_utf8);
416 11 my ( $args, $extra ) = $class->_split_frontmatter($fm);
417 11 $class->new(%$args, extra => $extra, body => $body, file_path => $file);
418 };
419 11 ▶ return $task if $task;
Branch: 1 branch
BranchTrueFalseTotal
if $task 11 0 11
420 0 ▶ my $why = $@ || 'unknown error';
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
421 0 chomp $why;
422 # Moo's "Missing required arguments: id, title" carries an "at <file> line N"
423 # pointing into generated constructor code, which only buries the filename
424 # that matters.
425 0 $why =~ s/ at .+ line \d+\.?\z//s;
426 0 die "$why ($file)\n";
427 }
428  
429  
430 10 ▶sub save {
Subroutine save: called CC=4 SCAR=14.0
Pod documented
431 10 my ($self, $dir) = @_;
432 10 ▶ croak "Task has no file_path; ref-backed tasks must be persisted via BoardStore/save_task"
Branch: 1 branch
BranchTrueFalseTotal
unless $dir or $self->has_file_path 0 10 10
Condition66%2/3
$dir or $self->has_file_path
l!l&&r!l&&!r
640
MC/DC0%0/2
$dir or $self->has_file_path
$dir $self->has_file_path
Truth table66%2/3
A or B
A= $dirB= $self->has_file_path
ABresult
1X1
011
000
433 if !$dir && !$self->has_file_path;
434 10 ▶ my $file = $dir ? path($dir)->child($self->filename) : path($self->file_path);
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 6 4 10
435 10 $file->spew_utf8($self->to_markdown);
436 10 $self->file_path($file);
437 10 return $file;
438 }
439  
440  
441 9 1;
442  
443 __END__
« blib/lib/App/karr/Encoding.pm ↑ Summary blib/lib/App/karr/Cmd/Materialize.pm »