Skip to content
Snippets Groups Projects
slubsipbuilderbagit.t 56.25 KiB
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;

use Test::More tests => 126;
use Test::Output;
use Test::Exception;
use Test::File;
use Path::Tiny;
use File::Copy::Recursive qw(dircopy);
use File::Path qw(rmtree);


# *** Read before running tests ***
# Usage: perl -I lib/ t/slubsipbuilderbagit.t 
# Warning: While running project "SLUB_SIP_Builder" two directories "export_dir_kitodo/bagit" and "tmp/bagit" will be created.
# Start scipt in the project directory to run all tests properly.
# Tested on Win10 and Debian 10

sub workaroundBagInfoTXT($$$){
	my $file_path = $_[0];
	my $key = $_[1];
	my $value = $_[2];
	my $file_name = SLUB::LZA::SIPBuilderBagIt::getFileName($file_path);
	
	# read a data file into an array
	open my $handle, '<', $file_path;
	chomp(my @lines = <$handle>);
	close $handle;
	# grep and change certain array element
	foreach my $zeile(@lines) {
		my @keyvalue = split(/: /, $zeile, 2);    # split on first :
		if($keyvalue[0] eq $key){
			$zeile = "$key: $value";
		}
	}
	# save array back to file
	open my $fh, '>', $file_path or die "Cannot open $file_path: $!";
	foreach (@lines){
		print $fh "$_\n"; # Print each entry in our array to the file
	}
	close $fh;
}

sub workaroundTagmanifestMD5($$$){
	my $file_path = $_[0];
	my $key = $_[1];
	my $value = $_[2];
	my $file_name = SLUB::LZA::SIPBuilderBagIt::getFileName($file_path);
	
	# read a data file into an array
	open my $handle, '<', $file_path;
	chomp(my @lines = <$handle>);
	close $handle;
	# grep and change certain array element
	foreach my $zeile(@lines) {
		my @keyvalue = split(/  /, $zeile, 2);    # split
		if($keyvalue[1] eq $value){
			$zeile = "$key  $value";
		}
	}
	# save array back to file
	open my $fh, '>', $file_path or die "Cannot open $file_path: $!";
	foreach (@lines){
		print $fh "$_\n"; # Print each entry in our array to the file
	}
	close $fh;
}