Skip to content

Commit cdf3bcc

Browse files
jonathanpeppersjonpryor
authored andcommitted
[tests] migrate test targets to MSBuild (#949)
The end goal here is to enable Windows users to easily run the various types of tests. Commands have been migrated to MSBuild, keeping `make` commands the same for macOS and linux. ~~ Usage ~~ Windows: ``` msbuild Xamarin.Android.sln /t:RunAllTests msbuild Xamarin.Android.sln /t:RunNUnitTests msbuild Xamarin.Android.sln /t:RunJavaInteropTests msbuild Xamarin.Android.sln /t:RunApkTests ``` macOS/linux should remain unchanged: ``` make run-all-tests make run-nunit-tests make run-ji-tests make run-apk-tests ``` ~~ Changes ~~ - Added a new `build-tools/scripts/RunTests.targets` - `Before.Xamarin.Android.sln.targets` includes these test targets - A new `<SetEnvironmentVariable/>` task is needed, added to `xa-prep-tasks` - Ported any environment variables set in Makefile to MSBuild - `Mono.Android-Tests.csproj` needs to remove the `@(ProjectReference)` within `xa-prep-tasks`: - Otherwise `xa-prep-tasks.dll` can become locked on Windows - A nested `xabuild.exe` call will attempt to overwrite it - Update `.gitignore` for `*.rawproto` and the generated `Xamarin.Android.Common.props`
1 parent ed6f18c commit cdf3bcc

File tree

9 files changed

+121
-69
lines changed

9 files changed

+121
-69
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ TestResult-*.csv
1616
Resource.designer.cs
1717
logcat-*.txt
1818
apk-sizes-*.txt
19+
*.rawproto
20+
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props

Before.Xamarin.Android.sln.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildThisFileDirectory)build-tools\scripts\PrepareWindows.targets" Condition=" '$(OS)' == 'Windows_NT' " />
4+
<Import Project="$(MSBuildThisFileDirectory)build-tools\scripts\RunTests.targets" />
45
</Project>

Makefile

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ prefix = /usr/local
66
CONFIGURATION = Debug
77
RUNTIME := $(shell if [ -f "`which mono64`" ] ; then echo mono64 ; else echo mono; fi) --debug=casts
88
SOLUTION = Xamarin.Android.sln
9-
10-
NUNIT_TESTS = \
11-
bin/Test$(CONFIGURATION)/Xamarin.Android.Build.Tests.dll
12-
13-
NUNIT_CONSOLE = packages/NUnit.ConsoleRunner.3.7.0/tools/nunit3-console.exe
9+
TEST_TARGETS = build-tools/scripts/RunTests.targets
1410

1511
ifneq ($(V),0)
1612
MONO_OPTIONS += --debug
@@ -121,12 +117,9 @@ prepare-image-dependencies:
121117
include build-tools/scripts/BuildEverything.mk
122118
include tests/api-compatibility/api-compatibility.mk
123119

