Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 390 lines (343 sloc) 13.318 kB
b4d3f8b @theory Stub out `init` command implementation.
authored
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
bbdc1dc @theory Require Perl 5.10.1.
authored
5 use v5.10.1;
b4d3f8b @theory Stub out `init` command implementation.
authored
6 use utf8;
dbe6512 @theory Write `[core]` section header when no variables.
authored
7 use Test::More tests => 93;
b4d3f8b @theory Stub out `init` command implementation.
authored
8 #use Test::More 'no_plan';
8fe3bb0 @theory Implement `make_directories()`.
authored
9 use App::Sqitch;
10 use Path::Class;
11 use Test::Dir;
9f1b95a @theory Import dir_exists_ok only from Test::Dir.
authored
12 use Test::File qw(file_not_exists_ok file_exists_ok);
8fe3bb0 @theory Implement `make_directories()`.
authored
13 use Test::Exception;
46a2898 @theory Write engine specific vars with defaults as comments.
authored
14 use Test::File::Contents;
8fe3bb0 @theory Implement `make_directories()`.
authored
15 use File::Path qw(remove_tree make_path);
16 use lib 't/lib';
17 use MockCommand;
b4d3f8b @theory Stub out `init` command implementation.
authored
18
19 my $CLASS;
20
21 BEGIN {
22 $CLASS = 'App::Sqitch::Command::init';
23 use_ok $CLASS or die;
24 }
8fe3bb0 @theory Implement `make_directories()`.
authored
25
26 isa_ok $CLASS, 'App::Sqitch::Command', $CLASS;
d7370f1 @theory Start work on `write_config()`.
authored
27 chdir 't';
28
29 sub read_config($) {
30 my $conf = App::Sqitch::Config->new;
31 $conf->load_file(shift);
32 $conf->data;
33 }
8fe3bb0 @theory Implement `make_directories()`.
authored
34
dbec163 @theory More `write_config()` tests.
authored
35 $ENV{SQITCH_CONFIG} = 'nonexistent.conf';
36 $ENV{SQITCH_USER_CONFIG} = 'nonexistent.user';
37 $ENV{SQITCH_SYSTEM_CONFIG} = 'nonexistent.sys';
38
8fe3bb0 @theory Implement `make_directories()`.
authored
39 ##############################################################################
40 # Test make_directories.
d7370f1 @theory Start work on `write_config()`.
authored
41 my $sqitch = App::Sqitch->new(sql_dir => dir 'init.mkdir');
42 isa_ok my $init = $CLASS->new(sqitch => $sqitch), $CLASS, 'New init object';
43
8fe3bb0 @theory Implement `make_directories()`.
authored
44 can_ok $init, 'make_directories';
45 for my $attr (map { "$_\_dir"} qw(sql deploy revert test)) {
46 dir_not_exists_ok $sqitch->$attr;
47 }
48
d7370f1 @theory Start work on `write_config()`.
authored
49 my $sql_dir = $sqitch->sql_dir->stringify;
50 END { remove_tree $sql_dir }
8fe3bb0 @theory Implement `make_directories()`.
authored
51
52 ok $init->make_directories, 'Make the directories';
53 for my $attr (map { "$_\_dir"} qw(sql deploy revert test)) {
54 dir_exists_ok $sqitch->$attr;
55 }
56 is_deeply +MockCommand->get_info, [
57 map { ["Created " . $sqitch->$_] } map { "$_\_dir" } qw(deploy revert test)
58 ], 'Each should have been sent to info';
59
60 # Do it again.
61 ok $init->make_directories, 'Make the directories again';
62 is_deeply +MockCommand->get_info, [], 'Nothing should have been sent to info';
63
64 # Delete one of them.
65 remove_tree $sqitch->revert_dir->stringify;
66 ok $init->make_directories, 'Make the directories once more';
67 dir_exists_ok $sqitch->revert_dir, 'revert dir exists again';
68 is_deeply +MockCommand->get_info, [
69 ['Created ' . $sqitch->revert_dir],
70 ], 'Should have noted creation of revert dir';
71
72 # Handle errors.
73 remove_tree $sql_dir;
74 make_path $sql_dir;
75 chmod 0000, $sql_dir;
76 END { chmod 0400, $sql_dir }
77 throws_ok { $init->make_directories } qr/FAIL/, 'Should fail on permissio issue';
78 is_deeply +MockCommand->get_fail, [
79 ['Error creating ' . $sqitch->deploy_dir . ': Permission denied'],
80 ], 'Failure should have been emitted';
d7370f1 @theory Start work on `write_config()`.
authored
81
82 ##############################################################################
83 # Test write_config().
84 can_ok $init, 'write_config';
85
a65c9ec @theory Fix anticipated Win32 test failures in `t/init.t`.
authored
86 my $write_dir = 'init.write';
87 make_path $write_dir;
88 END { remove_tree $write_dir }
89 chdir $write_dir;
d7370f1 @theory Start work on `write_config()`.
authored
90 END { chdir File::Spec->updir }
2c88ec9 @theory Add --local to config command.
authored
91 my $conf_file = $sqitch->config->local_file;
dbec163 @theory More `write_config()` tests.
authored
92
dbe6512 @theory Write `[core]` section header when no variables.
authored
93 $sqitch = App::Sqitch->new;
94 ok $init = $CLASS->new(sqitch => $sqitch), 'Another init object';
dbec163 @theory More `write_config()` tests.
authored
95 file_not_exists_ok $conf_file;
d7370f1 @theory Start work on `write_config()`.
authored
96
dbe6512 @theory Write `[core]` section header when no variables.
authored
97 # Write empty config.
98 ok $init->write_config, 'Write the config';
99 file_exists_ok $conf_file;
100 is_deeply read_config $conf_file, {
101 }, 'The configuration file should have no variables';
102 is_deeply +MockCommand->get_info, [
103 ['Created ' . $conf_file]
104 ], 'The creation should be sent to info';
a65c9ec @theory Fix anticipated Win32 test failures in `t/init.t`.
authored
105
106 my $deploy_dir = File::Spec->catfile(qw(sql deploy));
107 my $revert_dir = File::Spec->catfile(qw(sql revert));
108 my $test_dir = File::Spec->catfile(qw(sql test));
dbe6512 @theory Write `[core]` section header when no variables.
authored
109 file_contents_like $conf_file, qr{\Q# [core]
110 # engine =
111 # plan_file = sqitch.plan
112 # sql_dir = sql
a65c9ec @theory Fix anticipated Win32 test failures in `t/init.t`.
authored
113 # deploy_dir = $deploy_dir
114 # revert_dir = $revert_dir
115 # test_dir = $test_dir
dbe6512 @theory Write `[core]` section header when no variables.
authored
116 # extension = sql
117 }m, 'Entire core section should be commented-out';
118 unlink $conf_file;
119
120 # Set one option.
121 $sqitch = App::Sqitch->new(extension => 'foo');
122 ok $init = $CLASS->new(sqitch => $sqitch), 'Another init object';
d7370f1 @theory Start work on `write_config()`.
authored
123 ok $init->write_config, 'Write the config';
dbec163 @theory More `write_config()` tests.
authored
124 file_exists_ok $conf_file;
125 is_deeply read_config $conf_file, {
beeb12c @theory Write configured settings to init config as comments.
authored
126 'core.extension' => 'foo',
d7370f1 @theory Start work on `write_config()`.
authored
127 }, 'The configuration should have been written with only one setting';
128 is_deeply +MockCommand->get_info, [
dbec163 @theory More `write_config()` tests.
authored
129 ['Created ' . $conf_file]
d7370f1 @theory Start work on `write_config()`.
authored
130 ], 'The creation should be sent to info';
131
beeb12c @theory Write configured settings to init config as comments.
authored
132 file_contents_like $conf_file, qr{\Q
133 # engine =
134 # plan_file = sqitch.plan
135 # sql_dir = sql
a65c9ec @theory Fix anticipated Win32 test failures in `t/init.t`.
authored
136 # deploy_dir = $deploy_dir
137 # revert_dir = $revert_dir
138 # test_dir = $test_dir
beeb12c @theory Write configured settings to init config as comments.
authored
139 }m, 'Other settings should be commented-out';
140
dbec163 @theory More `write_config()` tests.
authored
141 # Go again.
142 ok $init->write_config, 'Write the config again';
143 is_deeply read_config $conf_file, {
beeb12c @theory Write configured settings to init config as comments.
authored
144 'core.extension' => 'foo',
dbec163 @theory More `write_config()` tests.
authored
145 }, 'The configuration should be unchanged';
146 is_deeply +MockCommand->get_info, [
147 ], 'Nothing should have been sent to info';
148
149 USERCONF: {
150 # Delete the file and write with a user config loaded.
151 unlink $conf_file;
152 local $ENV{SQITCH_USER_CONFIG} = file +File::Spec->updir, 'user.conf';
beeb12c @theory Write configured settings to init config as comments.
authored
153 my $sqitch = App::Sqitch->new(extension => 'foo');
154 ok my $init = $CLASS->new(sqitch => $sqitch), 'Make an init object with user config';
dbec163 @theory More `write_config()` tests.
authored
155 file_not_exists_ok $conf_file;
156 ok $init->write_config, 'Write the config with a user conf';
157 file_exists_ok $conf_file;
158 is_deeply read_config $conf_file, {
beeb12c @theory Write configured settings to init config as comments.
authored
159 'core.extension' => 'foo',
dbec163 @theory More `write_config()` tests.
authored
160 }, 'The configuration should just have core.sql_dir';
161 is_deeply +MockCommand->get_info, [
162 ['Created ' . $conf_file]
163 ], 'The creation should be sent to info again';
beeb12c @theory Write configured settings to init config as comments.
authored
164 file_contents_like $conf_file, qr{\Q
165 # engine =
166 # plan_file = sqitch.plan
167 # sql_dir = sql
a65c9ec @theory Fix anticipated Win32 test failures in `t/init.t`.
authored
168 # deploy_dir = $deploy_dir
169 # revert_dir = $revert_dir
170 # test_dir = $test_dir
beeb12c @theory Write configured settings to init config as comments.
authored
171 }m, 'Other settings should be commented-out';
dbec163 @theory More `write_config()` tests.
authored
172 }
173
174 SYSTEMCONF: {
175 # Delete the file and write with a system config loaded.
176 unlink $conf_file;
beeb12c @theory Write configured settings to init config as comments.
authored
177 local $ENV{SQITCH_SYSTEM_CONFIG} = file +File::Spec->updir, 'sqitch.conf';
178 my $sqitch = App::Sqitch->new(extension => 'foo');
179 ok my $init = $CLASS->new(sqitch => $sqitch), 'Make an init object with system config';
dbec163 @theory More `write_config()` tests.
authored
180 file_not_exists_ok $conf_file;
181 ok $init->write_config, 'Write the config with a system conf';
182 file_exists_ok $conf_file;
183 is_deeply read_config $conf_file, {
beeb12c @theory Write configured settings to init config as comments.
authored
184 'core.extension' => 'foo',
185 'core.engine' => 'pg',
dbec163 @theory More `write_config()` tests.
authored
186 }, 'The configuration should just have core.sql_dir';
187 is_deeply +MockCommand->get_info, [
188 ['Created ' . $conf_file]
189 ], 'The creation should be sent to info again';
a65c9ec @theory Fix anticipated Win32 test failures in `t/init.t`.
authored
190
191 my $deploy_dir = File::Spec->catfile(qw(migrations deploy));
192 my $revert_dir = File::Spec->catfile(qw(migrations revert));
193 my $test_dir = File::Spec->catfile(qw(migrations test));
beeb12c @theory Write configured settings to init config as comments.
authored
194 file_contents_like $conf_file, qr{\Q
195 # plan_file = sqitch.plan
196 # sql_dir = migrations
a65c9ec @theory Fix anticipated Win32 test failures in `t/init.t`.
authored
197 # deploy_dir = $deploy_dir
198 # revert_dir = $revert_dir
199 # test_dir = $test_dir
beeb12c @theory Write configured settings to init config as comments.
authored
200 }m, 'Other settings should be commented-out';
dbec163 @theory More `write_config()` tests.
authored
201 }
202
a203c1e @theory Add engine config to file created by init.
authored
203 ##############################################################################
dbec163 @theory More `write_config()` tests.
authored
204 # Now get it to write a bunch of other stuff.
205 unlink $conf_file;
206 $sqitch = App::Sqitch->new(
207 plan_file => 'my.plan',
208 deploy_dir => 'dep',
209 revert_dir => 'rev',
210 test_dir => 'tst',
211 extension => 'ddl',
2de1b99 @theory Turn `App::Sqitch->engine` into an object.
authored
212 _engine => 'sqlite',
dbec163 @theory More `write_config()` tests.
authored
213 );
214
215 ok $init = $CLASS->new(sqitch => $sqitch),
a203c1e @theory Add engine config to file created by init.
authored
216 'Create new init with sqitch non-default attributes';
dbec163 @theory More `write_config()` tests.
authored
217 ok $init->write_config, 'Write the config with core attrs';
218 is_deeply +MockCommand->get_info, [
219 ['Created ' . $conf_file]
220 ], 'The creation should be sent to info once more';
221
222 is_deeply read_config $conf_file, {
223 'core.plan_file' => 'my.plan',
224 'core.deploy_dir' => 'dep',
225 'core.revert_dir' => 'rev',
226 'core.test_dir' => 'tst',
227 'core.extension' => 'ddl',
228 'core.engine' => 'sqlite',
229 }, 'The configuration should have been written with all the core values';
230
a203c1e @theory Add engine config to file created by init.
authored
231 ##############################################################################
232 # Now get it to write core.sqlite stuff.
233 unlink $conf_file;
234 $sqitch = App::Sqitch->new(
235 _engine => 'sqlite',
236 client => '/to/sqlite3',
237 db_name => 'my.db',
238 );
239
240 ok $init = $CLASS->new(sqitch => $sqitch),
241 'Create new init with sqitch with non-default engine attributes';
242 ok $init->write_config, 'Write the config with engine attrs';
243 is_deeply +MockCommand->get_info, [
244 ['Created ' . $conf_file]
245 ], 'The creation should be sent to info yet again';
246
247 is_deeply read_config $conf_file, {
248 'core.engine' => 'sqlite',
249 'core.sqlite.client' => '/to/sqlite3',
250 'core.sqlite.db_name' => 'my.db',
251 }, 'The configuration should have been written with sqlite values';
252
46a2898 @theory Write engine specific vars with defaults as comments.
authored
253 file_contents_like $conf_file, qr/^\t# sqitch_prefix = sqitch\n/m,
254 'sqitch_prefix should be included in a comment';
255
b2243bd @theory Write engine section header if no vars.
authored
256 # Try it with no options.
257 unlink $conf_file;
258 $sqitch = App::Sqitch->new(_engine => 'sqlite');
259 ok $init = $CLASS->new(sqitch => $sqitch),
260 'Create new init with sqitch with default engine attributes';
261 ok $init->write_config, 'Write the config with engine attrs';
262 is_deeply +MockCommand->get_info, [
263 ['Created ' . $conf_file]
264 ], 'The creation should be sent to info again again';
265 is_deeply read_config $conf_file, {
266 'core.engine' => 'sqlite',
267 }, 'The configuration should have been written with only the engine var';
268
269 file_contents_like $conf_file, qr{^\Q# [core "sqlite"]
270 # sqitch_prefix = sqitch
271 # db_name =
272 # client = sqlite3
273 }m, 'Engine section should be present but commented-out';
274
beeb12c @theory Write configured settings to init config as comments.
authored
275 # Now build it with other config.
276 USERCONF: {
277 # Delete the file and write with a user config loaded.
278 unlink $conf_file;
279 local $ENV{SQITCH_USER_CONFIG} = file +File::Spec->updir, 'user.conf';
280 my $sqitch = App::Sqitch->new(
281 _engine => 'sqlite',
282 db_name => 'my.db',
283 );
284 ok my $init = $CLASS->new(sqitch => $sqitch),
285 'Make an init with sqlite and user config';
286 file_not_exists_ok $conf_file;
287 ok $init->write_config, 'Write the config with sqlite config';
288 is_deeply +MockCommand->get_info, [
289 ['Created ' . $conf_file]
290 ], 'The creation should be sent to info once more';
291
292 is_deeply read_config $conf_file, {
293 'core.engine' => 'sqlite',
294 'core.sqlite.db_name' => 'my.db',
295 }, 'New config should have been written with sqlite values';
296
297 file_contents_like $conf_file, qr{^\t# client = /opt/local/bin/sqlite3\E\n}m,
298 'Configured client should be included in a comment';
299
300 file_contents_like $conf_file, qr/^\t# sqitch_prefix = meta\n/m,
301 'Configured sqitch_prefix should be included in a comment';
302 }
303
a203c1e @theory Add engine config to file created by init.
authored
304 ##############################################################################
305 # Now get it to write core.pg stuff.
306 unlink $conf_file;
307 $sqitch = App::Sqitch->new(
308 _engine => 'pg',
309 client => '/to/psql',
310 db_name => 'thingies',
311 username => 'anna',
312 host => 'banana',
313 port => 93453,
314 );
315
316 ok $init = $CLASS->new(sqitch => $sqitch),
317 'Create new init with sqitch with more non-default engine attributes';
318 ok $init->write_config, 'Write the config with more engine attrs';
319 is_deeply +MockCommand->get_info, [
320 ['Created ' . $conf_file]
321 ], 'The creation should be sent to info one more time';
322
323 is_deeply read_config $conf_file, {
324 'core.engine' => 'pg',
325 'core.pg.client' => '/to/psql',
326 'core.pg.db_name' => 'thingies',
327 'core.pg.username' => 'anna',
328 'core.pg.host' => 'banana',
329 'core.pg.port' => 93453,
330 }, 'The configuration should have been written with pg values';
46a2898 @theory Write engine specific vars with defaults as comments.
authored
331
332 file_contents_like $conf_file, qr/^\t# sqitch_schema = sqitch\n/m,
333 'sqitch_schema should be included in a comment';
334 file_contents_like $conf_file, qr/^\t# password = \n/m,
335 'password should be included in a comment';
beeb12c @theory Write configured settings to init config as comments.
authored
336
b2243bd @theory Write engine section header if no vars.
authored
337 # Try it with no config or options.
338 unlink $conf_file;
339 $sqitch = App::Sqitch->new(_engine => 'pg');
340 ok $init = $CLASS->new(sqitch => $sqitch),
341 'Create new init with sqitch with default engine attributes';
342 ok $init->write_config, 'Write the config with engine attrs';
343 is_deeply +MockCommand->get_info, [
344 ['Created ' . $conf_file]
345 ], 'The creation should be sent to info again again again';
346 is_deeply read_config $conf_file, {
347 'core.engine' => 'pg',
348 }, 'The configuration should have been written with only the engine var';
349
350 file_contents_like $conf_file, qr{^\Q# [core "pg"]
351 # db_name =
352 # client = psql
353 # sqitch_schema = sqitch
354 # password =
355 # port =
356 # host =
357 # username =
358 }m, 'Engine section should be present but commented-out';
359
beeb12c @theory Write configured settings to init config as comments.
authored
360 USERCONF: {
361 # Delete the file and write with a user config loaded.
362 unlink $conf_file;
363 local $ENV{SQITCH_USER_CONFIG} = file +File::Spec->updir, 'user.conf';
364 my $sqitch = App::Sqitch->new(
365 _engine => 'pg',
366 db_name => 'thingies',
367 );
368 ok my $init = $CLASS->new(sqitch => $sqitch),
369 'Make an init with pg and user config';
370 file_not_exists_ok $conf_file;
371 ok $init->write_config, 'Write the config with pg config';
372 is_deeply +MockCommand->get_info, [
373 ['Created ' . $conf_file]
374 ], 'The pg config creation should be sent to info';
375
376 is_deeply read_config $conf_file, {
377 'core.engine' => 'pg',
378 'core.pg.db_name' => 'thingies',
379 }, 'The configuration should have been written with pg options';
380
381 file_contents_like $conf_file, qr/^\t# sqitch_schema = meta\n/m,
382 'Configured sqitch_schema should be in a comment';
383 file_contents_like $conf_file, qr/^\t# password = \n/m,
384 'password should be included in a comment';
385 file_contents_like $conf_file, qr/^\t# username = postgres\n/m,
386 'Configured username should be in a comment';
387 file_contents_like $conf_file, qr/^\t# host = localhost\n/m,
388 'Configured host should be in a comment';
389 }
Something went wrong with that request. Please try again.