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