Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #457 from kraih/fatal_deprecations
Fatal deprecations
  • Loading branch information
kraih committed Feb 23, 2013
2 parents 7b78ff3 + 68f3116 commit b0fc0f3
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 61 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.87 2013-02-23
- Improved tests.
- Add Mojo::Util::deprecated

3.86 2013-02-22
- Welcome to the Mojolicious core team Joel Berger.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Home.pm
Expand Up @@ -10,7 +10,7 @@ use File::Basename 'dirname';
use File::Find 'find';
use File::Spec::Functions qw(abs2rel catdir catfile splitdir);
use FindBin;
use Mojo::Util qw(class_to_path slurp);
use Mojo::Util qw(class_to_path slurp deprecated);

sub new { shift->SUPER::new->parse(@_) }

Expand Down Expand Up @@ -82,9 +82,8 @@ sub rel_file { catfile(@{shift->{parts} || []}, split '/', shift) }

# DEPRECATED in Rainbow!
sub slurp_rel_file {
warn <<EOF;
Mojo::Home->slurp_rel_file is DEPRECATED in favor of Mojo::Util->slurp!!!
EOF
deprecated "Mojo::Home->slurp_rel_file is DEPRECATED "
. "in favor of Mojo::Util->slurp!!!";
slurp shift->rel_file(@_);
}

