App-spaceless

 view release on metacpan or  search on metacpan

t/app_spaceless.t  view on Meta::CPAN


  subtest 'script3' => sub {
    my($out, $err, $exit) = capture { $run_cmd->($tmp->file('caller3.cmd')) };
    is $exit, 0, 'exit okay';
    is $err, '', 'error is empty';
    like $out, qr{THREE}, "out matches";
  };
};

subtest 'bourne shell' => sub {
  skip_all 'test does not work on MSWin32' if $^O eq 'MSWin32';
  my($sh_exe) = grep { -e $_ } grep !/\s/, map { "$_/sh$Config{exe_ext}" } @PATH;
  skip_all 'unable to find sh' unless defined $sh_exe;
  note 'full path:', $sh_exe;

  my $tmp = dir( tempdir( CLEANUP => 1 ) );
  
  my $run_sh = sub {
    my($path) = @_;
    my @cmd = ($sh_exe, $path);
    note "execute: @cmd";
    system @cmd;
    $?;
  };
  
  my $script1 = file( $tmp, 'test1.sh' );
  do {
    $script1->spew("#!/bin/sh\necho hi there\n");
    my($out, $err, $ret) = capture { $run_sh->($script1) };
    skip_all "really really simple sh script didn't exit 0" unless $ret == 0;
    skip_all "really really simple sh script had error output" unless $err eq '';
    skip_all "really really simple sh script didn't have the expected output" unless $out =~ /hi there/;
  };
  
  my $dir1 = dir($tmp, 'Program Files', 'Foo', 'bin');
  my $dir2 = dir($tmp, 'Program Files (x86)', 'Foo', 'bin');
  note capture_stdout { map { $_->mkpath(1,0700) } $dir1, $dir2 };
  ok -d $dir1, "dir $dir1";
  ok -d $dir2, "dir $dir2";

  unshift @PATH, $dir1, $dir2;

  my $set_path;

  subtest 'spaceless --sh' => sub {
    my($out, $err, $exit) = capture { App::spaceless->main('--sh') };
    is $exit, 0, 'exit is 0';
    is $err, '', 'error is empty';
    isnt $out, '', 'output is not empty';
    $set_path = $out;
  };
  
  note "[set_path] begin";
  note $set_path;
  note "[set_path] end";

  splice @PATH, 0, 2; 

  $tmp->file('caller2.sh')->openw->print($set_path, "\n\nscript2.sh\n");
  $dir1->file('script2.sh')->openw->print("#!$sh_exe\necho this is script TWO\n");
  chmod(0700, $dir1->file('script2.sh'));
  
  subtest 'script2' => sub {
    my($out, $err, $exit) = capture { $run_sh->($tmp->file('caller2.sh')) };
    is $exit, 0, 'exit okay';
    is $err, '', 'error is empty';
    like $out, qr{TWO}, "out matches";
  };

  $tmp->file('caller3.sh')->openw->print($set_path, "\n\nscript3.sh\n");
  $dir2->file('script3.sh')->openw->print("#!$sh_exe\necho this is script THREE\n");
  chmod(0700, $dir2->file('script3.sh'));

  subtest 'script3' => sub {
    my($out, $err, $exit) = capture { $run_sh->($tmp->file('caller3.sh')) };
    is $exit, 0, 'exit okay';
    is $err, '', 'error is empty';
    like $out, qr{THREE}, "out matches";
  };
};

subtest 'actual spacelessness' => sub {
  skip_all 'only for MSWin32 and cygwin' unless $^O =~ /^(MSWin32|cygwin)$/;

  my $tmp = dir( tempdir( CLEANUP => 1 ));
  
  my @foo = map { $tmp->subdir($_) } 'nospace1', 'one space', 'no_space2', 'reallyreallyreallyreallyreallylongpath';
  note capture_stdout { $_->mkpath(1, 0700) for @foo };
  
  $ENV{FOO} = join $Config{path_sep}, @foo;
  note "FOO=$ENV{FOO}";
  
  my $path_set;
  
  subtest 'spaceless --sh FOO' => sub {
    my($out, $err, $exit) = capture { App::spaceless->main('--sh', 'FOO') };
    is $exit, 0, 'exit okay';
    is $err, '', 'no error';
    isnt $out, '', 'some output';
    $path_set = $out;
  };
  
  my $path;
  
  if($path_set =~ /FOO='(.*?)'/)
  {
    $path = $1;
    pass "found path $path";
  }
  else
  {
    fail 'path not found';
  }
  
  unlike $path, qr{\s}, "no white space";
  like $path, qr{nospace1}, "contains nospace1";
  like $path, qr{no_space2}, "contains no_space2";
  like $path, qr{reallyreallyreallyreallyreallylongpath}, 'contains reallyreallyreallyreallyreallylongpath';
  
};

subtest 'trim' => sub {
  my $tmp = dir( tempdir ( CLEANUP => 1 ));
  skip_all "$tmp matches dir1 or dir2" if $tmp =~ /dir[12]/;
  
  $ENV{FOO} = join $Config{path_sep}, $tmp->subdir('dir1'), $tmp->subdir('dir2');
  $tmp->subdir('dir1')->mkpath(0,0700);
  
  my $path_set;
  
  subtest 'spaceless --trim --sh FOO' => sub {
    my($out, $err, $exit) = capture { App::spaceless->main('--trim', '--sh', 'FOO') };



( run in 1.015 second using v1.01-cache-2.11-cpan-63c85eba8c4 )