Skip to content

Commit c7222bc

Browse files
dellis1972jonpryor
authored andcommitted
[Xamarin.Android.Build.Tests] Fix DesignTimeBuild Test (#711)
The test was using the global cache to store the downloaded files. As a result if we get multiple tests/commits building at the same time we end up deleting files half way through a test run. So lets make use of a local cache for the test by using the `XAMARIN_CACHEPATH` environment variable.
1 parent b0af9cc commit c7222bc

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using Xamarin.ProjectTools;
33
using NUnit.Framework;
44
using System.Linq;
@@ -37,14 +37,19 @@ public void RepetitiveBuild ()
3737
[Test]
3838
public void DesignTimeBuild ([Values(false, true)] bool isRelease)
3939
{
40+
var path = Path.Combine (Root, "temp", $"DesignTimeBuild_{isRelease}");
41+
var cachePath = Path.Combine (path, "Cache");
42+
var envVar = new Dictionary<string, string> () {
43+
{ "XAMARIN_CACHEPATH", cachePath },
44+
};
4045
var url = "http://dl-ssl.google.com/android/repository/build-tools_r24-macosx.zip";
4146
var md5 = MD5.Create ();
4247
var hash = string.Concat (md5.ComputeHash (Encoding.UTF8.GetBytes (url)).Select (b => b.ToString ("X02")));
43-
var zipPath = Path.Combine (CachePath, "zips", $"{hash}.zip");
48+
var zipPath = Path.Combine (cachePath, "zips", $"{hash}.zip");
4449
if (File.Exists (zipPath))
4550
File.Delete (zipPath);
4651

47-
var extractedDir = Path.Combine (CachePath, "Lib1");
52+
var extractedDir = Path.Combine (cachePath, "Lib1");
4853
if (Directory.Exists (extractedDir))
4954
Directory.Delete (extractedDir, recursive: true);
5055

@@ -65,19 +70,24 @@ public void DesignTimeBuild ([Values(false, true)] bool isRelease)
6570
new BuildItem.ProjectReference (@"..\Lib1\Lib1.csproj", lib.ProjectName, lib.ProjectGuid),
6671
},
6772
};
68-
var path = Path.Combine (Root, "temp", $"DesignTimeBuild_{isRelease}");
73+
6974
using (var l = CreateDllBuilder (Path.Combine (path, lib.ProjectName))) {
7075
using (var b = CreateApkBuilder (Path.Combine (path, proj.ProjectName))) {
7176
l.Verbosity = LoggerVerbosity.Diagnostic;
77+
Assert.IsTrue(l.Clean(lib), "Lib1 should have cleaned successfully");
7278
Assert.IsTrue (l.Build (lib), "Lib1 should have built successfully");
7379
b.Verbosity = LoggerVerbosity.Diagnostic;
7480
b.ThrowOnBuildFailure = false;
75-
Assert.IsTrue (b.UpdateAndroidResources (proj, parameters: new string [] { "DesignTimeBuild=true" }),
81+
Assert.IsTrue (b.Clean(proj), "App should have cleaned successfully");
82+
Assert.IsTrue (b.UpdateAndroidResources (proj, doNotCleanupOnUpdate: true, parameters: new string [] { "DesignTimeBuild=true" }, environmentVariables: envVar),
7683
"first build failed");
7784
Assert.IsTrue (b.LastBuildOutput.Contains ("Skipping download of "),
7885
"failed to skip the downloading of files.");
79-
Assert.IsTrue (b.Build (proj), "second build failed");
80-
Assert.IsTrue (File.Exists (zipPath), $"Zip should have been downloaded to {zipPath}");
86+
WaitFor (1000);
87+
b.Target = "Build";
88+
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true, parameters: new string [] { "DesignTimeBuild=false" }, environmentVariables: envVar), "second build failed");
89+
Assert.IsFalse(b.Output.IsTargetSkipped ("_BuildAdditionalResourcesCache"), "_BuildAdditionalResourcesCache should have run.");
90+
Assert.IsTrue (b.LastBuildOutput.Contains($"Downloading {url}") || b.LastBuildOutput.Contains ($"reusing existing archive: {zipPath}"), $"{url} should have been downloaded.");
8191
Assert.IsTrue (File.Exists (Path.Combine (extractedDir, "1", "content", "android-N", "aapt")), $"Files should have been extracted to {extractedDir}");
8292
}
8393
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Xamarin.ProjectTools;
66
using NUnit.Framework;
77
using System.Linq;
8+
using System.Threading;
89

910
namespace Xamarin.Android.Build.Tests
1011
{
@@ -51,6 +52,12 @@ public string Root {
5152
}
5253
}
5354

55+
protected void WaitFor(int milliseconds)
56+
{
57+
var pause = new ManualResetEvent(false);
58+
pause.WaitOne(milliseconds);
59+
}
60+
5461
protected static string RunAdbCommand (string command, bool ignoreErrors = true)
5562
{
5663
string ext = Environment.OSVersion.Platform != PlatformID.Unix ? ".exe" : "";

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/Builder.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using Microsoft.Build.Framework;
55
using System.Text;
6+
using System.Collections.Generic;
67

78
namespace Xamarin.ProjectTools
89
{
@@ -97,7 +98,7 @@ string GetPathFromRegistry (string valueName)
9798
return null;
9899
}
99100

100-
protected bool BuildInternal (string projectOrSolution, string target, string [] parameters = null)
101+
protected bool BuildInternal (string projectOrSolution, string target, string [] parameters = null, Dictionary<string, string> environmentVariables = null)
101102
{
102103
string buildLogFullPath = (!string.IsNullOrEmpty (BuildLogFile))
103104
? Path.GetFullPath (Path.Combine (Root, Path.GetDirectoryName (projectOrSolution), BuildLogFile))
@@ -154,7 +155,13 @@ protected bool BuildInternal (string projectOrSolution, string target, string []
154155
args.AppendFormat (" /p:{0}", param);
155156
}
156157
}
158+
if (environmentVariables != null) {
159+
foreach (var kvp in environmentVariables) {
160+
psi.EnvironmentVariables[kvp.Key] = kvp.Value;
161+
}
162+
}
157163
psi.Arguments = args.ToString ();
164+
158165
psi.CreateNoWindow = true;
159166
psi.UseShellExecute = false;
160167
psi.RedirectStandardOutput = true;

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/ProjectBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ public void Save (XamarinProject project, bool doNotCleanupOnUpdate = false, boo
5454
project.UpdateProjectFiles (ProjectDirectory, files, doNotCleanupOnUpdate);
5555
}
5656

57-
public bool Build (XamarinProject project, bool doNotCleanupOnUpdate = false, string [] parameters = null, bool saveProject = true)
57+
public bool Build (XamarinProject project, bool doNotCleanupOnUpdate = false, string [] parameters = null, bool saveProject = true, Dictionary<string, string> environmentVariables = null)
5858
{
5959
Save (project, doNotCleanupOnUpdate, saveProject);
6060

6161
Output = project.CreateBuildOutput (this);
6262

6363
project.NuGetRestore (ProjectDirectory, PackagesDirectory);
6464

65-
bool result = BuildInternal (Path.Combine (ProjectDirectory, project.ProjectFilePath), Target, parameters);
65+
bool result = BuildInternal (Path.Combine (ProjectDirectory, project.ProjectFilePath), Target, parameters, environmentVariables);
6666
built_before = true;
6767

6868
if (CleanupAfterSuccessfulBuild)
@@ -83,12 +83,12 @@ public bool Clean (XamarinProject project, bool doNotCleanupOnUpdate = false)
8383
}
8484
}
8585

86-
public bool UpdateAndroidResources (XamarinProject project, bool doNotCleanupOnUpdate = false, string [] parameters = null)
86+
public bool UpdateAndroidResources (XamarinProject project, bool doNotCleanupOnUpdate = false, string [] parameters = null, Dictionary<string, string> environmentVariables = null)
8787
{
8888
var oldTarget = Target;
8989
Target = "UpdateAndroidResources";
9090
try {
91-
return Build (project, doNotCleanupOnUpdate: doNotCleanupOnUpdate, parameters: parameters);
91+
return Build (project, doNotCleanupOnUpdate: doNotCleanupOnUpdate, parameters: parameters, environmentVariables: environmentVariables);
9292
}
9393
finally {
9494
Target = oldTarget;

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
377377
</PropertyGroup>
378378
</Target>
379379

380+
<PropertyGroup>
381+
<_BeforeBuildAdditionalResourcesCache>
382+
_CreatePropertiesCache;
383+
</_BeforeBuildAdditionalResourcesCache>
384+
</PropertyGroup>
385+
380386
<Target Name="_BuildAdditionalResourcesCache"
381387
Inputs="@(ReferencePath);@(ReferenceDependencyPaths);$(MSBuildProjectFullPath);$(NugetPackagesConfig);$(_AndroidBuildPropertiesCache)"
382388
Outputs="$(_AndroidResourcePathsCache)"

0 commit comments

Comments
 (0)