File: 07b-interp-static-source.t

package info (click to toggle)
libhtml-mason-perl 1%3A1.58-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,796 kB
  • sloc: perl: 8,618; sh: 49; makefile: 2
file content (193 lines) | stat: -rw-r--r-- 6,295 bytes parent folder | download | duplicates (6)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use strict;
use warnings;

use File::Spec;
use HTML::Mason::Tests;
use HTML::Mason::Tools qw(load_pkg);
use IO::File;

package HTML::Mason::Commands;
sub write_component
{
    my ($comp, $text) = @_;
    my $file = $comp->source_file;
    my $fh = new IO::File ">$file" or die "Cannot write to $file: $!";
    $fh->print($text);
    $fh->close();
}

package main;
my $tests = make_tests();
$tests->run;

sub make_tests
{
    my $group = HTML::Mason::Tests->tests_class->new( name => 'interp-static-source',
                                                      description => 'interp static source mode' );

#------------------------------------------------------------

    foreach my $i (1..4) {
        $group->add_support( path => "support/remove_component$i",
                             component => "I will be removed ($i).\n",
                           );
    }

#------------------------------------------------------------

    foreach my $i (1..4) {
        $group->add_support( path => "support/change_component$i",
                             component => "I will be changed ($i).\n",
                           );
    }

#------------------------------------------------------------

    $group->add_test( name => 'change_component_without_static_source',
                      description => 'test that on-the-fly component changes are detected with static_source=0',
                      component => <<'EOF',
<& support/change_component1 &>\
<%perl>
sleep(2);  # Make sure timestamp changes
write_component($m->fetch_comp('support/change_component1'), "I have changed!\n");
</%perl>
<& support/change_component1 &>
EOF
                      expect => <<'EOF',
I will be changed (1).
I have changed!
EOF
                    );

#------------------------------------------------------------

    $group->add_test( name => 'change_component_with_static_source',
                      description => 'test that changing component has no effect with static_source=1',
                      interp_params => { static_source => 1 },
                      component => <<'EOF',
<& support/change_component2 &>\
<%perl>
sleep(1);  # Make sure timestamp changes
write_component($m->fetch_comp('support/change_component2'), "I have changed!\n");
my $comp = $m->interp->load("/interp-static-source/support/change_component2");
$m->comp($comp);
</%perl>
<& support/change_component2 &>
EOF
                      expect => <<'EOF',
I will be changed (2).
I will be changed (2).
I will be changed (2).
EOF
                    );

#------------------------------------------------------------

    my $static_source_touch_file = File::Spec->catfile($group->base_path, '.__static_source_touch');
    $group->add_test( name => 'change_component_with_static_source_touch_file',
                      description => 'test that changing component has no effect until touch file is touched',
                      interp_params => { static_source => 1,
                                         static_source_touch_file => $static_source_touch_file },
                      component => <<'EOF',
<%perl>
my $path = "/interp-static-source/support/change_component3";
$m->comp($path);
sleep(1);  # Make sure timestamp changes
write_component($m->fetch_comp('support/change_component3'), "I have changed!\n");
$m->interp->check_static_source_touch_file;
$m->comp($path);
my $touch_file = $m->interp->static_source_touch_file;
my $fh = new IO::File ">$touch_file"
   or die "cannot write to '$touch_file': $!";
$fh->close();
$m->interp->check_static_source_touch_file;
$m->comp($path);
</%perl>
EOF
                      expect => <<'EOF',
I will be changed (3).
I will be changed (3).
I have changed!
EOF
                    );

#------------------------------------------------------------

    $group->add_test( name => 'remove_component_without_static_source',
                      description => 'test that removing source causes component not found with static_source=0',
                      component => <<'EOF',
<& support/remove_component1 &>
<%perl>
my $file = $m->fetch_comp('support/remove_component1')->source_file;
unlink($file) or die "could not unlink '$file'";
</%perl>
<& support/remove_component1 &>
EOF
                      expect_error => qr/could not find component for path/,
                    );

#------------------------------------------------------------

    $group->add_test( name => 'remove_component_with_static_source',
                      description => 'test that removing source has no effect with static_source=1',
                      interp_params => { static_source => 1 },
                      component => <<'EOF',
<%init>
# flush_code_cache actually broke this behavior at one point
$m->interp->flush_code_cache;
</%init>

<& support/remove_component2 &>
<%perl>
my $file = $m->fetch_comp('support/remove_component2')->source_file;
unlink($file) or die "could not unlink '$file'";
my $comp = $m->interp->load("/interp-static-source/support/remove_component2")
  or die "could not load component";
$m->comp($comp);
</%perl>
<& support/remove_component2 &>
EOF
                      expect => <<'EOF',

I will be removed (2).

I will be removed (2).
I will be removed (2).
EOF
                    );

#------------------------------------------------------------

    $group->add_test( name => 'flush_code_cache_with_static_source',
                      description => 'test that code cache flush & object file removal works with static_source=1',
                      interp_params => { static_source => 1 },
                      component => <<'EOF',
<& support/change_component4 &>
<%perl>
write_component($m->fetch_comp('support/change_component4'), "I have changed!\n");

# Not enough - must delete object file
$m->interp->flush_code_cache;
my $comp = $m->interp->load("/interp-static-source/support/change_component4");
$m->comp($comp);

# This should work
unlink($comp->object_file);
undef $comp;
$m->interp->flush_code_cache;
my $comp2 = $m->interp->load("/interp-static-source/support/change_component4");
$m->comp($comp2);
</%perl>
<& support/change_component4 &>
EOF
                      expect => <<'EOF',
I will be changed (4).

I will be changed (4).
I have changed!
I have changed!
EOF
                    );

    return $group;
}