App-MtAws

 view release on metacpan or  search on metacpan

t/lib/JournalTest.pm  view on Meta::CPAN

	return $self;
}

sub test_all
{
	my ($self) = @_;

	$self->test(q{test_journal});
	$self->test(q{test_real_files});

	$self->test(q{test_all_files});
	$self->test(q{test_new_files});
	$self->test(q{test_existing_files});
}

sub binarypath
{
	my ($self, $path) = @_;
	encode($self->{filenames_encoding}, $path, Encode::DIE_ON_ERR|Encode::LEAVE_SRC)
}

sub characterpath
{
	my ($self, $path) = @_;
	decode($self->{filenames_encoding}, $path, Encode::DIE_ON_ERR|Encode::LEAVE_SRC)
}

sub test
{
	my ($self, $testname) = @_;

	my $pwd  = Cwd::getcwd();

	mkpath($self->binarypath($self->{absdataroot}));;
	die unless -d $self->binarypath($self->{absdataroot});

	if (defined ($self->{curdir})) {
		mkpath $self->binarypath($self->{curdir});
		$self->{dataroot} = File::Spec->abs2rel($self->{absdataroot}, $self->{curdir});
		chdir $self->binarypath($self->{curdir}) or die "[$self->{dataroot}\t$self->{curdir}]";
	} else {
		chdir $self->binarypath($self->{mtroot}) or die;
	}

	$self->$testname();


	chdir $self->binarypath($pwd) or die;
	-d && rmtree($_) for ($self->binarypath($self->{tmproot}));
}

sub check_absfilename
{
	my ($self, $should_exist, $relfilename, $absfilename) = @_;

	my $absfilename_old = $self->characterpath(File::Spec->rel2abs($self->binarypath($relfilename), $self->binarypath($self->{dataroot})));
	my $absfilename_correct = File::Spec->catfile($self->{dataroot}, $relfilename);
	my $absfilename_wrong = $self->characterpath(File::Spec->abs2rel(File::Spec->rel2abs($self->binarypath($relfilename), $self->binarypath($self->{dataroot}))));

	if ($should_exist) {
		my $ino_old = stat($self->binarypath($absfilename_old))->ino;
		ok $ino_old;
		is stat($self->binarypath($absfilename_correct))->ino, $ino_old;
		is stat($self->binarypath($absfilename_wrong))->ino, $ino_old;
	}

	#TODO: add File::Spec->canonpath() to _correct and fix absfilename_correct=./dirA/file3
	ok $absfilename_old =~ m{^/};

	ok $absfilename !~ m{//};

	ok $absfilename =~ m{^\Q$self->{dataroot}/\E} unless $self->{dataroot} =~ m{^\.(/|$)};
	is ($absfilename, $absfilename_correct, "absfilename match");
}

sub test_journal
{
	my ($self) = @_;
	$self->create_journal();

	my $j = App::MtAws::Journal->new(journal_encoding => $self->{journal_encoding},
		journal_file => $self->{journal_file}, root_dir => $self->{dataroot}, follow => $self->{follow});
	$j->read_journal(should_exist => 1);

	my @checkfiles = grep { $_->{type} eq 'normalfile' && $_->{journal} && $_->{journal} eq 'created' } @{$self->{testfiles}};
	ok(( scalar @checkfiles == scalar keys %{ $j->{journal_h} } ), "journal - number of planed and real files match");

	for my $cf (@checkfiles) {
		ok (my $jf = $j->{journal_h}->{$cf->{filename}}, "file $cf->{filename} exists in Journal");
		ok ($jf->{size} == $cf->{filesize}, "file size match $jf->{size} == $cf->{filesize}");
		ok ($jf->{treehash} eq $cf->{final_hash}, "treehash matches");
		ok ($jf->{archive_id} eq $cf->{archive_id}, "archive id matches");
		$self->check_absfilename(0, $cf->{filename}, $j->absfilename($cf->{filename}));
	}
}

sub test_real_files
{
	my ($self) = @_;
	$self->create_files();

	my $j = App::MtAws::Journal->new(journal_encoding => $self->{journal_encoding},
		journal_file => $self->{journal_file}, root_dir => $self->{dataroot}, filter => $self->{filter}, follow => $self->{follow});
	$j->read_files({new=>1,existing=>1});

	my @checkfiles = grep { $_->{type} ne 'dir' && !$_->{exclude} } @{$self->{testfiles}};
	ok((scalar @checkfiles) == scalar @{$j->{listing}{new}}+scalar @{$j->{listing}{existing}}, "number of planed and real files match");

	my %testfile_h = map { $_->{filename } => $_} @checkfiles;
	for my $realfile (@{$j->{listing}{new}}, @{$j->{listing}{existing}}) {
		ok ( $testfile_h{ $realfile->{relfilename} }, "found file $realfile->{relfilename} exists in planned test file list" );
		$self->check_absfilename(1, $realfile->{relfilename}, $j->absfilename($realfile->{relfilename}));
	}
}

sub test_all_files
{
	my ($self) = @_;
	$self->create_journal();
	$self->create_files('skip');
	my $j = App::MtAws::Journal->new(journal_encoding => $self->{journal_encoding},
		journal_file => $self->{journal_file}, root_dir => $self->{dataroot}, filter => $self->{filter}, follow => $self->{follow});
	$j->read_files({new=>1,existing=>1});



( run in 1.856 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )