Skip to content

Commit d4343f9

Browse files
jonpryorjonathanpeppers
authored andcommitted
[tests] InstallAndroidDependenciesTest uses platform-tools 34.0.1 (#7873)
Context: bec42ef Context: 9752257 Context: … Context: https://dl-ssl.google.com/android/repository/repository2-3.xml Whenever Google updates their `repository2-3.xml`, there is a chance that our `AndroidDependenciesTests.InstallAndroidDependenciesTest()` unit test will fail, as `repository2-3.xml` containsn *only one* platform-tools package version, and when that changes, out test breaks. Tracking down the cause of this breakage is annoying, usually because when this happens we've forgotten that platform-tools package version changes are the primary reason `InstallAndroidDependencies()` fails. 😅 Update `AndroidDependenciesTests.InstallAndroidDependenciesTest()` to set `$(AndroidSdkPlatformToolsVersion)`=34.0.1, fixing the test, *and also* update the test so that when the wrong `$(_AndroidSdkDirectory)` value is found, we read the *current version* of the platform-tools package from `repository2-3.xml` and include that information in our assertion message.
1 parent 3b5409c commit d4343f9

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AndroidDependenciesTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Xml;
56
using System.Xml.Linq;
67
using NUnit.Framework;
78
using Xamarin.Android.Tools;
@@ -31,6 +32,7 @@ public void InstallAndroidDependenciesTest ()
3132
var proj = new XamarinAndroidApplicationProject {
3233
TargetSdkVersion = apiLevel.ToString (),
3334
};
35+
const string ExpectedPlatformToolsVersion = "34.0.1";
3436
using (var b = CreateApkBuilder ()) {
3537
b.CleanupAfterSuccessfulBuild = false;
3638
string defaultTarget = b.Target;
@@ -39,19 +41,50 @@ public void InstallAndroidDependenciesTest ()
3941
Assert.IsTrue (b.Build (proj, parameters: new string [] {
4042
"AcceptAndroidSDKLicenses=true",
4143
"AndroidManifestType=GoogleV2", // Need GoogleV2 so we can install API-32
42-
"AndroidSdkPlatformToolsVersion=34.0.0",
44+
$"AndroidSdkPlatformToolsVersion={ExpectedPlatformToolsVersion}",
4345
}), "InstallAndroidDependencies should have succeeded.");
4446
b.Target = defaultTarget;
4547
b.BuildLogFile = "build.log";
4648
Assert.IsTrue (b.Build (proj, true), "build should have succeeded.");
47-
Assert.IsTrue (b.LastBuildOutput.ContainsText ($"Output Property: _AndroidSdkDirectory={sdkPath}"), $"_AndroidSdkDirectory was not set to new SDK path `{sdkPath}`.");
49+
bool usedNewDir = b.LastBuildOutput.ContainsText ($"Output Property: _AndroidSdkDirectory={sdkPath}");
50+
if (!usedNewDir) {
51+
// Is this because the platform-tools version changed (again?!)
52+
try {
53+
var currentPlatformToolsVersion = GetCurrentPlatformToolsVersion ();
54+
if (currentPlatformToolsVersion != ExpectedPlatformToolsVersion) {
55+
Assert.Fail ($"_AndroidSdkDirectory not set to new SDK path `{sdkPath}`, *probably* because Google's repository has a newer platform-tools package! " +
56+
$"repository2-3.xml contains platform-tools {currentPlatformToolsVersion}; expected {ExpectedPlatformToolsVersion}!");
57+
}
58+
}
59+
catch (Exception e) {
60+
TestContext.WriteLine ($"Could not extract platform-tools version from repository2-3.xml: {e}");
61+
}
62+
}
63+
Assert.IsTrue (usedNewDir, $"_AndroidSdkDirectory was not set to new SDK path `{sdkPath}`.");
4864
Assert.IsTrue (b.LastBuildOutput.ContainsText ($"JavaPlatformJarPath={sdkPath}"), $"JavaPlatformJarPath did not contain new SDK path `{sdkPath}`.");
4965
}
5066
} finally {
5167
Environment.SetEnvironmentVariable ("TEST_ANDROID_SDK_PATH", old);
5268
}
5369
}
5470

71+
static string GetCurrentPlatformToolsVersion ()
72+
{
73+
var s = new XmlReaderSettings {
74+
XmlResolver = null,
75+
};
76+
var r = XmlReader.Create ("https://dl-ssl.google.com/android/repository/repository2-3.xml", s);
77+
var d = XDocument.Load (r);
78+
79+
var platformToolsPackage = d.Root.Elements ("remotePackage")
80+
.Where (e => "platform-tools" == (string) e.Attribute("path"))
81+
.FirstOrDefault ();
82+
83+
var revision = platformToolsPackage.Element ("revision");
84+
85+
return $"{revision.Element ("major")}.{revision.Element ("minor")}.{revision.Element ("micro")}";
86+
}
87+
5588
[Test]
5689
[TestCase ("AotAssemblies", false)]
5790
[TestCase ("AndroidEnableProfiledAot", false)]

0 commit comments

Comments
 (0)