From a68c272ca332a8770ee0cea6f7e274da398f7193 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Wed, 26 Jun 2019 14:39:02 -0700 Subject: Improve docknot dist test Use Capture::Tiny and File::Copy::Recursive to make the docknot dist test a bit simpler and easier to maintain. --- t/data/dist/docs/metadata/blurb | 1 + t/data/dist/docs/metadata/description | 1 + t/data/dist/docs/metadata/requirements | 1 + t/dist/basic.t | 82 ++++++++++++++++------------------ 4 files changed, 42 insertions(+), 43 deletions(-) create mode 100644 t/data/dist/docs/metadata/blurb create mode 100644 t/data/dist/docs/metadata/description create mode 100644 t/data/dist/docs/metadata/requirements (limited to 't') diff --git a/t/data/dist/docs/metadata/blurb b/t/data/dist/docs/metadata/blurb new file mode 100644 index 0000000..1d115d1 --- /dev/null +++ b/t/data/dist/docs/metadata/blurb @@ -0,0 +1 @@ +Test package. diff --git a/t/data/dist/docs/metadata/description b/t/data/dist/docs/metadata/description new file mode 100644 index 0000000..1d115d1 --- /dev/null +++ b/t/data/dist/docs/metadata/description @@ -0,0 +1 @@ +Test package. diff --git a/t/data/dist/docs/metadata/requirements b/t/data/dist/docs/metadata/requirements new file mode 100644 index 0000000..1d115d1 --- /dev/null +++ b/t/data/dist/docs/metadata/requirements @@ -0,0 +1 @@ +Test package. diff --git a/t/dist/basic.t b/t/dist/basic.t index 3bd119f..663680e 100755 --- a/t/dist/basic.t +++ b/t/dist/basic.t @@ -10,8 +10,13 @@ use 5.024; use autodie; use warnings; +use lib 't/lib'; + +use Test::RRA qw(use_prereq); + +use Capture::Tiny qw(capture_stdout); use Cwd qw(getcwd); -use File::Copy; +use File::Copy::Recursive qw(dircopy); use File::Spec; use File::Temp; use IPC::Run qw(run); @@ -23,60 +28,51 @@ use Test::More; my $cwd = getcwd() or die "$0: cannot get working directory: $!\n"; my $dataroot = File::Spec->catfile($cwd, 't', 'data', 'dist'); -# Set up a temporary directory, copy all files from the data directory, and -# commit them. We have to rename the test while we copy it to avoid having it -# picked up by the main package test suite. -my $dir = File::Temp->newdir(); -chdir($dir); -systemx(qw(git init source)); -chdir('source'); -for my $file (qw(Build.PL MANIFEST MANIFEST.SKIP)) { - copy(File::Spec->catfile($dataroot, $file), File::Spec->curdir()) - or die "$0: cannot copy $file: $!\n"; -} -mkdir('docs'); -mkdir(File::Spec->catfile('docs', 'metadata')); -my $metadata_path - = File::Spec->catfile($dataroot, 'docs', 'metadata', 'metadata.json'); -copy($metadata_path, File::Spec->catfile('docs', 'metadata')) - or die "$0: cannot copy docs/metadata/metadata.json: $!\n"; -for my $file (qw(blurb description requirements)) { - open(my $fh, '>', File::Spec->catfile('docs', 'metadata', $file)); - close($fh); +# Set up a temporary directory. +my $dir = File::Temp->newdir(); +my $sourcedir = File::Spec->catfile($dir, 'source'); +my $distdir = File::Spec->catfile($dir, 'dist'); + +# Check whether git is available and can be used to initialize a repository. +eval { systemx('git', 'init', '-q', File::Spec->catfile($dir, 'source')) }; +if ($@) { + plan skip_all => 'git init failed (possibly no git binary)'; } -mkdir('lib'); -copy(File::Spec->catfile($dataroot, 'lib', 'Empty.pm'), 'lib') - or die "$0: cannot copy lib/Empty.pm: $!\n"; -mkdir('t'); -my $testdir = File::Spec->catfile('t', 'api'); -mkdir($testdir); -my $test_path = File::Spec->catfile($dataroot, 't', 'api', 'empty.t.in'); -copy($test_path, File::Spec->catfile($testdir, 'empty.t')) - or die "$0: cannot copy t/api/empty.t: $!\n"; + +# Copy all files from the data directory, and commit them. We have to rename +# the test while we copy it to avoid having it picked up by the main package +# test suite. +dircopy($dataroot, $sourcedir) + or die "$0: cannot copy $dataroot to $sourcedir: $!\n"; +my $testpath = File::Spec->catfile($sourcedir, 't', 'api', 'empty.t'); +rename($testpath . '.in', $testpath); +chdir($sourcedir); systemx(qw(git add -A .)); -systemx(qw(git commit -m Initial)); +systemx(qw(git commit -q -m Initial)); -# Check whether we have all the necessary tools to set up the test. This test -# relies on the external git and tar utilities, which may not be available on -# all systems. -if (!run(['git', 'archive', 'HEAD'], q{|}, ['tar', 'tf', q{-}])) { +# Check whether we have all the necessary tools to run the test. +my $out; +if (!run(['git', 'archive', 'HEAD'], q{|}, ['tar', 'tf', q{-}], \$out)) { + chdir($cwd); plan skip_all => 'git and tar not available'; } else { - plan tests => 2; + plan tests => 3; } # Load the module. Change back to the starting directory for this so that # coverage analysis works. chdir($cwd); require_ok('App::DocKnot::Dist'); -chdir(File::Spec->catfile($dir, 'source')); -# Setup finished. Now we can create a distribution tarball. -my $distdir = File::Spec->catfile($dir, 'dist'); +# Setup finished. Now we can create a distribution tarball. Be careful to +# change working directories before letting $dir go out of scope so that +# cleanup works properly. mkdir($distdir); -my $dist = App::DocKnot::Dist->new({ distdir => $distdir }); -$dist->make_distribution(); +chdir($sourcedir); +eval { + my $dist = App::DocKnot::Dist->new({ distdir => $distdir }); + capture_stdout { $dist->make_distribution() }; +}; ok(-f File::Spec->catfile($distdir, 'Empty-1.00.tar.gz'), 'dist exists'); - -# Change directories so that the temporary directory can be cleaned up. chdir($cwd); +is($@, q{}, 'no errors'); -- cgit v1.2.3