1use strict;
2use warnings;
3package GeneratePackage;
4# vim:ts=8:sw=2:et:sta:sts=2
5
6our @ISA = ('Exporter');
7our @EXPORT = qw(tmpdir generate_file);
8
9use Cwd;
10use File::Spec;
11use File::Path;
12use File::Temp;
13use IO::File;
14
15BEGIN {
16  my $cwd = File::Spec->rel2abs(Cwd::cwd);
17  sub _original_cwd { return $cwd }
18}
19
20my @tmpdirs;
21sub tmpdir {
22  my (@args) = @_;
23  my $tmpdir = File::Temp::tempdir(
24    'MMD-XXXXXXXX',
25    CLEANUP => 0,
26    DIR => ($ENV{PERL_CORE} ? _original_cwd : File::Spec->tmpdir),
27    @args,
28  );
29  Test::More::note "using temp dir $tmpdir";
30  push @tmpdirs, $tmpdir;
31  return $tmpdir;
32}
33
34my $tmp;
35
36sub generate_file {
37  my ($dir, $rel_filename, $content) = @_;
38
39  File::Path::mkpath($dir) or die "failed to create '$dir'";
40  my $abs_filename = File::Spec->catfile($dir, $rel_filename);
41
42  Test::More::note("working on $abs_filename");
43
44  my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
45  print $fh $content;
46  close $fh;
47
48  return $abs_filename;
49}
50
51END {
52  die "tests failed; leaving temp dir $tmp behind"
53    if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
54  chdir _original_cwd;
55  foreach my $tmp (@tmpdirs) {
56    Test::More::note "removing temp dir $tmp";
57    File::Path::rmtree($tmp);
58  }
59}
60
611;
62