Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion source/StoneAge.Data.FileSystem.Tests/FileSystemTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -112,6 +112,25 @@ public async Task WhenFileExist_ExpectItOverWritten()
contents.Should().BeEquivalentTo(new byte[5]);
}

[Test]
public async Task WhenFileDataIsNull_ExpectErrorMessage()
{
//---------------Arrange-------------------
var path = Path.GetTempPath();
var fileName = Guid.NewGuid() + ".txt";
var document = new DocumentBuilder()
.With_Name(fileName)
.With_Bytes(null) // explicitly set null data
.Create_Document();

var sut = new FileSystem();
//---------------Act----------------------
var result = await sut.Write(path, document);
//---------------Assert-----------------------
result.HadError.Should().BeTrue();
result.ErrorMessages.Should().Contain("No file data provided; cannot write file.");
}

private static void Write_File_Contents_For_Testing(string path, string fileName)
{
File.WriteAllText(Path.Combine(path, fileName), "test line");
Expand Down
9 changes: 8 additions & 1 deletion source/StoneAge.Data.FileSystem/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -196,6 +196,13 @@ public async Task<IEnumerable<string>> ReadAllLines(string path)
private WriteFileResult Write_File_To_Path(IDocument file, string filePath, FileMode fileMode)
{
var result = new WriteFileResult();

if (file.Data == null)
{
result.ErrorMessages.Add("No file data provided; cannot write file.");
return result;
}

try
{
using (var stream = new FileStream(filePath, fileMode))
Expand Down