Skip to content

Commit

Permalink
Merge pull request #58 from nyarla/kalaclista-context-path
Browse files Browse the repository at this point in the history
feat(Kalaclista::Context::Path): add new class for filepath context
  • Loading branch information
nyarla authored Mar 4, 2024
2 parents f6be382 + 87c6b2e commit f839807
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/Kalaclista/Context/Path.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package Kalaclista::Context::Path;

use v5.38;

use Exporter::Lite ();
use Carp qw(croak);

use Kalaclista::Path;

our @EXPORT = qw(rootdir);

sub rootdir { state $root ||= shift; $root }

sub import {
croak qq|you can't pass to more than 1 arguments| if @_ > 2;
croak qq|missing regexp to detect rootdir; usage: 'use @{[ __PACKAGE__ ]} qr{^lib\$}'| if @_ != 2;

rootdir( Kalaclista::Path->detect( pop @_ ) );

my $exporter = Exporter::Lite->can('import');
goto $exporter;
}

1;
26 changes: 26 additions & 0 deletions t/Kalaclista-Context-Path/00_compile.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test2::V0;

subtest failed => sub {
my $failure = dies {
eval 'use Kalaclista::Context::Path';
die $@ if $@;
};

like $failure, qr|^missing regexp to detect rootdir; usage: 'use Kalaclista::Context::Path qr\{\^lib\$\}'|;
};

subtest ok => sub {
my $success = lives {
eval 'use Kalaclista::Context::Path qr{^t$}';
die $@ if $@;
};

ok $success;
};

done_testing;
18 changes: 18 additions & 0 deletions t/Kalaclista-Context-Path/10_rootdir.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test2::V0;

use Kalaclista::Path;
use Kalaclista::Context::Path qr{^t$};

subtest rootdir => sub {
my $test = rootdir;
my $expected = Kalaclista::Path->detect(qr{^t$});

is $test->to_string, $expected->to_string;
};

done_testing;

0 comments on commit f839807

Please sign in to comment.