提交 781c5b0e 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #19446 from CyrusNajmabadi/nullPaths

Be resilient to documents/projects with null-filepaths in our persistence layer.
......@@ -368,11 +368,11 @@ private void DoSimultaneousReads(Func<Task<string>> read, string expectedValue)
Assert.Equal(new List<Exception>(), exceptions);
}
private Solution CreateOrOpenSolution()
protected Solution CreateOrOpenSolution(bool nullPaths = false)
{
string solutionFile = Path.Combine(_persistentFolder, "Solution1.sln");
bool newSolution;
if (newSolution = !File.Exists(solutionFile))
var solutionFile = Path.Combine(_persistentFolder, "Solution1.sln");
var newSolution = !File.Exists(solutionFile);
if (newSolution)
{
File.WriteAllText(solutionFile, "");
}
......@@ -386,20 +386,22 @@ private Solution CreateOrOpenSolution()
if (newSolution)
{
string projectFile = Path.Combine(Path.GetDirectoryName(solutionFile), "Project1.csproj");
var projectFile = Path.Combine(Path.GetDirectoryName(solutionFile), "Project1.csproj");
File.WriteAllText(projectFile, "");
solution = solution.AddProject(ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "Project1", "Project1", LanguageNames.CSharp, projectFile));
solution = solution.AddProject(ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "Project1", "Project1", LanguageNames.CSharp,
filePath: nullPaths ? null : projectFile));
var project = solution.Projects.Single();
string documentFile = Path.Combine(Path.GetDirectoryName(projectFile), "Document1.cs");
var documentFile = Path.Combine(Path.GetDirectoryName(projectFile), "Document1.cs");
File.WriteAllText(documentFile, "");
solution = solution.AddDocument(DocumentInfo.Create(DocumentId.CreateNewId(project.Id), "Document1", filePath: documentFile));
solution = solution.AddDocument(DocumentInfo.Create(DocumentId.CreateNewId(project.Id), "Document1",
filePath: nullPaths ? null : documentFile));
}
return solution;
}
private IPersistentStorage GetStorage(Solution solution)
protected IPersistentStorage GetStorage(Solution solution)
{
var storage = GetStorageService().GetStorage(solution);
......@@ -409,7 +411,7 @@ private IPersistentStorage GetStorage(Solution solution)
protected abstract IPersistentStorageService GetStorageService();
private Stream EncodeString(string text)
protected Stream EncodeString(string text)
{
var bytes = _encoding.GetBytes(text);
var stream = new MemoryStream(bytes);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.SQLite;
using Xunit;
namespace Microsoft.CodeAnalysis.UnitTests.WorkspaceServices
{
......@@ -14,5 +17,24 @@ public class SQLitePersistentStorageTests : AbstractPersistentStorageTests
{
protected override IPersistentStorageService GetStorageService()
=> new SQLitePersistentStorageService(_persistentEnabledOptionService, testing: true);
[Fact]
public async Task TestNullFilePaths()
{
var solution = CreateOrOpenSolution(nullPaths: true);
var streamName = "stream";
using (var storage = GetStorage(solution))
{
var project = solution.Projects.First();
var document = project.Documents.First();
Assert.False(await storage.WriteStreamAsync(project, streamName, EncodeString("")));
Assert.False(await storage.WriteStreamAsync(document, streamName, EncodeString("")));
Assert.Null(await storage.ReadStreamAsync(project, streamName));
Assert.Null(await storage.ReadStreamAsync(document, streamName));
}
}
}
}
\ No newline at end of file
......@@ -202,6 +202,14 @@ string GetDocumentIdString(Document document)
void AddIfUnknownId(string value, HashSet<string> stringsToAdd)
{
// Null strings are not supported at all. Just ignore these. Any read/writes
// to null values will fail and will return 'false/null' to indicate failure
// (which is part of the documented contract of the persistence layer API).
if (value == null)
{
return;
}
if (!_stringToIdMap.TryGetValue(value, out var id))
{
stringsToAdd.Add(value);
......
......@@ -34,6 +34,14 @@ private void FetchStringTable(SqlConnection connection)
private int? TryGetStringId(SqlConnection connection, string value)
{
// Null strings are not supported at all. Just ignore these. Any read/writes
// to null values will fail and will return 'false/null' to indicate failure
// (which is part of the documented contract of the persistence layer API).
if (value == null)
{
return null;
}
// First see if we've cached the ID for this value locally. If so, just return
// what we already have.
if (_stringToIdMap.TryGetValue(value, out int existingId))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册