Skip to content

Commit 4e0c97c

Browse files
authored
fix: Validating options before SDK init (#370)
* added options validity check before init * add logger before validate * moved validation right before initializing the sdk * updated CHANGELOG.md * tweaked and added tests * improved tests * clarifying prebuild message
1 parent 069bc00 commit 4e0c97c

File tree

4 files changed

+87
-13
lines changed

4 files changed

+87
-13
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

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

910

Diff for: src/Sentry.Unity/SentryUnity.cs

+14-12
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,30 @@ public static class SentryUnity
1212
/// <summary>
1313
/// Initializes Sentry Unity SDK while configuring the options.
1414
/// </summary>
15-
/// <param name="unitySentryOptionsConfigure">Callback to configure the options.</param>
16-
public static void Init(Action<SentryUnityOptions> unitySentryOptionsConfigure)
15+
/// <param name="sentryUnityOptionsConfigure">Callback to configure the options.</param>
16+
public static void Init(Action<SentryUnityOptions> sentryUnityOptionsConfigure)
1717
{
18-
var unitySentryOptions = new SentryUnityOptions();
19-
SentryOptionsUtility.SetDefaults(unitySentryOptions);
18+
var sentryUnityOptions = new SentryUnityOptions();
19+
SentryOptionsUtility.SetDefaults(sentryUnityOptions);
2020

21-
unitySentryOptionsConfigure.Invoke(unitySentryOptions);
21+
sentryUnityOptionsConfigure.Invoke(sentryUnityOptions);
2222

23-
SentryOptionsUtility.TryAttachLogger(unitySentryOptions);
24-
Init(unitySentryOptions);
23+
SentryOptionsUtility.TryAttachLogger(sentryUnityOptions);
24+
Init(sentryUnityOptions);
2525
}
2626

2727
/// <summary>
2828
/// Initializes Sentry Unity SDK while providing an options object.
2929
/// </summary>
30-
/// <param name="unitySentryOptions">The options object.</param>
30+
/// <param name="sentryUnityOptions">The options object.</param>
3131
[EditorBrowsable(EditorBrowsableState.Never)]
32-
public static void Init(SentryUnityOptions unitySentryOptions)
32+
public static void Init(SentryUnityOptions sentryUnityOptions)
3333
{
34-
unitySentryOptions.DiagnosticLogger?.LogDebug(unitySentryOptions.ToString());
35-
36-
SentrySdk.Init(unitySentryOptions);
34+
if (sentryUnityOptions.ShouldInitializeSdk())
35+
{
36+
sentryUnityOptions.DiagnosticLogger?.LogDebug(sentryUnityOptions.ToString());
37+
SentrySdk.Init(sentryUnityOptions);
38+
}
3739
}
3840
}
3941
}

Diff for: test/Sentry.Unity.Tests/IntegrationTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public IEnumerator Init_OptionsAreDefaulted()
224224
SentryUnityOptions? actualOptions = null;
225225
using var _ = InitSentrySdk(o =>
226226
{
227-
o.Dsn = null; // InitSentrySDK already sets a test dsn
227+
o.Dsn = string.Empty; // InitSentrySDK already sets a test dsn
228228
actualOptions = o;
229229
});
230230

Diff for: test/Sentry.Unity.Tests/SentryUnityTests.cs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using NUnit.Framework;
3+
using UnityEditor;
4+
using UnityEngine;
5+
using UnityEngine.TestTools;
6+
7+
namespace Sentry.Unity.Tests
8+
{
9+
[TestFixture]
10+
public class SentryUnityTests : IPrebuildSetup, IPostBuildCleanup
11+
{
12+
// If an options scriptable object exists Sentry SDK initializes itself on 'BeforeSceneLoad'.
13+
// We check in prebuild if those options exist and are enabled, disable them and restore them on Cleanup
14+
private ScriptableSentryUnityOptions? _optionsToRestore;
15+
16+
public void Setup()
17+
{
18+
var options = AssetDatabase.LoadAssetAtPath(ScriptableSentryUnityOptions.GetConfigPath(ScriptableSentryUnityOptions.ConfigName),
19+
typeof(ScriptableSentryUnityOptions)) as ScriptableSentryUnityOptions;
20+
if (options?.Enabled != true)
21+
{
22+
return;
23+
}
24+
25+
Debug.Log("Disabling local options for the duration of the test.");
26+
_optionsToRestore = options;
27+
_optionsToRestore.Enabled = false;
28+
}
29+
30+
public void Cleanup()
31+
{
32+
if (_optionsToRestore != null)
33+
{
34+
_optionsToRestore.Enabled = true;
35+
}
36+
}
37+
38+
[TearDown]
39+
public void TearDown()
40+
{
41+
if (SentrySdk.IsEnabled)
42+
{
43+
SentrySdk.Close();
44+
}
45+
}
46+
47+
[Test]
48+
public void SentryUnity_OptionsValid_Initializes()
49+
{
50+
var options = new SentryUnityOptions();
51+
SentryOptionsUtility.SetDefaults(options);
52+
options.Dsn = "https://[email protected]/5439417";
53+
54+
SentryUnity.Init(options);
55+
56+
Assert.IsTrue(SentrySdk.IsEnabled);
57+
}
58+
59+
[Test]
60+
public void SentryUnity_OptionsInvalid_DoesNotInitialize()
61+
{
62+
var options = new SentryUnityOptions();
63+
SentryOptionsUtility.SetDefaults(options);
64+
65+
// Even tho the defaults are set the DSN is missing making the options invalid for initialization
66+
SentryUnity.Init(options);
67+
68+
Assert.IsFalse(SentrySdk.IsEnabled);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)