App-Access2CSV

 view release on metacpan or  search on metacpan

t/domain.t  view on Meta::CPAN

	access_max   => 64,
	big_count    => 2**53,
	many         => 10,
);

# Characters used to build the multibyte partitions, as UTF-8 bytes (the
# form mdbtools and the command line use) and as Perl characters
Readonly::Hash my %TEXT => (
	german  => "Gr\x{fc}\x{df}e",                           # umlaut + sharp s
	emoji   => "\x{1F600}",                                 # 4 bytes in UTF-8
	family  => "\x{1F468}\x{200D}\x{1F469}\x{200D}\x{1F467}", # joined emoji sequence
	zalgo   => 'Z' . ("\x{301}\x{316}\x{334}" x 10) . 'algo', # stacked combining marks
	rtl     => "report\x{202E}vsc.exe",                     # right-to-left override
	arabic  => "\x{645}\x{628}\x{64a}\x{639}\x{627}\x{62a}", # plain RTL text
);

# The OS texts for the errors we expect
Readonly::Hash my %OS => (
	enoent       => do { local $! = ENOENT; "$!" },
	enametoolong => do { local $! = ENAMETOOLONG; "$!" },
);

# The valid boolean spellings (Params::Validate::Strict / Readonly::Values::Boolean)
Readonly::Array my @BOOL_TRUE  => qw(1 true TRUE yes on);
Readonly::Array my @BOOL_FALSE => qw(0 false FALSE no off);
Readonly::Array my @BOOL_BAD   => ('', 2, -1, 'True', 'Yes', '0.0', ' 1');
Readonly::Array my @BOOLEANS   => qw(overwrite verbose dry_run show_counts progress);

local $ENV{PATH} = join(':', install_fake_mdbtools(), $ENV{PATH});

# Snapshot of the catalog, to prove no test leaves it changed
my $CATALOG = dclone(\%App::Access2CSV::I18N::MESSAGES);

sub verbose_diag {
	my ($label, $data) = @_;
	diag("$label: ", Test::More::explain($data)) if $ENV{TEST_VERBOSE};
	return;
}

sub bytes { return encode('UTF-8', shift) }

sub new_database {
	my $dir = tempdir(CLEANUP => 1);
	return ($dir, make_database($dir, @_));
}

sub dir_entries {
	my $dir = shift;
	opendir my $dh, $dir or return [];
	return [sort grep { !/\A\.\.?\z/ } readdir $dh];
}

sub slurp {
	my $path = shift;
	open my $fh, '<:raw', $path or die "$path: $!";
	local $/;
	return scalar <$fh>;
}

# export(\@tables, %settings): run a quiet exporter over a database of
# @tables; returns (status, stderr, output dir)
sub export {
	my ($tables, %settings) = @_;
	my ($dir, $db) = new_database(@{$tables});
	my $out = "$dir/out";
	my $status;
	my (undef, $stderr) = capture {
		$status = eval { $CONFIG{exporter}->new(output_dir => $out, progress => 0, %settings)->run($db) };
	};
	return ($status, $stderr, $out);
}

#######################################################################
# App::Access2CSV::I18N::i18n
#######################################################################

subtest 'i18n key: known | unknown | empty | undef | reference' => sub {
	my $i18n = $CONFIG{i18n};

	is($i18n->i18n('dry_run_title'), 'DRY RUN', 'valid: known key');
	throws_ok { $i18n->i18n('x') } qr/\AUnknown message key: x at /, 'invalid: unknown key (1 character, shortest non-empty)';
	throws_ok { $i18n->i18n('') } qr/Parameter 'key' .*must be at least 1/, 'invalid: empty (just below the 1-character minimum)';
	throws_ok { $i18n->i18n(undef) } qr/Required parameter 'key' is missing/, 'invalid: undef';
	throws_ok { $i18n->i18n({}) } qr/Required parameter 'key' is missing/, 'invalid: empty hashref';
	throws_ok { $i18n->i18n("dry_run_title\n") } qr/\AUnknown message key: dry_run_title\n at /, 'invalid: valid key plus newline';
};

