[Jifty-commit] r4218 - in jifty/trunk: lib/Jifty

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Oct 9 17:42:35 EDT 2007


Author: sartak
Date: Tue Oct  9 17:42:35 2007
New Revision: 4218

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Test.pm

Log:
 r43560 at onn:  sartak | 2007-10-09 17:42:13 -0400
 Add the ability for apps to have very fine-grained testing (such as overriding config for a subdir of t/, or even a particular test file).
 Still needs tests and I suppose some more doc :)


Modified: jifty/trunk/lib/Jifty/Test.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Test.pm	(original)
+++ jifty/trunk/lib/Jifty/Test.pm	Tue Oct  9 17:42:35 2007
@@ -12,6 +12,7 @@
 use File::Path;
 use File::Spec;
 use File::Temp;
+use Hash::Merge;
 
 =head1 NAME
 
@@ -247,14 +248,73 @@
     Jifty->new();
 }
 
+=head2 load_test_configs FILENAME
+
+This will load all the test config files that apply to FILENAME (default:
+C<$0>, the current test script file). Say you are running the test script
+C</home/bob/MyApp/t/user/12-delete.t>. The files that will be loaded are:
+
+=over 4
+
+=item C</home/bob/MyApp/t/user/12-delete.t-config.yaml>
+
+=item C</home/bob/MyApp/t/user/test_config.yaml>
+
+=item C</home/bob/MyApp/t/test_config.yaml>
+
+=back
+
+..followed by the usual Jifty configuration files (such as
+C<MyApp/etc/config.yml> and C<MyApp/etc/site_config.yml>). The options in a
+more specific test file override the options in a less specific test file.
+
+The options are returned in a single hashref.
+
+=cut
+
+sub load_test_configs {
+    my $class = shift;
+    my ($test_config_file) = @_;
+    my $test_options = {};
+
+    $test_config_file = $0 if !defined($test_config_file);
+    $test_config_file .= "-config.yaml";
+
+    Hash::Merge::set_behavior('RIGHT_PRECEDENT');
+
+    my $depth = $ENV{JIFTY_TEST_DEPTH} || 30;
+
+    for (1 .. $depth)
+    {
+        my $file_options = Jifty::Config->load_file($test_config_file);
+
+        # merge the new options into what we have so far
+        $test_options = Hash::Merge::merge($file_options, $test_options);
+
+        # are we at the app root? if so, then we can stop moving up
+        my $parent = File::Spec->parent($test_config_file);
+        return $test_options
+            if Jifty::Util->is_app_root($parent);
+
+        # set up the next iteration (this would be at the top, but we special
+        # case the first iteration)
+        $test_config_file = File::Spec->catfile($parent, "test_config.yaml");
+    }
+
+    Jifty->log->fatal("Stopping looking for test config files after recursing upwards $depth times. Either you have a nonstandard layout or an incredibly deep test hierarchy. If you really do have an incredibly deep test hierarchy, you can set the environment variable JIFTY_TEST_DEPTH to a larger value.");
+
+    return $test_options;
+}
+
 =head2 test_config
 
 Returns a hash which overrides parts of the application's
 configuration for testing.  By default, this changes the database name
 by appending a 'test', as well as setting the port to a random port
-between 10000 and 15000.
+between 10000 and 15000. Individual test configurations may override these
+defaults (see C<load_test_configs>).
 
-It is passed the current configuration.
+It is passed the current configuration before any test config is loaded.
 
 You can override this to provide application-specific test
 configuration, e.g:
@@ -268,13 +328,16 @@
         return $hash;
     }
 
+Note that this is deprecated in favor of having real config files in your
+test directory.
+
 =cut
 
 sub test_config {
     my $class = shift;
     my ($config) = @_;
 
-    return {
+    my $defaults = {
         framework => {
             Database => {
                 Database => $config->framework('Database')->{Database} . $class->_testfile_to_dbname(),
@@ -288,6 +351,9 @@
             LogLevel => 'WARN'
         }
     };
+
+    Hash::Merge::set_behavior('RIGHT_PRECEDENT');
+    return Hash::Merge::merge($defaults, $class->load_test_configs);
 }
 
 


More information about the Jifty-commit mailing list