AddUiFrameworkPackageAnalyzerVerifier.cs 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.CSharp.Testing;
  6. using Microsoft.CodeAnalysis.Testing;
  7. namespace System.Reactive.Analyzers.Test.Verifiers
  8. {
  9. #pragma warning disable CA1812 // CA1812 wants this to be a static class, but since it has a base class, it can't be.
  10. internal sealed class AddUiFrameworkPackageAnalyzerVerifier :
  11. CSharpAnalyzerVerifier<AddUiFrameworkPackageAnalyzer, AddUiFrameworkPackageAnalyzerVerifier.Test, DefaultVerifier>
  12. #pragma warning restore CA1812
  13. {
  14. #pragma warning disable CA1812 // CA1812 thinks this is never instantiated, even though it's used as a type argument for a parameter with a new() constrain!
  15. public sealed class Test : CSharpAnalyzerTest<AddUiFrameworkPackageAnalyzer, DefaultVerifier>
  16. #pragma warning restore CA1812
  17. {
  18. public Test()
  19. {
  20. SolutionTransforms.Add((solution, projectId) =>
  21. {
  22. // We need to make the System.Reactive.dll assembly available to the compiler.
  23. // In particular, it needs to be the reference assembly, because these tests
  24. // need to model what developers will see in real projects. (If these tests
  25. // supply the compiler with the runtime library, which is the build output of
  26. // the System.Reactive project, we get different errors in the tests than the
  27. // errors they aim to provoke.)
  28. // That's why this test project references System.Reactive.MakeRefAssemblies.
  29. // Note: this means we need to be careful never to try to load the
  30. // System.Reactive.dll assembly in this project. Being a reference assembly,
  31. // it's suitable for use by the compiler, but if the runtime tries to load it,
  32. // we'll get:
  33. // System.BadImageFormatException: Could not load file or assembly
  34. // So that means we can't use Rx at all in this particular test project.
  35. // We can't even refer to any Rx-defined type such as Observable.
  36. var rxPath = Path.Combine(
  37. Path.GetDirectoryName(typeof(AddUiFrameworkPackageAnalyzerVerifier).Assembly.Location),
  38. "System.Reactive.dll");
  39. MetadataReference rxMetadataRef = MetadataReference.CreateFromFile(rxPath);
  40. var compilationOptions = solution.GetProject(projectId).CompilationOptions;
  41. compilationOptions = compilationOptions
  42. .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication);
  43. solution = solution
  44. .WithProjectCompilationOptions(projectId, compilationOptions)
  45. .AddMetadataReference(projectId, rxMetadataRef);
  46. return solution;
  47. });
  48. ReferenceAssemblies = ReferenceAssemblies.Net.Net80Windows;
  49. // Adding a NuGet reference to Rx would more directly represent real developer
  50. // scenarios, but we don't build new packages in day to day dev in the IDE, so this
  51. // risks running tests against an out of date build. It also complicates things:
  52. // we'd need to set up a local package feed and keep it up to date. Also, NuGet
  53. // caching presumes that if the version didn't change, the package didn't change
  54. // either, but our versions only change when we commit. So this also makes it quite
  55. // likely that we end up running these tests against something other than what we
  56. // meant to.
  57. // However, if it turns out that at some point in the future we want to change over
  58. // to package references, we can do it like this:
  59. //
  60. // var nugetConfigPath = Path.Combine(
  61. // Path.GetDirectoryName(typeof(AddUiFrameworkPackageAnalyzerVerifier).Assembly.Location),
  62. // "NuGet.Config");
  63. // ReferenceAssemblies = ReferenceAssemblies
  64. // .AddPackages([new PackageIdentity("System.Reactive", "7.0.0")])
  65. // .WithNuGetConfigFilePath(nugetConfigPath);
  66. }
  67. }
  68. }
  69. }