CC: | zmughal [...] cpan.org |
Subject: | directory URI used as a base URI |
I noticed a use case when a directory is used as a base URI that is not covered
by the current uri() function.
I have attached a patch from
<https://github.com/zmughal/Path-Class-URI/commit/505b89f56ceed229aae8c2168a6ce57eb237ec2e>
to this ticket for your review. The tests that I have added demonstrate the
behaviour I want to support.
Cheers,
- zaki
Subject: | path-class-uri-base.patch |
diff --git a/lib/Path/Class/URI.pm b/lib/Path/Class/URI.pm
index c30230b..c40f6be 100644
--- a/lib/Path/Class/URI.pm
+++ b/lib/Path/Class/URI.pm
@@ -24,6 +24,7 @@ sub Path::Class::Entity::uri {
my $self = shift;
my $path = $self->stringify;
$path =~ tr!\\!/! if $^O eq "MSWin32";
+ $path .= '/' if $self->isa('Path::Class::Dir'); # preserve directory if used as base URI
if ($self->is_absolute) {
return URI->new("file://$path");
} else {
diff --git a/t/03_uri_base.t b/t/03_uri_base.t
new file mode 100644
index 0000000..5ae453a
--- /dev/null
+++ b/t/03_uri_base.t
@@ -0,0 +1,38 @@
+use strict;
+use Test::Base;
+
+use Path::Class;
+use Path::Class::URI;
+plan tests => 2 * blocks;
+
+filters 'chomp';
+filters { input => 'eval' };
+
+run {
+ my $block = shift;
+ my $f = 'file';
+ my $base_path = $block->input;
+ my $abs = URI->new_abs($f, $base_path->uri);
+ is $abs, $block->expected;
+
+ my $abs_path = file_from_uri($abs);
+ is $abs_path, $block->abs_path;
+};
+
+__END__
+
+===
+--- input
+Path::Class::dir('/path/to');
+--- expected
+file:///path/to/file
+--- abs_path
+/path/to/file
+
+===
+--- input
+Path::Class::file('/path/to/other_file');
+--- expected
+file:///path/to/file
+--- abs_path
+/path/to/file