File: 40-autodetect-executable.t

package info (click to toggle)
libextutils-builder-compiler-perl 0.032-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: perl: 1,252; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download
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
#! perl

use strict;
use warnings;

use Test::More 0.89;

use Config;
use Cwd 'getcwd';
use ExtUtils::Builder::Planner;
use File::Temp 'tempdir';
use IPC::Open2 qw/open2/;
use File::Basename qw/basename dirname/;
use File::Spec::Functions qw/catfile/;

sub capturex {
	local @ENV{qw/PATH IFS CDPATH ENV BASH_ENV/};
	my $pid = open2(my($in, $out), @_);
	binmode $in, ':crlf' if $^O eq 'MSWin32';
	my $ret = do { local $/; <$in> };
	waitpid $pid, 0;
	return $ret;
}

my $olddir = getcwd;
my $dir = tempdir(CLEANUP => 1);

chdir $dir;
mkdir 't';

my $planner = ExtUtils::Builder::Planner->new;
$planner->load_extension('ExtUtils::Builder::AutoDetect::C', undef,
	type => 'executable',
);

my $source_file = catfile('t', 'executable.c');
{
	open my $fh, '>', $source_file or die "Can't create $source_file: $!";
	my $content = <<END;
#include <stdio.h>

int main(int argc, char **argv, char **env) {
	printf("Dubrovnik\\n");
	return 0;
}
END
	print $fh $content or die "Can't write to $source_file: $!";
	close $fh or die "Can't close $source_file: $!";
}

ok(-e $source_file, "source file '$source_file' created");

my $object_file = catfile(dirname($source_file), basename($source_file, '.c') . $Config{obj_ext});
$planner->compile($source_file, $object_file);

my $exe_file = catfile(dirname($source_file), basename($object_file, $Config{obj_ext}) . $Config{exe_ext});
$planner->link([$object_file], $exe_file);

my $plan = $planner->materialize;
ok $plan;

ok eval { $plan->run($exe_file, logger => \&note); 1 } or diag "Got exception: $@";

ok(-e $object_file, "object file $object_file has been created");
ok(-e $exe_file, "lib file $exe_file has been created");

my $output = eval { capturex($exe_file) };
is ($output, "Dubrovnik\n", 'Output is "Dubrovnik"') or diag("Error: $@");

chdir $olddir;

done_testing;