subtest 'i18n count: below 0 | 0 | 1 | 2 | very large | fraction' => sub {
	# English: 1 is "one", everything else "other"
	my $i18n = $CONFIG{i18n};
	my $say = sub { $i18n->i18n('summary', { params => [$_[0], 0], count => $_[0] }) };

	throws_ok { $say->(-1) } qr/Parameter 'count' \(-1\) must be at least 0/, 'invalid: -1 (just below the minimum)';
	is($say->(0), 'Processed 0 tables, 0 failed', 'minimum 0: plural');
	is($say->(1), 'Processed 1 table, 0 failed', '1: singular');
	is($say->(2), 'Processed 2 tables, 0 failed', '2 (just above 1): plural');
	like($say->($CONFIG{big_count}), qr/\AProcessed 9007199254740992 tables, /, '2**53: plural, no overflow');
	throws_ok { $say->(1.5) } qr/Parameter 'count' \(1\.5\) must be/, 'invalid: fraction';
	throws_ok { $say->('one') } qr/Parameter 'count' \(one\) must be/, 'invalid: word';

	# The plural edge moves with the language
	local $App::Access2CSV::I18N::MESSAGES{fr} = { summary => { one => '%d table (fr)', other => '%d tables (fr)' } };
	local $App::Access2CSV::I18N::MESSAGES{ja} = { summary => { one => 'never used', other => '%d (ja)' } };
	my %expect = (
		fr => { 0 => '0 table (fr)', 1 => '1 table (fr)', 2 => '2 tables (fr)' },
		ja => { 0 => '0 (ja)', 1 => '1 (ja)', 2 => '2 (ja)' },
	);
	foreach my $lang (sort keys %expect) {
		local $ENV{LANG} = "${lang}_XX.UTF-8";
		foreach my $count (sort keys %{ $expect{$lang} }) {
			is($i18n->i18n('summary', { params => [$count], count => $count }), $expect{$lang}{$count}, "$lang, count $count");
		}
	}
};

subtest 'i18n params: none | exact | multibyte text' => sub {
	# Values are copied into the text exactly: no corruption, and length
	# is counted in characters for character strings, bytes for bytes
	my $i18n = $CONFIG{i18n};
	is($i18n->i18n('missing_database', { params => [] }), 'Missing database filename', 'empty list, no placeholders');
	is($i18n->i18n('progress', { params => [1, 1, 'T'] }), '[1/1] T', 'exact number of values');

	foreach my $name (sort keys %TEXT) {
		my $chars = $TEXT{$name};
		my $text = $i18n->i18n('fatal', { params => [$chars] });
		is($text, "access2csv: $chars", "$name as characters: unchanged");
		is(length($text), length('access2csv: ') + length($chars), "$name as characters: length in characters");
		is($i18n->i18n('fatal', { params => [bytes($chars)] }), 'access2csv: ' . bytes($chars), "$name as UTF-8 bytes: unchanged");
	}
};

t/domain.t  view on Meta::CPAN

		sub new { bless {}, shift } sub debug { } sub info { } sub warn { }
		package Local::NoWarn;
		sub new { bless {}, shift } sub debug { } sub info { }
	}
	my $class = $CONFIG{exporter};
	lives_ok { $class->new(logger => Local::Full->new()) } 'valid: debug, info and warn';
	throws_ok { $class->new(logger => Local::NoWarn->new()) } qr/'logger'.*warn method/, 'invalid: one method short';
	throws_ok { $class->new(logger => {}) } qr/Parameter 'logger' must be an object/, 'invalid: plain hash';
	throws_ok { $class->new(logger => 'log.txt') } qr/Parameter 'logger' must be an object/, 'invalid: a file name';
};

