-
Notifications
You must be signed in to change notification settings - Fork 568
Fix BuildArchive updates for existing jar entries #11552
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| using System.Collections.Generic; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 💡 Formatting — New files should have Rule: Nullable reference types / File-scoped namespaces |
||
| using System.IO; | ||
| using System.Text; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using NUnit.Framework; | ||
| using Xamarin.Android.Tasks; | ||
| using Xamarin.Tools.Zip; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests | ||
| { | ||
| [TestFixture] | ||
| public class BuildArchiveTests | ||
| { | ||
| string tempDirectory; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Rule: Nullable reference types |
||
|
|
||
| [SetUp] | ||
| public void Setup () | ||
| { | ||
| tempDirectory = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ()); | ||
| Directory.CreateDirectory (tempDirectory); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown () | ||
| { | ||
| Directory.Delete (tempDirectory, recursive: true); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ExistingJavaArchiveEntriesAreUpdated () | ||
| { | ||
| var apk = Path.Combine (tempDirectory, "app.apk"); | ||
| var jar = Path.Combine (tempDirectory, "classes.jar"); | ||
|
|
||
| CreateArchive (apk, ("commonMain/default/manifest", "existing"), ("stale.txt", "stale")); | ||
| CreateArchive (jar, ("commonMain/default/manifest", "current")); | ||
|
|
||
| var item = new TaskItem ($"{jar}#commonMain/default/manifest"); | ||
| item.SetMetadata ("ArchivePath", "commonMain/default/manifest"); | ||
| item.SetMetadata ("JavaArchiveEntry", "commonMain/default/manifest"); | ||
|
|
||
| var task = new BuildArchive { | ||
| BuildEngine = new MockBuildEngine (TestContext.Out), | ||
| ApkOutputPath = apk, | ||
| FilesToAddToArchive = new ITaskItem [] { item }, | ||
| }; | ||
|
|
||
| Assert.IsTrue (task.RunTask (), "task should have succeeded"); | ||
|
|
||
| using (var archive = ZipArchive.Open (apk, FileMode.Open)) { | ||
| archive.AssertEntryContents (apk, "commonMain/default/manifest", "current"); | ||
| archive.AssertDoesNotContainEntry (apk, "stale.txt"); | ||
| } | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 💡 Testing — Consider adding a test for the case where multiple jar items target the same Rule: Test edge cases |
||
| [Test] | ||
| public void ExistingJavaArchiveEntriesAreSkippedWhenUpToDate () | ||
| { | ||
| var apk = Path.Combine (tempDirectory, "app.apk"); | ||
| var jar = Path.Combine (tempDirectory, "classes.jar"); | ||
|
|
||
| CreateArchive (apk, ("commonMain/default/manifest", "current")); | ||
| CreateArchive (jar, ("commonMain/default/manifest", "current")); | ||
|
|
||
| var item = new TaskItem ($"{jar}#commonMain/default/manifest"); | ||
| item.SetMetadata ("ArchivePath", "commonMain/default/manifest"); | ||
| item.SetMetadata ("JavaArchiveEntry", "commonMain/default/manifest"); | ||
| var messages = new List<BuildMessageEventArgs> (); | ||
|
|
||
| var task = new BuildArchive { | ||
| BuildEngine = new MockBuildEngine (TestContext.Out, messages: messages), | ||
| ApkOutputPath = apk, | ||
| FilesToAddToArchive = new ITaskItem [] { item }, | ||
| }; | ||
|
|
||
| Assert.IsTrue (task.RunTask (), "task should have succeeded"); | ||
|
|
||
| Assert.That (messages, Has.Some.Property (nameof (BuildMessageEventArgs.Message)).EqualTo ($"Skipping commonMain/default/manifest from {jar} as it is up to date.")); | ||
|
|
||
| using (var archive = ZipArchive.Open (apk, FileMode.Open)) { | ||
| archive.AssertEntryContents (apk, "commonMain/default/manifest", "current"); | ||
| } | ||
| } | ||
|
|
||
| static void CreateArchive (string path, params (string name, string contents) [] entries) | ||
| { | ||
| using (var stream = File.Create (path)) | ||
| using (var archive = ZipArchive.Create (stream)) { | ||
| foreach (var entry in entries) { | ||
| archive.AddEntry (entry.name, entry.contents, encoding: Encoding.UTF8); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
🤖 💡 Performance — The jar is now opened unconditionally, whereas previously the
apk.ContainsEntrycheck was beforeFile.OpenRead/ZipArchive.Open, allowing duplicates from current-build items to be skipped without touching the jar at all. This is probably fine in practice (OS file cache, rare duplicate path), but worth noting as a trade-off for correctness.Rule: Place cheap checks before expensive ones