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
124
125
|
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(
run_psql_args => '-1Xq -v ON_ERROR_STOP=1', # No -b on 9.1
);
# On 9.1, must run each file separately
$pg->run_psql_scripts('db/schema.sql');
$pg->run_psql_scripts('db/fixture.sql');
$pg->run_psql_scripts('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;
# Don't warn over use of Regex dispatch type
local $ENV{CATALYST_NOWARN_DEPRECATE} = 1;
my $prove = App::Prove->new;
$prove->process_args(@ARGV);
# If no arguments, test everything
$prove->argv(['t']) unless @{$prove->argv} || @state;
# verbose if we have a single file
$prove->verbose(1) if @{$prove->argv} and -f $prove->argv->[-1] && !$ENV{CI};
# we always want to recurse
$prove->recurse(1);
# we always want to save state
$prove->state([ @state, 'save' ]);
$prove->run;
}
1;
|