Fix tests when the path to perl has spaces in it
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 27filter_generated.t
CommitLineData
dde95f2c 1use strict;
f8c2ca5e 2use warnings;
7dc01f79 3use DBIx::Class::Schema::Loader;
4use DBIx::Class::Schema::Loader::Utils 'slurp_file';
dde95f2c 5use File::Path;
7dc01f79 6use Test::More tests => 19;
dde95f2c 7use Test::Exception;
8use lib qw(t/lib);
9use make_dbictest_db;
10use dbixcsl_test_dir qw/$tdir/;
11
dde95f2c 12my $dump_path = "$tdir/dump";
13
14my %original_class_data;
15
7dc01f79 16my ($schema_file_count, $result_file_count);
17
dde95f2c 18{
19 package DBICTest::Schema::1;
7dc01f79 20 use Test::More;
21 use base 'DBIx::Class::Schema::Loader';
dde95f2c 22 __PACKAGE__->loader_options(
23 dump_directory => $dump_path,
7dc01f79 24 quiet => 1,
25 filter_generated_code => sub {
26 my ($type, $class, $text) = @_;
27
28 like $type, qr/^(?:schema|result)\z/,
29 'got correct file type';
30
31 if ($type eq 'schema') {
32 $schema_file_count++;
33 is $class, 'DBICTest::Schema::1',
34 'correct class for schema type file passed to filter';
35 }
36 elsif ($type eq 'result') {
37 $result_file_count++;
38 like $class, qr/^DBICTest::Schema::1::Result::(?:Foo|Bar)\z/,
39 'correct class for result type file passed to filter';
40 }
41 else {
42 die 'invalid file type passed to filter';
43 }
44
dde95f2c 45 $original_class_data{$class} = $text;
494e0205 46 if ($class =~ /::1$/) {
dde95f2c 47 $text = "No Gotcha!";
494e0205 48 }
dde95f2c 49 else {
494e0205 50 $text .= q{my $foo = "Kilroy was here";};
51 }
dde95f2c 52 return $text;
53 },
54 );
55}
56
7dc01f79 57{
58 package DBICTest::Schema::2;
59 use base 'DBIx::Class::Schema::Loader';
60 __PACKAGE__->loader_options(
61 dump_directory => $dump_path,
62 quiet => 1,
a32f56bb 63 filter_generated_code => qq{"$^X" t/bin/simple_filter},
7dc01f79 64 );
65}
66
dde95f2c 67DBICTest::Schema::1->connect($make_dbictest_db::dsn);
68
7dc01f79 69# schema is generated in 2 passes
70
71is $schema_file_count, 2,
72 'correct number of schema files passed to filter';
73
74is $result_file_count, 4,
75 'correct number of result files passed to filter';
76
77my $foo = slurp_file "$dump_path/DBICTest/Schema/1/Result/Foo.pm";
494e0205 78ok ! -e "$dump_path/DBICTest/Schema/1.pm",
79 "No package means no file written";
80ok $original_class_data{"DBICTest::Schema::1"},
81 "Even though we processed the missing class";
82like $foo, qr/# Created by .* THE FIRST PART/s,
83 "We get the whole autogenerated text";
84like $foo, qr/Kilroy was here/, "Can insert text";
dde95f2c 85
7dc01f79 86DBICTest::Schema::2->connect($make_dbictest_db::dsn);
87
88$foo = slurp_file "$dump_path/DBICTest/Schema/2/Result/Foo.pm";
89
90like $foo, qr/Kilroy was here/,
91 "Can insert text via command filter";
92
dde95f2c 93END { rmtree($dump_path, 1, 1); }