Skip to content

Commit

Permalink
csv files are written without BOM - fixes #118
Browse files Browse the repository at this point in the history
  • Loading branch information
philipmat committed Sep 21, 2020
1 parent 9e37bd9 commit 2e6cd8a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
5 changes: 3 additions & 2 deletions alternatives/dotnet/discogs/CsvExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace discogs
Expand Down Expand Up @@ -64,11 +65,11 @@ public async Task ExportAsync(T value)
{
var fs = File.Create(csvFile, bufferSize: BufferSize);
var gzStream = new GZipStream(fs, CompressionMode.Compress, leaveOpen: false);
stream = new StreamWriter(gzStream, encoding: System.Text.Encoding.UTF8);
stream = new StreamWriter(gzStream, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
}
else
{
stream = new StreamWriter(csvFile, append: false, encoding: System.Text.Encoding.UTF8, bufferSize: BufferSize);
stream = new StreamWriter(csvFile, append: false, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), bufferSize: BufferSize);
}
stream.WriteLine(CsvExtensions.ToCsv(kvp.Value));
return (csvFile, stream);
Expand Down
60 changes: 60 additions & 0 deletions alternatives/dotnet/tests/CsvExporterItegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using discogs;
using FluentAssertions;
using Xunit;

namespace tests
{
public class CsvExporterIntegrationTests : IDisposable
{
public static readonly string TestPath = Path.Combine(Path.GetTempPath(), "CvsExporter");

public CsvExporterIntegrationTests()
{
// deletes folder as a shorcut to cleaning out and starting the test with blank slate
if (Directory.Exists(TestPath)) Directory.Delete(TestPath, recursive: true);
Directory.CreateDirectory(TestPath);
}

public void Dispose()
{
Directory.Delete(TestPath, recursive: true);
}

[Fact]
public async Task Export_DoesNotCreateBomAsync()
{
//Given
var exporter = new CsvExporter<SimpleRecord>(TestPath, compress: false);
var record = new SimpleRecord();

//When
await exporter.ExportAsync(record);
await exporter.CompleteExportAsync(1);

//Then
var outputFile = Path.Combine(TestPath, "test_1.csv");
File.Exists(outputFile).Should().BeTrue();
byte[] content = await File.ReadAllBytesAsync(outputFile);

content[0].Should().Be((byte)'f', because: "foo is the first word in the file");
content[1].Should().Be((byte)'o', because: "foo is the first word in the file");
}

private class SimpleRecord : IExportToCsv
{
public IEnumerable<(string StreamName, string[] RowValues)> ExportToCsv()
{
yield return ("test_1", new string[] { "1.0", "1.1" });
}

public IReadOnlyDictionary<string, string[]> GetCsvExportScheme()
=> new Dictionary<string, string[]> {
["test_1"] = new string[] { "foo", "bar" }
};
}
}
}

0 comments on commit 2e6cd8a

Please sign in to comment.