use 5.20.0;

use ExtUtils::testlib;
use Test::More ;
use Test::Exception ;
use Test::Memory::Cycle;
use Config::Model ;
use Config::Model::Tester::Setup qw/init_test setup_test_dir/;
use Test::Log::Log4perl;

use warnings;
use strict;

use feature qw/postderef signatures/;
no warnings qw/experimental::postderef experimental::signatures/;

Test::Log::Log4perl->ignore_priority("info");

my ($model, $trace) = init_test();

# pseudo root where config files are written by config-model
my $wr_root = setup_test_dir();

sub get_instance ($name) {
    my $instance = $model->instance(
        root_class_name => 'Dpkg::Control',
        root_dir        => $wr_root,
        instance_name   => $name,
    );

    my $root = $instance->config_root ;
    $instance->initial_load_stop;
    return $instance;
}

# cleanup before tests

subtest "PWD cannot be used for Source package name" => sub {
    my $bad_dir = $wr_root->child('1');
    $bad_dir->mkpath;
    chdir $bad_dir;

    my $i = get_instance('bad_test');

    # setting Standards-Version to avoid an undef warning
    # Source is a mandatory value and must be set before calling apply_fixes
    $i->load('source Standards-Version=3.6.0 Source=foo-pkg');
    $i->apply_fixes;
    ok(1,"apply_fixes is done");
    is($i->grab_value('source Source'),'foo-pkg',"check that Source is not modified");
    chdir $wr_root;
};

# Test Build-Profiles which has a complex match regexp
subtest "Build-Profiles" => sub {
    my $instance = get_instance('build_profile');

    my $bp = $instance->grab("binary:test-bp Build-Profiles");

    my @bad = ('plop', '<check> plop', 'plop <!nocheck>');
    foreach my $t (@bad) {
        throws_ok {
            $bp->store($t);
        } 'Config::Model::Exception::WrongValue', "set bad value $t";
    }

    my @test = ('<check>', '<!nocheck>','<pkg.foo-bar.baz-foo>');
    foreach my $t (@test) {
        $bp->store($t);
        ok(1,"set $t");
    }
};

# see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1145300
subtest "unbreakable line in description - no change" => sub {
    my $instance = get_instance('description');
    $instance->load("source Source=dont-care");

    my $desc = $instance->grab("binary:test-desc Description");
    my $str = <<EOD ;
Using CF-Compliant Calendars with Climate Projection Data
Support for all calendars as specified in the Climate
and Forecast (CF) Metadata Conventions for climate and forecasting data.
is formally defined in the CF Metadata Conventions document available at
<https://cf-convention.github.io/Data/cf-conventions/cf-conventions-1.13/cf-conventions.html#time-coordinate>.
EOD
    $desc->store($str);

    $instance->apply_fixes;
    is($desc->fetch, $str, "description is not changed");
};

subtest "unbreakable line in description with change" => sub {
    my $instance = get_instance('description');
    $instance->load("source Source=dont-care");

    my $desc = $instance->grab("binary:test-desc Description");
    my $str = <<EOD ;
Using CF-Compliant Calendars with Climate Projection Data Support for all calendars as specified in the Climate and Forecast (CF) Metadata Conventions for climate and forecasting data.
[snip]
is formally defined in the CF Metadata Conventions document available at
<https://cf-convention.github.io/Data/cf-conventions/cf-conventions-1.13/cf-conventions.html#time-coordinate>.
EOD
    $desc->store($str);

    my $expect = <<EXP;
Using CF-Compliant Calendars with Climate Projection Data Support for
all calendars as specified in the Climate and Forecast (CF) Metadata
Conventions for climate and forecasting data. [snip] is formally defined
in the CF Metadata Conventions document available at
<https://cf-convention.github.io/Data/cf-conventions/cf-conventions-1.13/cf-conventions.html#time-coordinate>.
EXP
    chomp ($expect);
    $instance->apply_fixes;
    is($desc->fetch, $expect, "URL is not broken");
};

memory_cycle_ok($model, "check memory cycles");

done_testing();