subtest 'new language: code | empty | reference' => sub {
	local $App::Access2CSV::I18N::MESSAGES{de} = { dry_run_title => 'PROBELAUF' };
	my $class = $CONFIG{exporter};
	is($class->new(language => 'de')->i18n('dry_run_title'), 'PROBELAUF', 'valid code with a catalog');
	is($class->new(language => 'xx')->i18n('dry_run_title'), 'DRY RUN', 'valid code without a catalog');
	is($class->new(language => '')->i18n('dry_run_title'), 'DRY RUN', 'empty: use the environment');
	throws_ok { $class->new(language => []) } qr/'language'/, 'invalid: reference';
};

#######################################################################
# App::Access2CSV::Exporter::run
#######################################################################

subtest 'run database: file | missing | directory | device | empty | undef' => sub {
	my ($dir, $db) = new_database('T');
	my $e = $CONFIG{exporter}->new(output_dir => "$dir/out", progress => 0, dry_run => 1);

	my $status;
	capture { $status = $e->run($db) };
	is($status, $CONFIG{exit_ok}, 'valid: readable regular file');
	throws_ok { $e->run("$dir/missing") } qr/\ACannot read database \Q$dir\E.missing: \Q$OS{enoent}\E at /, 'missing';
	throws_ok { $e->run($dir) } qr/\ADatabase \Q$dir\E is not a regular file at /, 'directory';
	throws_ok { $e->run('/dev/null') } qr/\ADatabase \/dev\/null is not a regular file at /, 'device';
	throws_ok { $e->run('') } qr/Parameter 'database' .*must be at least 1/, 'empty (below the 1-character minimum)';
	throws_ok { $e->run(undef) } qr/Required parameter 'database' is missing/, 'undef';
};

subtest 'run database: path component length 255 | 256' => sub {
	# NAME_MAX edge: 255 bytes is a legal (if missing) name; 256 is too long
	my $dir = tempdir(CLEANUP => 1);
	my $e = $CONFIG{exporter}->new(progress => 0);
	my $at_max = "$dir/" . ('d' x $CONFIG{name_max});
	my $over = "$dir/" . ('d' x ($CONFIG{name_max} + 1));
	throws_ok { $e->run($at_max) } qr/: \Q$OS{enoent}\E at /, '255: accepted by the OS, just missing';
	throws_ok { $e->run($over) } qr/: \Q$OS{enametoolong}\E at /, '256: too long';
};

subtest 'table name length: 1 | 64 | 65 | 255 characters' => sub {
	# Access allows 64 characters: names up to that are kept, longer ones
	# (a damaged or hostile database) are shortened to 64.  0.001.0 did
	# not shorten, so names over 251 bytes failed with "File name too
	# long" (see the cpantesters report in t/stdin.t)
	my $max = $CONFIG{access_max};
	my %names = (
		one      => 'a',
		at_max   => 'b' x $max,
		over_max => 'c' x ($max + 1),
		os_max   => 'd' x $CONFIG{name_max},
	);
	my ($status, $stderr, $out) = export([values %names]);
	verbose_diag('files', dir_entries($out));
	is($status, $CONFIG{exit_ok}, 'every table exported');
	ok(-f "$out/$names{$_}$CONFIG{csv_suffix}", "$_: kept as it is") foreach qw(one at_max);
	ok(-f "$out/" . ('c' x $max) . $CONFIG{csv_suffix}, 'over_max: shortened to 64');
	ok(-f "$out/" . ('d' x $max) . $CONFIG{csv_suffix}, 'longer than the OS allows: shortened, not failed');
	is(scalar(@{ dir_entries($out) }), scalar(keys %names), 'one file each, no temporary files');
};

subtest 'table name length: shortened names that collide are numbered' => sub {
	my $max = $CONFIG{access_max};
	my ($status, undef, $out) = export([('f' x $max) . 'one', ('f' x $max) . 'two']);
	is($status, $CONFIG{exit_ok}, 'both exported');
	is_deeply(dir_entries($out), [('f' x $max) . $CONFIG{csv_suffix}, ('f' x $max) . "_2$CONFIG{csv_suffix}"], 'second one gets _2');
};

