Skip to content

Commit 603929a

Browse files
[tests] migrate test targets to MSBuild
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 - Mono.Android-Tests.csproj needs to conditionally `<ProjectReference />` xa-prep-tasks - Otherwise xa-prep-tasks.dll can become locked on Windows - A nested xabuild.exe call will attempt to ovewrite it - Update .gitignore for *.rawproto and the generated Xamarin.Android.Common.props
1 parent b2623b9 commit 603929a

File tree

9 files changed

+130
-63
lines changed

9 files changed

+130
-63
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: 7 additions & 61 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
@@ -116,12 +112,8 @@ endif # msbuild
116112
include build-tools/scripts/BuildEverything.mk
117113
include tests/api-compatibility/api-compatibility.mk
118114

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

126118
clean:
127119
$(MSBUILD) $(MSBUILD_FLAGS) /t:Clean Xamarin.Android.sln
@@ -133,59 +125,13 @@ distclean:
133125
git clean -xdff
134126
git submodule foreach git clean -xdff
135127

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

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

187136
run-apk-tests:
188-
$(call RUN_APK_TESTS, $(TEST_APK_PROJECTS), )
189-
ifeq ($(CONFIGURATION),Release)
190-
$(call RUN_APK_TESTS, $(TEST_APK_PROJECTS_AOT), /p:AotAssemblies=true)
191-
endif
137+
$(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
@@ -14,7 +14,7 @@
1414
AndroidSdkPath="$(AndroidSdkPath)"
1515
AndroidNdkPath="$(AndroidNdkPath)"
1616
JavaSdkPath="$(JavaSdkDirectory)"
17-
Output="$(_TopDir)\external\Java.Interop\bin\BuildDebug\JdkInfo.props">
17+
Output="$(_TopDir)\external\Java.Interop\bin\Build$(Configuration)\JdkInfo.props">
1818
<Output TaskParameter="JavaSdkDirectory" PropertyName="_JavaSdkDirectory" />
1919
</JdkInfo>
2020
<Copy
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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=" '$(RUNTIME)' != '' ">$(RUNTIME)</_Runtime>
12+
<_Runtime Condition=" '$(RUNTIME)' == '' And '$(HostOS)' != 'Windows' ">mono</_Runtime>
13+
<_NUnit>$(_Runtime) packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console.exe</_NUnit>
14+
<_Test Condition=" '$(TEST)' != '' ">--test=&quot;$(TEST)&quot;</_Test>
15+
<_XABuild Condition=" '$(HostOS)' == 'Windows' ">$(_TopDir)\bin\$(Configuration)\bin\xabuild.exe</_XABuild>
16+
<_XABuild Condition=" '$(HostOS)' != 'Windows' ">$(_TopDir)\tools\scripts\xabuild</_XABuild>
17+
<_XABuildProperties>/p:Configuration=$(Configuration) /p:XAIntegratedTests=$(XAIntegratedTests)</_XABuildProperties>
18+
</PropertyGroup>
19+
<ItemGroup>
20+
<_TestAssembly Include="$(_TopDir)\bin\Test$(Configuration)\Xamarin.Android.Build.Tests.dll" Condition=" '$(TestAssembly)' == '' " />
21+
<_TestAssembly Include="$(TestAssembly)" Condition=" '$(TestAssembly)' != '' " />
22+
<_JavaInteropTestResults Include="$(JavaInteropSourceDirectory)\TestResult-*.xml" />
23+
<_ApkTestProject Include="$(_TopDir)\src\Mono.Android\Test\Mono.Android-Tests.csproj" />
24+
<_ApkTestProject Include="$(_TopDir)\tests\CodeGen-Binding\Xamarin.Android.JcwGen-Tests\Xamarin.Android.JcwGen-Tests.csproj" />
25+
<_ApkTestProject Include="$(_TopDir)\tests\locales\Xamarin.Android.Locale-Tests\Xamarin.Android.Locale-Tests.csproj" />
26+
<_ApkTestProject Include="$(_TopDir)\tests\Xamarin.Android.Bcl-Tests\Xamarin.Android.Bcl-Tests.csproj" />
27+
<_ApkTestProject Include="$(_TopDir)\tests\Xamarin.Forms-Performance-Integration\Droid\Xamarin.Forms.Performance.Integration.Droid.csproj" />
28+
<_ApkTestProjectAot Include="$(_TopDir)\src\Mono.Android\Test\Mono.Android-Tests.csproj" />
29+
</ItemGroup>
30+
<Target Name="RunNUnitTests">
31+
<Exec
32+
Command="$(_NUnit) $(NUNIT_EXTRA) %(_TestAssembly.Identity) $(_Test) --result=&quot;TestResult-%(Filename).xml;format=nunit2&quot; --output=&quot;bin\Test$(Configuration)\TestOutput-%(Filename).txt&quot;"
33+
WorkingDirectory="$(_TopDir)"
34+
ContinueOnError="True"
35+
/>
36+
<ItemGroup>
37+
<_RenameTestCasesGlob Include="$(_TopDir)\TestResult-*Tests.xml" />
38+
</ItemGroup>
39+
<PropertyGroup>
40+
<_RenamedTestCases>@(_RenameTestCasesGlob)</_RenamedTestCases>
41+
</PropertyGroup>
42+
<MSBuild
43+
Projects="$(MSBuildThisFileDirectory)TestApks.targets"
44+
Targets="RenameTestCases"
45+
Properties="Configuration=$(Configuration);RenameTestCasesGlob=$(_RenamedTestCases)"
46+
/>
47+
</Target>
48+
<Target Name="RunJavaInteropTests">
49+
<MSBuild Projects="$(JavaInteropSourceDirectory)\Java.Interop.sln" Condition=" '$(HostOS)' == 'Windows' " />
50+
<Exec
51+
Command ="make -C &quot;$(JavaInteropSourceDirectory)&quot; CONFIGURATION=$(Configuration) all"
52+
Condition=" '$(HostOS)' != 'Windows' "
53+
/>
54+
<SetEnvironmentVariable Name="ANDROID_SDK_PATH" Value="$(AndroidSdkFullPath)" />
55+
<MSBuild
56+
Projects="$(JavaInteropSourceDirectory)\build-tools\scripts\RunNUnitTests.targets"
57+
Condition=" '$(HostOS)' == 'Windows' "
58+
/>
59+
<Exec
60+
Command ="make -C &quot;$(JavaInteropSourceDirectory)&quot; CONFIGURATION=$(Configuration) run-all-tests || true"
61+
Condition=" '$(HostOS)' != 'Windows' "
62+
/>
63+
<ItemGroup>
64+
<_RenameTestCasesGlob Include="$(JavaInteropSourceDirectory)\TestResult-*Tests.xml" />
65+
</ItemGroup>
66+
<PropertyGroup>
67+
<_RenamedTestCases>@(_RenameTestCasesGlob)</_RenamedTestCases>
68+
</PropertyGroup>
69+
<MSBuild
70+
Projects="$(MSBuildThisFileDirectory)TestApks.targets"
71+
Targets="RenameTestCases"
72+
Properties="Configuration=$(Configuration);RenameTestCasesGlob=$(_RenamedTestCases)"
73+
/>
74+
<Copy SourceFiles="@(_JavaInteropTestResults)" DestinationFolder="$(_TopDir)" />
75+
</Target>
76+
<Target Name="RunApkTests">
77+
<Exec Command="$(_XABuild) %(_ApkTestProject.Identity) /t:SignAndroidPackage $(_XABuildProperties)" />
78+
<MSBuild Projects="$(_TopDir)\tests\RunApkTests.targets" />
79+
<Exec
80+
Command="$(_XABuild) %(_ApkTestProjectAot.Identity) /t:SignAndroidPackage $(_XABuildProperties) /p:AotAssemblies=True"
81+
Condition=" '$(Configuration)' == 'Release' "
82+
/>
83+
<MSBuild
84+
Projects="$(_TopDir)\tests\RunApkTests.targets"
85+
Condition=" '$(Configuration)' == 'Release' "
86+
/>
87+
</Target>
88+
<Target Name="RunAllTests" DependsOnTargets="RunNUnitTests;RunJavaInteropTests;RunApkTests" />
89+
</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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
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">
105+
<ProjectReference Include="..\..\..\build-tools\android-toolchain\android-toolchain.mdproj" Condition=" '$(XAIntegratedTests)' == 'True' ">
106106
<Name>android-toolchain</Name>
107107
<Project>{8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}</Project>
108108
<Private>False</Private>

0 commit comments

Comments
 (0)