Merge pull request #975 from jkoritzinsky/folders-support

Support validating providing files in nested paths within the Analyzer and CodeFix test suites.
This commit is contained in:
Sam Harwell 2022-04-05 07:20:22 -07:00 коммит произвёл GitHub
Родитель fcc55f2c04 f69f30289d
Коммит 5cbdc32128
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 110 добавлений и 16 удалений

Просмотреть файл

@ -7,6 +7,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
@ -1209,19 +1210,22 @@ namespace Microsoft.CodeAnalysis.Testing
foreach (var (newFileName, source) in projectState.Sources)
{
var documentId = DocumentId.CreateNewId(additionalProjectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, source, filePath: newFileName);
var (fileName, folders) = GetNameAndFoldersFromPath(projectState.DefaultPrefix, newFileName);
solution = solution.AddDocument(documentId, fileName, source, folders: folders, filePath: newFileName);
}
foreach (var (newFileName, source) in projectState.AdditionalFiles)
{
var documentId = DocumentId.CreateNewId(additionalProjectId, debugName: newFileName);
solution = solution.AddAdditionalDocument(documentId, newFileName, source, filePath: newFileName);
var (fileName, folders) = GetNameAndFoldersFromPath(projectState.DefaultPrefix, newFileName);
solution = solution.AddAdditionalDocument(documentId, fileName, source, folders: folders, filePath: newFileName);
}
foreach (var (newFileName, source) in projectState.AnalyzerConfigFiles)
{
var documentId = DocumentId.CreateNewId(additionalProjectId, debugName: newFileName);
solution = solution.AddAnalyzerConfigDocument(documentId, newFileName, source, filePath: newFileName);
var (fileName, folders) = GetNameAndFoldersFromPath(projectState.DefaultPrefix, newFileName);
solution = solution.AddAnalyzerConfigDocument(documentId, fileName, source, folders: folders, filePath: newFileName);
}
}
@ -1230,19 +1234,22 @@ namespace Microsoft.CodeAnalysis.Testing
foreach (var (newFileName, source) in primaryProject.Sources)
{
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, source, filePath: newFileName);
var (fileName, folders) = GetNameAndFoldersFromPath(primaryProject.DefaultPrefix, newFileName);
solution = solution.AddDocument(documentId, fileName, source, folders: folders, filePath: newFileName);
}
foreach (var (newFileName, source) in primaryProject.AdditionalFiles)
{
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddAdditionalDocument(documentId, newFileName, source, filePath: newFileName);
var (fileName, folders) = GetNameAndFoldersFromPath(primaryProject.DefaultPrefix, newFileName);
solution = solution.AddAdditionalDocument(documentId, fileName, source, folders: folders, filePath: newFileName);
}
foreach (var (newFileName, source) in primaryProject.AnalyzerConfigFiles)
{
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddAnalyzerConfigDocument(documentId, newFileName, source, filePath: newFileName);
var (fileName, folders) = GetNameAndFoldersFromPath(primaryProject.DefaultPrefix, newFileName);
solution = solution.AddAnalyzerConfigDocument(documentId, fileName, source, folders: folders, filePath: newFileName);
}
solution = AddProjectReferences(solution, projectId, primaryProject.AdditionalProjectReferences.Select(name => projectIdMap[name]));
@ -1265,6 +1272,54 @@ namespace Microsoft.CodeAnalysis.Testing
}
}
protected (string fileName, IEnumerable<string> folders) GetNameAndFoldersFromPath(string projectPathPrefix, string path)
{
// Normalize to platform path separators for simplicity later on
var normalizedPath = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
var normalizedDefaultPathPrefix = projectPathPrefix.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (!Path.IsPathRooted(normalizedDefaultPathPrefix))
{
// If our default path isn't rooted, then we assume that we don't have any rooted paths
// and just use the file name
return (Path.GetFileName(normalizedPath), folders: new string[0]);
}
// | Default path | Project root path |
// |--------------|-------------------|
// | /0/Temp | /0/ |
// | /0/ | /0/ |
var projectRootPath = Path.GetFileName(normalizedDefaultPathPrefix) == string.Empty
? normalizedDefaultPathPrefix
: (Path.GetDirectoryName(normalizedDefaultPathPrefix) + Path.DirectorySeparatorChar);
// If the default path prefix is a directory name (ending with a directory separator)
// then treat it as the project root.
if (!normalizedPath.StartsWith(projectRootPath))
{
// If our path doesn't start with the default path prefix, then the file is out of tree.
if (Path.IsPathRooted(normalizedPath))
{
// If the user provides a rooted path as the file name, just use that as-is.
return (path, folders: new string[0]);
}
// Otherwise, to match VS behavior we will report no folders and only the file name.
return (Path.GetFileName(normalizedPath), folders: new string[0]);
}
var subpath = normalizedPath.Substring(projectRootPath.Length);
var fileName = Path.GetFileName(subpath);
if (Path.GetDirectoryName(subpath) == string.Empty)
{
return (fileName, folders: new string[0]);
}
var folders = Path.GetDirectoryName(subpath)!.Split(Path.DirectorySeparatorChar);
return (fileName, folders);
}
/// <summary>
/// Creates a solution that will be used as parent for the sources that need to be checked.
/// </summary>

