summaryrefslogtreecommitdiffstats
path: root/t/basic.t
blob: bf775fd3604087270efa4edb9c80105ca5ea98dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Plack::Test;
use lib 't/lib';
use Test::PMCR;

use File::Copy;
use File::Spec::Functions 'catfile';
use HTTP::Request::Common;

use Plack::Middleware::Class::Refresh;

my $app = sub {
    return [
        200,
        [],
        [join "\n", Foo->call, Foo::Bar->call, Baz::Quux->call]
    ];
};

{
    my $dir = Test::PMCR::setup_temp_dir('basic');

    require Foo;
    require Foo::Bar;
    require Baz::Quux;

    test_psgi
        app    => Plack::Middleware::Class::Refresh->wrap($app),
        client => sub {
            my $cb = shift;
            {
                my $warnings;
                local $SIG{__WARN__} = sub { $warnings .= $_[0] };
                my $res = $cb->(GET 'http://localhost/');
                is($res->code, 200, "right code");
                is($res->content, "Foo\nFoo::Bar\nBaz::Quux");
                is($warnings, undef, "no warnings when not verbose");
            }
            copy(catfile(qw(t data_new basic Foo.pm)), catfile($dir, 'Foo.pm'))
                || die "couldn't copy: $!";
            {
                my $warnings;
                local $SIG{__WARN__} = sub { $warnings .= $_[0] };
                my $res = $cb->(GET 'http://localhost/');
                is($res->code, 200, "right code");
                is($res->content, "FOO\nFoo::Bar\nBaz::Quux", "right content");
                is($warnings, undef, "no warnings when not verbose");
            }
        };
}

Class::Refresh->unload_module($_) for 'Foo', 'Foo::Bar', 'Baz::Quux';

{
    my $dir = Test::PMCR::setup_temp_dir('basic');

    require Foo;
    require Foo::Bar;
    require Baz::Quux;

    test_psgi
        app    => Plack::Middleware::Class::Refresh->wrap($app, verbose => 1),
        client => sub {
            my $cb = shift;
            {
                my $warnings;
                local $SIG{__WARN__} = sub { $warnings .= $_[0] };
                my $res = $cb->(GET 'http://localhost/');
                is($res->code, 200, "right code");
                is($res->content, "Foo\nFoo::Bar\nBaz::Quux");
                is($warnings, undef, "no warnings yet");
            }
            copy(catfile(qw(t data_new basic Foo.pm)), catfile($dir, 'Foo.pm'))
                || die "couldn't copy: $!";
            {
                my $warnings;
                local $SIG{__WARN__} = sub { $warnings .= $_[0] };
                my $res = $cb->(GET 'http://localhost/');
                is($res->code, 200, "right code");
                is($res->content, "FOO\nFoo::Bar\nBaz::Quux", "right content");
                like($warnings, qr/^Class Foo has been changed, refreshing/,
                     "right warning");
            }
        };
}

done_testing;