-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Xamarin.Android.Build.Tasks] First Pass at adding Unit tests for the…
… MSBuild Tasks This commit adds basic support for the MSBuild Unit Tests. The goal of these tests is to ensure that the build of an android applicaition works consistently on all the supported platforms. The basic layout of a Unit test is as follows [Test] public void BuildReleaseApplication () { var proj = new XamarinAndroidApplicationProject () { IsRelease = true, }; using (var b = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) { Assert.IsTrue (b.Build (proj), "Build should have succeeded."); } } It is a standard Unit test. First we create a XamarinAndroidApplicatonProject and set its properties. You can use the proj to add new source files, references, assets, resources and nuget packages. By default you will get a standard "HelloWorld" android application. Once you have the project you will need to create either a Apk or Dll builder via the helper methods CreateApkBuilder CreateDllBuilder CreateApkBuilder will create an apk and by default will execute the `SignAndroidPackage` build target. the CreateDllBuilder will produce a dll. The source files are created in a temp directory relative to the build output of the unit tests. By default this is src/Xamarin.Android.Build.Tasks/UnitTests/Xamarin.Android.Build.Tests/bin/$(Configuraiton)/temp Once you have a builder you can then call Build passing in the project. There are also methods available for Clean and Save. Running any of these will cause the Unit test to shell out to xbuild/msbuild and attempt to build the project. Test results are written to a build.log file. If a unit test passes then the test directory is removed. If a test fails the directory and the build.log will be left in place for investigation.
- Loading branch information
1 parent
4821159
commit 4efc24a
Showing
60 changed files
with
19,379 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -4,3 +4,5 @@ Configuration.Override.props | |
obj | ||
packages | ||
.DS_Store | ||
.nuget | ||
TestResult.xml |
This file contains 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 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
39 changes: 39 additions & 0 deletions
39
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs
This file contains 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,39 @@ | ||
using System; | ||
using System.IO; | ||
using NUnit.Framework; | ||
using Xamarin.ProjectTools; | ||
|
||
namespace Xamarin.Android.Build.Tests | ||
{ | ||
[Parallelizable (ParallelScope.Fixtures)] | ||
public class BuildTest : BaseTest | ||
{ | ||
[Test] | ||
public void BuildReleaseApplication () | ||
{ | ||
var proj = new XamarinAndroidApplicationProject () { | ||
IsRelease = true, | ||
}; | ||
using (var b = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) { | ||
Assert.IsTrue (b.Build (proj), "Build should have succeeded."); | ||
} | ||
} | ||
|
||
[Test] | ||
public void BuildReleaseApplicationWithNugetPackages () | ||
{ | ||
var proj = new XamarinAndroidApplicationProject () { | ||
IsRelease = true, | ||
Packages = { | ||
KnownPackages.AndroidSupportV4_21_0_3_0, | ||
}, | ||
}; | ||
using (var b = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) { | ||
Assert.IsTrue (b.Build (proj), "Build should have succeeded."); | ||
DirectoryAssert.Exists (Path.Combine (Root, "temp","packages", "Xamarin.Android.Support.v4.21.0.3.0"), | ||
"Nuget Package Xamarin.Android.Support.v4.21.0.3.0 should have been restored."); | ||
} | ||
} | ||
} | ||
} | ||
|
110 changes: 110 additions & 0 deletions
110
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs
This file contains 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,110 @@ | ||
using System; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using Xamarin.ProjectTools; | ||
using NUnit.Framework; | ||
using System.Linq; | ||
|
||
namespace Xamarin.Android.Build.Tests | ||
{ | ||
public class BaseTest | ||
{ | ||
static BaseTest () | ||
{ | ||
try { | ||
string ext = Environment.OSVersion.Platform != PlatformID.Unix ? ".exe" : ""; | ||
string adb = Path.Combine (Environment.GetEnvironmentVariable ("ANDROID_SDK_PATH"), "platform-tools", "adb" + ext); | ||
var proc = System.Diagnostics.Process.Start (new System.Diagnostics.ProcessStartInfo (adb, "devices") { RedirectStandardOutput = true, UseShellExecute = false }); | ||
proc.WaitForExit (); | ||
var output = proc.StandardOutput.ReadToEnd ().Trim (); | ||
// We wouldn't like to unexpectedly deploy to connected devices while running tests, so filter by target name for now... | ||
HasDevices = output.Split ('\n').Where (s => s.Contains ("emulator-")).Count () > 0; | ||
} | ||
catch (Exception ex) { | ||
Console.Error.WriteLine ("Failed to determine whether there is Android target emulator or not" + ex); | ||
} | ||
} | ||
|
||
public static readonly bool HasDevices; | ||
|
||
protected bool IsWindows { | ||
get { return Environment.OSVersion.Platform == PlatformID.Win32NT; } | ||
} | ||
|
||
public string CacheRootPath { | ||
get { | ||
return IsWindows ? Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData) | ||
: Environment.GetFolderPath (Environment.SpecialFolder.Personal); | ||
} | ||
} | ||
|
||
public string CachePath { | ||
get { | ||
return IsWindows ? Path.Combine (CacheRootPath, "Xamarin") | ||
: Path.Combine (CacheRootPath, ".local", "share", "Xamarin"); | ||
} | ||
} | ||
|
||
public string StagingPath { | ||
get { return Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); } | ||
} | ||
|
||
public string Root { | ||
get { | ||
return Path.GetDirectoryName (new Uri (typeof (XamarinProject).Assembly.CodeBase).LocalPath); | ||
} | ||
} | ||
|
||
protected ProjectBuilder CreateApkBuilder (string directory, bool cleanupAfterSuccessfulBuild = false, bool cleanupOnDispose = true) | ||
{ | ||
TestContext.CurrentContext.Test.Properties ["Output"] = new string [] { Path.Combine (Root, directory) }; | ||
return BuildHelper.CreateApkBuilder (directory, cleanupAfterSuccessfulBuild, cleanupOnDispose); | ||
} | ||
|
||
protected ProjectBuilder CreateDllBuilder (string directory, bool cleanupAfterSuccessfulBuild = false, bool cleanupOnDispose = true) | ||
{ | ||
TestContext.CurrentContext.Test.Properties ["Output"] = new string [] { Path.Combine (Root, directory) }; | ||
return BuildHelper.CreateDllBuilder (directory, cleanupAfterSuccessfulBuild, cleanupOnDispose); | ||
} | ||
|
||
[OneTimeSetUp] | ||
public void FixtureSetup () | ||
{ | ||
// Clean the Resource Cache. | ||
if (string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("BUILD_HOST"))) | ||
return; | ||
if (Directory.Exists (CachePath)) { | ||
foreach (var subDir in Directory.GetDirectories (CachePath, "*", SearchOption.TopDirectoryOnly)) { | ||
// ignore known useful directories. | ||
if (subDir.EndsWith ("Mono for Android", StringComparison.OrdinalIgnoreCase) || | ||
subDir.EndsWith ("Cache", StringComparison.OrdinalIgnoreCase) || | ||
subDir.EndsWith ("Log", StringComparison.OrdinalIgnoreCase) | ||
|| subDir.EndsWith ("Logs", StringComparison.OrdinalIgnoreCase)) | ||
continue; | ||
Console.WriteLine ("[FixtureSetup] Removing Resource Cache Directory {0}", subDir); | ||
Directory.Delete (subDir, recursive: true); | ||
} | ||
} | ||
} | ||
|
||
[TearDown] | ||
protected virtual void CleanupTest () | ||
{ | ||
if (TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed) { | ||
if (TestContext.CurrentContext.Test.Properties ["Output"] == null) | ||
return; | ||
// find the "root" directory just below "temp" and clean from there because | ||
// some tests create multiple subdirectories | ||
var output = Path.GetFullPath (((string [])TestContext.CurrentContext.Test.Properties ["Output"]) [0]); | ||
while (!Directory.GetParent (output).Name.EndsWith ("temp", StringComparison.OrdinalIgnoreCase)) { | ||
output = Directory.GetParent (output).FullName; | ||
} | ||
if (Directory.Exists (output)) { | ||
FileSystemUtils.SetDirectoryWriteable (output); | ||
Directory.Delete (output, recursive: true); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BuildHelper.cs
This file contains 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,29 @@ | ||
using System; | ||
using Microsoft.Build.Framework; | ||
using Xamarin.ProjectTools; | ||
|
||
namespace Xamarin.Android.Build.Tests | ||
{ | ||
public static class BuildHelper | ||
{ | ||
// In general here I don't make use of IDisposable pattern for ProjectBuilder because | ||
// in case it failed I rather want to preserve the files to investigate. | ||
|
||
public static ProjectBuilder CreateApkBuilder (string directory, bool cleanupAfterSuccessfulBuild = false, bool cleanupOnDispose = true) | ||
{ | ||
var ret = CreateDllBuilder (directory, cleanupAfterSuccessfulBuild, cleanupOnDispose); | ||
ret.Target = "SignAndroidPackage"; | ||
return ret; | ||
} | ||
|
||
public static ProjectBuilder CreateDllBuilder (string directory, bool cleanupAfterSuccessfulBuild = false, bool cleanupOnDispose = true) | ||
{ | ||
return new ProjectBuilder (directory) { | ||
CleanupAfterSuccessfulBuild = cleanupAfterSuccessfulBuild, | ||
CleanupOnDispose = cleanupOnDispose, | ||
Verbosity = LoggerVerbosity.Diagnostic | ||
}; | ||
} | ||
} | ||
} | ||
|
60 changes: 60 additions & 0 deletions
60
....Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj
This file contains 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,60 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="..\..\..\..\Configuration.props" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{53E4ABF0-1085-45F9-B964-DCAE4B819998}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<RootNamespace>Xamarin.Android.Build.Tests</RootNamespace> | ||
<AssemblyName>Xamarin.Android.Build.Tests</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>..\..\..\..\bin\TestDebug</OutputPath> | ||
<DefineConstants>DEBUG;</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<ConsolePause>false</ConsolePause> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>full</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>..\..\..\..\bin\TestRelease</OutputPath> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<ConsolePause>false</ConsolePause> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="Microsoft.Build" /> | ||
<Reference Include="Microsoft.Build.Framework" /> | ||
<Reference Include="System.Xml" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="nunit.framework"> | ||
<HintPath>..\..\..\..\packages\NUnit.3.2.1\lib\net45\nunit.framework.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Utilities\BaseTest.cs" /> | ||
<Compile Include="Utilities\BuildHelper.cs" /> | ||
<Compile Include="BuildTest.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Xamarin.ProjectTools\Xamarin.ProjectTools.csproj"> | ||
<Project>{2DD1EE75-6D8D-4653-A800-0A24367F7F38}</Project> | ||
<Name>Xamarin.ProjectTools</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Folder Include="Properties\" /> | ||
<Folder Include="Utilities\" /> | ||
</ItemGroup> | ||
</Project> |
6 changes: 6 additions & 0 deletions
6
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/packages.config
This file contains 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,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="NUnit" version="3.2.1" targetFramework="net45" /> | ||
<package id="Unofficial.Ionic.Zip" version="1.9.1.8" targetFramework="net45" /> | ||
<package id="NUnit.ConsoleRunner" version="3.2.1" /> | ||
</packages> |
30 changes: 30 additions & 0 deletions
30
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/AndroidBuildActions.cs
This file contains 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,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml; | ||
using Microsoft.Build.Construction; | ||
|
||
namespace Xamarin.ProjectTools | ||
{ | ||
public static class AndroidBuildActions | ||
{ | ||
public const string AndroidResource = "AndroidResource"; | ||
public const string AndroidAsset = "AndroidAsset"; | ||
public const string AndroidEnvironment = "AndroidEnvironment"; | ||
public const string AndroidInterfaceDescription = "AndroidInterfaceDescription"; | ||
public const string AndroidJavaSource = "AndroidJavaSource"; | ||
public const string AndroidJavaLibrary = "AndroidJavaLibrary"; | ||
public const string AndroidLintConfig = "AndroidLintConfig"; | ||
public const string AndroidNativeLibrary = "AndroidNativeLibrary"; | ||
public const string ProguardConfiguration = "ProguardConfiguration"; | ||
public const string TransformFile = "TransformFile"; | ||
public const string InputJar = "InputJar"; | ||
public const string ReferenceJar = "ReferenceJar"; | ||
public const string EmbeddedJar = "EmbeddedJar"; | ||
public const string EmbeddedNativeLibrary = "EmbeddedNativeLibrary"; | ||
public const string EmbeddedReferenceJar = "EmbeddedReferenceJar"; | ||
public const string LibraryProjectZip = "LibraryProjectZip"; | ||
public const string LibraryProjectProperties = "LibraryProjectProperties"; | ||
} | ||
} |
Oops, something went wrong.