示例#1
0
        public async Task TestUnresolvedAnalyzerReference()
        {
            var workspace             = new AdhocWorkspace();
            var project               = workspace.CurrentSolution.AddProject("empty", "empty", LanguageNames.CSharp);
            var mockFileChangeService = new Mock <IVsFileChangeEx>();

            using (var analyzer = new VisualStudioAnalyzer(
                       @"PathToAnalyzer",
                       fileChangeService: mockFileChangeService.Object,
                       hostDiagnosticUpdateSource: null,
                       projectId: project.Id,
                       workspace: workspace,
                       loader: null,
                       language: project.Language))
            {
                var analyzerReference = analyzer.GetReference();
                project = project.WithAnalyzerReferences(new AnalyzerReference[]
                {
                    analyzerReference,
                });

                var checksum = await project.State.GetChecksumAsync(CancellationToken.None).ConfigureAwait(false);

                Assert.NotNull(checksum);

                var assetBuilder = new CustomAssetBuilder(workspace);
                var serializer   = new Serializer(workspace);

                var asset = assetBuilder.Build(analyzerReference, CancellationToken.None);

                using (var stream = SerializableBytes.CreateWritableStream())
                    using (var writer = new ObjectWriter(stream))
                    {
                        await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false);

                        stream.Position = 0;
                        using (var reader = ObjectReader.TryGetReader(stream))
                        {
                            var recovered        = serializer.Deserialize <AnalyzerReference>(asset.Kind, reader, CancellationToken.None);
                            var assetFromStorage = assetBuilder.Build(recovered, CancellationToken.None);

                            Assert.Equal(asset.Checksum, assetFromStorage.Checksum);

                            // This won't round trip, but we should get an UnresolvedAnalyzerReference, with the same path
                            Assert.Equal(analyzerReference.FullPath, recovered.FullPath);
                        }
                    }
            }
        }
        public async Task TestUnresolvedAnalyzerReference()
        {
            using (var workspace = new TestWorkspace())
            {
                var lazyWorkspace = new Lazy <VisualStudioWorkspaceImpl>(() => throw Utilities.ExceptionUtilities.Unreachable);

                var hostDiagnosticUpdateSource = new HostDiagnosticUpdateSource(lazyWorkspace, new MockDiagnosticUpdateSourceRegistrationService());

                var project = workspace.CurrentSolution.AddProject("empty", "empty", LanguageNames.CSharp);
                using var analyzer = new VisualStudioAnalyzer(
                          @"C:\PathToAnalyzer",
                          hostDiagnosticUpdateSource,
                          projectId: project.Id,
                          language: project.Language);

                var analyzerReference = analyzer.GetReference();
                project = project.WithAnalyzerReferences(new[] { analyzerReference });

                var checksum = await project.State.GetChecksumAsync(CancellationToken.None).ConfigureAwait(false);

                Assert.NotNull(checksum);

                var serializer = workspace.Services.GetService <ISerializerService>();

                var asset = WorkspaceAnalyzerReferenceAsset.Create(analyzerReference, serializer, CancellationToken.None);

                using var stream = SerializableBytes.CreateWritableStream();

                using (var writer = new ObjectWriter(stream, leaveOpen: true))
                {
                    await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false);
                }

                stream.Position = 0;
                using (var reader = ObjectReader.TryGetReader(stream))
                {
                    var recovered        = serializer.Deserialize <AnalyzerReference>(asset.Kind, reader, CancellationToken.None);
                    var assetFromStorage = WorkspaceAnalyzerReferenceAsset.Create(recovered, serializer, CancellationToken.None);

                    Assert.Equal(asset.Checksum, assetFromStorage.Checksum);

                    // This won't round trip, but we should get an UnresolvedAnalyzerReference, with the same path
                    Assert.Equal(analyzerReference.FullPath, recovered.FullPath);
                }
            }
        }