Permalink
Browse files

refactor

  • Loading branch information...
1 parent f800b10 commit e5e063c28a5277b0c9aa65e73cc334ee90e4b9be @xaicron committed Jul 11, 2011
Showing with 62 additions and 75 deletions.
  1. +20 −1 bin/envfile
  2. +21 −31 lib/App/envfile.pm
  3. +13 −21 t/{02_load_envfile.t → 02_parse_envfile.t}
  4. +8 −22 t/{03_run.t → 03_run_with_env.t}
View
@@ -4,7 +4,26 @@ use strict;
use warnings;
use App::envfile;
-App::envfile->new->run(@ARGV);
+main(@ARGV);
+exit;
+
+sub main {
+ my ($envfile, @commands) = @_;
+ usage() unless -f $envfile;
+
+ my $envf = App::envfile->new;
+ my $env = $envf->parse_envfile($envfile);
+ $envf->run_with_env($env, \@commands);
+}
+
+sub usage {
+ my $self = shift;
+print << 'USAGE';
+Usage: evnfile file commands
+
+USAGE
+ exit 1;
+}
__END__
View
@@ -10,42 +10,37 @@ sub new {
bless {}, $class;
}
-sub run {
- my ($self, $envfile, @args) = @_;
- $self->usage unless defined $envfile;
- $self->load_envfile($envfile);
- exec(@args);
+sub run_with_env {
+ my ($self, $env, $commands) = @_;
+ local %ENV = %ENV;
+ for my $key (keys %$env) {
+ $ENV{$key} = $env->{$key};
+ }
+ exec(@$commands);
}
-sub load_envfile {
+sub parse_envfile {
my ($self, $file) = @_;
- $self->usage unless -f $file;
open my $fh, '<', $file or die "$file: $!\n";
+ my $env = {};
while (defined (my $line = readline $fh)) {
chomp $line;
next if index($line, '#') == 0;
next if $line =~ /^\s*$/;
- my ($key, $value) = $self->_split_line($line);
- $ENV{$key} = $value;
+ my ($key, $value) = $self->_parse_line($line);
+ $env->{$key} = $value;
}
close $fh;
+
+ return $env;
}
-sub _split_line {
+sub _parse_line {
my ($self, $line) = @_;
my ($key, $value) = map { my $str = $_; $str =~ s/^\s+|\s+$//g; $str } split '=', $line, 2;
return $key, $value;
}
-sub usage {
- my $self = shift;
-print << 'USAGE';
-Usage: evnfile file commands
-
-USAGE
- exit 1;
-}
-
1;
__END__
@@ -85,30 +80,25 @@ Create App::envfile instance.
my $envf = App::envfile->new();
-=item C<< run($envfile, @commands) >>
+=item C<< run_with_env(\%env, \@commands) >>
-Runs another program.
+Runs another program with environment modified according to C<< \%env >>.
- $envf->run($envfile, @commands);
+ $envf->run_with_env(\%env, \@commands);
-=item C<< load_envfile($envfile) >>
+=item C<< parse_envfile($envfile) >>
-Sets %ENV from file.
+Parse the C<< envfile >>. Returned value is HASHREF.
- $envf->load_envfile($envfile);
+ my $env = $envf->parse_envfile($envfile);
Supported file format are:
KEY=VALUE
+ # comment
KEY2=VALUE
...
-=item C<< usage() >>
-
-Show usage.
-
- $envf->usage();
-
=back
=head1 AUTHOR
@@ -5,19 +5,16 @@ use File::Temp qw(tempfile tempdir);
use App::envfile;
-sub test_load_envfile {
+sub test_parse_envfile {
my %specs = @_;
my ($input, $expects, $desc) = @specs{qw/input expects desc/};
local $Test::Builder::Level = $Test::Builder::Level + 1;
subtest $desc => sub {
- local %ENV;
my $tempfile = write_envfile($input);
my $envf = App::envfile->new;
- $envf->load_envfile($tempfile);
- for my $key (sort keys %$expects) {
- is $ENV{$key}, $expects->{$key}, "$key ok";
- }
+ my $got = $envf->parse_envfile($tempfile);
+ is_deeply $got, $expects, 'parse ok';
};
}
@@ -30,58 +27,58 @@ sub write_envfile {
return $filename;
}
-test_load_envfile(
+test_parse_envfile(
expects => { FOO => 'bar' },
desc => 'simple',
input => << 'ENV');
FOO=bar
ENV
-test_load_envfile(
+test_parse_envfile(
expects => { FOO => 'bar', HOGE => 'fuga' },
desc => 'multi',
input => << 'ENV');
FOO=bar
HOGE=fuga
ENV
-test_load_envfile(
+test_parse_envfile(
expects => { FOO => 'bar=baz' },
desc => 'contains split charctor',
input => << 'ENV');
FOO=bar=baz
ENV
-test_load_envfile(
+test_parse_envfile(
expects => { 'HOGE FUGA' => 'piyo' },
desc => 'key contains space',
input => << 'ENV');
HOGE FUGA=piyo
ENV
-test_load_envfile(
+test_parse_envfile(
expects => { 'FOO' => 'bar baz' },
desc => 'value contains space',
input => << 'ENV');
FOO=bar baz
ENV
-test_load_envfile(
+test_parse_envfile(
expects => { 'FOO' => 'bar baz' },
desc => 'spaces',
input => << 'ENV');
FOO = bar baz
ENV
-test_load_envfile(
+test_parse_envfile(
expects => { 'FOO' => 'bar' },
desc => 'skip comment',
input => << 'ENV');
# here is comment
FOO = bar
ENV
-test_load_envfile(
+test_parse_envfile(
expects => { 'FOO' => 'bar' },
desc => 'skip white line',
input => << 'ENV');
@@ -91,13 +88,8 @@ FOO = bar
ENV
subtest 'file not found' => sub {
- no warnings 'redefine';
- *App::envfile::usage = sub {
- ok "call this method", "call usage";
- die "oops";
- };
- eval { App::envfile->new->load_envfile('foo.bar') };
- like $@, qr/oops/;
+ eval { App::envfile->new->parse_envfile('foo.bar') };
+ like $@, qr/No such file or directory/;
};
done_testing;
@@ -23,40 +23,26 @@ BEGIN {
use App::envfile;
-sub test_run {
+sub test_run_with_env {
my %specs = @_;
my ($input, $expects, $desc) = @specs{qw/input expects desc/};
- my ($envfile, $envmap) = @$input;
-
- local %ENV = %ENV;
-
- {
- no warnings 'redefine';
- *App::envfile::load_envfile = sub {
- my ($self, $file) = @_;
- is $file, $envfile, 'load_envfile ok';
- for my $key (sort keys %$envmap) {
- $ENV{$key} = $envmap->{$key};
- }
- };
- }
-
- my $command = join ',', map { "\$ENV{$_}" } sort keys %$envmap;
+ my $command = join ',', map { "\$ENV{$_}" } sort keys %$input;
subtest $desc => sub {
my $envf = App::envfile->new;
- $envf->run($envfile, $^X, '-e', "print qq|$command|");
+ my $buf = $envf->run_with_env($input, [$^X, '-e', "print qq|$command|"]);
+ is $buf, $expects, 'child ok';
};
}
-test_run(
- input => [file => { FOO => 'bar' }],
+test_run_with_env(
+ input => { FOO => 'bar' },
expects => 'bar',
desc => 'with FOO',
);
-test_run(
- input => [file => { FOO => 'bar', BAR => 'baz' }],
+test_run_with_env(
+ input => { FOO => 'bar', BAR => 'baz' },
expects => 'baz,bar',
desc => 'with FOO, BAR',
);

0 comments on commit e5e063c

Please sign in to comment.