Просмотреть файл

@ -17,6 +17,7 @@ namespace Microsoft.CodeAnalysis.Testing.Model
state.Name,
state.AssemblyName,
state.Language,
state.DefaultPrefix,
state.ReferenceAssemblies ?? defaultReferenceAssemblies,
state.OutputKind ?? OutputKind.DynamicallyLinkedLibrary,
state.DocumentationMode ?? DocumentationMode.Diagnose,
@ -34,6 +35,7 @@ namespace Microsoft.CodeAnalysis.Testing.Model
string name,
string assemblyName,
string language,
string defaultPrefix,
ReferenceAssemblies referenceAssemblies,
OutputKind outputKind,
DocumentationMode documentationMode,
@ -48,6 +50,7 @@ namespace Microsoft.CodeAnalysis.Testing.Model
Name = name;
AssemblyName = assemblyName;
Language = language;
DefaultPrefix = defaultPrefix;
ReferenceAssemblies = referenceAssemblies;
OutputKind = outputKind;
DocumentationMode = documentationMode;
@ -86,6 +89,8 @@ namespace Microsoft.CodeAnalysis.Testing.Model
public ImmutableArray<Diagnostic> AdditionalDiagnostics { get; }
internal string DefaultPrefix { get; }
public EvaluatedProjectState WithSources(ImmutableArray<(string filename, SourceText content)> sources)
{
if (sources == Sources)
@ -110,6 +115,7 @@ namespace Microsoft.CodeAnalysis.Testing.Model
Optional<string> name = default,
Optional<string> assemblyName = default,
Optional<string> language = default,
Optional<string> defaultPrefix = default,
Optional<ReferenceAssemblies> referenceAssemblies = default,
Optional<OutputKind> outputKind = default,
Optional<DocumentationMode> documentationMode = default,
@ -125,6 +131,7 @@ namespace Microsoft.CodeAnalysis.Testing.Model
GetValueOrDefault(name, Name),
GetValueOrDefault(assemblyName, AssemblyName),
GetValueOrDefault(language, Language),
GetValueOrDefault(defaultPrefix, DefaultPrefix),
GetValueOrDefault(referenceAssemblies, ReferenceAssemblies),
GetValueOrDefault(outputKind, OutputKind),
GetValueOrDefault(documentationMode, DocumentationMode),

Просмотреть файл

@ -77,7 +77,7 @@ namespace Microsoft.CodeAnalysis.Testing
public MetadataReferenceCollection AdditionalReferences { get; } = new MetadataReferenceCollection();
private protected string DefaultPrefix { get; }
internal string DefaultPrefix { get; }
private protected string DefaultExtension { get; }
}

Просмотреть файл

