Simplify and centralize temporary file creation
Change-Id: I6674783e7707d66efea05d52381114779f3a64ee
diff --git a/lib/Test/KorAP/XML/TEI.pm b/lib/Test/KorAP/XML/TEI.pm
new file mode 100644
index 0000000..e94c8bb
--- /dev/null
+++ b/lib/Test/KorAP/XML/TEI.pm
@@ -0,0 +1,25 @@
+package Test::KorAP::XML::TEI;
+use strict;
+use warnings;
+use File::Temp qw/tempfile/;
+use Exporter 'import';
+
+our @EXPORT_OK = qw(korap_tempfile);
+
+# Create a temporary file and file handle
+# That will stay intact, if KORAPXMLTEI_DONTUNLINK is set to true.
+sub korap_tempfile {
+ my $pattern = shift;
+ $pattern .= '_' if $pattern;
+
+ # default: remove temp. file created by func. tempfile
+ # to keep temp. files use e.g. 'KORAPXMLTEI_DONTUNLINK=1 prove -lr t/script.t'
+ return tempfile(
+ 'KorAP-XML-TEI_' . ($pattern // '') . 'XXXXXXXXXX',
+ SUFFIX => '.tmp',
+ TMPDIR => 1,
+ UNLINK => $ENV{KORAPXMLTEI_DONTUNLINK} ? 0 : 1
+ )
+};
+
+1;
diff --git a/t/script.t b/t/script.t
index 401d76b..8d17be6 100644
--- a/t/script.t
+++ b/t/script.t
@@ -2,18 +2,12 @@
use warnings;
use File::Basename 'dirname';
use File::Spec::Functions qw/catfile/;
-use File::Temp qw/tempfile/;
use IO::Uncompress::Unzip qw(unzip $UnzipError);
use Test::More;
use Test::Output;
-
use Test::XML::Loy;
-
-our %ENV;
-# default: remove temp. file created by func. tempfile
-# to keep temp. files use e.g. 'KORAPXMLTEI_DONTUNLINK=1 prove -lr t/script.t'
-my $_UNLINK = $ENV{KORAPXMLTEI_DONTUNLINK}?0:1;
+use Test::KorAP::XML::TEI qw!korap_tempfile!;
my $f = dirname(__FILE__);
my $script = catfile($f, '..', 'script', 'tei2korapxml');
@@ -35,7 +29,7 @@
# Load example file
my $file = catfile($f, 'data', 'goe_sample.i5.xml');
-my ($fh, $outzip) = tempfile("KorAP-XML-TEI_script_XXXXXXXXXX", SUFFIX => ".tmp", TMPDIR => 1, UNLINK => $_UNLINK);
+my ($fh, $outzip) = korap_tempfile('script_out');
# Generate zip file (unportable!)
stderr_like(
@@ -183,7 +177,7 @@
# Tokenize with external tokenizer
my $cmd = catfile($f, 'cmd', 'tokenizer.pl');
-my ($fh2, $outzip2) = tempfile("KorAP-XML-TEI_script_XXXXXXXXXX", SUFFIX => ".tmp", TMPDIR => 1, UNLINK => $_UNLINK);
+my ($fh2, $outzip2) = korap_tempfile('script_out2');
stderr_like(
sub { `cat '$file' | perl '$script' --tc='perl $cmd' > '$outzip2'` },
@@ -218,7 +212,7 @@
# TODO: call $script with approp. parameter for internal tokenization (actual: '$_GEN_TOK_INT = 1' hardcoded)
-my ($fh3, $outzip3) = tempfile("KorAP-XML-TEI_script_XXXXXXXXXX", SUFFIX => ".tmp", TMPDIR => 1, UNLINK => $_UNLINK);
+my ($fh3, $outzip3) = korap_tempfile('script_out3');
# ~ test conservative tokenization ~
diff --git a/t/tei.t b/t/tei.t
index b602df6..53f372e 100644
--- a/t/tei.t
+++ b/t/tei.t
@@ -1,21 +1,16 @@
use strict;
use warnings;
use Test::More;
-use File::Temp 'tempfile';
+use Test::KorAP::XML::TEI qw!korap_tempfile!;
use FindBin;
BEGIN {
unshift @INC, "$FindBin::Bin/../lib";
};
-our %ENV;
-# default: remove temp. file created by func. tempfile
-# to keep temp. files use e.g. 'KORAPXMLTEI_DONTUNLINK=1 prove -lr t/script.t'
-my $_UNLINK = $ENV{KORAPXMLTEI_DONTUNLINK}?0:1;
-
use_ok('KorAP::XML::TEI', 'remove_xml_comments');
-my ($fh, $filename) = tempfile("KorAP-XML-TEI_tei_XXXXXXXXXX", SUFFIX => ".tmp", TMPDIR => 1, UNLINK => $_UNLINK);
+my ($fh, $filename) = korap_tempfile('tei');
print $fh <<'HTML';
mehrzeiliger
diff --git a/t/test.t b/t/test.t
new file mode 100644
index 0000000..5b6fbe1
--- /dev/null
+++ b/t/test.t
@@ -0,0 +1,26 @@
+use strict;
+use warnings;
+use Test::More;
+
+use FindBin;
+BEGIN {
+ unshift @INC, "$FindBin::Bin/../lib";
+};
+
+use_ok('Test::KorAP::XML::TEI','korap_tempfile');
+
+my ($fh, $filename) = korap_tempfile('test');
+ok($fh, 'Filehandle created');
+ok($filename, 'Filename returned');
+close($fh);
+
+like($filename, qr!KorAP-XML-TEI_test_.+?\.tmp$!, 'Filename pattern');
+
+($fh, $filename) = korap_tempfile();
+ok($fh, 'Filehandle created');
+ok($filename, 'Filename returned');
+close($fh);
+
+like($filename, qr!KorAP-XML-TEI_.+?\.tmp$!, 'Filename pattern');
+
+done_testing;
diff --git a/t/zipper.t b/t/zipper.t
index 6147f62..2ee7fab 100644
--- a/t/zipper.t
+++ b/t/zipper.t
@@ -2,7 +2,7 @@
use warnings;
use Test::More;
use File::Spec::Functions qw/catfile/;
-use File::Temp qw/tempfile/;
+use Test::KorAP::XML::TEI qw!korap_tempfile!;
use IO::Uncompress::Unzip;
use FindBin;
@@ -10,15 +10,10 @@
unshift @INC, "$FindBin::Bin/../lib";
};
-our %ENV;
-# default: remove temp. file created by func. tempfile
-# to keep temp. files use e.g. 'KORAPXMLTEI_DONTUNLINK=1 prove -lr t/script.t'
-my $_UNLINK = $ENV{KORAPXMLTEI_DONTUNLINK}?0:1;
-
require_ok('KorAP::XML::TEI::Zipper');
my $data;
-my ($fh, $outzip) = tempfile("KorAP-XML-TEI_zipper_XXXXXXXXXX", SUFFIX => ".tmp", TMPDIR => 1, UNLINK => $_UNLINK);
+my ($fh, $outzip) = korap_tempfile('zipper');
my $zip = KorAP::XML::TEI::Zipper->new($outzip);
$fh->close;