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
7 changes: 7 additions & 0 deletions Coder.Editor/DocumentStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public DocumentResult Load(string path)
return DocumentResult.Failed($"Could not read {path}: {ex.Message}");
}

// An interrupted save or a newly created file leaves nothing in it, and the deserializer
// refuses empty text as a bad argument, which would read to the user as a parse failure.
if (string.IsNullOrWhiteSpace(yaml))
{
return DocumentResult.Failed($"{path} is empty.");
}

AstNode? root;
try
{
Expand Down
21 changes: 21 additions & 0 deletions Coder.Test/Editor/CoderEditorAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,27 @@ public void Open_ReportsAFileThatIsNotADocument()
Assert.IsNull(app.DocumentPath);
}

/// <summary>
/// Tests that an empty or whitespace-only file is reported as empty rather than as a document
/// the editor cannot parse, since an interrupted save leaves exactly that behind.
/// </summary>
/// <param name="content">What the file holds.</param>
[TestMethod]
[DataRow("")]
[DataRow(" \n")]
public void Open_ReportsAnEmptyFile(string content)
{
DocumentStore store = NewStore();
string empty = Path.Combine(root, "empty" + DocumentStore.Extension);
File.WriteAllText(empty, content);
CoderEditorApp app = NewApp(store);

Assert.IsFalse(app.Open(empty));

Assert.AreEqual($"{empty} is empty.", app.Status);
Assert.IsNull(app.DocumentPath);
}

/// <summary>
/// Tests that a write the filesystem refuses is reported rather than thrown, so a bad
/// destination cannot take the editor down mid-session.
Expand Down
Loading