Skip to content

Commit a3a6c8f

Browse files
authored
Merge pull request #3 from Kode-Rex/cosine/bugfix/issue-tracking-fhiy3o
Fix null data handling in Write_File_To_Path method
2 parents 0578830 + 339d6c3 commit a3a6c8f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

source/StoneAge.Data.FileSystem.Tests/FileSystemTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Linq;
44
using System.Threading.Tasks;
@@ -112,6 +112,25 @@ public async Task WhenFileExist_ExpectItOverWritten()
112112
contents.Should().BeEquivalentTo(new byte[5]);
113113
}
114114

115+
[Test]
116+
public async Task WhenFileDataIsNull_ExpectErrorMessage()
117+
{
118+
//---------------Arrange-------------------
119+
var path = Path.GetTempPath();
120+
var fileName = Guid.NewGuid() + ".txt";
121+
var document = new DocumentBuilder()
122+
.With_Name(fileName)
123+
.With_Bytes(null) // explicitly set null data
124+
.Create_Document();
125+
126+
var sut = new FileSystem();
127+
//---------------Act----------------------
128+
var result = await sut.Write(path, document);
129+
//---------------Assert-----------------------
130+
result.HadError.Should().BeTrue();
131+
result.ErrorMessages.Should().Contain("No file data provided; cannot write file.");
132+
}
133+
115134
private static void Write_File_Contents_For_Testing(string path, string fileName)
116135
{
117136
File.WriteAllText(Path.Combine(path, fileName), "test line");

source/StoneAge.Data.FileSystem/FileSystem.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -196,6 +196,13 @@ public async Task<IEnumerable<string>> ReadAllLines(string path)
196196
private WriteFileResult Write_File_To_Path(IDocument file, string filePath, FileMode fileMode)
197197
{
198198
var result = new WriteFileResult();
199+
200+
if (file.Data == null)
201+
{
202+
result.ErrorMessages.Add("No file data provided; cannot write file.");
203+
return result;
204+
}
205+
199206
try
200207
{
201208
using (var stream = new FileStream(filePath, fileMode))

0 commit comments

Comments
 (0)