Skip to content

Commit

Permalink
fix: Added check for Sentry.framework copy success (#496)
Browse files Browse the repository at this point in the history
* check if file got copied

* added tests

* fixed expected exception

* Format code

* test cleanup

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
  • Loading branch information
bitsandfoxes and getsentry-bot authored Jan 10, 2022
1 parent ccf4fa7 commit 3fd4f4c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Sentry.Unity.Editor.iOS/BuildPostProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public static void OnPostProcessBuild(BuildTarget target, string pathToProject)

try
{
CopyFrameworkToBuildDirectory(pathToProject, options?.DiagnosticLogger);
var frameworkDirectory = PlayerSettings.iOS.sdkVersion == iOSSdkVersion.DeviceSDK ? "Device" : "Simulator";
var pathToFramework = Path.GetFullPath(Path.Combine("Packages", SentryPackageInfo.GetName(), "Plugins", "iOS", frameworkDirectory, "Sentry.framework"));

CopyFrameworkToBuildDirectory(pathToProject, pathToFramework, options?.DiagnosticLogger);

using var sentryXcodeProject = SentryXcodeProject.Open(pathToProject);
sentryXcodeProject.AddSentryFramework();
Expand Down Expand Up @@ -73,9 +76,9 @@ public static void OnPostProcessBuild(BuildTarget target, string pathToProject)
}
}

private static void CopyFrameworkToBuildDirectory(string pathToProject, IDiagnosticLogger? logger)
internal static void CopyFrameworkToBuildDirectory(string pathToXcodeProject, string pathToSentryFramework, IDiagnosticLogger? logger)
{
var targetPath = Path.Combine(pathToProject, "Frameworks", "Sentry.framework");
var targetPath = Path.Combine(pathToXcodeProject, "Frameworks", "Sentry.framework");
if (Directory.Exists(targetPath))
{
// If the target path already exists we can bail. Unity doesn't allow an appending builds when switching
Expand All @@ -84,20 +87,17 @@ private static void CopyFrameworkToBuildDirectory(string pathToProject, IDiagnos
return;
}

var packageName = SentryPackageInfo.GetName();
var frameworkDirectory = PlayerSettings.iOS.sdkVersion == iOSSdkVersion.DeviceSDK ? "Device" : "Simulator";

var frameworkPath = Path.GetFullPath(Path.Combine("Packages", packageName, "Plugins", "iOS", frameworkDirectory, "Sentry.framework"));
if (Directory.Exists(frameworkPath))
if (Directory.Exists(pathToSentryFramework))
{
logger?.LogDebug("Copying Sentry.framework from '{0}' to '{1}'", frameworkPath, targetPath);
logger?.LogDebug("Copying 'Sentry.framework' from '{0}' to '{1}'", pathToSentryFramework, targetPath);

Directory.CreateDirectory(Path.Combine(pathToProject, "Frameworks"));
FileUtil.CopyFileOrDirectoryFollowSymlinks(frameworkPath, targetPath);
Directory.CreateDirectory(Path.Combine(pathToXcodeProject, "Frameworks"));
FileUtil.CopyFileOrDirectory(pathToSentryFramework, targetPath);
}
else

if (!Directory.Exists(targetPath))
{
throw new FileNotFoundException($"Failed to copy 'Sentry.framework' from '{frameworkPath}' to Xcode project");
throw new FileNotFoundException($"Failed to copy 'Sentry.framework' from '{pathToSentryFramework}' to Xcode project {targetPath}");
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions test/Sentry.Unity.Editor.iOS.Tests/BuildPostProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Sentry.Unity.Tests.SharedClasses;

namespace Sentry.Unity.Editor.iOS.Tests
{
public class BuildPostProcessorTests
{
private string _testDirectoryPath = null!;
private string _sentryFrameworkPath = null!;
private string _xcodeProjectPath = null!;

[SetUp]
public void Setup()
{
_testDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString().Substring(0, 4));
_sentryFrameworkPath = Path.Combine(_testDirectoryPath, "Sentry.framework");
_xcodeProjectPath = Path.Combine(_testDirectoryPath, "XcodeProject");

Directory.CreateDirectory(_testDirectoryPath);
Directory.CreateDirectory(_sentryFrameworkPath);
Directory.CreateDirectory(_xcodeProjectPath);
}

[TearDown]
public void TearDown() => Directory.Delete(_testDirectoryPath, true);

[Test]
public void CopyFrameworkToBuildDirectory_CopiesFramework()
{
BuildPostProcess.CopyFrameworkToBuildDirectory(_xcodeProjectPath, _sentryFrameworkPath, new TestLogger());

Assert.IsTrue(Directory.Exists(Path.Combine(_xcodeProjectPath, "Frameworks", "Sentry.framework")));
}

[Test]
public void CopyFrameworkToBuildDirectory_FrameworkAlreadyCopied_LogsSkipMessage()
{
var testLogger = new TestLogger();

BuildPostProcess.CopyFrameworkToBuildDirectory(_xcodeProjectPath, _sentryFrameworkPath, testLogger);
BuildPostProcess.CopyFrameworkToBuildDirectory(_xcodeProjectPath, _sentryFrameworkPath, testLogger);

Assert.IsTrue(testLogger.Logs.Any(log =>
log.logLevel == SentryLevel.Debug &&
log.message.Contains("'Sentry.framework' has already copied")));
}

[Test]
public void CopyFrameworkToBuildDirectory_FailedToCopyFramework_ThrowsFileNotFoundException() =>
Assert.Throws<FileNotFoundException>(() =>
BuildPostProcess.CopyFrameworkToBuildDirectory(_xcodeProjectPath, "non-existent-path", new TestLogger()));
}
}

0 comments on commit 3fd4f4c

Please sign in to comment.