subtest 'table name length in multibyte text: shortened at a whole character' => sub {
	# At most 64 characters and 240 bytes of UTF-8 (64 emoji would be 256
	# bytes, over the 255 most file systems allow).  A name is never cut
	# inside a character, nor between a letter and its accent or inside a
	# joined emoji, which would leave a broken or different-looking name.
	my %units = (
		'u-umlaut: 1 character, 2 bytes'              => "\x{fc}",
		'emoji: 1 character, 4 bytes'                 => $TEXT{emoji},
		'joined emoji: 5 characters, 18 bytes'        => $TEXT{family},
		'e and combining acute: 2 characters, 3 bytes' => "e\x{301}",
	);
	foreach my $case (sort keys %units) {
		my $unit = bytes($units{$case});
		my $by_chars = int($CONFIG{access_max} / length($units{$case}));
		my $by_bytes = int($CONFIG{max_name_bytes} / length($unit));
		my $fits = ($by_chars < $by_bytes) ? $by_chars : $by_bytes;
		my ($status, $stderr, $out) = export([$unit x ($fits + 1)]);
		is($status, $CONFIG{exit_ok}, "$case: exported");
		is_deeply(dir_entries($out), [($unit x $fits) . $CONFIG{csv_suffix}], "$case: $fits whole ones kept");
	}
};

subtest 'table name bytes that are not UTF-8 become "_"' => sub {
	# macOS refuses file names that are not valid UTF-8 ("Illegal byte
	# sequence"), so 0.001.0 failed these tables there.  Each bad byte is
	# replaced, on every system, so the name is the same everywhere.
	my %names = (
		'Latin-1 e-acute'          => ["Caf\xE9", 'Caf_'],
		'cut-off UTF-8 character'  => ["Tea\xC3", 'Tea_'],
		'UTF-16 surrogate'         => ["a\xED\xA0\x80b", 'a_b'],
		'valid UTF-8 kept'         => [bytes("Cr\x{e8}me"), bytes("Cr\x{e8}me")],
	);
	my ($status, $stderr, $out) = export([map { $_->[0] } values %names]);
	is($status, $CONFIG{exit_ok}, 'every table exported') or diag($stderr);
	is_deeply(dir_entries($out), [sort map { $_->[1] . $CONFIG{csv_suffix} } values %names], 'bad bytes replaced, valid UTF-8 kept');

	# Then shortened like any other name
	($status, undef, $out) = export(["\xE9" x $CONFIG{name_max}]);
	is($status, $CONFIG{exit_ok}, 'long Latin-1 name: exported');
	is_deeply(dir_entries($out), [('_' x $CONFIG{access_max}) . $CONFIG{csv_suffix}], 'long Latin-1 name: replaced, then shortened to 64');
};

subtest 'table name characters: German, emoji, Zalgo, RTL text' => sub {
	# Kept exactly, byte for byte, in the file name and in the data
	my @names = map { bytes($TEXT{$_}) } qw(german emoji family zalgo arabic);
	my ($status, $stderr, $out) = export(\@names);
	is($status, $CONFIG{exit_ok}, 'all exported');
	is_deeply(dir_entries($out), [sort map { "$_.csv" } @names], 'file names byte-identical to the table names');
	like(slurp("$out/" . bytes($TEXT{family}) . '.csv'), qr/\Q${\ bytes($TEXT{family}) }\E/, 'joined emoji intact in the data');
};

