From 534971e91c8b6cbb1a324afe93bb98c484dccc07 Mon Sep 17 00:00:00 2001 From: Stefan Bertels Date: Wed, 7 Oct 2020 23:26:52 +0200 Subject: [PATCH 1/3] update test to show IgnoreBlankLines is wrongly evaluated --- tests/CsvHelper.Tests/CsvReaderTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CsvHelper.Tests/CsvReaderTests.cs b/tests/CsvHelper.Tests/CsvReaderTests.cs index 54ea9ccc6..2cb1b235c 100644 --- a/tests/CsvHelper.Tests/CsvReaderTests.cs +++ b/tests/CsvHelper.Tests/CsvReaderTests.cs @@ -239,10 +239,10 @@ public void GetMissingFieldGenericByIndexStrictTest() var data = new Queue(); data.Enqueue(new[] { "One", "Two" }); data.Enqueue(new[] { "1", "2" }); - data.Enqueue(null); var parserMock = new ParserMock(data); var reader = new CsvReader(parserMock); + reader.Configuration.IgnoreBlankLines = false; reader.Read(); try From d3178ec45622ca075309e0a2655f2953a43337d4 Mon Sep 17 00:00:00 2001 From: Stefan Bertels Date: Wed, 7 Oct 2020 23:43:18 +0200 Subject: [PATCH 2/3] adjust more tests --- tests/CsvHelper.Tests/CsvReaderTests.cs | 1 + tests/CsvHelper.Tests/Exceptions/ExceptionMessageTests.cs | 1 + .../Reading/NullableValuesInEmptyColumnsInputTests.cs | 3 +++ 3 files changed, 5 insertions(+) diff --git a/tests/CsvHelper.Tests/CsvReaderTests.cs b/tests/CsvHelper.Tests/CsvReaderTests.cs index 2cb1b235c..0c228ad75 100644 --- a/tests/CsvHelper.Tests/CsvReaderTests.cs +++ b/tests/CsvHelper.Tests/CsvReaderTests.cs @@ -863,6 +863,7 @@ public void DoNotIgnoreBlankLinesTest() { csv.Configuration.Delimiter = ","; csv.Configuration.IgnoreBlankLines = false; + csv.Configuration.MissingFieldFound = null; csv.Configuration.RegisterClassMap(); writer.WriteLine("Id,Name"); diff --git a/tests/CsvHelper.Tests/Exceptions/ExceptionMessageTests.cs b/tests/CsvHelper.Tests/Exceptions/ExceptionMessageTests.cs index 83eed0448..def13136c 100644 --- a/tests/CsvHelper.Tests/Exceptions/ExceptionMessageTests.cs +++ b/tests/CsvHelper.Tests/Exceptions/ExceptionMessageTests.cs @@ -23,6 +23,7 @@ public void GetMissingFieldTest() }; var reader = new CsvReader( parser ); + reader.Configuration.IgnoreBlankLines = false; reader.Read(); reader.Read(); try diff --git a/tests/CsvHelper.Tests/Reading/NullableValuesInEmptyColumnsInputTests.cs b/tests/CsvHelper.Tests/Reading/NullableValuesInEmptyColumnsInputTests.cs index c9c26c3c2..99185f66b 100644 --- a/tests/CsvHelper.Tests/Reading/NullableValuesInEmptyColumnsInputTests.cs +++ b/tests/CsvHelper.Tests/Reading/NullableValuesInEmptyColumnsInputTests.cs @@ -23,6 +23,7 @@ public void SingleColumnCsvWithHeadersAndSingleNullDataRowTest() using (var csv = new CsvReader(parser)) { csv.Configuration.IgnoreBlankLines = false; + csv.Configuration.MissingFieldFound = null; csv.Configuration.TypeConverterOptionsCache.GetOptions().NullValues.Add(string.Empty); // Read header row, assert header row columns: @@ -60,6 +61,7 @@ public void SingleColumnCsvWithHeadersAndPresentAndNullDataRowTest() using (var csv = new CsvReader(parser)) { csv.Configuration.IgnoreBlankLines = false; + csv.Configuration.MissingFieldFound = null; csv.Configuration.TypeConverterOptionsCache.GetOptions().NullValues.Add(string.Empty); // Read header row, assert header row columns: @@ -122,6 +124,7 @@ public void TwoColumnCsvWithHeadersAndPresentAndNullDataRowTest() using (var csv = new CsvReader(parser)) { csv.Configuration.IgnoreBlankLines = false; + csv.Configuration.MissingFieldFound = null; csv.Configuration.TypeConverterOptionsCache.GetOptions().NullValues.Add(string.Empty); // Read empty fields as nulls instead of `""`. // Read header row, assert header row columns: From d7392068c5c889c92022d10192996fa39fe069ed Mon Sep 17 00:00:00 2001 From: Stefan Bertels Date: Wed, 7 Oct 2020 23:43:49 +0200 Subject: [PATCH 3/3] fix code according to changed unit tests --- src/CsvHelper/CsvReader.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/CsvHelper/CsvReader.cs b/src/CsvHelper/CsvReader.cs index a4e14e251..ca66cdc58 100644 --- a/src/CsvHelper/CsvReader.cs +++ b/src/CsvHelper/CsvReader.cs @@ -329,11 +329,7 @@ public virtual string GetField(int index) if (index >= context.Record.Length || index < 0) { - if (context.ReaderConfiguration.IgnoreBlankLines) - { - context.ReaderConfiguration.MissingFieldFound?.Invoke(null, index, context); - } - + context.ReaderConfiguration.MissingFieldFound?.Invoke(null, index, context); return default; } @@ -546,10 +542,7 @@ public virtual T GetField(int index, ITypeConverter converter) if (index >= context.Record.Length || index < 0) { context.CurrentIndex = index; - if (context.ReaderConfiguration.IgnoreBlankLines) - { - context.ReaderConfiguration.MissingFieldFound?.Invoke(null, index, context); - } + context.ReaderConfiguration.MissingFieldFound?.Invoke(null, index, context); return default; }