Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Values set in a `SentryCliOptionsConfiguration`, such as an auth token read from an environment variable, no longer get saved into `SentryCliOptions.asset` by the Sentry window after a build. The configuration now runs on a copy of the options. ([#2927](https://github.com/getsentry/sentry-unity/pull/2927))

## 4.12.0

### Behavioral Changes
Expand Down
16 changes: 13 additions & 3 deletions src/Sentry.Unity.Editor/SentryScriptableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ internal static T CreateOrLoad<T>(string path) where T : ScriptableObject

internal static T? Load<T>(string path) where T : ScriptableObject => AssetDatabase.LoadAssetAtPath<T>(path);

public static SentryCliOptions? LoadCliOptions()
public static SentryCliOptions? LoadCliOptions() => LoadCliOptions(SentryCliOptions.GetConfigPath());

internal static SentryCliOptions? LoadCliOptions(string path)
{
var cliOptions = Load<SentryCliOptions>(SentryCliOptions.GetConfigPath());
cliOptions?.CliOptionsConfiguration?.Configure(cliOptions);
var savedCliOptions = Load<SentryCliOptions>(path);
if (savedCliOptions == null)
{
return null;
}

// The configuration runs on a copy. The asset is the instance the Sentry window edits and saves, so values
// set in code, such as an auth token read from the environment, would otherwise get written to disk.
var cliOptions = Object.Instantiate(savedCliOptions);
cliOptions.CliOptionsConfiguration?.Configure(cliOptions);

return cliOptions;
}
Expand Down
27 changes: 27 additions & 0 deletions test/Sentry.Unity.Editor.Tests/SentryScriptableObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,31 @@ public void Load_SentryCliOptionsExist_LoadsSavedOptionsAsset()
Assert.NotNull(actualOptions);
Assert.AreEqual(expectedAuth, actualOptions!.Auth);
}

[Test]
public void LoadCliOptions_ConfigurationSetsAuth_DoesNotChangeSavedAsset()
{
var savedOptions = SentryScriptableObject.CreateOrLoad<SentryCliOptions>(_assetPath);
savedOptions.CliOptionsConfiguration = ScriptableObject.CreateInstance<AuthFromCodeConfiguration>();

var cliOptions = SentryScriptableObject.LoadCliOptions(_assetPath);

Assert.NotNull(cliOptions);
Assert.AreEqual(AuthFromCodeConfiguration.Auth, cliOptions!.Auth);
Assert.IsNull(savedOptions.Auth);
Assert.IsNull(SentryScriptableObject.Load<SentryCliOptions>(_assetPath)!.Auth);
}

[Test]
public void LoadCliOptions_OptionsAssetDoesNotExist_ReturnsNull()
{
Assert.IsNull(SentryScriptableObject.LoadCliOptions(_assetPath));
}

private class AuthFromCodeConfiguration : SentryCliOptionsConfiguration
{
public const string Auth = "auth_from_code";

public override void Configure(SentryCliOptions cliOptions) => cliOptions.Auth = Auth;
}
}
Loading