App-Sqitch

 view release on metacpan or  search on metacpan

t/target.t  view on Meta::CPAN

is_deeply $target->variables, {a => 1}, 'Variables should be set';

# Pass a URI but no name.
isa_ok $target = $CLASS->new(
    sqitch => $sqitch,
    uri    => $uri,
), $CLASS, 'Target with URI';
like $target->name, qr{db:pg://hi:?\@localhost/blah},
    'Name should be URI without password';
is $target->target, $target->name, 'Target should be alias for name';
is $target->engine_key, 'pg', 'Engine key should be "pg"';
isa_ok $target->engine, 'App::Sqitch::Engine::pg', 'Engine';
is $target->dsn, $uri->dbi_dsn, 'DSN should be from URI';
is $target->username, $uri->user, 'Username should be from URI';
is $target->password, $uri->password, 'Password should be from URI';

# Set the URI via SQITCH_TARGET.
ENV: {
    local $ENV{SQITCH_TARGET} = 'db:pg:';
    isa_ok my $target = $CLASS->new(sqitch => $sqitch), $CLASS,
        'Target from environment';
    is $target->name, 'db:pg:', 'Name should be set';
    is $target->uri, 'db:pg:', 'URI should be set';
    is $target->engine_key, 'pg', 'Engine key should be "pg"';
    isa_ok $target->engine, 'App::Sqitch::Engine::pg', 'Engine';
}

# Set up a config.
CONSTRUCTOR: {
    my (@get_params, $orig_get);
    my $mock = TestConfig->mock(
        get => sub { my $c = shift; push @get_params => \@_; $orig_get->($c, @_); }
    );
    $orig_get = $mock->original('get');
    $config->replace('core.engine' => 'sqlite');

    # Pass neither, but rely on the engine in the Sqitch object.
    my $sqitch = App::Sqitch->new(config => $config);
    isa_ok my $target = $CLASS->new(sqitch => $sqitch), $CLASS, 'Default target';
    is $target->name, 'db:sqlite:', 'Name should be "db:sqlite:"';
    is $target->uri, URI::db->new('db:sqlite:'), 'URI should be "db:sqlite:"';
    is_deeply \@get_params, [
        [key => 'core.target'],
        [key => 'core.engine'],
        [key => 'engine.sqlite.target'],
    ], 'Should have tried to get engine target';

    # Try with just core.engine.
    delete $sqitch->options->{engine};
    $config->update('core.engine' => 'mysql');
    @get_params = ();
    isa_ok $target = $CLASS->new(sqitch => $sqitch), $CLASS, 'Default target';
    is $target->name, 'db:mysql:', 'Name should be "db:mysql:"';
    is $target->uri, URI::db->new('db:mysql:'), 'URI should be "db:mysql"';
    is_deeply \@get_params, [
        [key => 'core.target'],
        [key => 'core.engine'],
        [key => 'engine.mysql.target'],
    ], 'Should have tried to get core.target, core.engine and then the target';

    # Try with no engine option but a name that looks like a URI.
    @get_params = ();
    delete $sqitch->options->{engine};
    isa_ok $target = $CLASS->new(
        sqitch => $sqitch,
        name   => 'db:pg:',
    ), $CLASS, 'Target with URI in name';
    is $target->name, 'db:pg:', 'Name should be "db:pg:"';
    is $target->uri, URI::db->new('db:pg:'), 'URI should be "db:pg"';
    is_deeply \@get_params, [], 'Should have fetched no config';

    # Try it with a name with no engine.
    throws_ok { $CLASS->new(sqitch => $sqitch, name => 'db:') } 'App::Sqitch::X',
        'Should have error for no engine in URI';
    is $@->ident, 'target', 'Should have target ident';
    is $@->message, __x(
        'No engine specified by URI {uri}; URI must start with "db:$engine:"',
        uri => 'db:',
    ), 'Should have message about no engine-less URI';

    # Try it with no configured core engine or target.
    $config->replace;
    throws_ok { $CLASS->new(sqitch => $sqitch) } 'App::Sqitch::X',
        'Should have error for no engine or target';
    is $@->ident, 'target', 'Should have target ident';
    is $@->message, __(
        'No project configuration found. Run the "init" command to initialize a project',
    ), 'Should have message about no configuration';

    # Try it with a config file but no engine config.
    MOCK: {
        my $mock_init = TestConfig->mock(initialized => 1);
        throws_ok { $CLASS->new(sqitch => $sqitch) } 'App::Sqitch::X',
            'Should again have error for no engine or target';
        is $@->ident, 'target', 'Should have target ident again';
        is $@->message, __(
            'No engine specified; specify via target or core.engine',
        ), 'Should have message about no specified engine';
    }

    # Try with engine-less URI.
    @get_params = ();
    isa_ok $target = $CLASS->new(
        sqitch => $sqitch,
        uri    => URI::db->new('db:'),
    ), $CLASS, 'Engineless target';
    is $target->name, 'db:', 'Name should be "db:"';
    is $target->uri, URI::db->new('db:'), 'URI should be "db:"';
    is_deeply \@get_params, [], 'Should not have tried to get engine target';

    is $target->sqitch, $sqitch, 'Sqitch should be as passed';
    is $target->engine_key, undef, 'Engine key should be undef';
    throws_ok { $target->engine } 'App::Sqitch::X',
        'Should get exception for no engine';
    is $@->ident, 'engine', 'Should have engine ident';
    is $@->message, __(
        'No engine specified; specify via target or core.engine',
    ), 'Should have message about no engine';

    is $target->top_dir, dir, 'Should have default top_dir';
    is $target->deploy_dir, $target->top_dir->subdir('deploy'),



( run in 1.076 second using v1.01-cache-2.11-cpan-e93a5daba3e )