use strict;
use FindBin;
use Test::More;
use Config;
use Cwd qw/cwd/;
use File::Temp qw/tempdir/;

require q[./t/helper.pm];

my $pwd  = cwd;
my $dist = "$FindBin::Bin/../test_dists/FailTests/FailTests-1.0.tar.gz";
my $tmpdir  = tempdir( TEMPLATE => File::Spec->tmpdir . "/mechacpan_t_XXXXXXXX", CLEANUP => 1 );
chdir $tmpdir;

local $SIG{__WARN__} = sub {note shift};

my $dir = cwd;

# --skip-tests(-for)
{
  isnt( App::MechaCPAN::main( 'install', $dist ), 0, "Fail as expected: $dist" );
  is( cwd, $dir, 'Returned to whence it started' );

  is( App::MechaCPAN::main( '--skip-tests', 'install', $dist ), 0, "Skipped tests: $dist" );
  is( cwd, $dir, 'Returned to whence it started' );

  is( App::MechaCPAN::main( '--skip-tests-for', $dist, 'install', $dist ), 0, "Skipped tests for: $dist" );
  is( cwd, $dir, 'Returned to whence it started' );
}

# --no-update
{
  my $dist = 'Test::More';
  local $Module::CoreList::version{$]}{$dist} = 0
    if $] >= 5.028000;
  no strict 'refs';
  no warnings 'redefine';
  my $ran_configure = 0;
  local *App::MechaCPAN::Install::_configure = sub { die $ran_configure = 1 };
  local $@;

  eval { App::MechaCPAN::main( 'install', $dist ) };
  is( $ran_configure, 1, "Did try and install $dist by default" );

  $ran_configure = 0;
  eval { App::MechaCPAN::main( 'install', '--update', $dist ) };
  is( $ran_configure, 1, "Did try and install $dist when asked to" );

  $ran_configure = 0;
  eval { App::MechaCPAN::main( 'install', '--no-update', $dist ) };
  is( $ran_configure, 0, "Didn't try and install $dist when told not to upgrade" );
}

# --smart-tests
{
  my $depdist = "$FindBin::Bin/../test_dists/FailWDeps/FailWDeps-1.0.tar.gz";
  isnt( App::MechaCPAN::main( 'install', $depdist ), 0, "Fail as expected: $depdist" );

  isnt( App::MechaCPAN::main( '--smart-tests', 'install', $depdist ), 0, "Smart tests ran the tests: $dist" );
  is( App::MechaCPAN::main( '--smart-tests', 'install', $dist, $depdist), 0, "Smart tests didn't run the tests: $dist" );

  is( cwd, $dir, 'Returned to whence it started' );
}

# --skip-tests-for, but instead of testing from ::main, test just the sub that
# decides if the tests should be skipped or not
{
  my $target = {
    meta     => CPAN::Meta->new({ version => '1.0', name => 'FooBar'}),
    src_name => 'FooBar-1.0',
    modules  => {
      'Foo::Bar' => { inital_version => undef },
      'Foo::Baz' => { inital_version => undef },
    },
  };
  my $cache = { opts => { 'skip-tests-for' => { 'Foo::Bar' => 1 } } };

  App::MechaCPAN::Install::_test_prereq( $target, $cache );
  is( $target->{skip_tests}, 1, 'skip-tests-for skips when a provided module name matches' );

  delete $target->{skip_tests};
  $cache->{opts}->{'skip-tests-for'} = { $target->{src_name} => 1 };

  App::MechaCPAN::Install::_test_prereq( $target, $cache );
  is( $target->{skip_tests}, 1, 'skip-tests-for skips when a package name is provided' );

  my $skip_key = $target->{modules}->{'Foo::Bar'};
  $cache->{opts}->{'skip-tests-for'} = { $skip_key => 1 };
  delete $target->{skip_tests};

  App::MechaCPAN::Install::_test_prereq( $target, $cache );
  is( $target->{skip_tests}, '', 'skip-tests-for does not skip when a provided module value matches' );
}

chdir $pwd;
done_testing;
