Skip to content

Commit

Permalink
fix: default env and version (#199)
Browse files Browse the repository at this point in the history
* fix: default env and version

* Changelog
  • Loading branch information
bruno-garcia authored May 27, 2021
1 parent 20b6636 commit b21203b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- Check/create directory before saving ([#196](https://github.com/getsentry/sentry-unity/pull/196))
- Exclude SentryOptions.json from release package ([#195](https://github.com/getsentry/sentry-unity/pull/195))
- default env and version ([#199](https://github.com/getsentry/sentry-unity/pull/199))

## 0.0.14

Expand Down
17 changes: 17 additions & 0 deletions src/Sentry.Unity/SentryUnity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using UnityEngine;

namespace Sentry.Unity
{
Expand Down Expand Up @@ -27,6 +28,22 @@ public static void Init(Action<SentryUnityOptions> unitySentryOptionsConfigure)
public static void Init(SentryUnityOptions unitySentryOptions)
{
unitySentryOptions.TryAttachLogger();

// Uses the game `version` as Release unless the user defined one via the Options
if (unitySentryOptions.Release == null)
{
unitySentryOptions.Release = Application.version;
unitySentryOptions.DiagnosticLogger?.Log(SentryLevel.Debug,
"Setting Sentry Release to Unity App.Version: {0}",
null, unitySentryOptions.Release);
}

unitySentryOptions.Environment = unitySentryOptions.Environment is { } environment
? environment
: Application.isEditor // TODO: Should we move it out and use via IApplication something?
? "editor"
: "production";

SentrySdk.Init(unitySentryOptions);
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/Sentry.Unity/SentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,6 @@ public SentryUnityOptions()
// IL2CPP doesn't support Process.GetCurrentProcess().StartupTime
DetectStartupTime = StartupTimeDetectionMode.Fast;

// Uses the game `version` as Release unless the user defined one via the Options
Release ??= Application.version; // TODO: Should we move it out and use via IApplication something?

Environment = Environment is { } environment
? environment
: Application.isEditor // TODO: Should we move it out and use via IApplication something?
? "editor"
: "production";

this.AddInAppExclude("UnityEngine");
this.AddInAppExclude("UnityEditor");
this.AddEventProcessor(new UnityEventProcessor());
Expand Down
43 changes: 43 additions & 0 deletions test/Sentry.Unity.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Sentry.Protocol;
using Sentry.Unity.Integrations;
using Sentry.Unity.Tests.TestBehaviours;
using UnityEngine;
Expand Down Expand Up @@ -37,6 +39,47 @@ public IEnumerator BugFarmScene_ObjectCreatedWithExceptionLogicAndCalled_OneEven
Assert.AreEqual(1, testEventCapture.Events.Count);
}

[UnityTest]
public IEnumerator BugFarmScene_EventCaptured_IncludesApplicationVersionAsRelease()
{
yield return SetupSceneCoroutine("1_BugFarmScene");

// arrange
var testEventCapture = new TestEventCapture();
using var _ = InitSentrySdk(o =>
{
o.AddIntegration(new UnityApplicationLoggingIntegration(eventCapture: testEventCapture));
});
var testBehaviour = new GameObject("TestHolder").AddComponent<TestMonoBehaviour>();

testBehaviour.gameObject.SendMessage(nameof(testBehaviour.TestException));

// assert
Assert.AreEqual(Application.version, testEventCapture.Events.First().Release);
}

[UnityTest]
public IEnumerator BugFarmScene_EventCaptured_IncludesApplicationInEditorOrProduction()
{
yield return SetupSceneCoroutine("1_BugFarmScene");

// arrange
var testEventCapture = new TestEventCapture();
using var _ = InitSentrySdk(o =>
{
o.AddIntegration(new UnityApplicationLoggingIntegration(eventCapture: testEventCapture));
});
var testBehaviour = new GameObject("TestHolder").AddComponent<TestMonoBehaviour>();

testBehaviour.gameObject.SendMessage(nameof(testBehaviour.TestException));

var actual = testEventCapture.Events.First();
Assert.AreEqual(Application.isEditor
? "editor"
: "production",
actual.Environment);
}

private static IEnumerator SetupSceneCoroutine(string sceneName)
{
// load scene with initialized Sentry, SceneManager.LoadSceneAsync(sceneName);
Expand Down
6 changes: 6 additions & 0 deletions test/Sentry.Unity.Tests/UnitySentryOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public void Options_WriteRead_Equals()
AssertOptions(optionsActual, optionsExpected);
}

[Test]
public void Ctor_Release_IsNull() => Assert.IsNull(new SentryUnityOptions().Release);

[Test]
public void Ctor_Environment_IsNull() => Assert.IsNull(new SentryUnityOptions().Environment);

private static void AssertOptions(SentryUnityOptions actual, SentryUnityOptions expected)
{
Assert.AreEqual(expected.Enabled, actual.Enabled);
Expand Down

0 comments on commit b21203b

Please sign in to comment.