App-Access2CSV

 view release on metacpan or  search on metacpan

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

package FakeMDB;

# Test helper: installs stand-in mdb-tables, mdb-export and mdb-count
# scripts in a temporary directory, so that the exporter can be tested
# without mdbtools or a real Access database.
#
# The "database" is a text file listing one table name per line; the
# fake mdb-tables prints it back.  A first line of "FAIL" makes
# mdb-tables fail.  mdb-export's behaviour depends on the table name:
#   Broken   - exits 1 with "corrupt table" on stderr
#   Killed   - kills itself with SIGTERM (Unix only: Windows has no signals)
#   Unicode  - UTF-8 text that cp1252 can represent ("Cafe" with e-acute, Euro)
#   Japanese - UTF-8 text that cp1252 cannot represent
#   Latin1   - bytes that are not valid UTF-8
#   Truncated - output that stops in the middle of a UTF-8 character
#   Slow     - writes part of the table, then waits 30 seconds (for
#              interrupting an export half-way)
#   anything else - a two-line CSV naming the table

use strict;
use warnings;
use autodie qw(:all);

use File::Spec;
use File::Temp qw(tempdir);

use Exporter qw(import);
our @EXPORT_OK = qw(install_fake_mdbtools make_database fake_path);

# PATH as it was before any test changed it.  Tests set PATH to stand-in
# directories; appending the *current* PATH would carry an earlier directory
# (with all three programs) along, so a program meant to be missing would
# still be found.
my $ORIGINAL_PATH = $ENV{PATH} // '';

# fake_path(@programs): a whole PATH value for tests.  On Unix, only the
# stand-in directory, so real mdbtools cannot interfere.  On Windows the
# original PATH is kept after it: with PATH replaced entirely, Windows
# could not start any process at all (not even perl.exe).
sub fake_path {
	my $dir = install_fake_mdbtools(@_);
	return $dir unless $^O eq 'MSWin32';
	require Config;
	return join($Config::Config{path_sep}, $dir, $ORIGINAL_PATH);
}

# Windows: the stand-ins are Perl scripts with a .cmd file beside each,
# so that File::Which finds them (through PATHEXT).  But IPC::Run3 starts
# programs with CreateProcess, which can only run real executables, not
# .cmd files; and going through cmd.exe instead would let it interpret
# "&", "|" and "%" in table names.  So, on Windows only, run3 is wrapped:
# a call to a stand-in "X.cmd" becomes "perl X.pl ..." - still a direct
# list-form call, with no shell.  Real mdbtools on Windows are .exe files
# and are not affected.  Both IPC::Run3's run3 and the copy the Exporter
# imported are wrapped, whichever was loaded first.
_redirect_stand_ins() if $^O eq 'MSWin32';

sub _redirect_stand_ins {
	require IPC::Run3;
	my %wrapped;
	my $wrap = sub {
		my $name = shift;
		no strict 'refs';
		my $original = defined(&{$name}) ? \&{$name} : return;
		return if $wrapped{$original};
		my $wrapper = sub {
			my ($cmd, @rest) = @_;
			if(ref($cmd) eq 'ARRAY' && $cmd->[0] =~ /\A(.+)\.cmd\z/i && -f "$1.pl") {
				$cmd = [$^X, "$1.pl", @{$cmd}[1 .. $#{$cmd}]];
			}



( run in 0.515 second using v1.01-cache-2.11-cpan-60884a8df76 )