#!/usr/local/bin/perl -w

=head1 NAME

91vss2revml.t - testing of vss output

=cut

use strict ;

use Carp ;
use Test ;
use VCP::TestUtils ;

my $t = -d 't' ? 't/' : '' ;

my $project = "revml2vss";

my $vssroot_0 = "${t}vssroot_0";
my $vssroot_1 = "${t}vssroot_1";
my $vssroot_2 = "${t}vssroot_2";
my $vssroot_3 = "${t}vssroot_3";

my $vssspec_0 = "vss:Admin\@$vssroot_0:$project";
my $vssspec_1 = "vss:Admin\@$vssroot_1:$project";
my $vssspec_2 = "vss:Admin\@$vssroot_2:$project";
my $vssspec_3 = "vss:Admin\@$vssroot_3:$project";

my $deepdir = "one/two/three/four/five";

my @tests = (
##
## vss -> revml, bootstrap export
##
sub {
   my $infile  = $t . "test-vss-in-0.revml" ;
   ##
   ## Idempotency test
   ##
   ## These depend on the "test-foo-in-0.revml" files built in the makefile.
   ## See MakeMaker.PL for how those are generated.
   ##
   ## We are also testing to see if we can re-root the files under foo/...
   ##

   my $state = "${t}91vss2revml_state_A";
   rm_dir_tree $state;

   my $in  = slurp $infile ;
   my $out = get_vcp_output(
      "-d",
      "$vssspec_0/...",
      "--repo-id=vss:test_repository",
      {
         revml_out_spec => [
            "--db-dir=$state", "--repo-id=revml:test_repository"
         ]
      } 
   );

   s_content qw( rep_desc time vss_info ), \$in, \$out ;
   s_content qw( rev_root ),               \$in, $project ;

   $in =~ s{(id="|_id>)/+ignored}{$1/$project}g;
   $in =~ s{(id="|_id>)ignored}{$1/$project}g;

   $in =~ s{(user_id>).*</user_id>}{$1Admin</user_id>}g;
   rm_elts    qw( mod_time change_id source_change_id ), \$in, \$out ;

   ## Readd is created, deleted, and recovered, so there is no
   ## record of the deletion.  Remove the <deleted/> rev from the
   ## input revml.
   $in =~ s{^\s*<rev id=['"][^"']+readd#[0-9]+\.1">.*?</rev>\r?\n}{}sm;

   ## So, in VSS, we are left with two consecutive versions.  VCP
   ## properly gets a delta from one to the other.  Clean up $in
   ## and $out so that they don't differ here
   $in =~ s{(<previous_id>[^<]+readd#1)\.1}{$1};
   $in  =~ s{^\s*<content[^>]*>[^<]*readd#3[^<]*</content>\r?\n}{}sm;
   $out =~ s{^\s*<delta[^>]*>[^<]*readd#3[^<]*</delta>\r?\n}{}sm;

   ok_or_diff $out, $in;
},

sub { die },
## Test a single file extraction from a vss repo.  This file exists in
## change 1.

sub {
   my $state = "${t}91vss2revml_state_B";
   rm_dir_tree $state;
   ok(
      get_vcp_output(
         "$vssspec_0/main/add/f1",
         "--repo-id=vss:test_repository",
         {
            revml_out_spec => [
               "--db-dir=$state", "--repo-id=revml:test_repository"
            ]
         },
      ),
      qr{<rev_root>depot/foo/main/add</.+<name>f1<.+<rev_id>1<.+<rev_id>2<.+</revml>}s
   ) ;
},

## Test a single file extraction from a vss repo.  This file does not exist
## in change 1.
sub {
   my $state = "${t}91vss2revml_state_C";
   rm_dir_tree $state;
   ok(
      get_vcp_output(
         "$vssspec_0/main/add/f2",
         "--repo-id=vss:test_repository",
         {
            revml_out_spec => [
               "--db-dir=$state", "--repo-id=revml:test_repository"
            ],
         },
      ),
      qr{<rev_root>depot/foo/main/add</.+<name>f2<.+<change_id>2<.+<change_id>3<.+</revml>}s
   ) ;

},

##
## vss->revml, re-rooting a dir tree 
## copies //depot/foo/main/a/deeply/ as if it was a whole repo
## into a target dir as if it were a complete repository.             
##
sub {
   my $state = "${t}91vss2revml_state_D";
   rm_dir_tree $state;

   my $infile  = $t . "test-vss-in-0.revml" ;
   my $in  = slurp $infile ;
   my $out = get_vcp_output(
      "$vssspec_0/main/a/deeply/...",
      "--repo-id=vss:test_repository",
      { 
         revml_out_spec => [
            "--db-dir=$state", "--repo-id=revml:test_repository"
         ]
      } 
   );
   
   s_content qw( rep_desc time ), \$in, \$out ;
   s_content qw( rev_root ),                    \$in, "depot/foo/main/a/deeply" ;
   rm_elts   qw( mod_time change_id vss_info ),  \$in, \$out ;

   ## We know this changes, let's ASSume it's ok
   rm_elts   qw( branches ), \$in, \$out;

   ## Strip out all files from $in that shouldn't be there
   rm_elts    qw( rev ), qr{(?:(?!a/deeply).)*?}s, \$in ;
   
   ## Adjust the $in paths to look like the result paths.  $in is
   ## now the "expected" output.
   $in =~ s{<(name|source_name)>main/a/deeply/}{<$1>}g ;
   $in =~ s{(id="|_id>)/+ignored}{$1//depot/foo}g;
   $in =~ s{(id="|_id>)ignored}{$1depot/foo}g;

   ## We don't know what client name was used.
   $out =~ s{(user_id>.*)\@.*</user_id>}{$1</user_id>}g;
   
   ok_or_diff $out, $in;
},


##
## revml -> vss -> revml, incremental export
##

sub {
   my $infile  = $t . "test-vss-in-1.revml" ;

   my $state = "${t}91vss2revml_state_E";
   rm_dir_tree $state;
   copy_dir_tree "${t}91vss2revml_state_A" => $state;

   # see if got the right # of files, changes
   # vss2revml will do detailed checking (the following code)
   my $in  = slurp $infile ;
   my $out = get_vcp_output(
      "$vssspec_1/...",
      "--repo-id=vss:test_repository",
      "--continue",
      {
         revml_out_spec => [
            "--db-dir=$state", "--repo-id=revml:test_repository"
         ],
      }
   );

   $in =~ s{</rev_root>}{/foo</rev_root>} ;
   s_content  qw( rep_desc time vss_info ), \$in, \$out ;
   s_content  qw( rev_root ),              \$in, "depot/foo" ;

   $in =~ s{(id="|_id>)/+ignored}{$1//depot/foo}g;
   $in =~ s{(id="|_id>)ignored}{$1depot/foo}g;
   
   ## We don't know what client name was used.
   $out =~ s{(user_id>.*)\@.*</user_id>}{$1</user_id>}g;
   
   ok_or_diff $out, $in;
},


##
## vss -> revml, incremental export in bootstrap mode
##
sub {
   my $infile  = $t . "test-vss-in-1-bootstrap.revml" ;

   my $state = "${t}91vss2revml_state_F";
   rm_dir_tree $state;
   copy_dir_tree "${t}91vss2revml_state_A" => $state;

   my $in  = slurp $infile ;
   my $out = get_vcp_output(
      "$vssspec_1/...",
      "--repo-id=vss:test_repository",
      "--continue",
      "--bootstrap=...",
      {
         revml_out_spec => [
            "--db-dir=$state", "--repo-id=revml:test_repository"
         ]
      }
   );

   $in =~ s{</rev_root>}{/foo</rev_root>} ;
   s_content  qw( rep_desc time vss_info ), \$in, \$out ;
   s_content  qw( rev_root ),              \$in, "depot/foo" ;

   $in =~ s{(id="|_id>)/+ignored}{$1//depot/foo}g;
   $in =~ s{(id="|_id>)ignored}{$1depot/foo}g;

   ## We don't know what client name was used.
   $out =~ s{(user_id>.*)\@.*</user_id>}{$1</user_id>}g;
   
   ok_or_diff $out, $in;
},

## Check contents of t/vssroot_2
##   extract vss to revml
##   build expected from revml
##
## vss->revml, re-rooting a dir tree
##
sub {
   my $infile  = $t . "test-vss-in-0.revml" ;
   my $in = slurp $infile;
   my $out = get_vcp_output(
      "$vssspec_2/...",
      "--repo-id=vss:test_repository",
   );

   s_content  qw( rep_desc time ), \$in, \$out ;
   rm_elts    qw( vss_info       ), \$in, \$out ;

   ## Adjust the $in paths to look like the result paths.  $in is
   ## now the "expected" output.
   s_content  qw( rev_root ), \$in, "depot/foo" ;
   $in =~ s{(<(?:source_)?name>)}{${1}${deepdir}/}g;
   $in =~ s{(id="|id>)/ignored}{$1//depot/foo/${deepdir}}g;
   $in =~ s{(_id>)ignored}{$1//depot/foo/${deepdir}}g;

   ## We don't know what client name was used.
   $out =~ s{(user_id>.*)\@.*</user_id>}{$1</user_id>}g;
   
   ok_or_diff $out, $in;
},

## We don't check the contents of t/vssroot_3 because that's hard and
## not incredibly necessary.

) ;

plan tests => scalar @tests ;

my $why_skip = vss_borken ;

$why_skip ? skip $why_skip, 1 : $_->() for @tests ;
