aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/TestAppProve.pm
blob: 4a55d587b9cd0429fb8e7cd92ad911f92f086ee3 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use strict; use warnings;
package FixMyStreet::TestAppProve;
use App::Prove;

use sigtrap qw(handler signal_handler normal-signals);

use YAML ();
use Path::Tiny 'path';
use Test::PostgreSQL;
use Data::Dumper;
use Getopt::Long ':config' => qw(bundling pass_through no_ignore_case);

=head1 NAME

FixMyStreet::TestAppProve - spin up a clean database and configuration for tests

=head1 USAGE

see bin/run-tests for usage

=cut

sub cleanup {
    unlink "conf/general-test-autogenerated.$$.yml";
}

sub signal_handler {
    cleanup();
    exit(0);
}

END {
    cleanup();
}

my $pg;

sub spin_up_database {
    warn "Spinning up a Pg cluster/database...\n";
    $pg = Test::PostgreSQL->new(
        seed_scripts => [
            'db/schema.sql',
            'db/fixture.sql',
            'db/generate_secret.sql',
        ],
    );

    warn sprintf "# Connected to %s\n", $pg->dsn;

    return {
        FMS_DB_PORT => $pg->port,
        FMS_DB_NAME => 'test',
        FMS_DB_USER => 'postgres',
        FMS_DB_HOST => 'localhost',
        FMS_DB_PASS => '',
    };
}

sub get_config {
    my $cls = shift;
    my $extra_config = shift;
    my $config_file = delete $extra_config->{config_file};
    my $db_config_file = delete $extra_config->{db_config_file};

    my $config = YAML::Load( path($config_file)->slurp );
    if ($db_config_file) {
        my $db_config = YAML::Load( path($db_config_file)->slurp );
        $config->{FMS_DB_PORT} = $db_config->{FMS_DB_PORT};
        $config->{FMS_DB_NAME} = $db_config->{FMS_DB_NAME};
        $config->{FMS_DB_USER} = $db_config->{FMS_DB_USER};
        $config->{FMS_DB_HOST} = $db_config->{FMS_DB_HOST};
        $config->{FMS_DB_PASS} = $db_config->{FMS_DB_PASS};
    } else {
        my $new_db_config = $cls->spin_up_database();
        $config = { %$config, %$new_db_config };
    }

    $config = { %$config, %$extra_config };

    my $config_out = "general-test-autogenerated.$$.yml";
    path("conf/$config_out")->spew( YAML::Dump($config) );
    return $config_out;
}

sub run {
    my ($class, @args) = @_;
    local @ARGV = @args;

    my $config_file = 'conf/general.yml-example';
    my $db_config_file;

    my $recurse;
    my @state;

    GetOptions (
        # our own config variables
        'config=s'    => \$config_file,
        'db-config=s' => \$db_config_file,

        # App::Prove variables we want to munge
        'r|recurse' => \$recurse,
        'state=s@'  => \@state,
    );

    my $config_out = $class->get_config({ config_file => $config_file, db_config_file => $db_config_file });
    local $ENV{FMS_OVERRIDE_CONFIG} = $config_out;

    my $prove = App::Prove->new;
    $prove->process_args(@ARGV);

    # If no arguments, test everything
    $prove->argv(['t']) unless @{$prove->argv};
    # verbose if we have a single file
    $prove->verbose(1) if @{$prove->argv} and -f $prove->argv->[-1];
    # we always want to recurse
    $prove->recurse(1);
    # we always want to save state
    $prove->state([ @state, 'save' ]);

    $prove->run;
}

1;