1use 5.008004;
2use Test2::V0 -no_srand => 1;
3use Test::Alien::Build;
4use Alien::Build::Plugin::Test::Mock;
5use Path::Tiny qw( path );
6
7subtest 'basic' => sub {
8  alienfile_ok q{
9    use alienfile;
10    plugin 'Test::Mock';
11  };
12};
13
14subtest 'probe' => sub {
15
16  subtest 'share' => sub {
17
18    alienfile_ok q{
19      use alienfile;
20      plugin 'Test::Mock' => (
21        'probe' => 'share'
22      );
23    };
24
25    alien_install_type_is 'share';
26
27  };
28
29  subtest 'share' => sub {
30
31    alienfile_ok q{
32      use alienfile;
33      plugin 'Test::Mock' => (
34        'probe' => 'system'
35      );
36    };
37
38    alien_install_type_is 'system';
39
40  };
41
42  subtest 'share' => sub {
43
44    alienfile_ok q{
45      use alienfile;
46      plugin 'Test::Mock' => (
47        'probe' => 'die'
48      );
49    };
50
51    alien_install_type_is 'share';
52
53  };
54
55};
56
57subtest 'download' => sub {
58
59  subtest 'default' => sub {
60    my $build = alienfile_ok q{
61      use alienfile;
62      plugin 'Test::Mock' => (
63        probe => 'share',
64        download => 1,
65      );
66    };
67    alien_download_ok;
68    my $tarball = path($build->install_prop->{download});
69    is(
70      $tarball,
71      object {
72        call basename => 'foo-1.00.tar.gz';
73        call slurp => path('corpus/dist/foo-1.00.tar.gz')->slurp;
74      },
75    );
76  };
77
78  subtest 'override' => sub {
79    my $build = alienfile_ok q{
80      use alienfile;
81      plugin 'Test::Mock' => (
82        download => { 'bar-1.00.tar.gz' => 'fauxtar' },
83      );
84    };
85    alien_download_ok;
86    my $tarball = path($build->install_prop->{download});
87    is(
88      $tarball,
89      object {
90        call basename => 'bar-1.00.tar.gz';
91        call slurp => 'fauxtar';
92      },
93    );
94  };
95
96};
97
98subtest 'extract' => sub {
99
100  subtest 'default' => sub {
101    my $build = alienfile_ok q{
102      use alienfile;
103      plugin 'Test::Mock' => (
104        probe => 'share',
105        download => 1,
106        extract => 1,
107      );
108    };
109    alien_extract_ok;
110    my $dir = path($build->install_prop->{extract});
111    is(
112      $dir,
113      object {
114        call basename => 'foo-1.00';
115        call [child => 'configure' ] => object {
116          call slurp => path('corpus/dist/foo-1.00/configure')->slurp;
117        };
118        call [child => 'foo.c' ] => object {
119          call slurp => path('corpus/dist/foo-1.00/foo.c')->slurp;
120        };
121      },
122    );
123  };
124
125  subtest 'override' => sub {
126    my $build = alienfile_ok q{
127      use alienfile;
128      plugin 'Test::Mock' => (
129        probe => 'share',
130        download => 1,
131        extract => {
132          'bar-1.00' => {
133            one => 1,
134            two => 2,
135            three => 3,
136          },
137        },
138      );
139    };
140    alien_extract_ok;
141    my $dir = path($build->install_prop->{extract});
142    is(
143      $dir,
144      object {
145        call basename => 'bar-1.00';
146        call_list sub { sort shift->children } => [ map { object { call basename => $_ } } sort qw( one two three ) ];
147        call [ child => 'one' ] => object { call slurp => 1 };
148        call [ child => 'two' ] => object { call slurp => 2 };
149        call [ child => 'three' ] => object { call slurp => 3 };
150      },
151    );
152  };
153
154};
155
156subtest 'build' => sub {
157
158  subtest 'default' => sub {
159    my $build = alienfile_ok q{
160      use alienfile;
161      plugin 'Test::Mock' => (
162        probe => 'share',
163        download => 1,
164        extract => 1,
165        build => 1,
166      );
167    };
168    alien_build_ok;
169    is(
170      path($build->install_prop->{_ab_build_share}),
171      object {
172        call_list sub { sort shift->children } => [ map { object { call basename => $_ } } sort qw( configure foo.c foo.o libfoo.a ) ];
173      },
174    );
175    is(
176      path($build->install_prop->{prefix}),
177      object {
178        call_list sub { sort shift->children } => [ map { object { call basename => $_ } } sort qw( _alien lib ) ];
179        call [ child => 'lib' ] => object {
180          call_list sub { sort shift->children } => [ map { object { call basename => $_ } } sort qw( libfoo.a pkgconfig ) ];
181          call [ child => 'pkgconfig' ] => object {
182            call_list sub { sort shift->children } => [ map { object { call basename => $_ } } sort qw( foo.pc ) ];
183            call [ child => 'foo.pc' ] => object {
184              call slurp => match qr/-lfoo/;
185            };
186          };
187        };
188      },
189    );
190  };
191
192  subtest 'override' => sub {
193    my $build = alienfile_ok q{
194      use alienfile;
195      plugin 'Test::Mock' => (
196        probe => 'share',
197        download => 1,
198        extract => 1,
199        build => [
200          {
201            file1 => 'content1',
202          },
203          {
204            file2 => 'content2',
205          },
206        ],
207      );
208    };
209    alien_build_ok;
210    is(
211      path($build->install_prop->{_ab_build_share}),
212      object {
213        call_list sub { sort shift->children } => [ map { object { call basename => $_ } } sort qw( file1 configure foo.c ) ];
214        call [ child => 'file1' ] => object {
215          call slurp => 'content1';
216        };
217      },
218    );
219    is(
220      path($build->install_prop->{prefix}),
221      object {
222        call_list sub { sort shift->children } => [ map { object { call basename => $_ } } sort qw( _alien file2 ) ];
223        call [ child => 'file2' ] => object {
224          call slurp => 'content2';
225        };
226      },
227    );
228  };
229};
230
231subtest 'gather' => sub {
232  foreach my $install_type (qw( share system ))
233  {
234
235    subtest $install_type => sub {
236      subtest 'default' => sub {
237        my $build = alienfile_ok q{
238          use alienfile;
239          plugin 'Test::Mock' => (
240            download => 1,
241            extract  => 1,
242            build    => 1,
243            gather   => 1,
244          );
245        };
246        $build->meta->register_hook(probe => sub { $install_type });
247        alien_install_type_is $install_type;
248        alien_build_ok;
249        is(
250          $build->runtime_prop,
251          hash {
252            field cflags => match qr/^-I/;
253            field libs   => match qr/^-L.*-lfoo$/;
254            etc;
255          },
256        );
257        note "cflags = @{[ $build->runtime_prop->{cflags} ]}";
258        note "libs = @{[ $build->runtime_prop->{libs} ]}";
259      };
260    };
261
262    subtest $install_type => sub {
263      subtest 'override' => sub {
264        my $build = alienfile_ok q{
265          use alienfile;
266          plugin 'Test::Mock' => (
267            download => 1,
268            extract  => 1,
269            build    => 1,
270            gather   => { cflags => '-I/foo/include', libs => '-L/foo/lib -lfoo' },
271          );
272        };
273        $build->meta->register_hook(probe => sub { $install_type });
274        alien_install_type_is $install_type;
275        alien_build_ok;
276        is(
277          $build->runtime_prop,
278          hash {
279            field cflags => '-I/foo/include';
280            field libs   => '-L/foo/lib -lfoo';
281            etc;
282          },
283        );
284      };
285    };
286
287  };
288};
289
290done_testing;
291