-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enumerate all .NET Framework installations (#572)
Co-authored-by: Bruno Garcia <[email protected]>
- Loading branch information
1 parent
2e3a433
commit db6d590
Showing
13 changed files
with
402 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#if NETFX | ||
using Sentry.PlatformAbstractions; | ||
|
||
namespace Sentry.Integrations | ||
{ | ||
internal class NetFxInstallationsIntegration : ISdkIntegration | ||
{ | ||
public void Register(IHub hub, SentryOptions options) | ||
{ | ||
if (!Runtime.Current.IsMono()) | ||
{ | ||
options.AddEventProcessor(new NetFxInstallationsEventProcessor(options)); | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Sentry/PlatformAbstractions/FrameworkInstallationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Sentry.PlatformAbstractions | ||
{ | ||
internal static class FrameworkInstallationExtensions | ||
{ | ||
internal static string? GetVersionNumber(this FrameworkInstallation frameworkInstall) | ||
=> frameworkInstall?.ShortName | ||
?? (frameworkInstall?.Version != null ? $"v{frameworkInstall.Version}" : null); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Sentry/PlatformAbstractions/NetFxInstallationsEventProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#if NETFX | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using Sentry.Extensibility; | ||
|
||
namespace Sentry.PlatformAbstractions | ||
{ | ||
internal class NetFxInstallationsEventProcessor : ISentryEventProcessor | ||
{ | ||
internal static readonly string NetFxInstallationsKey = ".NET Framework"; | ||
|
||
private readonly Lazy<Dictionary<string, string>> _netFxInstallations = | ||
new Lazy<Dictionary<string, string>>(() => GetInstallationsDictionary(), LazyThreadSafetyMode.ExecutionAndPublication); | ||
|
||
private volatile bool _netFxInstallationEnabled = true; | ||
|
||
private readonly SentryOptions _options; | ||
|
||
internal NetFxInstallationsEventProcessor(SentryOptions options) => _options = options; | ||
|
||
internal static Dictionary<string, string> GetInstallationsDictionary() | ||
{ | ||
var versionsDictionary = new Dictionary<string, string>(); | ||
var installations = FrameworkInfo.GetInstallations(); | ||
foreach (var profile in installations.Select(p => p.Profile).Distinct()) | ||
{ | ||
versionsDictionary.Add($"{NetFxInstallationsKey} {profile}", | ||
string.Join(", ", installations.Where(p => p.Profile == profile) | ||
.Select(p => $"\"{p.GetVersionNumber()}\""))); | ||
} | ||
return versionsDictionary; | ||
} | ||
|
||
public SentryEvent? Process(SentryEvent @event) | ||
{ | ||
if (_netFxInstallationEnabled) | ||
{ | ||
if (!@event.Contexts.ContainsKey(NetFxInstallationsKey)) | ||
{ | ||
try | ||
{ | ||
@event.Contexts[NetFxInstallationsKey] = _netFxInstallations.Value; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_options.DiagnosticLogger?.LogError("Failed to add NetFxInstallations into event.", ex); | ||
//In case of any failure, this process function will be disabled to avoid throwing exceptions for future events. | ||
_netFxInstallationEnabled = false; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
_options.DiagnosticLogger.LogDebug("NetFxInstallation disabled due to previous error."); | ||
} | ||
return @event; | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/Sentry.Tests/Integrations/NetFxInstallationsIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#if NETFX | ||
using Sentry.Integrations; | ||
using Sentry.PlatformAbstractions; | ||
using Xunit; | ||
|
||
namespace Sentry.Tests.Integrations | ||
{ | ||
public class NetFxInstallationsIntegrationTests | ||
{ | ||
[SkippableFact] | ||
public void Register_CurrentRuntimeIsMono_NetFxInstallationsEventProcessorNotAdded() | ||
{ | ||
Skip.If(!Runtime.Current.IsMono()); | ||
|
||
//Arrance | ||
var options = new SentryOptions(); | ||
var integration = new NetFxInstallationsIntegration(); | ||
|
||
//Act | ||
integration.Register(null, options); | ||
|
||
//Assert | ||
Assert.DoesNotContain(options.EventProcessors, p => p.GetType() == typeof(NetFxInstallationsEventProcessor)); | ||
} | ||
|
||
[SkippableFact] | ||
public void Register_CurrentRuntimeIsNotMono_NetFxInstallationsEventProcessorAdded() | ||
{ | ||
Skip.If(Runtime.Current.IsMono()); | ||
|
||
//Arrance | ||
var options = new SentryOptions(); | ||
var integration = new NetFxInstallationsIntegration(); | ||
|
||
//Act | ||
integration.Register(null, options); | ||
|
||
//Assert | ||
Assert.Contains(options.EventProcessors, p => p.GetType() == typeof(NetFxInstallationsEventProcessor)); | ||
} | ||
} | ||
} | ||
#endif |
78 changes: 78 additions & 0 deletions
78
test/Sentry.Tests/PlatformAbstractions/FrameworkInstallationExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using Sentry.PlatformAbstractions; | ||
using Xunit; | ||
|
||
namespace Sentry.Tests.PlatformAbstractions | ||
{ | ||
public class FrameworkInstallationExtensionsTests | ||
{ | ||
[Fact] | ||
public void GetVersionNumber_NullFrameworkInstallation_NullVersion() | ||
{ | ||
//Arrange | ||
FrameworkInstallation frameworkInstallation = null; | ||
//Act | ||
var version = frameworkInstallation.GetVersionNumber(); | ||
|
||
//Assert | ||
Assert.Null(version); | ||
} | ||
|
||
[Fact] | ||
public void GetVersionNumber_NullShortVersionAndNullVersion_NullVersion() | ||
{ | ||
//Arrange | ||
var frameworkInstallation = new FrameworkInstallation(); | ||
|
||
//Act | ||
var version = frameworkInstallation.GetVersionNumber(); | ||
|
||
//Assert | ||
Assert.Null(version); | ||
} | ||
|
||
[Fact] | ||
public void GetVersionNumber_ValidShortVersion_ShortVersion() | ||
{ | ||
//Arrange | ||
var frameworkInstallation = new FrameworkInstallation(); | ||
var expectedShortVersion = "v1.2.3"; | ||
frameworkInstallation.ShortName = expectedShortVersion; | ||
|
||
//Act | ||
var version = frameworkInstallation.GetVersionNumber(); | ||
|
||
//Assert | ||
Assert.Equal(expectedShortVersion, version); | ||
} | ||
|
||
|
||
[Fact] | ||
public void GetVersionNumber_ValidVersionAndNullShortVersion_NullVersion() | ||
{ | ||
//Arrange | ||
var frameworkInstallation = new FrameworkInstallation(); | ||
frameworkInstallation.Version = new Version("1.2"); | ||
|
||
//Act | ||
var version = frameworkInstallation.GetVersionNumber(); | ||
|
||
//Assert | ||
Assert.Equal("v1.2", version); | ||
} | ||
|
||
[Fact] | ||
public void GetVersionNumber_ValidMinorMajorVersionAndNullShortVersion_NullVersion() | ||
{ | ||
//Arrange | ||
var frameworkInstallation = new FrameworkInstallation(); | ||
frameworkInstallation.Version = new Version(1,2); | ||
|
||
//Act | ||
var version = frameworkInstallation.GetVersionNumber(); | ||
|
||
//Assert | ||
Assert.Equal("v1.2", version); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
test/Sentry.Tests/PlatformAbstractions/NetFxInstallationsEventProcessorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#if NETFX | ||
using Xunit; | ||
using System.Collections.Generic; | ||
using Sentry.PlatformAbstractions; | ||
|
||
namespace Sentry.Tests.PlatformAbstractions | ||
{ | ||
public class NetFxInstallationsEventProcessorTests | ||
{ | ||
private class Fixture | ||
{ | ||
public SentryOptions SentryOptions { get; set; } = new SentryOptions(); | ||
|
||
public NetFxInstallationsEventProcessor GetSut() => new NetFxInstallationsEventProcessor(SentryOptions); | ||
} | ||
|
||
private readonly Fixture _fixture = new Fixture(); | ||
|
||
[SkippableFact] | ||
public void Process_SentryEventWithNetFxList() | ||
{ | ||
Skip.If(Runtime.Current.IsMono(), "Mono not supported."); | ||
|
||
//Arrange | ||
var @event = new SentryEvent(); | ||
var sut = _fixture.GetSut(); | ||
|
||
//Act | ||
_ = sut.Process(@event); | ||
|
||
//Assert | ||
_ = Assert.IsAssignableFrom<Dictionary<string, string>>(@event.Contexts[NetFxInstallationsEventProcessor.NetFxInstallationsKey]); | ||
} | ||
|
||
[SkippableFact] | ||
public void Process_ContextWithGetInstallationsData() | ||
{ | ||
Skip.If(Runtime.Current.IsMono(), "Mono not supported."); | ||
|
||
//Arrange | ||
var @event = new SentryEvent(); | ||
var sut = _fixture.GetSut(); | ||
var installationList = FrameworkInfo.GetInstallations(); | ||
//Act | ||
_ = sut.Process(@event); | ||
|
||
//Assert | ||
var dictionary = @event.Contexts[NetFxInstallationsEventProcessor.NetFxInstallationsKey] as Dictionary<string, string>; | ||
foreach(var item in installationList) | ||
{ | ||
Assert.Contains($"\"{item.GetVersionNumber()}\"", dictionary[$"{NetFxInstallationsEventProcessor.NetFxInstallationsKey} {item.Profile}"]); | ||
} | ||
} | ||
|
||
[SkippableFact] | ||
public void Process_NetFxInstallationsKeyExist_UnchangedSentryEvent() | ||
{ | ||
Skip.If(Runtime.Current.IsMono(), "Mono not supported."); | ||
|
||
//Arrange | ||
var @event = new SentryEvent(); | ||
var sut = _fixture.GetSut(); | ||
var userBlob = "user blob"; | ||
@event.Contexts[NetFxInstallationsEventProcessor.NetFxInstallationsKey] = userBlob; | ||
|
||
//Act | ||
_ = sut.Process(@event); | ||
|
||
//Assert | ||
Assert.Equal(userBlob, @event.Contexts[NetFxInstallationsEventProcessor.NetFxInstallationsKey]); | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.