Expand Down
19 changes: 7 additions & 12 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -8,7 +8,7 @@ use List::Util 'first';
use Mojo::IOLoop;
use Mojo::Server::Daemon;
use Mojo::URL;
use Mojo::Util 'monkey_patch';
use Mojo::Util qw(monkey_patch deprecated);
use Mojo::UserAgent::CookieJar;
use Mojo::UserAgent::Transactor;
use Scalar::Util 'weaken';
Expand Down Expand Up @@ -61,19 +61,15 @@ sub app_url {

# DEPRECATED in Rainbow!
sub build_form_tx {
warn <<EOF;
Mojo::UserAgent->build_form_tx is DEPRECATED in favor of
Mojo::UserAgent->build_tx!!!
EOF
deprecated "Mojo::UserAgent->build_form_tx is DEPRECATED "
. "in favor of Mojo::UserAgent->build_tx!!!";
shift->transactor->form(@_);
}

# DEPRECATED in Rainbow!
sub build_json_tx {
warn <<EOF;
Mojo::UserAgent->build_json_tx is DEPRECATED in favor of
Mojo::UserAgent->build_tx!!!
EOF
deprecated "Mojo::UserAgent->build_json_tx is DEPRECATED "
. "in favor of Mojo::UserAgent->build_tx!!!";
shift->transactor->json(@_);
}

Expand All @@ -94,9 +90,8 @@ sub need_proxy {

# DEPRECATED in Rainbow!
sub post_form {
warn <<EOF;
Mojo::UserAgent->post_form is DEPRECATED in favor of Mojo::UserAgent->post!!!
EOF
deprecated
"Mojo::UserAgent->post_form is DEPRECATED in favor of Mojo::UserAgent->post!!!";
my $self = shift;
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
return $self->start($self->build_form_tx(@_), $cb);
Expand Down
14 changes: 5 additions & 9 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -11,7 +11,7 @@ use Mojo::Parameters;
use Mojo::Transaction::HTTP;
use Mojo::Transaction::WebSocket;
use Mojo::URL;
use Mojo::Util 'encode';
use Mojo::Util qw(encode deprecated);

has generators => sub { {} };

Expand Down Expand Up @@ -47,10 +47,8 @@ sub endpoint {

# DEPRECATED in Rainbow!
sub form {
warn <<EOF;
Mojo::UserAgent::Transactor->form is DEPRECATED in favor of
Mojo::UserAgent::Transactor->tx!!!
EOF
deprecated "Mojo::UserAgent::Transactor->form is DEPRECATED "
. "in favor of Mojo::UserAgent::Transactor->tx!!!";
my ($self, $url, $charset) = (shift, shift, shift);
my $form = ref $charset ? $charset : shift;
$charset = undef if ref $charset;
Expand All @@ -59,10 +57,8 @@ EOF

# DEPRECATED in Rainbow!
sub json {
warn <<EOF;
Mojo::UserAgent::Transactor->json is DEPRECATED in favor of
Mojo::UserAgent::Transactor->tx!!!
EOF
deprecated "Mojo::UserAgent::Transactor->json is DEPRECATED "
. "in favor of Mojo::UserAgent::Transactor->tx!!!";
my ($self, $url, $data) = (shift, shift, shift);
return $self->tx(POST => $url, @_, json => $data);
}
Expand Down
23 changes: 18 additions & 5 deletions lib/Mojo/Util.pm
@@ -1,7 +1,7 @@
package Mojo::Util;
use Mojo::Base 'Exporter';

use Carp 'croak';
use Carp qw(croak carp);
use Digest::MD5 qw(md5 md5_hex);
use Digest::SHA qw(sha1 sha1_hex);
use Encode 'find_encoding';
Expand Down Expand Up @@ -45,7 +45,7 @@ our @EXPORT_OK = (
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp spurt squish trim unquote),
qw(url_escape url_unescape xml_escape xor_encode)
qw(url_escape url_unescape deprecated xml_escape xor_encode)
);

# DEPRECATED in Rainbow!
Expand Down Expand Up @@ -98,6 +98,12 @@ sub decode {
return $bytes;
}
sub deprecated {
my $deprecation = shift;
local $Carp::CarpLevel = 1;
$ENV{MOJO_FATAL_DEPRECATIONS} ? croak($deprecation) : carp($deprecation);
}
sub encode { _encoding($_[0])->encode("$_[1]") }
sub get_line {
Expand All @@ -117,9 +123,8 @@ sub hmac_sha1_sum { _hmac(\&sha1, @_) }
# DEPRECATED in Rainbow!
sub html_escape {
warn <<EOF;
Mojo::Util->html_escape is DEPRECATED in favor of Mojo::Util->xml_escape!!!
EOF
deprecated "Mojo::Util->html_escape is DEPRECATED "
. "in favor of Mojo::Util->xml_escape!!!";
my ($string, $pattern) = @_;
$pattern ||= '^\n\r\t !#$%(-;=?-~';
return $string unless $string =~ /[^$pattern]/;
Expand Down Expand Up @@ -330,6 +335,7 @@ sub xml_escape {
return $string;
}


sub xor_encode {
my ($input, $key) = @_;

Expand Down Expand Up @@ -485,6 +491,10 @@ Convert camel case string to snake case and replace C<::> with C<->.
Decode bytes to characters and return C<undef> if decoding failed.
=head2 deprecated
deprecated "Mojo::foo DEPRECATED in favor of Mojo::bar";
=head2 encode
my $bytes = encode 'UTF-8', $chars;
Expand Down Expand Up @@ -621,6 +631,9 @@ C<^A-Za-z0-9\-._~>.
Decode percent encoded characters in string.
Warn about deprecated feature from perspective of caller. Set environment
variable MOJO_FATAL_DEPRECATIONS to make deprecations fatal.
=head2 xml_escape
my $escaped = xml_escape $string;
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojo';
# "Fry: Shut up and take my money!"
use Carp 'croak';
use Mojo::Exception;
use Mojo::Util 'decamelize';
use Mojo::Util qw(decamelize deprecated);
use Mojolicious::Commands;
use Mojolicious::Controller;
use Mojolicious::Plugins;
Expand Down Expand Up @@ -122,9 +122,9 @@ sub dispatch {

# DEPRECATED in Rainbow!
if ($plugins->has_subscribers('after_static_dispatch')) {
warn <<EOF and $plugins->emit_hook_reverse(after_static_dispatch => $c);
after_static_dispatch hook is DEPRECATED in favor of before_routes!!!
EOF
deprecated "after_static_dispatch hook is DEPRECATED "
. "in favor of before_routes!!!"
and $plugins->emit_hook_reverse(after_static_dispatch => $c);
}

# Routes
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -4,6 +4,7 @@ use Mojo::Base 'Mojolicious::Command';
use Getopt::Long 'GetOptions';
use List::Util 'max';
use Mojo::Server;
use Mojo::Util 'deprecated';

has hint => <<"EOF";
Expand Down Expand Up @@ -105,10 +106,8 @@ sub run {

# DEPRECATED in Rainbow!
sub start {
warn <<EOF;
Mojolicious::Commands->start is DEPRECATED in favor of
Mojolicious::Commands->start_app!!!
EOF
deprecated "Mojolicious::Commands->start is DEPRECATED "
. "in favor of Mojolicious::Commands->start_app!!!";
my $self = shift;
return $self->start_app($ENV{MOJO_APP} => @_) if $ENV{MOJO_APP};
return $self->new->app->start(@_);
Expand Down
1 change: 1 addition & 0 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojolicious::Plugin';

use Data::Dumper ();
use Mojo::ByteStream;
use Mojo::Util 'deprecated';

sub register {
my ($self, $app) = @_;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -2,7 +2,7 @@ package Mojolicious::Plugin::TagHelpers;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::ByteStream 'b';
use Mojo::Util 'xml_escape';
use Mojo::Util qw(xml_escape deprecated);

sub register {
my ($self, $app) = @_;
Expand All @@ -16,7 +16,7 @@ sub register {
# DEPRECATED in Rainbow!
$app->helper(
base_tag => sub {
warn "base_tag is DEPRECATED!!!\n";
deprecated "base_tag is DEPRECATED!!!";
_tag('base', href => shift->req->url->base, @_);
}
);
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojolicious::Routes::Route';
use List::Util 'first';
use Mojo::Cache;
use Mojo::Loader;
use Mojo::Util 'camelize';
use Mojo::Util qw(camelize deprecated);
use Mojolicious::Routes::Match;
use Scalar::Util 'weaken';

Expand Down Expand Up @@ -81,10 +81,8 @@ sub lookup {

# DEPRECATED in Rainbow!
sub namespace {
warn <<EOF;
Mojolicious::Routes->namespace is DEPRECATED in favor of
Mojolicious::Routes->namespaces!
EOF
deprecated "Mojolicious::Routes->namespace is DEPRECATED "
. "in favor of Mojolicious::Routes->namespaces!";
my $self = shift;
return $self->namespaces->[0] unless @_;
$self->namespaces->[0] = shift;
Expand Down
18 changes: 8 additions & 10 deletions lib/Test/Mojo.pm
Expand Up @@ -13,7 +13,7 @@ use Mojo::JSON;
use Mojo::JSON::Pointer;
use Mojo::Server;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
use Mojo::Util qw(decode encode deprecated);
use Test::More ();

has [qw(message tx)];
Expand Down Expand Up @@ -220,19 +220,17 @@ sub post_ok { shift->_request_ok(post => @_) }

# DEPRECATED in Rainbow!
sub post_form_ok {
warn <<EOF;
Test::Mojo->post_form_ok is DEPRECATED in favor of Test::Mojo->post_ok!!!
EOF
deprecated "Test::Mojo->post_form_ok is DEPRECATED "
. "in favor of Test::Mojo->post_ok!!!";
my ($self, $url) = (shift, shift);
my $tx = $self->tx($self->ua->post_form($url, @_))->tx;
return $self->_test('ok', $tx->is_finished, encode('UTF-8', "post $url"));
}

# DEPRECATED in Rainbow!
sub post_json_ok {
warn <<EOF;
Test::Mojo->post_json_ok is DEPRECATED in favor of Test::Mojo->post_ok!!!
EOF
deprecated "Test::Mojo->post_json_ok is DEPRECATED "
. "in favor of Test::Mojo->post_ok!!!";
my ($self, $url) = (shift, shift);
my $tx = $self->tx($self->ua->post_json($url, @_))->tx;
return $self->_test('ok', $tx->is_finished, encode('UTF-8', "post $url"));
Expand Down Expand Up @@ -377,9 +375,9 @@ sub _wait {

# DEPRECATED in Rainbow!
my $new = $self->{new} //= $wait;
warn <<EOF unless $new;
Testing WebSocket messages without Test::Mojo->message_ok is DEPRECATED!!!
EOF
deprecated "Testing WebSocket messages without "
. "Test::Mojo->message_ok is DEPRECATED!!!"
unless $new;
return $self->message if $new && !$wait;

# Wait for message
Expand Down
6 changes: 3 additions & 3 deletions lib/ojo.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::Collection 'c';
use Mojo::DOM;
use Mojo::JSON 'j';
use Mojo::UserAgent;
use Mojo::Util 'monkey_patch';
use Mojo::Util qw(monkey_patch deprecated);

# Silent oneliners
$ENV{MOJO_LOG_LEVEL} ||= 'fatal';
Expand Down Expand Up @@ -42,11 +42,11 @@ sub import {

# DEPRECATED in Rainbow!
my $f = sub {
warn "ojo->f is DEPRECATED in favor of ojo->p!!!\n";
deprecated "ojo->f is DEPRECATED in favor of ojo->p!!!";
_request($UA->build_form_tx(@_));
};
my $n = sub {
warn "ojo->n is DEPRECATED in favor of ojo->p!!!\n";
deprecated "ojo->n is DEPRECATED in favor of ojo->p!!!";
_request($UA->build_json_tx(@_));
};
monkey_patch $caller, f => $f, n => $n;
Expand Down
27 changes: 24 additions & 3 deletions t/mojo/util.t
Expand Up @@ -7,10 +7,10 @@ use FindBin;

use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(decode deprecated encode get_line hmac_md5_sum hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum monkey_patch punycode_decode),
qw(squish trim unquote secure_compare sha1_bytes sha1_sum slurp spurt),
qw(url_escape url_unescape xml_escape xor_encode);
qw(punycode_encode quote url_escape url_unescape xml_escape xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -400,4 +400,25 @@ is MojoMonkeyTest::yin(), 'yin', 'right result';
ok !!MojoMonkeyTest->can('yang'), 'function "yang" exists';
is MojoMonkeyTest::yang(), 'yang', 'right result';

# deprecated

{
my ($warn, $die) = @_;
local $SIG{__WARN__} = sub { $warn = shift; };
local $SIG{__DIE__} = sub { $die = shift; return undef; };
deprecated("This warns from caller");
local $ENV{MOJO_FATAL_DEPRECATIONS} = 1;
eval { deprecated("This dies from caller"); };
like(
$warn,
qr/This warns from caller at t\/mojo\/util.t line \d+/,
"warn message is right deprecation"
);
like(
$die,
qr/This dies from caller at t\/mojo\/util.t line \d+/,
"die message is right for deprecation"
);
}

done_testing();

0 comments on commit b0fc0f3

Please sign in to comment.