/
init.t
361 lines (317 loc) · 12 KB
/
init.t
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.10.1;
use utf8;
use Test::More tests => 87;
#use Test::More 'no_plan';
use App::Sqitch;
use Path::Class;
use Test::Dir;
use Test::File qw(file_not_exists_ok file_exists_ok);
use Test::Exception;
use Test::File::Contents;
use File::Path qw(remove_tree make_path);
use lib 't/lib';
use MockCommand;
my $CLASS;
BEGIN {
$CLASS = 'App::Sqitch::Command::init';
use_ok $CLASS or die;
}
isa_ok $CLASS, 'App::Sqitch::Command', $CLASS;
chdir 't';
sub read_config($) {
my $conf = App::Sqitch::Config->new;
$conf->load_file(shift);
$conf->data;
}
$ENV{SQITCH_CONFIG} = 'nonexistent.conf';
$ENV{SQITCH_USER_CONFIG} = 'nonexistent.user';
$ENV{SQITCH_SYSTEM_CONFIG} = 'nonexistent.sys';
##############################################################################
# Test make_directories.
my $sqitch = App::Sqitch->new(sql_dir => dir 'init.mkdir');
isa_ok my $init = $CLASS->new(sqitch => $sqitch), $CLASS, 'New init object';
can_ok $init, 'make_directories';
for my $attr (map { "$_\_dir"} qw(sql deploy revert test)) {
dir_not_exists_ok $sqitch->$attr;
}
my $sql_dir = $sqitch->sql_dir->stringify;
END { remove_tree $sql_dir }
ok $init->make_directories, 'Make the directories';
for my $attr (map { "$_\_dir"} qw(sql deploy revert test)) {
dir_exists_ok $sqitch->$attr;
}
is_deeply +MockCommand->get_info, [
map { ["Created " . $sqitch->$_] } map { "$_\_dir" } qw(deploy revert test)
], 'Each should have been sent to info';
# Do it again.
ok $init->make_directories, 'Make the directories again';
is_deeply +MockCommand->get_info, [], 'Nothing should have been sent to info';
# Delete one of them.
remove_tree $sqitch->revert_dir->stringify;
ok $init->make_directories, 'Make the directories once more';
dir_exists_ok $sqitch->revert_dir, 'revert dir exists again';
is_deeply +MockCommand->get_info, [
['Created ' . $sqitch->revert_dir],
], 'Should have noted creation of revert dir';
# Handle errors.
remove_tree $sql_dir;
make_path $sql_dir;
chmod 0000, $sql_dir;
END { chmod 0400, $sql_dir }
throws_ok { $init->make_directories } qr/FAIL/, 'Should fail on permissio issue';
is_deeply +MockCommand->get_fail, [
['Error creating ' . $sqitch->deploy_dir . ': Permission denied'],
], 'Failure should have been emitted';
##############################################################################
# Test write_config().
can_ok $init, 'write_config';
my $test_dir = 'init.write';
make_path $test_dir;
END { remove_tree $test_dir }
chdir $test_dir;
END { chdir File::Spec->updir }
my $conf_file = $sqitch->config->local_file;
$sqitch = App::Sqitch->new(extension => 'foo');
ok $init = $CLASS->new(sqitch => $sqitch), 'Anogher init object';
file_not_exists_ok $conf_file;
# Write config.
ok $init->write_config, 'Write the config';
file_exists_ok $conf_file;
is_deeply read_config $conf_file, {
'core.extension' => 'foo',
}, 'The configuration should have been written with only one setting';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info';
file_contents_like $conf_file, qr{\Q
# engine =
# plan_file = sqitch.plan
# sql_dir = sql
# deploy_dir = sql/deploy
# revert_dir = sql/revert
# test_dir = sql/test
}m, 'Other settings should be commented-out';
# Go again.
ok $init->write_config, 'Write the config again';
is_deeply read_config $conf_file, {
'core.extension' => 'foo',
}, 'The configuration should be unchanged';
is_deeply +MockCommand->get_info, [
], 'Nothing should have been sent to info';
USERCONF: {
# Delete the file and write with a user config loaded.
unlink $conf_file;
local $ENV{SQITCH_USER_CONFIG} = file +File::Spec->updir, 'user.conf';
my $sqitch = App::Sqitch->new(extension => 'foo');
ok my $init = $CLASS->new(sqitch => $sqitch), 'Make an init object with user config';
file_not_exists_ok $conf_file;
ok $init->write_config, 'Write the config with a user conf';
file_exists_ok $conf_file;
is_deeply read_config $conf_file, {
'core.extension' => 'foo',
}, 'The configuration should just have core.sql_dir';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info again';
file_contents_like $conf_file, qr{\Q
# engine =
# plan_file = sqitch.plan
# sql_dir = sql
# deploy_dir = sql/deploy
# revert_dir = sql/revert
# test_dir = sql/test
}m, 'Other settings should be commented-out';
}
SYSTEMCONF: {
# Delete the file and write with a system config loaded.
unlink $conf_file;
local $ENV{SQITCH_SYSTEM_CONFIG} = file +File::Spec->updir, 'sqitch.conf';
my $sqitch = App::Sqitch->new(extension => 'foo');
ok my $init = $CLASS->new(sqitch => $sqitch), 'Make an init object with system config';
file_not_exists_ok $conf_file;
ok $init->write_config, 'Write the config with a system conf';
file_exists_ok $conf_file;
is_deeply read_config $conf_file, {
'core.extension' => 'foo',
'core.engine' => 'pg',
}, 'The configuration should just have core.sql_dir';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info again';
file_contents_like $conf_file, qr{\Q
# plan_file = sqitch.plan
# sql_dir = migrations
# deploy_dir = migrations/deploy
# revert_dir = migrations/revert
# test_dir = migrations/test
}m, 'Other settings should be commented-out';
}
##############################################################################
# Now get it to write a bunch of other stuff.
unlink $conf_file;
$sqitch = App::Sqitch->new(
plan_file => 'my.plan',
deploy_dir => 'dep',
revert_dir => 'rev',
test_dir => 'tst',
extension => 'ddl',
_engine => 'sqlite',
);
ok $init = $CLASS->new(sqitch => $sqitch),
'Create new init with sqitch non-default attributes';
ok $init->write_config, 'Write the config with core attrs';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info once more';
is_deeply read_config $conf_file, {
'core.plan_file' => 'my.plan',
'core.deploy_dir' => 'dep',
'core.revert_dir' => 'rev',
'core.test_dir' => 'tst',
'core.extension' => 'ddl',
'core.engine' => 'sqlite',
}, 'The configuration should have been written with all the core values';
##############################################################################
# Now get it to write core.sqlite stuff.
unlink $conf_file;
$sqitch = App::Sqitch->new(
_engine => 'sqlite',
client => '/to/sqlite3',
db_name => 'my.db',
);
ok $init = $CLASS->new(sqitch => $sqitch),
'Create new init with sqitch with non-default engine attributes';
ok $init->write_config, 'Write the config with engine attrs';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info yet again';
is_deeply read_config $conf_file, {
'core.engine' => 'sqlite',
'core.sqlite.client' => '/to/sqlite3',
'core.sqlite.db_name' => 'my.db',
}, 'The configuration should have been written with sqlite values';
file_contents_like $conf_file, qr/^\t# sqitch_prefix = sqitch\n/m,
'sqitch_prefix should be included in a comment';
# Try it with no options.
unlink $conf_file;
$sqitch = App::Sqitch->new(_engine => 'sqlite');
ok $init = $CLASS->new(sqitch => $sqitch),
'Create new init with sqitch with default engine attributes';
ok $init->write_config, 'Write the config with engine attrs';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info again again';
is_deeply read_config $conf_file, {
'core.engine' => 'sqlite',
}, 'The configuration should have been written with only the engine var';
file_contents_like $conf_file, qr{^\Q# [core "sqlite"]
# sqitch_prefix = sqitch
# db_name =
# client = sqlite3
}m, 'Engine section should be present but commented-out';
# Now build it with other config.
USERCONF: {
# Delete the file and write with a user config loaded.
unlink $conf_file;
local $ENV{SQITCH_USER_CONFIG} = file +File::Spec->updir, 'user.conf';
my $sqitch = App::Sqitch->new(
_engine => 'sqlite',
db_name => 'my.db',
);
ok my $init = $CLASS->new(sqitch => $sqitch),
'Make an init with sqlite and user config';
file_not_exists_ok $conf_file;
ok $init->write_config, 'Write the config with sqlite config';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info once more';
is_deeply read_config $conf_file, {
'core.engine' => 'sqlite',
'core.sqlite.db_name' => 'my.db',
}, 'New config should have been written with sqlite values';
file_contents_like $conf_file, qr{^\t# client = /opt/local/bin/sqlite3\E\n}m,
'Configured client should be included in a comment';
file_contents_like $conf_file, qr/^\t# sqitch_prefix = meta\n/m,
'Configured sqitch_prefix should be included in a comment';
}
##############################################################################
# Now get it to write core.pg stuff.
unlink $conf_file;
$sqitch = App::Sqitch->new(
_engine => 'pg',
client => '/to/psql',
db_name => 'thingies',
username => 'anna',
host => 'banana',
port => 93453,
);
ok $init = $CLASS->new(sqitch => $sqitch),
'Create new init with sqitch with more non-default engine attributes';
ok $init->write_config, 'Write the config with more engine attrs';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info one more time';
is_deeply read_config $conf_file, {
'core.engine' => 'pg',
'core.pg.client' => '/to/psql',
'core.pg.db_name' => 'thingies',
'core.pg.username' => 'anna',
'core.pg.host' => 'banana',
'core.pg.port' => 93453,
}, 'The configuration should have been written with pg values';
file_contents_like $conf_file, qr/^\t# sqitch_schema = sqitch\n/m,
'sqitch_schema should be included in a comment';
file_contents_like $conf_file, qr/^\t# password = \n/m,
'password should be included in a comment';
# Try it with no config or options.
unlink $conf_file;
$sqitch = App::Sqitch->new(_engine => 'pg');
ok $init = $CLASS->new(sqitch => $sqitch),
'Create new init with sqitch with default engine attributes';
ok $init->write_config, 'Write the config with engine attrs';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The creation should be sent to info again again again';
is_deeply read_config $conf_file, {
'core.engine' => 'pg',
}, 'The configuration should have been written with only the engine var';
file_contents_like $conf_file, qr{^\Q# [core "pg"]
# db_name =
# client = psql
# sqitch_schema = sqitch
# password =
# port =
# host =
# username =
}m, 'Engine section should be present but commented-out';
USERCONF: {
# Delete the file and write with a user config loaded.
unlink $conf_file;
local $ENV{SQITCH_USER_CONFIG} = file +File::Spec->updir, 'user.conf';
my $sqitch = App::Sqitch->new(
_engine => 'pg',
db_name => 'thingies',
);
ok my $init = $CLASS->new(sqitch => $sqitch),
'Make an init with pg and user config';
file_not_exists_ok $conf_file;
ok $init->write_config, 'Write the config with pg config';
is_deeply +MockCommand->get_info, [
['Created ' . $conf_file]
], 'The pg config creation should be sent to info';
is_deeply read_config $conf_file, {
'core.engine' => 'pg',
'core.pg.db_name' => 'thingies',
}, 'The configuration should have been written with pg options';
file_contents_like $conf_file, qr/^\t# sqitch_schema = meta\n/m,
'Configured sqitch_schema should be in a comment';
file_contents_like $conf_file, qr/^\t# password = \n/m,
'password should be included in a comment';
file_contents_like $conf_file, qr/^\t# username = postgres\n/m,
'Configured username should be in a comment';
file_contents_like $conf_file, qr/^\t# host = localhost\n/m,
'Configured host should be in a comment';
}