Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Unity.Android.Logcat;
using UnityEditor;
using UnityEditor.Compilation;
using Assembly = System.Reflection.Assembly;

class AndroidLogcatNetTests
{
Expand Down Expand Up @@ -34,35 +35,69 @@ public void ValidateAssemblyReferences()
{
var logcatAssembly = GetLogcatAssembly();

static bool IsFrameworkReference(string name) =>
name == "mscorlib" || name == "netstandard" || name == "System" || name.StartsWith("System.", StringComparison.Ordinal);

var expectedReferences = new List<string>(new[]
{
"mscorlib",
"System",
"System.Xml.Linq",
"UnityEngine.IMGUIModule",
"UnityEngine.CoreModule",
"UnityEngine.VideoModule",
"UnityEngine.TextRenderingModule",
"UnityEngine.UIElementsModule",
"System.Core",
"UnityEngine.ImageConversionModule",
"UnityEngine.JSONSerializeModule",
"UnityEditor.CoreModule",
"UnityEditor.CoreModule"
});
#if UNITY_6000_1_OR_NEWER
// While running tests in Yamato in logcat package, the active build target is Android
// But in unity/unity it's not Android...
// That's why we expect this reference conditionally
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
{
expectedReferences.AddRange(new[]
{
"UnityEditor.Android.Extensions"
});
};
#endif
#if UNITY_6000_5_OR_NEWER
expectedReferences.AddRange(new[]
{
"Unity.Scripting"
});
#endif

#if UNITY_6000_7_OR_NEWER
expectedReferences.AddRange(new[]
{
"UnityEngine.ScriptingModule",
"UnityEngine.UICommonModule",
});
#endif

var referencedCount = expectedReferences.ToDictionary(s => s, s => 0);

var references = Assembly.ReflectionOnlyLoadFrom(logcatAssembly.outputPath).GetReferencedAssemblies().Select(a => a.Name);
foreach (var r in references)
// ReflectionOnlyLoadFrom is unsupported on CoreCLR; inspect the loaded assembly instead.

var references = typeof(AndroidLogcatConsoleWindow).Assembly.GetReferencedAssemblies().Select(a => a.Name);
var errors = new StringBuilder();
Console.WriteLine($"Logcat package references:\n{string.Join("\n", references)}");
foreach (var r in references.Where(n => !IsFrameworkReference(n)))
{
Assert.Contains(r, expectedReferences, $"Unexpected reference '{r}'");
referencedCount[r]++;
if (!expectedReferences.Contains(r))
errors.AppendLine($"Unexpected reference '{r}'");
else
referencedCount[r]++;
}

foreach (var r in referencedCount)
{
Assert.AreEqual(1, r.Value, $"'{r.Key}' was expected to be referenced once, but was referenced {r.Value} times, please adjust expectations, maybe the reference is no longer needed?");
if (r.Value != 1)
errors.AppendLine($"'{r.Key}' was expected to be referenced once, but was referenced {r.Value} times, please adjust expectations, maybe the reference is no longer needed?");
}

Assert.AreEqual(0, errors.Length, errors.ToString());
}

/// <summary>
Expand All @@ -74,13 +109,21 @@ public void ValidateAssemblyReferences()
public void ValidateUsingStatements()
{
var logcatAssembly = GetLogcatAssembly();
var usingStatementRegex = new Regex(@"using\s+(?<RootNamespace>[a-zA-Z]+)\.*\S*;");
var usingStatementRegex = new Regex(@"using\s+(?<Namespace>(?<RootNamespace>[a-zA-Z]+)\.*\S*);");

var expectedNamespaces = new[]
// Any namespace under these roots is allowed
var expectedRootNamespaces = new[]
{
"UnityEditor",
"UnityEngine",
"System"
"System",
};

// Only these exact namespaces are allowed
var expectedNamespaces = new[]
{
"Unity.Profiling",
"Unity.Scripting.LifecycleManagement",
};

var errors = new StringBuilder();
Expand All @@ -90,10 +133,11 @@ public void ValidateUsingStatements()
var result = usingStatementRegex.Matches(contents);
foreach (Match r in result)
{
var namezpace = r.Groups["RootNamespace"].Value;
if (!expectedNamespaces.Contains(namezpace))
var rootNamespace = r.Groups["RootNamespace"].Value;
var namezpace = r.Groups["Namespace"].Value;
if (!expectedRootNamespaces.Contains(rootNamespace) && !expectedNamespaces.Contains(namezpace))
{
errors.AppendLine($"Unexpected 'using {namezpace}...;' in {file}");
errors.AppendLine($"Unexpected 'using {namezpace};' in {file}");
}
}
}
Expand Down
Loading