From 0b00cd387195469939ea3c5fbe0a59d119068aee Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 19 Oct 2017 11:31:30 -0500 Subject: [PATCH] [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 - Ported any environment variables set in Makefile to MSBuild - Mono.Android-Tests.csproj needs to remove the `` 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 --- .gitignore | 2 + Before.Xamarin.Android.sln.targets | 1 + Makefile | 70 ++-------------- build-tools/scripts/PrepareWindows.targets | 2 +- build-tools/scripts/RunTests.targets | 80 +++++++++++++++++++ build-tools/scripts/TestApks.targets | 1 + .../SetEnvironmentVariable.cs | 27 +++++++ .../xa-prep-tasks/xa-prep-tasks.csproj | 1 + .../Test/Mono.Android-Tests.csproj | 6 -- 9 files changed, 121 insertions(+), 69 deletions(-) create mode 100644 build-tools/scripts/RunTests.targets create mode 100644 build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/SetEnvironmentVariable.cs diff --git a/.gitignore b/.gitignore index 0ad0debdd74..9a6147f6b51 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ TestResult-*.csv Resource.designer.cs logcat-*.txt apk-sizes-*.txt +*.rawproto +src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props diff --git a/Before.Xamarin.Android.sln.targets b/Before.Xamarin.Android.sln.targets index a1227711520..faadb3d78de 100644 --- a/Before.Xamarin.Android.sln.targets +++ b/Before.Xamarin.Android.sln.targets @@ -1,4 +1,5 @@ + \ No newline at end of file diff --git a/Makefile b/Makefile index 1371d81ed38..e02ebf4f8dd 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,7 @@ prefix = /usr/local CONFIGURATION = Debug RUNTIME := $(shell if [ -f "`which mono64`" ] ; then echo mono64 ; else echo mono; fi) --debug=casts SOLUTION = Xamarin.Android.sln - -NUNIT_TESTS = \ - bin/Test$(CONFIGURATION)/Xamarin.Android.Build.Tests.dll - -NUNIT_CONSOLE = packages/NUnit.ConsoleRunner.3.7.0/tools/nunit3-console.exe +TEST_TARGETS = build-tools/scripts/RunTests.targets ifneq ($(V),0) MONO_OPTIONS += --debug @@ -121,12 +117,9 @@ prepare-image-dependencies: include build-tools/scripts/BuildEverything.mk include tests/api-compatibility/api-compatibility.mk -run-all-tests: run-nunit-tests run-ji-tests run-apk-tests run-api-compatibility-tests - -rename-test-cases: - $(MSBUILD) $(MSBUILD_FLAGS) build-tools/scripts/TestApks.targets \ - /t:RenameTestCases /p:RenameTestCasesGlob="$(if $(RENAME_GLOB),$(RENAME_GLOB),`pwd`/TestResult-\*.xml)" \ - $(if $(KEEP_TEST_SOURCES),/p:DeleteTestCaseSourceFiles=False) +run-all-tests: + $(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunAllTests + $(MAKE) run-api-compatibility-tests clean: $(MSBUILD) $(MSBUILD_FLAGS) /t:Clean Xamarin.Android.sln @@ -138,60 +131,13 @@ distclean: git clean -xdff git submodule foreach git clean -xdff -# $(call RUN_NUNIT_TEST,filename,log-lref?) -define RUN_NUNIT_TEST - MONO_TRACE_LISTENER=Console.Out \ - USE_MSBUILD=$(if $(USE_MSBUILD),$(USE_MSBUILD),0) \ - $(RUNTIME) --runtime=v4.0.0 \ - $(NUNIT_CONSOLE) $(NUNIT_EXTRA) $(1) \ - $(if $(TEST),--test=$(TEST)) \ - --result="TestResult-$(basename $(notdir $(1))).xml;format=nunit2" \ - -output=bin/Test$(CONFIGURATION)/TestOutput-$(basename $(notdir $(1))).txt \ - || true ; \ - if [ -f "bin/Test$(CONFIGURATION)/TestOutput-$(basename $(notdir $(1))).txt" ] ; then \ - cat bin/Test$(CONFIGURATION)/TestOutput-$(basename $(notdir $(1))).txt ; \ - fi - $(MAKE) rename-test-cases RENAME_GLOB="`pwd`/TestResult-$(basename $(notdir $(1))).xml" -endef - -run-nunit-tests: $(NUNIT_TESTS) +run-nunit-tests: ifeq ($(SKIP_NUNIT_TESTS),) - $(foreach t,$(NUNIT_TESTS), $(call RUN_NUNIT_TEST,$(t),1)) + $(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunNUnitTests endif # $(SKIP_NUNIT_TESTS) == '' run-ji-tests: - $(MAKE) -C "$(call GetPath,JavaInterop)" CONFIGURATION=$(CONFIGURATION) all - ANDROID_SDK_PATH="$(call GetPath,AndroidSdk)" $(MAKE) -C "$(call GetPath,JavaInterop)" CONFIGURATION=$(CONFIGURATION) run-all-tests || true - $(MAKE) rename-test-cases RENAME_GLOB='"$(call GetPath,JavaInterop)"/TestResult-*Tests.xml' - cp "$(call GetPath,JavaInterop)"/TestResult-*.xml . - -# .apk files to test on-device need to: -# (1) Have their .csproj files listed here -# (2) Add a `@(UnitTestApk)` entry to `tests/RunApkTests.targets` -TEST_APK_PROJECTS = \ - src/Mono.Android/Test/Mono.Android-Tests.csproj \ - tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/Xamarin.Android.JcwGen-Tests.csproj \ - tests/locales/Xamarin.Android.Locale-Tests/Xamarin.Android.Locale-Tests.csproj \ - tests/Xamarin.Android.Bcl-Tests/Xamarin.Android.Bcl-Tests.csproj \ - tests/Xamarin.Forms-Performance-Integration/Droid/Xamarin.Forms.Performance.Integration.Droid.csproj - -TEST_APK_PROJECTS_AOT = \ - src/Mono.Android/Test/Mono.Android-Tests.csproj \ - -# Syntax: $(call BUILD_TEST_APK,path/to/project.csproj,additional_msbuild_flags) -define BUILD_TEST_APK - # Must use xabuild to ensure correct assemblies are resolved - MSBUILD="$(MSBUILD)" tools/scripts/xabuild $(MSBUILD_FLAGS) /t:SignAndroidPackage $(2) $(1) -endef # BUILD_TEST_APK - -# Syntax: $(call RUN_APK_TESTS,projects,additional_msbuild_flags) -define RUN_APK_TESTS - $(foreach p, $(1), $(call BUILD_TEST_APK, $(p),$(2))) - $(MSBUILD) $(MSBUILD_FLAGS) $(2) tests/RunApkTests.targets -endef + $(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunJavaInteropTests run-apk-tests: - $(call RUN_APK_TESTS, $(TEST_APK_PROJECTS), ) -ifeq ($(CONFIGURATION),Release) - $(call RUN_APK_TESTS, $(TEST_APK_PROJECTS_AOT), /p:AotAssemblies=true) -endif + $(MSBUILD) $(MSBUILD_FLAGS) $(TEST_TARGETS) /t:RunApkTests diff --git a/build-tools/scripts/PrepareWindows.targets b/build-tools/scripts/PrepareWindows.targets index e0202a77b56..1eda5dd5c9e 100644 --- a/build-tools/scripts/PrepareWindows.targets +++ b/build-tools/scripts/PrepareWindows.targets @@ -15,7 +15,7 @@ AndroidSdkPath="$(AndroidSdkDirectory)" AndroidNdkPath="$(AndroidNdkDirectory)" JavaSdkPath="$(JavaSdkDirectory)" - Output="$(_TopDir)\external\Java.Interop\bin\BuildDebug\JdkInfo.props"> + Output="$(_TopDir)\external\Java.Interop\bin\Build$(Configuration)\JdkInfo.props"> + + + Debug + <_TopDir>$(MSBuildThisFileDirectory)..\.. + + + + + False + <_Runtime Condition=" '$(HostOS)' != 'Windows' ">$(ManagedRuntime) + <_NUnit>$(_Runtime) packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console.exe + <_Test Condition=" '$(TEST)' != '' ">--test="$(TEST)" + <_XABuild>$(_TopDir)\bin\$(Configuration)\bin\xabuild + <_XABuildProperties>/p:Configuration=$(Configuration) /p:XAIntegratedTests=$(XAIntegratedTests) + + + <_TestAssembly Include="$(_TopDir)\bin\Test$(Configuration)\Xamarin.Android.Build.Tests.dll" /> + <_JavaInteropTestResults Include="$(JavaInteropSourceDirectory)\TestResult-*.xml" /> + <_ApkTestProject Include="$(_TopDir)\src\Mono.Android\Test\Mono.Android-Tests.csproj" /> + <_ApkTestProject Include="$(_TopDir)\tests\CodeGen-Binding\Xamarin.Android.JcwGen-Tests\Xamarin.Android.JcwGen-Tests.csproj" /> + <_ApkTestProject Include="$(_TopDir)\tests\locales\Xamarin.Android.Locale-Tests\Xamarin.Android.Locale-Tests.csproj" /> + <_ApkTestProject Include="$(_TopDir)\tests\Xamarin.Android.Bcl-Tests\Xamarin.Android.Bcl-Tests.csproj" /> + <_ApkTestProject Include="$(_TopDir)\tests\Xamarin.Forms-Performance-Integration\Droid\Xamarin.Forms.Performance.Integration.Droid.csproj" /> + <_ApkTestProjectAot Include="$(_TopDir)\src\Mono.Android\Test\Mono.Android-Tests.csproj" /> + + + + + + <_RenameTestCasesGlob Include="$(_TopDir)\TestResult-*Tests.xml" /> + + + <_RenamedTestCases>@(_RenameTestCasesGlob) + + + + + + + + + + <_RenameTestCasesGlob Include="$(JavaInteropSourceDirectory)\TestResult-*Tests.xml" /> + + + <_RenamedTestCases>@(_RenameTestCasesGlob) + + + + + + + + + + + + \ No newline at end of file diff --git a/build-tools/scripts/TestApks.targets b/build-tools/scripts/TestApks.targets index 4babe5d40df..699d99e61a6 100644 --- a/build-tools/scripts/TestApks.targets +++ b/build-tools/scripts/TestApks.targets @@ -195,6 +195,7 @@ Text="Please set `%24(Configuration)`." /> + <_DeleteSource Condition=" '$(KEEP_TEST_SOURCES)' != '' ">False <_DeleteSource Condition=" '$(DeleteTestCaseSourceFiles)' != '' ">$(DeleteTestCaseSourceFiles) <_DeleteSource Condition=" '$(_DeleteSource)' == '' ">True diff --git a/build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/SetEnvironmentVariable.cs b/build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/SetEnvironmentVariable.cs new file mode 100644 index 00000000000..e46cd9f3d92 --- /dev/null +++ b/build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/SetEnvironmentVariable.cs @@ -0,0 +1,27 @@ +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +using System; + +namespace Xamarin.Android.BuildTools.PrepTasks +{ + public class SetEnvironmentVariable : Task + { + [Required] + public string Name { get; set; } + + [Required] + public string Value { get; set; } + + public override bool Execute () + { + Log.LogMessage (MessageImportance.Low, $"Task {nameof (SetEnvironmentVariable)}"); + Log.LogMessage (MessageImportance.Low, $" {nameof (Name)}: {Name}"); + Log.LogMessage (MessageImportance.Low, $" {nameof (Value)}: {Value}"); + + Environment.SetEnvironmentVariable(Name, Value); + + return !Log.HasLoggedErrors; + } + } +} diff --git a/build-tools/xa-prep-tasks/xa-prep-tasks.csproj b/build-tools/xa-prep-tasks/xa-prep-tasks.csproj index 3d1c11690a7..79a3e2045b1 100644 --- a/build-tools/xa-prep-tasks/xa-prep-tasks.csproj +++ b/build-tools/xa-prep-tasks/xa-prep-tasks.csproj @@ -45,6 +45,7 @@ + diff --git a/src/Mono.Android/Test/Mono.Android-Tests.csproj b/src/Mono.Android/Test/Mono.Android-Tests.csproj index 5250a5ee621..962d8b01fb2 100644 --- a/src/Mono.Android/Test/Mono.Android-Tests.csproj +++ b/src/Mono.Android/Test/Mono.Android-Tests.csproj @@ -102,12 +102,6 @@ - - android-toolchain - {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA} - False - False - Xamarin.Android.Build.Tasks {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}