xamarin-macios/tests/dotnet/UnitTests/RegistrarTest.cs

55 строки
2.0 KiB
C#

namespace Xamarin.Tests {
[TestFixture]
public class RegistrarTest : TestBaseClass {
[TestCase (ApplePlatform.MacCatalyst, true)]
[TestCase (ApplePlatform.MacOSX, true)]
[TestCase (ApplePlatform.iOS, false)]
[TestCase (ApplePlatform.TVOS, false)]
public void InvalidStaticRegistrarValidation (ApplePlatform platform, bool validated)
{
var project = "MyRegistrarApp";
var configuration = "Debug";
var runtimeIdentifiers = GetDefaultRuntimeIdentifier (platform);
Configuration.IgnoreIfIgnoredPlatform (platform);
var projectPath = GetProjectPath (project, platform: platform);
Clean (projectPath);
var properties = GetDefaultProperties ();
properties ["Registrar"] = "static";
// enable the linker (so that the main assembly is modified)
properties ["LinkMode"] = "full";
properties ["MtouchLink"] = "full";
DotNet.AssertBuild (projectPath, properties);
var appDir = GetAppPath (projectPath, platform, runtimeIdentifiers, configuration);
var asmDir = Path.Combine (appDir, GetRelativeAssemblyDirectory (platform));
var appExecutable = Path.Combine (asmDir, project + ".dll");
// Save the first version of the main assembly in memory
var firstAssembly = File.ReadAllBytes (appExecutable);
// Build again, including additional code
properties ["AdditionalDefineConstants"] = "INCLUDED_ADDITIONAL_CODE";
DotNet.AssertBuild (projectPath, properties);
// Revert to the original version of the main assembly
File.WriteAllBytes (appExecutable, firstAssembly);
Environment.SetEnvironmentVariable ("XAMARIN_VALIDATE_STATIC_REGISTRAR_CODE", "1");
try {
if (validated) {
ExecuteProjectWithMagicWordAndAssert (projectPath, platform, runtimeIdentifiers);
} else if (CanExecute (platform, runtimeIdentifiers)) {
var rv = base.Execute (GetNativeExecutable (platform, appDir), out var output, out _);
Assert.AreEqual (1, rv.ExitCode, "Expected no validation");
}
} finally {
Environment.SetEnvironmentVariable ("XAMARIN_VALIDATE_STATIC_REGISTRAR_CODE", null);
}
}
}
}