subtest 'table name characters: invisible direction overrides are unsafe' => sub {
	# A right-to-left override makes "report<RLO>vsc.exe.csv" display as
	# "reportexe.csv.csv"-like text: a classic file-name spoofing trick.
	# Direction controls must be treated like other control characters.
	my @overrides = ("\x{202E}", "\x{202D}", "\x{2066}", "\x{2069}", "\x{200F}");
	my @names = map { bytes("t${_}x") } @overrides;
	push @names, bytes($TEXT{rtl});
	my ($status, undef, $out) = export(\@names);
	my @files = @{ dir_entries($out) };
	verbose_diag('files', \@files);
	is($status, $CONFIG{exit_ok}, 'exported');
	is(scalar(@files), scalar(@names), 'one file each');
	ok(!grep({ /\xE2\x80[\x8E\x8F\xAA-\xAE]|\xE2\x81[\xA6-\xA9]/ } @files), 'no direction control left in any file name');
	ok((grep { $_ eq 'report_vsc.exe.csv' } @files), 'spoofing name made visible');
};

subtest 'collision suffix: none | _2 | two digits' => sub {
	my @names = map { ('X' . (' ' x $_)) } 0 .. $CONFIG{many} - 1;
	my ($status, undef, $out) = export(\@names);
	my @files = @{ dir_entries($out) };
	is(scalar(@files), $CONFIG{many}, 'ten tables, ten files');
	ok((grep { $_ eq 'X.csv' } @files), 'first: no suffix');
	ok((grep { $_ eq 'X_2.csv' } @files), 'second: _2 (the lowest suffix)');
	ok((grep { $_ eq "X_$CONFIG{many}.csv" } @files), 'tenth: two-digit suffix');
};

subtest 'cp1252 data: the edges of Windows-1252' => sub {
	# U+00FF is the last Latin-1 letter; U+0100 the first outside;
	# U+20AC (Euro) is mapped; U+0080-U+009F are C1 controls, unmapped
	my %cases = (
		'U+00FF' => ["\x{ff}", "\xFF"],
		'U+20AC' => ["\x{20ac}", "\x80"],
		'sharp s and umlaut' => ["\x{df}\x{fc}", "\xDF\xFC"],
		'U+0100' => ["\x{100}", undef],
		'U+0081' => ["\x{81}", undef],
		'emoji'  => [$TEXT{emoji}, undef],
	);
	foreach my $case (sort keys %cases) {
		my ($chars, $expected) = @{ $cases{$case} };
		my $table = 'T';
		my ($dir, $db) = new_database($table);
		my $guard = do {
			require Test::Mockingbird;
			my $real = \&App::Access2CSV::Exporter::run3;
			Test::Mockingbird::mock_scoped('App::Access2CSV::Exporter::run3' => sub {
				my ($cmd, $in, $out, $err) = @_;
				return $real->(@_) unless $cmd->[0] =~ /mdb-export\z/;
				print {$out} bytes($chars) . "\n";
				$out->flush();
				${$err} = '';
				$? = 0;
				return 1;
			});
		};
		my $status;
		my (undef, $stderr) = capture {
			$status = $CONFIG{exporter}->new(output_dir => "$dir/out", progress => 0, encoding => 'cp1252')->run($db);
		};
		if(defined $expected) {
			is($status, $CONFIG{exit_ok}, "$case: converted");
			is(slurp("$dir/out/T.csv"), "$expected\n", "$case: right byte");
		} else {
			is($status, $CONFIG{exit_failure}, "$case: refused");
			like($stderr, qr/line 1: cannot be represented in cp1252/, "$case: reported");
		}
	}
};

subtest 'row counts: 0 | 1 | 2' => sub {
	# The plural edge in the log message
	require Test::Mockingbird;
	foreach my $rows (0, 1, 2) {
		my $real = \&App::Access2CSV::Exporter::run3;
		my $guard = Test::Mockingbird::mock_scoped('App::Access2CSV::Exporter::run3' => sub {
			my ($cmd, $in, $out, $err) = @_;
			return $real->(@_) unless $cmd->[0] =~ /mdb-count\z/;
			${$out} = "$rows\n";
			${$err} = '';
			$? = 0;
			return 1;
		});
		{
			package Local::Rec;
			sub new { bless { lines => [] }, shift }
			sub debug { } sub warn { }
			sub info { push @{ $_[0]{lines} }, $_[1] }
		}
		my $logger = Local::Rec->new();
		my ($dir, $db) = new_database('T');
		capture { $CONFIG{exporter}->new(output_dir => "$dir/out", progress => 0, show_counts => 1, logger => $logger)->run($db) };
		my $word = $rows == 1 ? 'row' : 'rows';
		ok((grep { /\($rows $word\)\z/ } @{ $logger->{lines} }), "$rows: '($rows $word)'");
	}
};

