Repository navigation
[Xamarin.Android.Build.Tasks] Move Lint to Microsoft.Android.Build.Tasks #13478
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aa1a4d1
[Xamarin.Android.Build.Tasks] Move Lint to Microsoft.Android.Build.Tasks
simonrozsival 6d01b88
[Microsoft.Android.Build.Tasks] Inline Lint regex patterns into [Gene…
simonrozsival b78e16a
[Microsoft.Android.Build.Tasks] Use ArgumentNullException.ThrowIfNull…
simonrozsival c30d8f6
[tests] Normalize fake Lint arguments on Windows
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...icrosoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/Tasks/LintTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Xml.Linq; | ||
| using Microsoft.Build.Framework; | ||
| using NUnit.Framework; | ||
| using Microsoft.Android.Tasks; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests.Tasks | ||
| { | ||
| [TestFixture] | ||
| public class LintTests : BaseTest | ||
| { | ||
| const string TaskName = "Microsoft.Android.Tasks.Lint"; | ||
|
|
||
| class TestableLint : Lint | ||
| { | ||
| public void Feed (string line) => LogEventsFromTextOutput (line, MessageImportance.Normal); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ParsesLintOutput () | ||
| { | ||
| var errors = new List<BuildErrorEventArgs> (); | ||
| var warnings = new List<BuildWarningEventArgs> (); | ||
| var targetDirectory = Path.Combine ("obj", "android"); | ||
| var task = new TestableLint { | ||
| BuildEngine = new MockBuildEngine (TestContext.Out, errors, warnings), | ||
| TargetDirectory = targetDirectory, | ||
| }; | ||
| foreach (var line in new [] { | ||
| "AndroidManifest.xml:19: Warning: <uses-permission> tag appears after <application> tag [ManifestOrder]", | ||
| "<uses-permission android:name=\"android.permission.INTERNET\" />", | ||
| "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", | ||
| "android/src/sdldroidtest/SDLSurface.java:43: Error: Call requires API level 21 (current min is 10): new android.view.SurfaceView [NewApi]", | ||
| "super (p0, p1, p2, p3);", | ||
| "~~~~~", | ||
| "Warning: The resource R.layout.main appears to be unused [UnusedResources]", | ||
| "Warning: The resource R.string.app_name appears to be unused [UnusedResources]", | ||
| "\t^", | ||
| }) { | ||
| task.Feed (line); | ||
| } | ||
|
|
||
| Assert.AreEqual (3, warnings.Count); | ||
| Assert.AreEqual ("XA0102", warnings [0].Code); | ||
| Assert.AreEqual (Path.Combine (targetDirectory, "AndroidManifest.xml"), warnings [0].File); | ||
| Assert.AreEqual (19, warnings [0].LineNumber); | ||
| Assert.AreEqual (1, warnings [0].ColumnNumber); | ||
| Assert.AreEqual (" <uses-permission> tag appears after <application> tag [ManifestOrder]", warnings [0].Message); | ||
| Assert.AreEqual ("XA0102", warnings [1].Code); | ||
| Assert.AreEqual ("The resource R.layout.main appears to be unused [UnusedResources]", warnings [1].Message); | ||
| Assert.AreEqual ("XA0102", warnings [2].Code); | ||
| Assert.AreEqual ("The resource R.string.app_name appears to be unused [UnusedResources]", warnings [2].Message); | ||
|
|
||
| Assert.AreEqual (1, errors.Count); | ||
| Assert.AreEqual ("XA0103", errors [0].Code); | ||
| Assert.AreEqual (Path.Combine (targetDirectory, "android/src/sdldroidtest/SDLSurface.java"), errors [0].File); | ||
| Assert.AreEqual (43, errors [0].LineNumber); | ||
| Assert.AreEqual (0, errors [0].ColumnNumber); | ||
| Assert.AreEqual (" Call requires API level 21 (current min is 10): new android.view.SurfaceView [NewApi]", errors [0].Message); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TaskRegistrationUsesFullAndCorePair () | ||
| { | ||
| var entries = LoadRegistrations (); | ||
| Assert.AreEqual (2, entries.Length); | ||
| Assert.That (entries.Select (e => (string?) e.Attribute ("AssemblyFile")), Is.All.EqualTo ("$(_MicrosoftAndroidBuildTasksAssembly)")); | ||
| Assert.That (entries.Select (e => (string?) e.Attribute ("Runtime")), Is.All.EqualTo ("NET")); | ||
| var full = entries.Single (e => (string?) e.Attribute ("Condition") == " '$(MSBuildRuntimeType)' == 'Full' "); | ||
| var core = entries.Single (e => (string?) e.Attribute ("Condition") == " '$(MSBuildRuntimeType)' == 'Core' "); | ||
| Assert.AreEqual ("TaskHostFactory", (string?) full.Attribute ("TaskFactory")); | ||
| Assert.IsNull (core.Attribute ("TaskFactory")); | ||
| } | ||
|
|
||
| [TestCase (TaskName)] | ||
| [TestCase ("Lint")] | ||
| public void RegisteredTaskNamesExecute (string taskName) | ||
| { | ||
| string directory = Path.Combine (Root, "temp", TestName); | ||
| string toolPath = Path.Combine (directory, "tools"); | ||
| string targetDirectory = Path.Combine (directory, "android project"); | ||
| Directory.CreateDirectory (toolPath); | ||
| Directory.CreateDirectory (targetDirectory); | ||
| CreateFakeLint (toolPath); | ||
|
|
||
| string project = Path.Combine (directory, "lint.proj"); | ||
| new XDocument (new XElement ("Project", | ||
| new XElement ("PropertyGroup", | ||
| new XElement ("_MicrosoftAndroidBuildTasksAssembly", typeof (Lint).Assembly.Location)), | ||
| LoadRegistrations (), | ||
| new XElement ("Target", new XAttribute ("Name", "Build"), | ||
| new XElement (taskName, | ||
| new XAttribute ("ToolPath", toolPath), | ||
| new XAttribute ("TargetDirectory", targetDirectory), | ||
| new XAttribute ("IntermediateOutputPath", directory), | ||
| new XAttribute ("JavaSdkPath", directory))))) | ||
| .Save (project); | ||
|
|
||
| string dotnet = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH") ?? "dotnet"; | ||
| var result = NativeAotBootstrapTestTools.Run (dotnet, "msbuild", project, "-t:Build", "-v:minimal", "-nr:false"); | ||
| string output = result.Output + result.Error; | ||
| Assert.AreEqual (0, result.ExitCode, output); | ||
| // `lint --version` reported 26.0.2, so the issues introduced up to 26.0.2 are disabled. | ||
| StringAssert.Contains ($"warning XA0102: args=--quiet --disable Instantiatable,StaticFieldLeak,UnusedResources,HardcodedDebugMode {targetDirectory}", output); | ||
| StringAssert.IsMatch (@"strings\.xml\(3,\d+\): warning XA0102: Unused resource \[UnusedResources\]", output); | ||
| } | ||
|
|
||
| static void CreateFakeLint (string toolPath) | ||
| { | ||
| if (OperatingSystem.IsWindows ()) { | ||
| File.WriteAllText (Path.Combine (toolPath, "lint.bat"), """ | ||
| @echo off | ||
| if "%~1"=="--version" ( | ||
| echo lint: version 26.0.2 | ||
| exit /b 0 | ||
| ) | ||
| rem Match the shell fixture's $* expansion by removing argument quoting. | ||
| echo Warning: args=%~1 %~2 %~3 %~4 | ||
| echo ^^ | ||
| echo res\values\strings.xml:3: Warning: Unused resource [UnusedResources] | ||
| echo ~~~~ | ||
| """.Replace ("\n", "\r\n")); | ||
| return; | ||
| } | ||
| string lint = Path.Combine (toolPath, "lint"); | ||
| File.WriteAllText (lint, """ | ||
| #!/bin/sh | ||
| if [ "$1" = "--version" ]; then | ||
| echo "lint: version 26.0.2" | ||
| exit 0 | ||
| fi | ||
| echo "Warning: args=$*" | ||
| echo "^" | ||
| echo "res/values/strings.xml:3: Warning: Unused resource [UnusedResources]" | ||
| echo " ~~~~" | ||
| """); | ||
| File.SetUnixFileMode (lint, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); | ||
| } | ||
|
|
||
| static XElement [] LoadRegistrations () | ||
| { | ||
| string commonTargets = Path.Combine (NativeAotBootstrapTestTools.FindRepositoryRoot (), | ||
| "src", "Xamarin.Android.Build.Tasks", "Xamarin.Android.Common.targets"); | ||
| return XDocument.Load (commonTargets).Descendants () | ||
| .Where (element => element.Name.LocalName == "UsingTask" && (string?) element.Attribute ("TaskName") == TaskName) | ||
| .Select (element => { | ||
| var registration = new XElement (element); | ||
| registration.Name = "UsingTask"; | ||
| return registration; | ||
| }) | ||
| .ToArray (); | ||
| } | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Shared.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using Microsoft.Build.Framework; | ||
|
|
||
| namespace Xamarin.Android.Tasks; | ||
|
|
||
| // Helpers compile-linked into both Xamarin.Android.Build.Tasks and Microsoft.Android.Build.Tasks. | ||
| partial class MonoAndroidHelper | ||
| { | ||
| public static int RunProcess (string name, string args, DataReceivedEventHandler onOutput, DataReceivedEventHandler onError, Dictionary<string, string>? environmentVariables = null) | ||
| { | ||
| return RunProcess (TaskEnvironment.Fallback, name, args, onOutput, onError, environmentVariables); | ||
| } | ||
|
|
||
| public static int RunProcess (TaskEnvironment taskEnvironment, string name, string args, DataReceivedEventHandler onOutput, DataReceivedEventHandler onError, Dictionary<string, string>? environmentVariables = null) | ||
| { | ||
| if (taskEnvironment == null) | ||
| throw new ArgumentNullException (nameof (taskEnvironment)); | ||
|
|
||
| var psi = taskEnvironment.GetProcessStartInfo (); | ||
| psi.FileName = name; | ||
| psi.Arguments = args; | ||
| psi.UseShellExecute = false; | ||
| psi.RedirectStandardOutput = true; | ||
| psi.RedirectStandardError = true; | ||
| psi.CreateNoWindow = true; | ||
| psi.WindowStyle = ProcessWindowStyle.Hidden; | ||
| if (environmentVariables != null) { | ||
| foreach (var pair in environmentVariables) { | ||
| psi.EnvironmentVariables [pair.Key] = pair.Value; | ||
| } | ||
| } | ||
| Process p = new Process (); | ||
| p.StartInfo = psi; | ||
|
|
||
| p.OutputDataReceived += onOutput; | ||
| p.ErrorDataReceived += onError; | ||
| p.Start (); | ||
| p.BeginErrorReadLine (); | ||
| p.BeginOutputReadLine (); | ||
| p.WaitForExit (); | ||
| try { | ||
| return p.ExitCode; | ||
| } finally { | ||
| p.Close (); | ||
| } | ||
| } | ||
|
|
||
| public static Dictionary<string, string> ItemGroupToCaseMap (ITaskItem [] items) | ||
| { | ||
| var map = new Dictionary<string, string> (items.Length, StringComparer.Ordinal); | ||
| foreach (var item in items) | ||
| map [item.ItemSpec] = item.GetMetadata ("OriginalName"); | ||
| return map; | ||
| } | ||
|
|
||
| public static string FixUpAndroidResourcePath (string file, string resourceDirectory, string resourceDirectoryFullPath, Dictionary<string, string> resource_name_case_map) | ||
| { | ||
| string? newfile = null; | ||
| if (file.StartsWith (resourceDirectory, StringComparison.InvariantCultureIgnoreCase)) { | ||
| newfile = file.Substring (resourceDirectory.Length).TrimStart (Path.DirectorySeparatorChar); | ||
| } | ||
| if (!resourceDirectoryFullPath.IsNullOrEmpty () && file.StartsWith (resourceDirectoryFullPath, StringComparison.InvariantCultureIgnoreCase)) { | ||
| newfile = file.Substring (resourceDirectoryFullPath.Length).TrimStart (Path.DirectorySeparatorChar); | ||
| } | ||
| if (!newfile.IsNullOrEmpty ()) { | ||
| if (resource_name_case_map.TryGetValue (newfile, out string? value)) | ||
| newfile = value; | ||
| newfile = Path.Combine ("Resources", newfile); | ||
| return newfile; | ||
| } | ||
| return string.Empty; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 💡 Testing — Consider adding a file-less
Error: ...record (followed by a marker or another diagnostic to flush it), then assertingXA0103, an emptyFile, and the expected message. Both file-less records here exercise theWarningalternative of the newly generatedNoFileWarningOrErrorRegEx; the existing error instead exercisesCodeErrorRegEx. This would cover the other alternative without requiring an Android SDK or JDK.{Rule: Test edge cases}