App-AnteREPL

 view release on metacpan or  search on metacpan

t/features.t  view on Meta::CPAN

export FIRST=loaded
SPACES="hello world" # comment
SINGLE='a $HOME value'
LITERAL=$HOME`whoami`$(echo hello)
WINDOWS='C:\Users\brett'
HASH=abc#def # comment
EMPTY= # comment
ENVFILE
    capture { $env->dispatch(qq{ENV RELOAD "$dir/vars.env"}); };
    is $ENV{FIRST}, 'loaded', 'RELOAD reads changed file';
    is $ENV{SPACES}, 'hello world', 'double quotes and trailing comment';
    is $ENV{SINGLE}, 'a $HOME value', 'single quotes keep dollar sign literal';
    is $ENV{LITERAL}, '$HOME`whoami`$(echo hello)', 'file parsing never executes or interpolates';
    is $ENV{WINDOWS}, 'C:\Users\brett', 'single quotes keep Windows paths literal';
    is $ENV{HASH}, 'abc#def', 'inline hash survives unless preceded by whitespace';
    ok !exists $ENV{COMPLEX}, 'ENV LOAD/RELOAD replace entire snapshot';
    my %valid = %ENV;
    for my $bad ("GOOD=yes\ninvalid\n", "A=x\nA=y\n", 'A="unterminated', 'A="\x00"') {
        put("$dir/bad.env", $bad);
        ok length(fails { $env->dispatch(qq{ENV LOAD "$dir/bad.env"}) }), 'bad environment is rejected';
        is_deeply \%ENV, \%valid, 'bad environment leaves live environment intact';
    }
    capture { $env->dispatch('ENV RESET'); };
    is_deeply \%ENV, {FIRST=>'startup',EMPTY=>''}, 'ENV RESET restores original startup snapshot';
    $env->reset_workspace;
    is $ENV{FIRST}, 'startup', 'Perl RESET does not replace process environment';
}

my $editor = "$dir/env-editor.pl";
put($editor, <<'EDITOR');
use strict;
use warnings;
my ($mode, $path) = @ARGV;
die 'wrong temporary file' unless $path =~ /environment\.env$/;
open my $out, '>', $path or die $!;
print {$out} $mode eq 'invalid' ? "BROKEN\n" : "EDITED=from_editor\n";
close $out or die $!;
exit($mode eq 'failure' ? 17 : 0);
EDITOR
{
    local %ENV = %ENV;
    my $env = App::AnteREPL->new;
    for my $mode ('invalid','failure') {
        local $ENV{EDITOR} = qq{"$^X" "$editor" $mode};
        my %before = %ENV;
        ok length(fails { $env->dispatch('EDIT ENV') }), "environment editor $mode reported";
        is_deeply \%ENV, \%before, "environment editor $mode is atomic";
    }
    $ENV{EDITOR} = qq{"$^X" "$editor" change};
    capture { $env->dispatch('EDIT ENV'); };
    is_deeply \%ENV, {EDITED=>'from_editor'}, 'EDIT ENV imports editor file';
    $ENV{EDITOR} = qq{"$^X" "$editor" change};
    capture { $env->dispatch('ENV EDIT'); };
    is_deeply \%ENV, {EDITED=>'from_editor'}, 'ENV EDIT aliases EDIT ENV';
}

my $err = gensym;
my $pid = open3(my $in, my $out, $err, $^X, "-I$FindBin::Bin/../lib", "$FindBin::Bin/../script/anterepl", '--quiet');
print {$in} "AUTO\n\$FOO = {\nbar => 1,\n};\nAUTO OFF\nRUN\n? \$FOO->{bar}\nQUIT!\n";
close $in;
my ($stdout, $stderr);
{ local $/; $stdout = <$out>; $stderr = <$err>; }
waitpid $pid, 0;
is $? >> 8, 0, 'anterepl CLI accepts AUTO multiline input';
is $stderr, '', 'AUTO CLI has no errors';
like $stdout, qr/AUTO off.\n1\n/, 'quiet AUTO CLI stores then runs multiline code';
unlike $stdout, qr/\$FOO =|bar =>/, 'AUTO CLI does not echo stored source';

done_testing;



( run in 0.959 second using v1.01-cache-2.11-cpan-60884a8df76 )