[PATCH] add DANCER_CONFFILE environment variable support with test

Jens Rehsack sno at netbsd.org
Thu Dec 27 17:36:06 GMT 2012


---
 lib/Dancer/Config.pm |   22 +++++++++++++++++++--
 t/01_config/09_env.t |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 2 deletions(-)
 create mode 100644 t/01_config/09_env.t

diff --git a/lib/Dancer/Config.pm b/lib/Dancer/Config.pm
index 6ca2041..3212891 100644
--- a/lib/Dancer/Config.pm
+++ b/lib/Dancer/Config.pm
@@ -144,7 +144,11 @@ sub _get_setting {
     return $SETTINGS->{$setting};
 }
 
-sub conffile { path(setting('confdir') || setting('appdir'), 'config.yml') }
+sub conffile {
+    my $conf_dir = setting('confdir') || setting('appdir');
+    my $conf_file = setting('conffile') || 'config.yml';
+    path($conf_dir, $conf_file);
+}
 
 sub environment_file {
     my $env = setting('environment');
@@ -160,6 +164,11 @@ sub init_confdir {
     setting confdir => $ENV{DANCER_CONFDIR} || setting('appdir');
 }
 
+sub init_conffile {
+    return setting('conffile') if setting('conffile');
+    setting conffile => $ENV{DANCER_CONFFILE} || 'config.yml';
+}
+
 sub init_envdir {
     return setting('envdir') if setting('envdir');
     my $appdirpath = defined setting('appdir')                 ?
@@ -170,6 +179,7 @@ sub init_envdir {
 }
 
 sub load {
+    init_conffile();
     init_confdir();
     init_envdir();
 
@@ -645,7 +655,7 @@ Maximum size of route cache (e.g. 1024, 2M) - see L<Dancer::Route::Cache>
 Maximum number of routes to cache - see L<Dancer::Route::Cache>
 
 
-=head2 DANCER_CONFDIR and DANCER_ENVDIR
+=head2 DANCER_CONFFILE, DANCER_CONFDIR and DANCER_ENVDIR
 
 It's possible to set the configuration directory and environment directory using this two
 environment variables. Setting `DANCER_CONFDIR` will have the same effect as doing
@@ -656,6 +666,14 @@ and setting `DANCER_ENVDIR` will be similar to:
 
     set envdir => '/path/to/environments'
 
+While setting `DANCER_CONFFILE` is analogous to
+
+    set conffile => 'cfgname.yml'
+
+results no setting in
+
+    set conffile => 'config.yml'
+
 =head1 AUTHOR
 
 This module has been written by Alexis Sukrieh <sukria at cpan.org> and others,
diff --git a/t/01_config/09_env.t b/t/01_config/09_env.t
new file mode 100644
index 0000000..875f326
--- /dev/null
+++ b/t/01_config/09_env.t
@@ -0,0 +1,53 @@
+use strict;
+use warnings;
+use Test::More import => ['!pass'];
+
+plan skip_all => "YAML needed to run this tests"
+    unless Dancer::ModuleLoader->load('YAML');
+plan skip_all => "File::Temp 0.22 required"
+    unless Dancer::ModuleLoader->load( 'File::Temp', '0.22' );
+plan skip_all => "File::Basename required"
+    unless Dancer::ModuleLoader->load('File::Basename');
+plan tests => 7;
+
+use Dancer ':syntax';
+use File::Spec;
+use lib File::Spec->catdir( 't', 'lib' );
+use TestUtils;
+
+my $tmpdir = File::Temp->newdir( CLEANUP => 1 );
+$ENV{DANCER_CONFDIR} = $tmpdir->dirname;
+my $tmpfile = File::Temp->new( TEMPLATE => "dancXXXX",
+                               SUFFIX => ".yml",
+                               DIR => $tmpdir->dirname,
+                               CLEANUP => 1 );
+$ENV{DANCER_CONFFILE} = File::Basename::basename($tmpfile->filename);
+
+# create the conffile
+my $conf = '
+port: 4500
+startup_info: 0
+charset: "UTF8"
+logger: file
+';
+write_file($tmpfile => $conf);
+
+ok(Dancer::Config->load, 'Config load works with a conffile');
+
+my $conffile = Dancer::Config->conffile;
+is $conffile => $tmpfile, 'conffile/confdir from $ENV used';
+
+is(setting('environment'), 'development', 
+    'setting environment looks good');
+is(setting('port'), '4500',
+    'setting port looks good');
+is(setting('startup_info'), 0,
+    'setting startup_info looks good');
+is(setting('logger'), 'file',
+    'setting logger looks good');
+
+# issue GH#153
+is(setting('charset'), 'utf8',
+    "charset setting is normalized");
+
+File::Temp::cleanup();
-- 
1.7.10.2 (Apple Git-33)


--------------020702070603050301010205--


More information about the dancer-users mailing list