Closed polettix closed 1 year ago
Relatively easy to reproduce (in CSV_XS). This will most likely be the new test-unit:
$ cat t/68_header.t
#!/usr/bin/perl
use strict;
use warnings;
use Test::More "no_plan";
#use Test::More tests => 24;
use Config;
BEGIN {
use_ok "Text::CSV_XS", "csv";
plan skip_all => "Cannot load Text::CSV_XS" if $@;
require "./t/util.pl";
}
my $tfn = "_68test.csv"; END { unlink $tfn, "_$tfn"; }
my @dta = (
[qw( foo bar zap )],
[qw( mars venus pluto )],
[qw( 1 2 3 )],
);
my @dth = (
{ foo => "mars", bar => "venus", zap => "pluto" },
{ foo => 1, bar => 2, zap => 3 },
);
{ open my $fh, ">", $tfn or die "$tfn: $!\n";
local $" = ",";
print $fh "@$_\n" for @dta;
close $fh;
}
is_deeply (csv (in => $tfn), \@dta, "csv ()");
is_deeply (csv (in => $tfn, bom => 1), \@dth, "csv (bom)");
is_deeply (csv (in => $tfn, headers => "auto"), \@dth, "csv (headers)");
is_deeply (csv (in => $tfn, bom => 1, headers => "auto"), \@dth, "csv (bom, headers)");
foreach my $arg ("", "bom", "auto", "bom, auto") {
open my $fh, "<", $tfn or die "$tfn: $!\n";
my %attr;
$arg =~ m/bom/ and $attr{bom} = 1;
$arg =~ m/auto/ and $attr{headers} = "auto";
ok (my $csv = Text::CSV_XS->new (), "New $arg");
is ($csv->record_number, 0, "Start");
if ($arg) {
is_deeply ([ $csv->header ($fh, \%attr) ], $dta[0], "Header") if $arg;
diag ($csv->record_number);
is_deeply ($csv->getline_hr ($fh), $dth[$_], "getline $_") for 0..$#dta;
diag ($csv->record_number);
}
else {
is_deeply ($csv->getline ($fh), $dta[$_], "getline $_") for 0..$#dta;
is ($csv->record_number, 3, "Done");
}
close $fh;
}
done_testing;
Still failing, but working on it …
fix for Text::CSV_XS being tested. The next commit makes it PASS all the way back to perl-5.6.1. Some more tests in progress, but likely I'll uload a new release today or tomorrow
Thanks. Shipped Text::CSV 2.03 with the fixes from Text::CSV_XS 1.51
Hi!
Using the module for a command-line tool, I'm consistently setting
headers => 'auto'
and give the possibility to specify adetect_bom
based on a command-line option.Tests show that when both options are active, headers are read twice, which ends up skipping the first line (where real headers live) and trying to set headers from the second line.
I'd say that this behaviour is surprising and sub-optimal, although admittedly there's no indication that this should not behave this way in the docs. I'm currently working this around like this:
If you're interested, this is a full test for this:
Output: