Skip to content

Commit

Permalink
Refactor code into internal method to make more execution paths testa…
Browse files Browse the repository at this point in the history
…ble. Then test them.
  • Loading branch information
jkeenan committed Mar 25, 2011
1 parent 1594ff8 commit 8504fc2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 34 deletions.
66 changes: 37 additions & 29 deletions config/auto/llvm.pm
Expand Up @@ -31,42 +31,21 @@ sub runstep {

my $verbose = $conf->options->get( 'verbose' );

# We will run various probes for LLVM. If the probes are unsuccessful, we will
# set_result to 'no', set 'has_llvm' to '', then return from runstep()
# with a value of 1. If a given probe does not rule out LLVM, we will
# proceed onward.
# We will run various probes for LLVM. If the probes are unsuccessful, we
# will set_result to 'no', set 'has_llvm' to '', then return from
# runstep() with a value of 1. If a given probe does not rule out LLVM,
# we will proceed onward.

my $llvm_bindir = `llvm-config --bindir`;
chomp $llvm_bindir;
if (! $llvm_bindir ) {
$self->_handle_result( $conf, 0 );
return 1;
}
my (@output, $version);
my @output;
chomp(@output = `"$llvm_bindir/lli" --version`);
if ( $output[1] =~ m/llvm\sversion\s(\d+\.\d+)/s ) {
$version = $1;
if ($version < $self->{lli_min_version}) {
if ($verbose) {
my $msg = "LLVM component 'lli' must be at least version ";
$msg .= "$self->{lli_min_version}; found version $version\n";
print $msg;
}
$self->_handle_result( $conf, 0 );
return 1;
}
else {
if ($verbose) {
print "Found 'lli' version $version\n";
}
}
}
else {
print "Unable to extract version for LLVM component 'lli'\n"
if $verbose;
$self->_handle_result( $conf, 0 );
return 1;
}
my $rv = $self->version_check($conf, \@output, $verbose);
return 1 unless $rv;

# Having gotten this far, we will take a simple C file, compile it into
# an LLVM bitcode file, execute it as bitcode, then compile it to native
Expand All @@ -83,7 +62,7 @@ sub runstep {
eval {
system(qq{llvm-gcc -O3 -emit-llvm $fullcfile -c -o $bcfile});
};
my $rv = '';
$rv = '';
if ($@) {
$rv = $self->_handle_failure_to_compile_into_bitcode(
$conf,
Expand Down Expand Up @@ -154,6 +133,35 @@ sub runstep {
return 1;
}

sub version_check {
my ($self, $conf, $outputref, $verbose) = @_;
my $version;
if ( $outputref->[1] =~ m/llvm\sversion\s(\d+\.\d+)/s ) {
$version = $1;
if ($version < $self->{lli_min_version}) {
if ($verbose) {
my $msg = "LLVM component 'lli' must be at least version ";
$msg .= "$self->{lli_min_version}; found version $version\n";
print $msg;
}
$self->_handle_result( $conf, 0 );
return;
}
else {
if ($verbose) {
print "Found 'lli' version $version\n";
}
return 1;
}
}
else {
print "Unable to extract version for LLVM component 'lli'\n"
if $verbose;
$self->_handle_result( $conf, 0 );
return;
}
}

sub _handle_failure_to_compile_into_bitcode {
my ($self, $conf, $verbose ) = @_;
print "Unable to compile C file into LLVM bitcode file\n"
Expand Down
81 changes: 76 additions & 5 deletions t/steps/auto/llvm-01.t
Expand Up @@ -5,7 +5,7 @@
use strict;
use warnings;
use File::Temp qw( tempdir );
use Test::More tests => 39;
use Test::More tests => 49;
use Carp;
use lib qw( lib t/configure/testlib );
use_ok('config::auto::llvm');
Expand Down Expand Up @@ -63,9 +63,7 @@ $step = test_step_constructor_and_description($conf);
SKIP: {
skip 'No sense testing for verbose output if LLVM not present',
2 unless ( $step->result() =~ /yes/ );
like( $stdout, qr/llvm-gcc/s,
"Got expected verbose output" );
like( $stdout, qr/Low Level Virtual Machine/s,
like( $stdout, qr/version/s,
"Got expected verbose output" );
}
}
Expand All @@ -84,9 +82,82 @@ is( $step->result(), 'no', "Got expected 'no' result" );
ok( ! $conf->data->get( 'has_llvm' ),
"'has_llvm' set to false value, as expected" );

##### 4 internal methods #####
my @output = (
'',
'llvm version 2.7',
);
my $verbose = 0;
ok( $step->version_check($conf, \@output, $verbose),
"Got expected return value: version_check() with sufficient version" );

$output[1] = 'llvm version 1.0';
$verbose = 0;
ok( ! $step->version_check($conf, \@output, $verbose),
"Got expected return value: version_check() with insufficient version" );
is( $step->result(), 'no',
"Got expected result: insufficient version" );
ok( ! $conf->data->get( 'has_llvm' ),
"'has_llvm' not set: insufficient version" );

$output[1] = 'llvm version 2.7',
$verbose = 1;
{
my ($stdout, $stderr);
capture(
sub { $step->version_check($conf, \@output, $verbose); },
\$stdout,
\$stderr,
);
like(
$stdout,
qr/Found 'lli' version/,
"Got expected verbose output: version_check() with sufficient version"
);
}
$output[1] = 'llvm version 1.0';
$verbose = 1;
{
my ($stdout, $stderr);
capture(
sub { $step->version_check($conf, \@output, $verbose); },
\$stdout,
\$stderr,
);
like(
$stdout,
qr/LLVM component 'lli' must be at least version/,
"Got expected verbose output: version_check() with insufficient version"
);
}

$output[1] = 'foobar';
$verbose = 0;
ok( ! $step->version_check($conf, \@output, $verbose),
"Got expected return value: version_check() with version not found" );
is( $step->result(), 'no',
"Got expected result: version not found" );
ok( ! $conf->data->get( 'has_llvm' ),
"'has_llvm' not set: version not found" );

$output[1] = 'foobar';
$verbose = 1;
{
my ($stdout, $stderr);
capture(
sub { $step->version_check($conf, \@output, $verbose); },
\$stdout,
\$stderr,
);
like(
$stdout,
qr/Unable to extract version for LLVM component/,
"Got expected verbose output: version_check() with version not detected"
);
}

##### 4 internal methods #####

$verbose = 0;
$step->set_result( undef );
$step->_handle_failure_to_compile_into_bitcode( $conf, $verbose );
is( $step->result(), 'no',
Expand Down

0 comments on commit 8504fc2

Please sign in to comment.