@ -9,6 +9,7 @@ Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.DiagnosticVerifier.set ->
Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.DisabledDiagnostics.get -> System.Collections.Generic.List<string>
Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.ExpectedDiagnostics.get -> System.Collections.Generic.List<Microsoft.CodeAnalysis.Testing.DiagnosticResult>
Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.FormatVerifierMessage(System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer> analyzers, Microsoft.CodeAnalysis.Diagnostic actual, Microsoft.CodeAnalysis.Testing.DiagnosticResult expected, string message) -> string
Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.GetNameAndFoldersFromPath(string projectPathPrefix, string path) -> (string fileName, System.Collections.Generic.IEnumerable<string> folders)
Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.GetSortedDiagnosticsAsync(Microsoft.CodeAnalysis.Solution solution, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer> analyzers, System.Collections.Immutable.ImmutableArray<(Microsoft.CodeAnalysis.Project project, Microsoft.CodeAnalysis.Diagnostic diagnostic)> additionalDiagnostics, Microsoft.CodeAnalysis.Testing.CompilerDiagnostics compilerDiagnostics, Microsoft.CodeAnalysis.Testing.IVerifier verifier, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<System.Collections.Immutable.ImmutableArray<(Microsoft.CodeAnalysis.Project project, Microsoft.CodeAnalysis.Diagnostic diagnostic)>>
Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.MarkupOptions.get -> Microsoft.CodeAnalysis.Testing.MarkupOptions
Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.MarkupOptions.set -> void

Просмотреть файл

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
@ -527,7 +528,9 @@ namespace Microsoft.CodeAnalysis.Testing
verifier.EqualOrDiff(newState.Sources[i].content.ToString(), actual.ToString(), $"content of '{newState.Sources[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(newState.Sources[i].content.Encoding, actual.Encoding, $"encoding of '{newState.Sources[i].filename}' was expected to be '{newState.Sources[i].content.Encoding?.WebName}' but was '{actual.Encoding?.WebName}'");
verifier.Equal(newState.Sources[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{newState.Sources[i].filename}' was expected to be '{newState.Sources[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(newState.Sources[i].filename, updatedDocuments[i].Name, $"file name was expected to be '{newState.Sources[i].filename}' but was '{updatedDocuments[i].Name}'");
var (fileName, folders) = GetNameAndFoldersFromPath(newState.DefaultPrefix, newState.Sources[i].filename);
verifier.Equal(fileName, updatedDocuments[i].Name, $"file name was expected to be '{fileName}' but was '{updatedDocuments[i].Name}'");
verifier.SequenceEqual(folders, updatedDocuments[i].Folders, message: $"folders was expected to be '{string.Join("/", folders)}' but was '{string.Join("/", updatedDocuments[i].Folders)}'");
}
var updatedAdditionalDocuments = project.AdditionalDocuments.ToArray();
@ -540,7 +543,9 @@ namespace Microsoft.CodeAnalysis.Testing
verifier.EqualOrDiff(newState.AdditionalFiles[i].content.ToString(), actual.ToString(), $"content of '{newState.AdditionalFiles[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(newState.AdditionalFiles[i].content.Encoding, actual.Encoding, $"encoding of '{newState.AdditionalFiles[i].filename}' was expected to be '{newState.AdditionalFiles[i].content.Encoding?.WebName}' but was '{actual.Encoding?.WebName}'");
verifier.Equal(newState.AdditionalFiles[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{newState.AdditionalFiles[i].filename}' was expected to be '{newState.AdditionalFiles[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(newState.AdditionalFiles[i].filename, updatedAdditionalDocuments[i].Name, $"file name was expected to be '{newState.AdditionalFiles[i].filename}' but was '{updatedAdditionalDocuments[i].Name}'");
var (fileName, folders) = GetNameAndFoldersFromPath(newState.DefaultPrefix, newState.AdditionalFiles[i].filename);
verifier.Equal(fileName, updatedAdditionalDocuments[i].Name, $"file name was expected to be '{fileName}' but was '{updatedAdditionalDocuments[i].Name}'");
verifier.SequenceEqual(folders, updatedAdditionalDocuments[i].Folders, message: $"folders was expected to be '{string.Join("/", folders)}' but was '{string.Join("/", updatedAdditionalDocuments[i].Folders)}'");
}
var updatedAnalyzerConfigDocuments = project.AnalyzerConfigDocuments().ToArray();
@ -553,7 +558,9 @@ namespace Microsoft.CodeAnalysis.Testing
verifier.EqualOrDiff(newState.AnalyzerConfigFiles[i].content.ToString(), actual.ToString(), $"content of '{newState.AnalyzerConfigFiles[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(newState.AnalyzerConfigFiles[i].content.Encoding, actual.Encoding, $"encoding of '{newState.AnalyzerConfigFiles[i].filename}' was expected to be '{newState.AnalyzerConfigFiles[i].content.Encoding?.WebName}' but was '{actual.Encoding?.WebName}'");
verifier.Equal(newState.AnalyzerConfigFiles[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{newState.AnalyzerConfigFiles[i].filename}' was expected to be '{newState.AnalyzerConfigFiles[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(newState.AnalyzerConfigFiles[i].filename, updatedAnalyzerConfigDocuments[i].Name, $"file name was expected to be '{newState.AnalyzerConfigFiles[i].filename}' but was '{updatedAnalyzerConfigDocuments[i].Name}'");
var (fileName, folders) = GetNameAndFoldersFromPath(newState.DefaultPrefix, newState.AnalyzerConfigFiles[i].filename);
verifier.Equal(fileName, updatedAnalyzerConfigDocuments[i].Name, $"file name was expected to be '{fileName}' but was '{updatedAnalyzerConfigDocuments[i].Name}'");
verifier.SequenceEqual(folders, updatedAnalyzerConfigDocuments[i].Folders, message: $"folders was expected to be '{string.Join("/", folders)}' but was '{string.Join("/", updatedAnalyzerConfigDocuments[i].Folders)}'");
}
}

Просмотреть файл

@ -24,7 +24,7 @@ namespace Microsoft.CodeAnalysis.Testing
=> Task.FromResult<IEnumerable<Diagnostic>>(_diagnostics.Where(diagnostic => diagnostic.project.Id == project.Id).Select(diagnostic => diagnostic.diagnostic));
public override Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
=> Task.FromResult(_diagnostics.Where(i => i.diagnostic.Location.GetLineSpan().Path == document.Name).Where(diagnostic => diagnostic.project.Id == document.Project.Id).Select(diagnostic => diagnostic.diagnostic));
=> Task.FromResult(_diagnostics.Where(i => i.diagnostic.Location.GetLineSpan().Path == document.FilePath).Where(diagnostic => diagnostic.project.Id == document.Project.Id).Select(diagnostic => diagnostic.diagnostic));
public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)
=> Task.FromResult(_diagnostics.Where(i => !i.diagnostic.Location.IsInSource).Where(diagnostic => diagnostic.project.Id == project.Id).Select(diagnostic => diagnostic.diagnostic));

Просмотреть файл

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
@ -182,7 +183,9 @@ namespace Microsoft.CodeAnalysis.Testing
verifier.EqualOrDiff(newState.Sources[i].content.ToString(), actual.ToString(), $"content of '{newState.Sources[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(newState.Sources[i].content.Encoding, actual.Encoding, $"encoding of '{newState.Sources[i].filename}' was expected to be '{newState.Sources[i].content.Encoding?.WebName}' but was '{actual.Encoding?.WebName}'");
verifier.Equal(newState.Sources[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{newState.Sources[i].filename}' was expected to be '{newState.Sources[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(newState.Sources[i].filename, updatedDocuments[i].Name, $"file name was expected to be '{newState.Sources[i].filename}' but was '{updatedDocuments[i].Name}'");
var (fileName, folders) = GetNameAndFoldersFromPath(newState.DefaultPrefix, newState.Sources[i].filename);
verifier.Equal(fileName, updatedDocuments[i].Name, $"file name was expected to be '{fileName}' but was '{updatedDocuments[i].Name}'");
verifier.SequenceEqual(folders, updatedDocuments[i].Folders, message: $"folders was expected to be '{string.Join("/", folders)}' but was '{string.Join("/", updatedDocuments[i].Folders)}'");
}
var updatedAdditionalDocuments = project.AdditionalDocuments.ToArray();
@ -195,7 +198,9 @@ namespace Microsoft.CodeAnalysis.Testing
verifier.EqualOrDiff(newState.AdditionalFiles[i].content.ToString(), actual.ToString(), $"content of '{newState.AdditionalFiles[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(newState.AdditionalFiles[i].content.Encoding, actual.Encoding, $"encoding of '{newState.AdditionalFiles[i].filename}' was expected to be '{newState.AdditionalFiles[i].content.Encoding?.WebName}' but was '{actual.Encoding?.WebName}'");
verifier.Equal(newState.AdditionalFiles[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{newState.AdditionalFiles[i].filename}' was expected to be '{newState.AdditionalFiles[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(newState.AdditionalFiles[i].filename, updatedAdditionalDocuments[i].Name, $"file name was expected to be '{newState.AdditionalFiles[i].filename}' but was '{updatedAdditionalDocuments[i].Name}'");
var (fileName, folders) = GetNameAndFoldersFromPath(newState.DefaultPrefix, newState.AdditionalFiles[i].filename);
verifier.Equal(fileName, updatedAdditionalDocuments[i].Name, $"file name was expected to be '{fileName}' but was '{updatedAdditionalDocuments[i].Name}'");
verifier.SequenceEqual(folders, updatedAdditionalDocuments[i].Folders, message: $"folders was expected to be '{string.Join("/", folders)}' but was '{string.Join("/", updatedAdditionalDocuments[i].Folders)}'");
}
var updatedAnalyzerConfigDocuments = project.AnalyzerConfigDocuments().ToArray();
@ -208,7 +213,9 @@ namespace Microsoft.CodeAnalysis.Testing
verifier.EqualOrDiff(newState.AnalyzerConfigFiles[i].content.ToString(), actual.ToString(), $"content of '{newState.AnalyzerConfigFiles[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(newState.AnalyzerConfigFiles[i].content.Encoding, actual.Encoding, $"encoding of '{newState.AnalyzerConfigFiles[i].filename}' was expected to be '{newState.AnalyzerConfigFiles[i].content.Encoding?.WebName}' but was '{actual.Encoding?.WebName}'");
verifier.Equal(newState.AnalyzerConfigFiles[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{newState.AnalyzerConfigFiles[i].filename}' was expected to be '{newState.AnalyzerConfigFiles[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(newState.AnalyzerConfigFiles[i].filename, updatedAnalyzerConfigDocuments[i].Name, $"file name was expected to be '{newState.AnalyzerConfigFiles[i].filename}' but was '{updatedAnalyzerConfigDocuments[i].Name}'");
var (fileName, folders) = GetNameAndFoldersFromPath(newState.DefaultPrefix, newState.AnalyzerConfigFiles[i].filename);
verifier.Equal(fileName, updatedAnalyzerConfigDocuments[i].Name, $"file name was expected to be '{fileName}' but was '{updatedAnalyzerConfigDocuments[i].Name}'");
verifier.SequenceEqual(folders, updatedAnalyzerConfigDocuments[i].Folders, message: $"folders was expected to be '{string.Join("/", folders)}' but was '{string.Join("/", updatedAnalyzerConfigDocuments[i].Folders)}'");
}
}

Просмотреть файл

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
@ -82,6 +83,7 @@ namespace Microsoft.CodeAnalysis.Testing
{
var updatedDocuments = project.Documents.ToArray();
var expectedSources = testState.Sources.Concat(testState.GeneratedSources).ToArray();
var numOriginalSources = testState.Sources.Count;
verifier.Equal(expectedSources.Length, updatedDocuments.Length, $"expected '{nameof(testState)}.{nameof(SolutionState.Sources)}' with '{nameof(testState)}.{nameof(SolutionState.GeneratedSources)}' to match '{nameof(updatedDocuments)}', but '{nameof(testState)}.{nameof(SolutionState.Sources)}' with '{nameof(testState)}.{nameof(SolutionState.GeneratedSources)}' contains '{expectedSources.Length}' documents and '{nameof(updatedDocuments)}' contains '{updatedDocuments.Length}' documents");
@ -91,13 +93,27 @@ namespace Microsoft.CodeAnalysis.Testing
verifier.EqualOrDiff(expectedSources[i].content.ToString(), actual.ToString(), $"content of '{expectedSources[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(expectedSources[i].content.Encoding, actual.Encoding, $"encoding of '{expectedSources[i].filename}' was expected to be '{expectedSources[i].content.Encoding?.WebName}' but was '{actual.Encoding?.WebName}'");
verifier.Equal(expectedSources[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{expectedSources[i].filename}' was expected to be '{expectedSources[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(expectedSources[i].filename, updatedDocuments[i].Name, $"file name was expected to be '{expectedSources[i].filename}' but was '{updatedDocuments[i].Name}'");
// Source-generated sources are implicitly in a subtree, so they have a different folders calculation.
var (fileName, folders) = i < numOriginalSources
? GetNameAndFoldersFromPath(DefaultFilePathPrefix, expectedSources[i].filename)
: GetNameAndFoldersFromSourceGeneratedFilePath(expectedSources[i].filename);
verifier.Equal(fileName, updatedDocuments[i].Name, $"file name was expected to be '{fileName}' but was '{updatedDocuments[i].Name}'");
verifier.SequenceEqual(folders, updatedDocuments[i].Folders, message: $"folders was expected to be '{string.Join("/", folders)}' but was '{string.Join("/", updatedDocuments[i].Folders)}'");
}
}
return diagnostics;
}
private static (string fileName, IEnumerable<string> folders) GetNameAndFoldersFromSourceGeneratedFilePath(string filePath)
{
// Source-generated files are always implicitly subpaths under the project root path.
var folders = Path.GetDirectoryName(filePath)!.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var fileName = Path.GetFileName(filePath);
return (fileName, folders);
}
private async Task<(Project project, ImmutableArray<Diagnostic> diagnostics)> ApplySourceGeneratorAsync(ImmutableArray<ISourceGenerator> sourceGenerators, Project project, IVerifier verifier, CancellationToken cancellationToken)
{
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
@ -109,7 +125,8 @@ namespace Microsoft.CodeAnalysis.Testing
var updatedProject = project;
foreach (var tree in result.GeneratedTrees)
{
updatedProject = updatedProject.AddDocument(tree.FilePath, await tree.GetTextAsync(cancellationToken).ConfigureAwait(false), filePath: tree.FilePath).Project;
var (fileName, folders) = GetNameAndFoldersFromSourceGeneratedFilePath(tree.FilePath);
updatedProject = updatedProject.AddDocument(fileName, await tree.GetTextAsync(cancellationToken).ConfigureAwait(false), folders: folders, filePath: tree.FilePath).Project;
}
return (updatedProject, result.Diagnostics);