Skip to content

Commit

Permalink
Fix NullReferenceException. (dotnet#5254)
Browse files Browse the repository at this point in the history
* Added null check for page in ManagedReferenceDocumentProcessor.cs and corresponding UT.

* Removed unneccessary using statement.

* Renamed variables.
  • Loading branch information
sharanya-rao authored and superyyrrzz committed Nov 4, 2019
1 parent 4b6abf5 commit ab40ef7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected override FileModel LoadArticle(FileAndType file, ImmutableDictionary<s
}

var page = YamlUtility.Deserialize<PageViewModel>(file.File);
if (page.Items == null || page.Items.Count == 0)
if (page?.Items == null || page.Items.Count == 0)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,38 @@ public void SystemKeysListShouldBeComplete()
}
}

[Fact]
public void LoadArticleWithEmptyFileShouldWarnAndReturnNull()
{
var fileWithNoContent = "TestData/mref/FileWithNoContent.yml";
var file = new FileAndType(Directory.GetCurrentDirectory(), fileWithNoContent, DocumentType.Article);
var processor = new ManagedReferenceDocumentProcessor();

var listener = TestLoggerListener.CreateLoggerListenerWithPhaseStartFilter(nameof(LoadArticleWithEmptyFileShouldWarnAndReturnNull), LogLevel.Info);
try
{
Logger.RegisterListener(listener);

FileModel actualFileModel;
using (new LoggerPhaseScope(nameof(LoadArticleWithEmptyFileShouldWarnAndReturnNull)))
{
actualFileModel = processor.Load(file, null);
}

var warnings = listener.GetItemsByLogLevel(LogLevel.Warning);
Assert.Single(warnings);
var warning = warnings.Single();
Assert.Equal("Please add `YamlMime` as the first line of file, e.g.: `### YamlMime:ManagedReference`, otherwise the file will be not treated as ManagedReference source file in near future.", warning.Message);
Assert.Equal(fileWithNoContent, warning.File);

Assert.Null(actualFileModel);
}
finally
{
Logger.UnregisterListener(listener);
}
}

private void BuildDocument(FileCollection files)
{
var parameters = new DocumentBuildParameters
Expand Down
Empty file.

0 comments on commit ab40ef7

Please sign in to comment.