diff --git a/lib/Kalaclista/Context/Path.pm b/lib/Kalaclista/Context/Path.pm new file mode 100644 index 0000000..5150560 --- /dev/null +++ b/lib/Kalaclista/Context/Path.pm @@ -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; diff --git a/t/Kalaclista-Context-Path/00_compile.t b/t/Kalaclista-Context-Path/00_compile.t new file mode 100644 index 0000000..1219220 --- /dev/null +++ b/t/Kalaclista-Context-Path/00_compile.t @@ -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; diff --git a/t/Kalaclista-Context-Path/10_rootdir.t b/t/Kalaclista-Context-Path/10_rootdir.t new file mode 100644 index 0000000..5b7839d --- /dev/null +++ b/t/Kalaclista-Context-Path/10_rootdir.t @@ -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;