Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] verify trimmer warnings where appropriate #9076

Merged
merged 1 commit into from
Jul 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<!-- For compat with user code not marked trimmable, only trim opt-in by default. -->
<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">full</TrimMode>
<TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' and '$(TrimMode)' == 'full' ">false</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' and ('$(TrimMode)' == 'full' or '$(IsAotCompatible)' == 'true') ">false</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
<!-- Prefer $(RuntimeIdentifiers) plural -->
<RuntimeIdentifiers Condition=" '$(RuntimeIdentifier)' == '' And '$(RuntimeIdentifiers)' == '' ">android-arm;android-arm64;android-x86;android-x64</RuntimeIdentifiers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,48 @@ public void BuildHasNoWarnings (bool isRelease, bool xamarinForms, bool multidex
}
}

[Test]
[TestCase ("", new string [0], false)]
[TestCase ("", new string [0], true)]
[TestCase ("SuppressTrimAnalysisWarnings=false", new string [] { "IL2055" }, true, 2)]
[TestCase ("TrimMode=full", new string [0], false)]
[TestCase ("TrimMode=full", new string [] { "IL2055" }, true, 2)]
[TestCase ("IsAotCompatible=true", new string [] { "IL2055", "IL3050" }, false)]
[TestCase ("IsAotCompatible=true", new string [] { "IL2055", "IL3050" }, true, 3)]
public void BuildHasTrimmerWarnings (string properties, string [] codes, bool isRelease, int? totalWarnings = null)
{
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntimeIdentifier ("arm64-v8a");
proj.MainActivity = proj.DefaultMainActivity
.Replace ("//${FIELDS}", "Type type = typeof (List<>);")
.Replace ("//${AFTER_ONCREATE}", "Console.WriteLine (type.MakeGenericType (typeof (object)));");
proj.SetProperty ("TrimmerSingleWarn", "false");

if (!string.IsNullOrEmpty (properties)) {
foreach (var property in properties.Split (';')) {
int index = property.IndexOf ('=');
if (index != -1) {
proj.SetProperty (property [..index], property [(index + 1)..]);
}
}
}

using var b = CreateApkBuilder (Path.Combine ("temp", TestName));
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");

if (codes.Length == 0) {
b.AssertHasNoWarnings ();
} else {
totalWarnings ??= codes.Length;
Assert.True (StringAssertEx.ContainsText (b.LastBuildOutput, $"{totalWarnings} Warning(s)"), $"Should receive {totalWarnings} warnings");
foreach (var code in codes) {
Assert.True (StringAssertEx.ContainsText (b.LastBuildOutput, code), $"Should receive {code} warning");
}
}
}

[Test]
[TestCase ("AndroidFastDeploymentType", "Assemblies", true, false)]
[TestCase ("AndroidFastDeploymentType", "Assemblies", false, false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace ${ROOT_NAMESPACE}
[Register ("${JAVA_PACKAGENAME}.MainActivity"), Activity (Label = "${PROJECT_NAME}", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
//${FIELDS}
int count = 1;

protected override void OnCreate (Bundle bundle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ${ROOT_NAMESPACE}
[Android.Runtime.Register ("${JAVA_PACKAGENAME}.MainActivity"), Activity (Label = "${PROJECT_NAME}", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
//${FIELDS}
int count = 1;

protected override void OnCreate (Bundle? bundle)
Expand Down