1
0

ApiApprovalTests.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 PublicApiGenerator;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using VerifyTests;
  13. using VerifyXunit;
  14. using Xunit;
  15. namespace ReactiveTests.Tests.Api
  16. {
  17. public class ApiApprovalTests : VerifyBase
  18. {
  19. static ApiApprovalTests()
  20. {
  21. VerifierSettings.OnVerifyMismatch((filePair, message, autoVerify) => DiffPlexReporter.Report(filePair.ReceivedPath, filePair.VerifiedPath, message));
  22. }
  23. public ApiApprovalTests()
  24. : base()
  25. {
  26. }
  27. // Note:
  28. // System.Reactive uses the .NET SDK's built in package validation, specifically the
  29. // PackageValidationBaselineVersion feature to ensure backwards compatibility
  30. // System.Reactive is using Microsoft.CodeAnalysis.PublicApiAnalyzers to ensure stability of
  31. // its public API.
  32. // TODO:
  33. // Move Aliases and Testing packages over to one of the mechanisms above
  34. // Add similar API checking to the new FrameworkIntegrations packages
  35. [Fact]
  36. public Task Aliases()
  37. {
  38. var publicApi = GeneratePublicApi(typeof(System.Reactive.Observable.Aliases.QueryLanguage).Assembly);
  39. return Verify(publicApi, "cs");
  40. }
  41. [Fact]
  42. public Task Testing()
  43. {
  44. var publicApi = GeneratePublicApi(typeof(Microsoft.Reactive.Testing.TestScheduler).Assembly);
  45. return Verify(publicApi, "cs");
  46. }
  47. private string GeneratePublicApi(Assembly assembly)
  48. {
  49. var options = MakeGeneratorOptions();
  50. return Filter(ApiGenerator.GeneratePublicApi(assembly, options));
  51. }
  52. private static ApiGeneratorOptions MakeGeneratorOptions()
  53. {
  54. return new()
  55. {
  56. AllowNamespacePrefixes = ["System", "Microsoft"]
  57. };
  58. }
  59. private static string Filter(string text)
  60. {
  61. return string.Join(Environment.NewLine, text.Split(
  62. [
  63. Environment.NewLine
  64. ], StringSplitOptions.RemoveEmptyEntries)
  65. .Where(l => !l.StartsWith("[assembly: AssemblyVersion("))
  66. .Where(l => !l.StartsWith("[assembly: AssemblyFileVersion("))
  67. .Where(l => !l.StartsWith("[assembly: AssemblyInformationalVersion("))
  68. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"CommitHash\""))
  69. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"RepositoryUrl\""))
  70. .Where(l => !string.IsNullOrWhiteSpace(l))
  71. );
  72. }
  73. }
  74. }