Commit | Line | Data |
613f422f |
1 | # sample.t -- a sample test file for Module::Build |
2 | |
3 | use strict; |
4 | use lib 't/lib'; |
5 | use MBTest; # or 'no_plan' |
6 | use DistGen; |
7 | use Config; |
8 | use IO::File; |
9 | use File::Spec; |
10 | use ExtUtils::Packlist; |
53fc1c7e |
11 | use ExtUtils::Installed; |
613f422f |
12 | use File::Path; |
13 | |
14 | # Ensure any Module::Build modules are loaded from correct directory |
15 | blib_load('Module::Build'); |
16 | blib_load('Module::Build::ConfigData'); |
17 | |
7f40cf6b |
18 | if ( $ENV{PERL_CORE} ) { |
19 | plan skip_all => 'bundle_inc tests will never succeed in PERL_CORE'; |
20 | } |
7dc9e1b4 |
21 | elsif ( ! $ENV{MB_TEST_EXPERIMENTAL} ) { |
22 | plan skip_all => '$ENV{MB_TEST_EXPERIMENTAL} is not set'; |
23 | } |
53fc1c7e |
24 | elsif ( ! MBTest::check_EUI() ) { |
25 | plan skip_all => 'ExtUtils::Installed takes too long on your system'; |
26 | } |
7f40cf6b |
27 | elsif ( Module::Build::ConfigData->feature('inc_bundling_support') ) { |
53fc1c7e |
28 | plan tests => 19; |
613f422f |
29 | } else { |
30 | plan skip_all => 'inc_bundling_support feature is not enabled'; |
31 | } |
32 | |
33 | # need to do a temp install of M::B being tested to ensure a packlist |
34 | # is available for bundling |
35 | |
36 | my $current_mb = Module::Build->resume(); |
37 | my $temp_install = MBTest->tmpdir(); |
38 | my $arch = $Config{archname}; |
39 | my $lib_path = File::Spec->catdir($temp_install,qw/lib perl5/); |
40 | my $arch_path = File::Spec->catdir( $lib_path, $arch ); |
41 | mkpath ( $arch_path ); |
42 | ok( -d $arch_path, "created temporary M::B pseudo-install directory"); |
43 | |
44 | unshift @INC, $lib_path, $arch_path; |
45 | local $ENV{PERL5LIB} = join( $Config{path_sep}, |
53fc1c7e |
46 | $lib_path, ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : () ) |
613f422f |
47 | ); |
48 | |
53fc1c7e |
49 | # must uninst=0 so we don't try to remove an installed M::B! |
50 | stdout_of( sub { $current_mb->dispatch( |
51 | 'install', install_base => $temp_install, uninst => 0 |
52 | ) |
53 | } |
54 | ); |
613f422f |
55 | |
56 | # create dist object in a temp directory |
57 | # enter the directory and generate the skeleton files |
58 | my $dist = DistGen->new( inc => 1 )->chdir_in->regen; |
59 | |
60 | # get a Module::Build object and test with it |
61 | my $mb = $dist->new_from_context(); # quiet by default |
62 | isa_ok( $mb, "Module::Build" ); |
63 | is( $mb->dist_name, "Simple", "dist_name is 'Simple'" ); |
64 | is_deeply( $mb->bundle_inc, [ 'Module::Build' ], |
65 | "Module::Build is flagged for bundling" |
66 | ); |
67 | |
53fc1c7e |
68 | # bundle stuff into distdir |
613f422f |
69 | stdout_stderr_of( sub { $mb->dispatch('distdir') } ); |
70 | |
71 | my $dist_inc = File::Spec->catdir($mb->dist_dir, 'inc'); |
72 | ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ), |
53fc1c7e |
73 | "dist_dir/inc/latest.pm created" |
613f422f |
74 | ); |
75 | |
76 | ok( -d File::Spec->catdir( $dist_inc, 'inc_Module-Build' ), |
77 | "dist_dir/inc/inc_Module_Build created" |
78 | ); |
79 | |
80 | my $mb_file = |
81 | File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build.pm/ ); |
82 | |
83 | ok( -e $mb_file, |
84 | "dist_dir/inc/inc_Module_Build/Module/Build.pm created" |
85 | ); |
86 | |
87 | ok( -e File::Spec->catfile( $dist_inc, qw/inc_Module-Build Module Build Base.pm/ ), |
88 | "dist_dir/inc/inc_Module_Build/Module/Build/Base.pm created" |
89 | ); |
90 | |
91 | # Force bundled M::B to a higher version so it gets loaded |
08fc25ad |
92 | # This has failed on Win32 for no known reason, so we'll skip if |
93 | # we can't edit the file. |
94 | |
95 | eval { |
96 | my $fh; |
53fc1c7e |
97 | chmod 0666, $mb_file; |
08fc25ad |
98 | $fh = IO::File->new($mb_file, "<") or die "Could not read $mb_file: $!"; |
99 | my $mb_code = do { local $/; <$fh> }; |
100 | $mb_code =~ s{\$VERSION\s+=\s+\S+}{\$VERSION = 9999;}; |
101 | $fh->close; |
102 | $fh = IO::File->new($mb_file, ">") or die "Could not write $mb_file: $!"; |
103 | print {$fh} $mb_code; |
104 | $fh->close; |
105 | }; |
106 | |
107 | my $err = $@; |
108 | diag $@ if $@; |
109 | SKIP: { |
110 | skip "Couldn't adjust \$VERSION in bundled M::B for testing", 10 |
111 | if $err; |
112 | |
113 | # test the bundling in dist_dir |
114 | chdir $mb->dist_dir; |
115 | |
116 | stdout_of( sub { Module::Build->run_perl_script('Build.PL',[],[]) } ); |
53fc1c7e |
117 | ok( -e 'MYMETA.yml', 'MYMETA was created' ); |
08fc25ad |
118 | |
119 | my $meta = IO::File->new('MYMETA.yml'); |
53fc1c7e |
120 | ok( $meta, "opened MYMETA.yml" ); |
08fc25ad |
121 | ok( scalar( grep { /generated_by:.*9999/ } <$meta> ), |
122 | "dist_dir Build.PL loaded bundled Module::Build" |
123 | ); |
53fc1c7e |
124 | close $meta; |
08fc25ad |
125 | |
126 | #--------------------------------------------------------------------------# |
127 | # test identification of dependencies |
128 | #--------------------------------------------------------------------------# |
129 | |
130 | $dist->chdir_in; |
131 | |
132 | $dist->add_file( 'mylib/Foo.pm', << 'HERE' ); |
613f422f |
133 | package Foo; |
134 | our $VERSION = 1; |
135 | 1; |
136 | HERE |
137 | |
08fc25ad |
138 | $dist->add_file( 'mylib/Bar.pm', << 'HERE' ); |
613f422f |
139 | package Bar; |
140 | use Foo; |
141 | our $VERSION = 42; |
142 | 1; |
143 | HERE |
144 | |
08fc25ad |
145 | $dist->change_file( 'Build.PL', << "HERE" ); |
613f422f |
146 | use inc::latest 'Module::Build'; |
147 | use inc::latest 'Foo'; |
148 | |
149 | Module::Build->new( |
150 | module_name => '$dist->{name}', |
151 | license => 'perl', |
152 | )->create_build_script; |
153 | HERE |
154 | |
08fc25ad |
155 | $dist->regen( clean => 1 ); |
613f422f |
156 | |
08fc25ad |
157 | make_packlist($_,'mylib') for qw/Foo Bar/; |
613f422f |
158 | |
08fc25ad |
159 | # get a Module::Build object and test with it |
160 | my $abs_mylib = File::Spec->rel2abs('mylib'); |
613f422f |
161 | |
162 | |
08fc25ad |
163 | unshift @INC, $abs_mylib; |
164 | $mb = $dist->new_from_context(); # quiet by default |
165 | isa_ok( $mb, "Module::Build" ); |
166 | is_deeply( [sort @{$mb->bundle_inc}], [ 'Foo', 'Module::Build' ], |
167 | "Module::Build and Foo are flagged for bundling" |
168 | ); |
613f422f |
169 | |
08fc25ad |
170 | my $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } ); |
613f422f |
171 | |
08fc25ad |
172 | ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ), |
173 | "./inc/latest.pm created" |
174 | ); |
613f422f |
175 | |
08fc25ad |
176 | ok( -d File::Spec->catdir( $dist_inc, 'inc_Foo' ), |
177 | "dist_dir/inc/inc_Foo created" |
178 | ); |
613f422f |
179 | |
08fc25ad |
180 | $dist->change_file( 'Build.PL', << "HERE" ); |
613f422f |
181 | use inc::latest 'Module::Build'; |
182 | use inc::latest 'Bar'; |
183 | |
184 | Module::Build->new( |
185 | module_name => '$dist->{name}', |
186 | license => 'perl', |
187 | )->create_build_script; |
188 | HERE |
189 | |
08fc25ad |
190 | $dist->regen( clean => 1 ); |
191 | make_packlist($_,'mylib') for qw/Foo Bar/; |
613f422f |
192 | |
08fc25ad |
193 | $mb = $dist->new_from_context(); # quiet by default |
194 | isa_ok( $mb, "Module::Build" ); |
195 | is_deeply( [sort @{$mb->bundle_inc}], [ 'Bar', 'Module::Build' ], |
196 | "Module::Build and Bar are flagged for bundling" |
197 | ); |
613f422f |
198 | |
08fc25ad |
199 | $output = stdout_stderr_of( sub { $mb->dispatch('distdir') } ); |
613f422f |
200 | |
08fc25ad |
201 | ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ), |
202 | "./inc/latest.pm created" |
203 | ); |
613f422f |
204 | |
08fc25ad |
205 | ok( -d File::Spec->catdir( $dist_inc, 'inc_Bar' ), |
206 | "dist_dir/inc/inc_Bar created" |
207 | ); |
208 | } |
613f422f |
209 | |
210 | |
211 | sub make_packlist { |
212 | my ($mod, $lib) = @_; |
213 | my $arch = $Config{archname}; |
214 | (my $mod_path = $mod) =~ s{::}{/}g; |
215 | my $mod_file = File::Spec->catfile( $lib, "$mod_path\.pm" ); |
216 | my $abs = File::Spec->rel2abs($mod_file); |
217 | my $packlist_path = File::Spec->catdir($lib, $arch, 'auto', $mod_path); |
218 | mkpath $packlist_path; |
219 | my $packlist = ExtUtils::Packlist->new; |
220 | $packlist->{$abs}++; |
221 | $packlist->write( File::Spec->catfile( $packlist_path, '.packlist' )); |
222 | } |
223 | |
224 | # vim:ts=2:sw=2:et:sta:sts=2 |