#!/usr/bin/env perl

# Some tests for 'darcs rec --edit-long-comment'

use lib 'lib/perl';
use Test::Darcs;
use Test::More qw/no_plan/;
use strict;
use vars qw/$DARCS/;
use File::Path;

my $GREP  = `which grep`;
chomp $GREP;

sub init_dir {
  my $d = shift;
  my $dq = shift;
  cleanup $d;
  mkdir $d;
  chdir $d;
  darcs 'init';
  `echo "FIRST LINE" > file.t`;
  darcs 'add file.t';
}

sub clean_dir {
  my $d = shift;
   chdir '../';
   rmtree($d);
   ok((!-d $d), 'temp1 directory was deleted');
}

$ENV{DARCS_EDITOR}="/bin/cat -n";

my $test_name = 'editor: space in command';

init_dir('temp1');
like(darcs('record --edit-long-comment -A a -a -m foo file.t'), qr/2.*END OF DESCRIPTION/i, $test_name);
clean_dir('temp1');

$test_name = 'editor: space in path';

my $d = 'temp2 dir';
init_dir($d);
like(darcs('record --edit-long-comment -A a -a -m foo file.t'), qr/2.*END OF DESCRIPTION/i, $test_name);
exit(1);
clean_dir($d);


$ENV{DARCS_EDITOR}=${GREP}.' "END OF"';

$test_name = 'editor: quoting in command';

init_dir('temp1','temp1');
like(darcs('record --edit-long-comment -A a -a -m foo file.t'), qr/END OF/i, $test_name);
clean_dir('temp1','temp1');

$ENV{DARCS_EDITOR}='/bin/echo';

$test_name = 'editor: evil filename';

init_dir('temp1','temp1');
`touch '; test-command'`;
`printf "#!/bin/sh\necho EVIL" > test-command`;
`chmod u+x test-command`;
my $op = darcs(qw(record --logfile='; test-command' --edit-long-comment -A a -a -m foo file.t));
unlike($op, qr/EVIL/i, $test_name);
clean_dir('temp1','temp1');
