Skip to content

fix: Validating options before SDK init #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- CaptureInEditor flag fixed for programmatic initialization ([#370](https://github.com/getsentry/sentry-unity/pull/370))
- Preventing numeric options to be set negative in the editor window ([#364](https://github.com/getsentry/sentry-unity/pull/364))


Expand Down
26 changes: 14 additions & 12 deletions src/Sentry.Unity/SentryUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@ public static class SentryUnity
/// <summary>
/// Initializes Sentry Unity SDK while configuring the options.
/// </summary>
/// <param name="unitySentryOptionsConfigure">Callback to configure the options.</param>
public static void Init(Action<SentryUnityOptions> unitySentryOptionsConfigure)
/// <param name="sentryUnityOptionsConfigure">Callback to configure the options.</param>
public static void Init(Action<SentryUnityOptions> sentryUnityOptionsConfigure)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this is a breaking change if someone is using named parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually a very good point. Thanks for pointing it out. Did not consider that at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good to keep in mind. But since we're on 0.x version and adoption is still low, we can go ahead with it here

{
var unitySentryOptions = new SentryUnityOptions();
SentryOptionsUtility.SetDefaults(unitySentryOptions);
var sentryUnityOptions = new SentryUnityOptions();
SentryOptionsUtility.SetDefaults(sentryUnityOptions);

unitySentryOptionsConfigure.Invoke(unitySentryOptions);
sentryUnityOptionsConfigure.Invoke(sentryUnityOptions);

SentryOptionsUtility.TryAttachLogger(unitySentryOptions);
Init(unitySentryOptions);
SentryOptionsUtility.TryAttachLogger(sentryUnityOptions);
Init(sentryUnityOptions);
}

/// <summary>
/// Initializes Sentry Unity SDK while providing an options object.
/// </summary>
/// <param name="unitySentryOptions">The options object.</param>
/// <param name="sentryUnityOptions">The options object.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Init(SentryUnityOptions unitySentryOptions)
public static void Init(SentryUnityOptions sentryUnityOptions)
{
unitySentryOptions.DiagnosticLogger?.LogDebug(unitySentryOptions.ToString());

SentrySdk.Init(unitySentryOptions);
if (sentryUnityOptions.ShouldInitializeSdk())
{
sentryUnityOptions.DiagnosticLogger?.LogDebug(sentryUnityOptions.ToString());
SentrySdk.Init(sentryUnityOptions);
}
}
}
}
2 changes: 1 addition & 1 deletion test/Sentry.Unity.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public IEnumerator Init_OptionsAreDefaulted()
SentryUnityOptions? actualOptions = null;
using var _ = InitSentrySdk(o =>
{
o.Dsn = null; // InitSentrySDK already sets a test dsn
o.Dsn = string.Empty; // InitSentrySDK already sets a test dsn
actualOptions = o;
});

Expand Down
71 changes: 71 additions & 0 deletions test/Sentry.Unity.Tests/SentryUnityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;

namespace Sentry.Unity.Tests
{
[TestFixture]
public class SentryUnityTests : IPrebuildSetup, IPostBuildCleanup
{
// If an options scriptable object exists Sentry SDK initializes itself on 'BeforeSceneLoad'.
// We check in prebuild if those options exist and are enabled, disable them and restore them on Cleanup
private ScriptableSentryUnityOptions? _optionsToRestore;

public void Setup()
{
var options = AssetDatabase.LoadAssetAtPath(ScriptableSentryUnityOptions.GetConfigPath(ScriptableSentryUnityOptions.ConfigName),
typeof(ScriptableSentryUnityOptions)) as ScriptableSentryUnityOptions;
if (options?.Enabled != true)
{
return;
}

Debug.Log("Disabling local options for the duration of the test.");
_optionsToRestore = options;
_optionsToRestore.Enabled = false;
}

public void Cleanup()
{
if (_optionsToRestore != null)
{
_optionsToRestore.Enabled = true;
}
}

[TearDown]
public void TearDown()
{
if (SentrySdk.IsEnabled)
{
SentrySdk.Close();
}
}

[Test]
public void SentryUnity_OptionsValid_Initializes()
{
var options = new SentryUnityOptions();
SentryOptionsUtility.SetDefaults(options);
options.Dsn = "https://[email protected]/5439417";

SentryUnity.Init(options);

Assert.IsTrue(SentrySdk.IsEnabled);
}

[Test]
public void SentryUnity_OptionsInvalid_DoesNotInitialize()
{
var options = new SentryUnityOptions();
SentryOptionsUtility.SetDefaults(options);

// Even tho the defaults are set the DSN is missing making the options invalid for initialization
SentryUnity.Init(options);

Assert.IsFalse(SentrySdk.IsEnabled);
}
}
}