1use Test2::V0 -no_srand => 1;
2use 5.008004;
3use FFI::Build::MM;
4use Capture::Tiny qw( capture_merged );
5use File::Glob qw( bsd_glob );
6use lib 't/lib';
7use Test::Platypus;
8
9sub dont_save_prop (&)
10{
11   my($code) = @_;
12   sub {
13    my $save = \&FFI::Build::MM::save_prop;
14    {
15      no warnings 'redefine';
16      *FFI::Build::MM::save_prop = sub {};
17    };
18    my $ret = eval { $code->() };
19    my $error = $@;
20    {
21      no warnings 'redefine';
22      *FFI::Build::MM::save_prop = $save;
23    }
24    die $error if $error;
25    $ret;
26  };
27}
28
29sub slurp ($)
30{
31  my $fn = shift;
32  open my $fh, '<', $fn;
33  my $content = do { local $/; <$fh> };
34  close $fh;
35  $content;
36}
37
38subtest 'basic' => dont_save_prop {
39
40  my $mm = FFI::Build::MM->new;
41  isa_ok $mm, 'FFI::Build::MM';
42
43  $mm->mm_args( DISTNAME => 'Foo-Bar-Baz' );
44
45  is( $mm->distname, 'Foo-Bar-Baz' );
46  is( $mm->sharedir, 'blib/lib/auto/share/dist/Foo-Bar-Baz' );
47  is( $mm->archdir,  'blib/arch/auto/Foo/Bar/Baz' );
48
49  subtest 'build with fbx file' => sub {
50    my $build = $mm->load_build('corpus/ffi_build_mm/lb1', undef, undef);
51    isa_ok $build, 'FFI::Build';
52    is [sort map { $_->basename } $build->source], ['hello1.c','hello2.c']
53  };
54
55  subtest 'build with fbx file with errors' => sub {
56      eval { $mm->load_build('corpus/ffi_build_mm/lb1bad', undef, undef) };
57      like ( $@, qr/skootch/, "caught compile error in fbx file" );
58
59  };
60
61  subtest 'build with default' => sub {
62    my $build = $mm->load_build('corpus/ffi_build_mm/lb2', undef, undef);
63    isa_ok $build, 'FFI::Build';
64    is [sort map { $_->basename } $build->source], ['hello1.c','hello2.c']
65  };
66
67  my $postamble = $mm->mm_postamble;
68  ok $postamble;
69  note "[postamble]\n$postamble\n";
70
71  $mm->sharedir('share');
72  is( $mm->sharedir, 'share' );
73
74  $mm->archdir(0);
75  ok( !$mm->archdir );
76};
77
78subtest 'with a build!' => sub {
79
80  chdir 'corpus/ffi_build_mm/project1';
81
82  unlink 'fbx.json' if -f 'fbx.json';
83
84  subtest 'namespace is clean' => sub {
85    ok( ! main->can($_), "$_ not imported yet" ) for qw( fbx_build fbx_test fbx_clean );
86  };
87
88  subtest 'do not save on request' => sub {
89
90    my $mm = FFI::Build::MM->new( save => 0 );
91    $mm->mm_args( DISTNAME => 'Crock-O-Stimpy' );
92    ok !-f 'fbx.json';
93
94  };
95
96  subtest 'perl Makefile.PL' => sub {
97
98    my $mm = FFI::Build::MM->new;
99    $mm->mm_args( DISTNAME => 'Crock-O-Stimpy' );
100    ok -f 'fbx.json';
101
102  };
103
104  subtest 'import' => sub {
105    FFI::Build::MM->import('cmd');
106    ok( main->can($_), "$_ not imported yet" ) for qw( fbx_build fbx_test fbx_clean );
107  };
108
109  subtest 'make' => sub {
110    my($out, $err) = capture_merged {
111      eval { fbx_build() };
112      $@;
113    };
114    note $out;
115    is $err, '';
116
117    is slurp 'blib/arch/auto/Crock/O/Stimpy/Stimpy.txt', "FFI::Build\@auto/share/dist/Crock-O-Stimpy/lib/@{[ FFI::Build::Platform->library_prefix ]}Crock-O-Stimpy@{[ scalar FFI::Build::Platform->library_suffix]}\n";
118
119    platypus 1 => sub {
120      my $ffi = shift;
121      $ffi->lib(grep !/\.pdb$/, bsd_glob 'blib/lib/auto/share/dist/Crock-O-Stimpy/lib/*');
122      note "lib=$_" for $ffi->lib;
123      is(
124        $ffi->function('frooble_runtime' => [] => 'int')->call,
125        47,
126      );
127    };
128  };
129
130  subtest 'make test' => sub {
131    my($out, $err) = capture_merged {
132      eval { fbx_test() };
133      $@;
134    };
135    note $out;
136    is $err, '';
137  };
138
139  subtest 'make clean' => sub {
140    fbx_clean();
141    ok !-f 'fbx.json';
142  };
143  File::Path::rmtree('blib', 0, oct(755));
144
145  chdir(File::Spec->updir) for 1..3;
146
147};
148
149subtest 'alien' => sub {
150  skip_all 'todo';
151};
152
153done_testing;
154