lib/Database/BI/CacheDir.pm

Structural Coverage (Approximate)

TER1 (Statement): 96.15%
TER2 (Branch): 88.46%
TER3 (LCSAJ): 100.0% (4/4)
Approximate LCSAJ segments: 27

LCSAJ Legend

โ— Covered โ€” this LCSAJ path was executed during testing.

โ— Not covered โ€” this LCSAJ path was never executed. These are the paths to focus on.

Multiple dots on a line indicate that multiple control-flow paths begin at that line. Hovering over any dot shows:

        start โ†’ end โ†’ jump
        

Uncovered paths show [NOT COVERED] in the tooltip.

Mutant Testing Legend

Survived (tests missed this) Killed (tests detected this) No mutation
    1: package Database::BI::CacheDir;
    2: 
    3: use strict;
    4: use warnings;
    5: 
    6: use version; our $VERSION = version->declare('0.025.0');
    7: 
    8: use Carp	qw(carp);
    9: use Mojo::File	();
   10: use Readonly;
   11: 
   12: # The configuration file the application reads when MOJO_CONFIG does not name
   13: # another (Mojolicious takes it from the moniker).
   14: Readonly my $CONFIG_FILE => 'database_bi.conf';
   15: 
   16: # The environment variables read after Object::Configure, in order.
   17: Readonly my @ENV_KEYS => qw(CACHEDIR CACHE_DIR);
   18: 
   19: =head1 NAME
   20: 
   21: Database::BI::CacheDir - Where a cache keeps its files
   22: 
   23: =head1 VERSION
   24: 
   25: 0.025.0
   26: 
   27: =head1 SYNOPSIS
   28: 
   29:     use Database::BI::CacheDir;
   30: 
   31:     sub new {
   32:         my ($class, %args) = @_;
   33:         my $dir = Database::BI::CacheDir::resolve(
   34:             class   => $class,
   35:             params  => \%args,
   36:             default => '.bi/geocode-cache',
   37:         );
   38:         ...
   39:     }
   40: 
   41: =head1 DESCRIPTION
   42: 
   43: Every object in this distribution that keeps a cache on disc asks C<resolve()>
   44: for its directory when it is made, so one setting moves them all.
   45: 
   46: =head1 SUBROUTINES
   47: 
   48: =head2 resolve
   49: 
   50: Returns the first of these that is set:
   51: 
   52: =over 4
   53: 
   54: =item 1. C<cache_dir> from L<Object::Configure> for C<class>: its config files
   55: (F<~/.conf/E<lt>classE<gt>.yml> and the rest), then the environment variable
   56: C<E<lt>ClassE<gt>__cache_dir> (C<::> written C<__>, e.g.
   57: C<Database__BI__Model__Geocode__cache_dir>).  A C<cache_dir> in C<params> is
   58: what those fall back on;
   59: 
   60: =item 2. C<$ENV{CACHEDIR}>;
   61: 
   62: =item 3. C<$ENV{CACHE_DIR}>;
   63: 
   64: =item 4. C<cache_dir> in F<database_bi.conf>; a relative one is under the
   65: application home;
   66: 
   67: =item 5. C<default>.
   68: 
   69: =back
   70: 
   71: The directory is not made here; the cache makes it when it first stores
   72: something.
   73: 
   74: =head3 API SPECIFICATION
   75: 
   76: =head4 INPUT
   77: 
   78:     class   => string,   # optional; the class being made, for Object::Configure
   79:     params  => hashref,  # optional; the arguments to its new(), read for cache_dir
   80:     config  => hashref,  # optional; the application's config; when the key is absent, database_bi.conf is read (see bi_config)
   81:     home    => string,   # optional; the folder a relative config cache_dir is under
   82:     default => string,   # optional; the directory when nothing else is set
   83: 
   84: =head4 OUTPUT
   85: 
   86: A directory name, or C<undef> when nothing is set and there is no C<default>.
   87: A config file Object::Configure cannot read is reported with C<carp> and
   88: skipped.
   89: 
   90: =cut
   91: 
   92: # resolve(%args) -> $directory | undef
   93: #
   94: # The one place the directory a cache keeps its files in is chosen, so every
   95: # object that caches on disc reads the same settings in the same order:
   96: #
   97: #   1. Object::Configure, for the class being made (its config files, then the
   98: #      environment variable <Class__Name>__cache_dir; a cache_dir the caller
   99: #      passed is what they fall back on, as Object::Configure always does);
  100: #   2. $ENV{CACHEDIR};
  101: #   3. $ENV{CACHE_DIR};
  102: #   4. cache_dir in database_bi.conf (relative: under the application home);
  103: #   5. the caller's default (relative: as given).
  104: #
  105: # Object::Configure is given an array for its logger, which no config file can
  106: # replace: a universal.yml that logs to syslog would otherwise be opened for
  107: # every object made.  A config file it cannot read is reported and skipped, so
  108: # a broken ~/.conf costs the cache its setting, not the page.
  109: sub resolve {
โ—110 โ†’ 115 โ†’ 130  110: 	local ($@, $!);
  111: 	my (%args) = @_;
  112: 
  113: 	my $params = $args{params} // {};
  114: 
  115: 	if (defined(my $class = $args{class})) {

Mutants (Total: 1, Killed: 1, Survived: 0)

116: my $configured; 117: my $ok = eval { 118: require Object::Configure; 119: $configured = Object::Configure::configure($class, { 120: (_set($params->{cache_dir}) ? (cache_dir => $params->{cache_dir}) : ()), 121: logger => [], 122: }); 123: 1; 124: }; 125: carp("Object::Configure could not configure $class: " . ($@ || 'no reason given')) 126: unless $ok; 127: return $configured->{cache_dir}

Mutants (Total: 2, Killed: 2, Survived: 0)

128: if $ok && ref($configured) eq 'HASH' && _set($configured->{cache_dir}); 129: } โ—130 โ†’ 132 โ†’ 136 130: return $params->{cache_dir} if _set($params->{cache_dir});

Mutants (Total: 2, Killed: 0, Survived: 2)
131: 132: for my $key (@ENV_KEYS) { 133: return $ENV{$key} if _set($ENV{$key});

Mutants (Total: 2, Killed: 2, Survived: 0)

134: } 135: โ—136 โ†’ 139 โ†’ 146 136: my ($config, $home) = exists($args{config}) 137: ? ($args{config}, $args{home}) 138: : bi_config(); 139: if (ref($config) eq 'HASH' && _set($config->{cache_dir})) {

Mutants (Total: 1, Killed: 1, Survived: 0)

140: my $dir = Mojo::File->new($config->{cache_dir}); 141: $dir = Mojo::File->new($home)->child($config->{cache_dir}) 142: if !$dir->is_abs && _set($home); 143: return $dir->to_string;

Mutants (Total: 2, Killed: 2, Survived: 0)

144: } 145: 146: return _set($args{default}) ? $args{default} : undef;

Mutants (Total: 2, Killed: 2, Survived: 0)

147: } 148: 149: =head2 bi_config 150: 151: my ($config, $home) = Database::BI::CacheDir::bi_config(); 152: 153: The application's configuration for a caller outside it: the file 154: C<$ENV{MOJO_CONFIG}> names, else F<database_bi.conf> in the current directory. 155: 156: =head3 API SPECIFICATION 157: 158: =head4 INPUT 159: 160: None. 161: 162: =head4 OUTPUT 163: 164: C<($config_hashref, $home)>, where C<$home> is the file's folder; an empty 165: list when there is no file or it does not read (the second is reported with 166: C<carp>). 167: 168: =cut 169: 170: # bi_config() -> ($config_hashref, $home) | () 171: # 172: # database_bi.conf for a caller outside the web application (a script, or a 173: # library user): the file MOJO_CONFIG names, else database_bi.conf in the 174: # current directory. Its folder is the home a relative setting is under. No 175: # file, or one that does not read, gives an empty list. 176: sub bi_config { โ—177 โ†’ 188 โ†’ 193 177: local ($@, $!); 178: my $file = _set($ENV{MOJO_CONFIG}) ? $ENV{MOJO_CONFIG} : $CONFIG_FILE; 179: 180: return () unless -f $file && -r _; 181: 182: my $config; 183: my $ok = eval { 184: require Mojolicious::Plugin::Config; 185: $config = Mojolicious::Plugin::Config->new->load($file, {}, undef); 186: 1; 187: }; 188: unless ($ok) {

Mutants (Total: 1, Killed: 1, Survived: 0)

189: carp("Cannot read $file: " . ($@ || 'no reason given')); 190: return (); 191: } 192: 193: return ($config, Mojo::File->new($file)->to_abs->dirname->to_string); 194: } 195: 196: sub _set { 197: my $value = shift; 198: return defined($value) && !ref($value) && length($value);

Mutants (Total: 2, Killed: 2, Survived: 0)

199: } 200: 201: 1; 202: 203: __END__ 204: 205: =head1 AUTHOR 206: 207: Nigel Horne C<< <njh@nigelhorne.com> >> 208: 209: =head1 LICENCE AND COPYRIGHT 210: 211: Copyright 2026 Nigel Horne. 212: 213: Usage is subject to the GPL2 licence terms. 214: 215: =cut