#######################################################################
# App::Access2CSV::run
#######################################################################

sub cli {
	my @argv = @_;
	my $status;
	my ($stdout, $stderr) = capture { $status = $CONFIG{app}->run(@argv) };
	return ($status, $stdout, $stderr);
}

subtest 'argv: 0 | 1 | 2 database names' => sub {
	my ($dir, $db) = new_database('T');
	my @common = ('--no-log', '--dry-run');
	my ($status, undef, $stderr) = cli(@common);
	is($status, $CONFIG{exit_usage}, '0 (below the only valid count)');
	like($stderr, qr/^Missing database filename$/m, 'message');
	($status) = cli(@common, $db);
	is($status, $CONFIG{exit_ok}, 'exactly one (the only valid count)');
	($status, undef, $stderr) = cli(@common, $db, $db);
	is($status, $CONFIG{exit_usage}, '2 (above)');
	like($stderr, qr/^Missing database filename$/m, 'message');
};

subtest 'argv --encoding: valid names | invalid' => sub {
	my ($dir, $db) = new_database('T');
	foreach my $value (qw(utf8 utf8-bom cp1252)) {
		my ($status) = cli('--no-log', '--dry-run', '--encoding', $value, $db);
		is($status, $CONFIG{exit_ok}, "valid: $value");
	}
	my ($status, undef, $stderr) = cli('--no-log', '--encoding', 'latin1', $db);
	is($status, $CONFIG{exit_usage}, 'invalid: latin1 (usage error)');
	like($stderr, qr/^Invalid setting: Parameter 'encoding' \(latin1\) must be one of utf8, utf8-bom, cp1252$/m, 'message');
};

subtest 'argv --table: 0 | 1 | many | non-ASCII' => sub {
	my $german = bytes($TEXT{german});
	my ($dir, $db) = new_database('A', 'B', $german);
	my %cases = (
		0    => [[], 3],
		1    => [['A'], 1],
		many => [['A', 'B', $german], 3],
		utf8 => [[$german], 1],
	);
	foreach my $case (sort keys %cases) {
		my ($tables, $files) = @{ $cases{$case} };
		my $out = "$dir/$case";
		my ($status) = cli('--no-log', '--no-progress', '--output-dir', $out, (map { ('--table', $_) } @{$tables}), $db);
		is($status, $CONFIG{exit_ok}, "$case: success");
		is(scalar(@{ dir_entries($out) }), $files, "$case: $files file(s)");
	}
};

subtest 'argv --log: empty | file | --no-log' => sub {
	# The default log goes to the current directory, so run in an empty one:
	# a log someone left in the real current directory must not matter, and
	# a failing test must not leave one there either
	my ($dir, $db) = new_database('T');
	my $work = tempdir(CLEANUP => 1);
	my $cwd = File::Spec->rel2abs(File::Spec->curdir());
	chdir $work or die "$work: $!";

	cli('--log', '', '--dry-run', $db);
	ok(!-e 'access2csv.log', "'' means no log (no default file created)");
	cli('--log', "$dir/x.log", '--dry-run', $db);
	ok(-e "$dir/x.log", 'a file name: that file');
	cli('--no-log', '--dry-run', $db);
	ok(!-e 'access2csv.log', '--no-log: no file');

	chdir $cwd or die "$cwd: $!";
};

subtest 'no failure above changed shared state' => sub {
	is_deeply(\%App::Access2CSV::I18N::MESSAGES, $CATALOG, 'message catalog unchanged');
	ok(!exists $ENV{LANG}, 'LANG not left set');
};

done_testing();



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