From fc8cf8585c4b4a15cb58f1c1bd0b03cfcd4b33ae Mon Sep 17 00:00:00 2001 From: Sawyer X Date: Mon, 19 Sep 2016 16:42:45 +0200 Subject: [PATCH 1/4] Fix another edge case in plugin compat: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Here's the thing. I don't know how to describe this problem. I never seem to be in the zone enough to fully understand it or explain it. However, I have these bits of gleaned information which I quote below. So, I think it's like this: If you have a plugin that uses DSL directly (which are only available on D2P1, in D2P2 you have to use `$self`⁠⁠), anyou didn't call `register_plugin`, those will not be available yet, and when you try to call them, it will fail that they do not exist. (Which makes sense. We create them for you, with a warning, if you call `⁠register_plug⁠`, which you would do for D2P1 plugin anyway.) However, and here's the kicker, if you do this while defining your `appname` to a class that does not exist, you get a different error. The error you get is that the specified `appname` class cannot call the keyword you wanted. This is because there was one more place in which I didn't resolve GH #1226. Here, then, is the final fix. It should be to just use the found "app_cb_dsl" class, but that broke a test. Ugh. Anyway, crazy, but works. How? Not sure. --- lib/Dancer2/Plugin.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Dancer2/Plugin.pm b/lib/Dancer2/Plugin.pm index fafc30bcb..11ac78abf 100644 --- a/lib/Dancer2/Plugin.pm +++ b/lib/Dancer2/Plugin.pm @@ -539,8 +539,9 @@ sub register_plugin { no strict 'refs'; $plugin_module->can($keyword) or *{"${plugin_module}::$keyword"} = sub { - my $coderef = shift()->app->name->can($keyword); - $coderef->(@_); + $_[0] + ? do { my $cc = shift()->app->name->can($keyword); $cc->(@_) } + : $app_dsl_cb->(@_); }; } }); From 26537004b3e749b98d773d00add9ee4b10ffd724 Mon Sep 17 00:00:00 2001 From: Sawyer X Date: Mon, 19 Sep 2016 16:50:11 +0200 Subject: [PATCH 2/4] add test --- t/plugin2/app_dsl_cb/app_dsl_cb.t | 20 ++++++++++++++++++++ t/plugin2/app_dsl_cb/lib/App.pm | 13 +++++++++++++ t/plugin2/app_dsl_cb/lib/App/TestPlugin.pm | 17 +++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 t/plugin2/app_dsl_cb/app_dsl_cb.t create mode 100644 t/plugin2/app_dsl_cb/lib/App.pm create mode 100644 t/plugin2/app_dsl_cb/lib/App/TestPlugin.pm diff --git a/t/plugin2/app_dsl_cb/app_dsl_cb.t b/t/plugin2/app_dsl_cb/app_dsl_cb.t new file mode 100644 index 000000000..8b90f4100 --- /dev/null +++ b/t/plugin2/app_dsl_cb/app_dsl_cb.t @@ -0,0 +1,20 @@ +use strict; +use warnings; +use lib 't/plugin2/app_dsl_cb/lib'; +use Test::More 'tests' => 4; +use Plack::Test; +use HTTP::Request::Common 'GET'; +use App with => { show_errors => 1 }; +use Test::Fatal 'exception'; + +my $app = App->to_app; +my $test = Plack::Test->create($app); + +my $res; +is( + exception { $res = $test->request( GET '/' ) }, + undef, + 'Did not crash', +); + +is( $res->content, 'GET DONE', 'Ran successfully' ); diff --git a/t/plugin2/app_dsl_cb/lib/App.pm b/t/plugin2/app_dsl_cb/lib/App.pm new file mode 100644 index 000000000..05dbb618c --- /dev/null +++ b/t/plugin2/app_dsl_cb/lib/App.pm @@ -0,0 +1,13 @@ +package App; +use strict; +use warnings; +use Dancer2 appname => 'Other'; +use App::TestPlugin; + +get '/' => sub { + my $res = foo_from_plugin('Foo'); + ::is( $res, 'OK', 'Plugin returned OK' ); + return 'GET DONE'; +}; + +1; diff --git a/t/plugin2/app_dsl_cb/lib/App/TestPlugin.pm b/t/plugin2/app_dsl_cb/lib/App/TestPlugin.pm new file mode 100644 index 000000000..8d3ce7d78 --- /dev/null +++ b/t/plugin2/app_dsl_cb/lib/App/TestPlugin.pm @@ -0,0 +1,17 @@ +package App::TestPlugin; +use strict; +use warnings; +use Dancer2::Plugin; + +plugin_keywords('foo_from_plugin'); + +sub foo_from_plugin { + my ( $self, $arg ) = @_; + ::is( $arg, 'Foo', 'Correct argument to plugin' ); + params(); + return 'OK'; +} + +register_plugin(); + +1; From 82b58e14e294ff9ef1adbcf4eb67497b33007dd6 Mon Sep 17 00:00:00 2001 From: Sawyer X Date: Mon, 19 Sep 2016 17:03:22 +0200 Subject: [PATCH 3/4] Better variable name --- lib/Dancer2/Plugin.pm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Dancer2/Plugin.pm b/lib/Dancer2/Plugin.pm index 11ac78abf..38cd3165f 100644 --- a/lib/Dancer2/Plugin.pm +++ b/lib/Dancer2/Plugin.pm @@ -540,7 +540,10 @@ sub register_plugin { $plugin_module->can($keyword) or *{"${plugin_module}::$keyword"} = sub { $_[0] - ? do { my $cc = shift()->app->name->can($keyword); $cc->(@_) } + ? do { + my $cb = shift()->app->name->can($keyword); + $cb->(@_); + } : $app_dsl_cb->(@_); }; } From e37f5a58727b54cb36cc616365fcd3bba094093f Mon Sep 17 00:00:00 2001 From: "Jason A. Crome" Date: Fri, 1 Apr 2022 21:57:49 -0400 Subject: [PATCH 4/4] reflect changes --- Changes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Changes b/Changes index cb98abbde..0485f9eb9 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,7 @@ {{$NEXT}} [ BUG FIXES ] - * None + * PR #1247: Fix edge case in plugin compat (Sawyer X) [ ENHANCEMENTS ] * None