124-
run-all-tests: run-nunit-tests run-ji-tests run-apk-tests run-api-compatibility-tests
125-
126-
rename-test-cases:
127-
$(MSBUILD) $(MSBUILD_FLAGS) build-tools/scripts/TestApks.targets \
128-
/t:RenameTestCases /p:RenameTestCasesGlob="$(if $(RENAME_GLOB),$(RENAME_GLOB),`pwd`/TestResult-\*.xml)" \
129-
$(if $(KEEP_TEST_SOURCES),/p:DeleteTestCaseSourceFiles=False)
120+
run-all-tests:
121+
$(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunAllTests
122+
$(MAKE) run-api-compatibility-tests
130123

131124
clean:
132125
$(MSBUILD) $(MSBUILD_FLAGS) /t:Clean Xamarin.Android.sln
@@ -138,60 +131,13 @@ distclean:
138131
git clean -xdff
139132
git submodule foreach git clean -xdff
140133

141-
# $(call RUN_NUNIT_TEST,filename,log-lref?)
142-
define RUN_NUNIT_TEST
143-
MONO_TRACE_LISTENER=Console.Out \
144-
USE_MSBUILD=$(if $(USE_MSBUILD),$(USE_MSBUILD),0) \
145-
$(RUNTIME) --runtime=v4.0.0 \
146-
$(NUNIT_CONSOLE) $(NUNIT_EXTRA) $(1) \
147-
$(if $(TEST),--test=$(TEST)) \
148-
--result="TestResult-$(basename $(notdir $(1))).xml;format=nunit2" \
149-
-output=bin/Test$(CONFIGURATION)/TestOutput-$(basename $(notdir $(1))).txt \
150-
|| true ; \
151-
if [ -f "bin/Test$(CONFIGURATION)/TestOutput-$(basename $(notdir $(1))).txt" ] ; then \
152-
cat bin/Test$(CONFIGURATION)/TestOutput-$(basename $(notdir $(1))).txt ; \
153-
fi
154-
$(MAKE) rename-test-cases RENAME_GLOB="`pwd`/TestResult-$(basename $(notdir $(1))).xml"
155-
endef
156-
157-
run-nunit-tests: $(NUNIT_TESTS)
134+
run-nunit-tests:
158135
ifeq ($(SKIP_NUNIT_TESTS),)
159-
$(foreach t,$(NUNIT_TESTS), $(call RUN_NUNIT_TEST,$(t),1))
136+
$(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunNUnitTests
160137
endif # $(SKIP_NUNIT_TESTS) == ''
161138

162139
run-ji-tests:
163-
$(MAKE) -C "$(call GetPath,JavaInterop)" CONFIGURATION=$(CONFIGURATION) all
164-
ANDROID_SDK_PATH="$(call GetPath,AndroidSdk)" $(MAKE) -C "$(call GetPath,JavaInterop)" CONFIGURATION=$(CONFIGURATION) run-all-tests || true
165-
$(MAKE) rename-test-cases RENAME_GLOB='"$(call GetPath,JavaInterop)"/TestResult-*Tests.xml'
166-
cp "$(call GetPath,JavaInterop)"/TestResult-*.xml .
167-
168-
# .apk files to test on-device need to:
169-
# (1) Have their .csproj files listed here
170-
# (2) Add a `@(UnitTestApk)` entry to `tests/RunApkTests.targets`
171-
TEST_APK_PROJECTS = \
172-
src/Mono.Android/Test/Mono.Android-Tests.csproj \
173-
tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/Xamarin.Android.JcwGen-Tests.csproj \
174-
tests/locales/Xamarin.Android.Locale-Tests/Xamarin.Android.Locale-Tests.csproj \
175-
tests/Xamarin.Android.Bcl-Tests/Xamarin.Android.Bcl-Tests.csproj \
176-
tests/Xamarin.Forms-Performance-Integration/Droid/Xamarin.Forms.Performance.Integration.Droid.csproj
177-
178-
TEST_APK_PROJECTS_AOT = \
179-
src/Mono.Android/Test/Mono.Android-Tests.csproj \
180-
181-
# Syntax: $(call BUILD_TEST_APK,path/to/project.csproj,additional_msbuild_flags)
182-
define BUILD_TEST_APK
183-
# Must use xabuild to ensure correct assemblies are resolved
184-
MSBUILD="$(MSBUILD)" tools/scripts/xabuild $(MSBUILD_FLAGS) /t:SignAndroidPackage $(2) $(1)
185-
endef # BUILD_TEST_APK
186-
187-
# Syntax: $(call RUN_APK_TESTS,projects,additional_msbuild_flags)
188-
define RUN_APK_TESTS
189-
$(foreach p, $(1), $(call BUILD_TEST_APK, $(p),$(2)))
190-
$(MSBUILD) $(MSBUILD_FLAGS) $(2) tests/RunApkTests.targets
191-
endef
140+
$(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunJavaInteropTests
192141

193142
run-apk-tests:
194-
$(call RUN_APK_TESTS, $(TEST_APK_PROJECTS), )
195-
ifeq ($(CONFIGURATION),Release)
196-
$(call RUN_APK_TESTS, $(TEST_APK_PROJECTS_AOT), /p:AotAssemblies=true)
197-
endif
143+
$(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunApkTests

build-tools/scripts/PrepareWindows.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
AndroidSdkPath="$(AndroidSdkDirectory)"
1616
AndroidNdkPath="$(AndroidNdkDirectory)"
1717
JavaSdkPath="$(JavaSdkDirectory)"
18-
Output="$(_TopDir)\external\Java.Interop\bin\BuildDebug\JdkInfo.props">
18+
Output="$(_TopDir)\external\Java.Interop\bin\Build$(Configuration)\JdkInfo.props">
1919
<Output TaskParameter="JavaSdkDirectory" PropertyName="_JavaSdkDirectory" />
2020
</JdkInfo>
2121
<Copy
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="RunAllTests" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<_TopDir>$(MSBuildThisFileDirectory)..\..</_TopDir>
6+
</PropertyGroup>
7+
<Import Project="$(_TopDir)\Configuration.props" />
8+
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\xa-prep-tasks.dll" TaskName="Xamarin.Android.BuildTools.PrepTasks.SetEnvironmentVariable" />
9+
<PropertyGroup>
10+
<XAIntegratedTests Condition=" '$(HostOS)' == 'Windows' ">False</XAIntegratedTests>
11+
<_Runtime Condition=" '$(HostOS)' != 'Windows' ">$(ManagedRuntime)</_Runtime>
12+
<_NUnit>$(_Runtime) packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console.exe</_NUnit>
13+
<_Test Condition=" '$(TEST)' != '' ">--test=&quot;$(TEST)&quot;</_Test>
14+
<_XABuild>$(_TopDir)\bin\$(Configuration)\bin\xabuild</_XABuild>
15+
<_XABuildProperties>/p:Configuration=$(Configuration) /p:XAIntegratedTests=$(XAIntegratedTests)</_XABuildProperties>
16+
</PropertyGroup>
17+
<ItemGroup>
18+
<_TestAssembly Include="$(_TopDir)\bin\Test$(Configuration)\Xamarin.Android.Build.Tests.dll" />
19+
<_JavaInteropTestResults Include="$(JavaInteropSourceDirectory)\TestResult-*.xml" />
20+
<_ApkTestProject Include="$(_TopDir)\src\Mono.Android\Test\Mono.Android-Tests.csproj" />
21+
<_ApkTestProject Include="$(_TopDir)\tests\CodeGen-Binding\Xamarin.Android.JcwGen-Tests\Xamarin.Android.JcwGen-Tests.csproj" />
22+
<_ApkTestProject Include="$(_TopDir)\tests\locales\Xamarin.Android.Locale-Tests\Xamarin.Android.Locale-Tests.csproj" />
23+
<_ApkTestProject Include="$(_TopDir)\tests\Xamarin.Android.Bcl-Tests\Xamarin.Android.Bcl-Tests.csproj" />
24+
<_ApkTestProject Include="$(_TopDir)\tests\Xamarin.Forms-Performance-Integration\Droid\Xamarin.Forms.Performance.Integration.Droid.csproj" />
25+
<_ApkTestProjectAot Include="$(_TopDir)\src\Mono.Android\Test\Mono.Android-Tests.csproj" />
26+
</ItemGroup>
27+
<Target Name="RunNUnitTests">
28+
<SetEnvironmentVariable Name="USE_MSBUILD" Value="0" Condition=" '$(USE_MSBUILD)' == '' " />
29+
<Exec
30+
Command="$(_NUnit) $(NUNIT_EXTRA) %(_TestAssembly.Identity) $(_Test) --result=&quot;TestResult-%(Filename).xml;format=nunit2&quot; --output=&quot;bin\Test$(Configuration)\TestOutput-%(Filename).txt&quot;"
31+
WorkingDirectory="$(_TopDir)"
32+
ContinueOnError="True"
33+
/>
34+
<ItemGroup>
35+
<_RenameTestCasesGlob Include="$(_TopDir)\TestResult-*Tests.xml" />
36+
</ItemGroup>
37+
<PropertyGroup>
38+
<_RenamedTestCases>@(_RenameTestCasesGlob)</_RenamedTestCases>
39+
</PropertyGroup>
40+
<MSBuild
41+
Projects="$(MSBuildThisFileDirectory)TestApks.targets"
42+
Targets="RenameTestCases"
43+
Properties="Configuration=$(Configuration);RenameTestCasesGlob=$(_RenamedTestCases)"
44+
/>
45+
</Target>
46+
<Target Name="RunJavaInteropTests">
47+
<MSBuild Projects="$(JavaInteropSourceDirectory)\Java.Interop.sln" Condition=" '$(HostOS)' == 'Windows' " />
48+
<Exec
49+
Command ="make -C &quot;$(JavaInteropSourceDirectory)&quot; CONFIGURATION=$(Configuration) all"
50+
Condition=" '$(HostOS)' != 'Windows' "
51+
/>
52+
<SetEnvironmentVariable Name="ANDROID_SDK_PATH" Value="$(AndroidSdkFullPath)" />
53+
<MSBuild Projects="$(JavaInteropSourceDirectory)\build-tools\scripts\RunNUnitTests.targets" />
54+
<ItemGroup>
55+
<_RenameTestCasesGlob Include="$(JavaInteropSourceDirectory)\TestResult-*Tests.xml" />
56+
</ItemGroup>
57+
<PropertyGroup>
58+
<_RenamedTestCases>@(_RenameTestCasesGlob)</_RenamedTestCases>
59+
</PropertyGroup>
60+
<MSBuild
61+
Projects="$(MSBuildThisFileDirectory)TestApks.targets"
62+
Targets="RenameTestCases"
63+
Properties="Configuration=$(Configuration);RenameTestCasesGlob=$(_RenamedTestCases)"
64+
/>
65+
<Copy SourceFiles="@(_JavaInteropTestResults)" DestinationFolder="$(_TopDir)" />
66+
</Target>
67+
<Target Name="RunApkTests">
68+
<Exec Command="$(_XABuild) %(_ApkTestProject.Identity) /t:SignAndroidPackage $(_XABuildProperties)" />
69+
<MSBuild Projects="$(_TopDir)\tests\RunApkTests.targets" />
70+
<Exec
71+
Command="$(_XABuild) %(_ApkTestProjectAot.Identity) /t:SignAndroidPackage $(_XABuildProperties) /p:AotAssemblies=True"
72+
Condition=" '$(Configuration)' == 'Release' "
73+
/>
74+
<MSBuild
75+
Projects="$(_TopDir)\tests\RunApkTests.targets"
76+
Condition=" '$(Configuration)' == 'Release' "
77+
/>
78+
</Target>
79+
<Target Name="RunAllTests" DependsOnTargets="RunNUnitTests;RunJavaInteropTests;RunApkTests" />
80+
</Project>

build-tools/scripts/TestApks.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
Text="Please set `%24(Configuration)`."
196196
/>
197197
<PropertyGroup>
198+
<_DeleteSource Condition=" '$(KEEP_TEST_SOURCES)' != '' ">False</_DeleteSource>
198199
<_DeleteSource Condition=" '$(DeleteTestCaseSourceFiles)' != '' ">$(DeleteTestCaseSourceFiles)</_DeleteSource>
199200
<_DeleteSource Condition=" '$(_DeleteSource)' == '' ">True</_DeleteSource>
200201
</PropertyGroup>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Build.Framework;
2+
using Microsoft.Build.Utilities;
3+
4+
using System;
5+
6+
namespace Xamarin.Android.BuildTools.PrepTasks
7+
{
8+
public class SetEnvironmentVariable : Task
9+
{
10+
[Required]
11+
public string Name { get; set; }
12+
13+
[Required]
14+
public string Value { get; set; }
15+
16+
public override bool Execute ()
17+
{
18+
Log.LogMessage (MessageImportance.Low, $"Task {nameof (SetEnvironmentVariable)}");
19+
Log.LogMessage (MessageImportance.Low, $" {nameof (Name)}: {Name}");
20+
Log.LogMessage (MessageImportance.Low, $" {nameof (Value)}: {Value}");
21+
22+
Environment.SetEnvironmentVariable(Name, Value);
23+
24+
return !Log.HasLoggedErrors;
25+
}
26+
}
27+
}

build-tools/xa-prep-tasks/xa-prep-tasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\HashFileContents.cs" />
4646
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\JdkInfo.cs" />
4747
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\PathToolTask.cs" />
48+
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\SetEnvironmentVariable.cs" />
4849
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\SystemUnzip.cs" />
4950
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\Which.cs" />
5051
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\GitBlame.cs" />

src/Mono.Android/Test/Mono.Android-Tests.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@
102102
<Import Project="..\..\..\bin\$(Configuration)\lib\xamarin.android\xbuild\Xamarin\Android\Xamarin.Android.CSharp.targets" />
103103
<Import Project="Mono.Android-Tests.targets" />
104104
<ItemGroup>
105-
<ProjectReference Include="..\..\..\build-tools\android-toolchain\android-toolchain.mdproj">
106-
<Name>android-toolchain</Name>
107-
<Project>{8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}</Project>
108-
<Private>False</Private>
109-
<ReferenceOutputAssembly>False</ReferenceOutputAssembly>
110-
</ProjectReference>
111105
<ProjectReference Include="..\..\Xamarin.Android.Build.Tasks\Xamarin.Android.Build.Tasks.csproj" Condition=" '$(XAIntegratedTests)' == 'True' ">
112106
<Name>Xamarin.Android.Build.Tasks</Name>
113107
<Project>{3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}</Project>

0 commit comments

Comments
 (0)