Skip to content

Commit

Permalink
Adding linker correctness tests to test library annotations for the l…
Browse files Browse the repository at this point in the history
…inker (dotnet#37618)
  • Loading branch information
joperezr authored Jun 16, 2020
1 parent f246d59 commit 5182718
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 2 deletions.
7 changes: 7 additions & 0 deletions eng/pipelines/libraries/execute-trimming-tests-steps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
archType: ''

steps:
# Execute tests
- script: $(Build.SourcesDirectory)$(dir)build$(scriptExt) -ci -arch ${{ parameters.archType }} $(_osParameter) -s libs.tests -c $(_BuildConfig) /p:TestAssemblies=false /p:TestTrimming=true $(_officialBuildParameter) $(_crossBuildPropertyArg) /bl:$(Build.SourcesDirectory)/artifacts/log/$(buildConfigUpper)/TrimmingTests.binlog
displayName: Run Trimming Tests
39 changes: 39 additions & 0 deletions eng/pipelines/runtime-linker-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# The purpose of this pipeline is to exercise local developer workflow in the consolidated
# runtime repo. In particular, it is supposed to run the root "build" script just like any
# normal developer normally would and monitor regressions w.r.t. this fundamental scenario.

trigger:
batch: true
branches:
include:
- master
- release/*.*

pr:
branches:
include:
- master
- release/*.*

jobs:
#
# Checkout repository
#
- template: /eng/pipelines/common/checkout-job.yml

#
# Build Release config vertical for Windows, Linux, Linux musl and OSX
#
- template: /eng/pipelines/common/platform-matrix.yml
parameters:
jobTemplate: /eng/pipelines/common/global-build-job.yml
buildConfig: release
platforms:
- Windows_NT_x64
- OSX_x64
- Linux_x64
jobParameters:
testGroup: innerloop
nameSuffix: Runtime_Release
buildArgs: -s clr+libs -c $(_BuildConfig)
extraStepsTemplate: /eng/pipelines/libraries/execute-trimming-tests-steps.yml
10 changes: 10 additions & 0 deletions eng/testing/linker/SupportFiles/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<_TrimmerDefaultAction>link</_TrimmerDefaultAction>
<SelfContained>true</SelfContained>
<EnableTargetingPackDownload>false</EnableTargetingPackDownload>
<PlatformManifestFile />
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>
</Project>
24 changes: 24 additions & 0 deletions eng/testing/linker/SupportFiles/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project>
<Target Name="RestoreProject">
<MSBuild Projects="$(MSBuildProjectFullPath)"
Properties="Configuration=$(Configuration)"
Targets="Restore" />
</Target>

<Target Name="AddFrameworkReference">
<ItemGroup>
<FrameworkReference Include="Microsoft.NETCore.App" />
</ItemGroup>
</Target>

<Target Name="UpdateRuntimePack"
DependsOnTargets="AddFrameworkReference;ResolveFrameworkReferences">
<ItemGroup>
<ResolvedRuntimePack Update="@(ResolvedRuntimePack)" PackageDirectory="$(RuntimePackDir)" />
<ResolvedTargetingPack Update="@(ResolvedTargetingPack)" Path="$(TargetingPackDir)" />
</ItemGroup>
</Target>

<Target Name="PublishTrimmed" DependsOnTargets="RestoreProject;UpdateRuntimePack;Publish" />

</Project>
11 changes: 11 additions & 0 deletions eng/testing/linker/project.csproj.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>{NetCoreAppCurrent}</TargetFramework>
<RuntimeIdentifier>{RuntimeIdentifier}</RuntimeIdentifier>
<RuntimePackDir>{RuntimePackDir}</RuntimePackDir>
<TargetingPackDir>{TargetingPackDir}</TargetingPackDir>
</PropertyGroup>

</Project>
97 changes: 97 additions & 0 deletions eng/testing/linker/trimmingTests.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<Project>
<PropertyGroup>
<TrimmingTestDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'trimmingTests'))</TrimmingTestDir>
<TrimmingTestProjectsDir>$([MSBuild]::NormalizeDirectory('$(TrimmingTestDir)', 'projects'))</TrimmingTestProjectsDir>
<TestDotNetPath>$([MSBuild]::NormalizePath('$(DotNetRoot)', 'dotnet'))</TestDotNetPath>
<ProjectTemplate>$(MSBuildThisFileDirectory)project.csproj.template</ProjectTemplate>
</PropertyGroup>

<ItemGroup>
<TestConsoleAppSourceFiles Include="$(MSBuildProjectDirectory)\*.cs" />

<TestSupportFiles Include="$(MSBuildThisFileDirectory)SupportFiles\Directory.Build.*">
<DestinationFolder>$(TrimmingTestDir)</DestinationFolder>
</TestSupportFiles>
</ItemGroup>

<Target Name="CreateTestDir"
Inputs="@(TestSupportFiles)"
Outputs="@(TestSupportFiles->'%(DestinationFolder)\%(FileName)%(Extension)')">
<MakeDir Directories="%(TestSupportFiles.DestinationFolder)" />
<Copy SourceFiles="@(TestSupportFiles)" DestinationFolder="%(TestSupportFiles.DestinationFolder)" />
</Target>

<Target Name="GetTestConsoleApps">
<ItemGroup>
<TestConsoleAppSourceFiles>
<ProjectDir>$([MSBuild]::NormalizeDirectory('$(TrimmingTestProjectsDir)', '$(MSBuildProjectName)', '%(Filename)', '$(PackageRID)'))</ProjectDir>
<TestRuntimeIdentifier>$(PackageRID)</TestRuntimeIdentifier>
</TestConsoleAppSourceFiles>
<!-- We need to separate Item metadata declaration in two in order to be able to use ProjectDir and TestRuntimeIdentifier bellow -->
<TestConsoleAppSourceFiles>
<ProjectFile>%(ProjectDir)project.csproj</ProjectFile>
<TestCommand>$([MSBuild]::NormalizePath('%(ProjectDir)', 'bin', '$(Configuration)', '$(NetCoreAppCurrent)', '%(TestRuntimeIdentifier)', 'publish', 'project'))</TestCommand>
<TestExecutionDirectory>$([MSBuild]::NormalizeDirectory('%(ProjectDir)', 'bin', '$(Configuration)', '$(NetCoreAppCurrent)', '%(TestRuntimeIdentifier)', 'publish'))</TestExecutionDirectory>
<RuntimePackDirectory>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'microsoft.netcore.app.runtime.%(TestRuntimeIdentifier)', '$(Configuration)'))</RuntimePackDirectory>
</TestConsoleAppSourceFiles>
</ItemGroup>

<ItemGroup>
<TestConsoleApps Include="@(TestConsoleAppSourceFiles->'%(ProjectFile)')">
<ProjectCompileItems>%(Identity)</ProjectCompileItems>
</TestConsoleApps>
</ItemGroup>
</Target>

<Target Name="GenerateProjects"
DependsOnTargets="GetTestConsoleApps;CreateTestDir"
Inputs="@(TestConsoleAppSourceFiles);$(ProjectTemplate);@(TestSupportFiles)"
Outputs="%(TestConsoleApps.Identity)">
<PropertyGroup>
<_projectDir>%(TestConsoleApps.ProjectDir)\</_projectDir>
<_projectFile>%(TestConsoleApps.ProjectFile)</_projectFile>
<_projectSourceFile>%(TestConsoleApps.ProjectCompileItems)</_projectSourceFile>
</PropertyGroup>

<MakeDir Directories="$(_projectDir)" />
<WriteLinesToFile File="$(_projectFile)"
Lines="$([System.IO.File]::ReadAllText('$(ProjectTemplate)').Replace('{NetCoreAppCurrent}', '$(NetCoreAppCurrent)').Replace('{RuntimePackDir}', '%(TestConsoleApps.RuntimePackDirectory)').Replace('{TargetingPackDir}','$(MicrosoftNetCoreAppRefPackDir)').Replace('{RuntimeIdentifier}','%(TestConsoleApps.TestRuntimeIdentifier)'))"
Overwrite="true" />
<Copy SourceFiles="$(_projectSourceFile)"
DestinationFolder="$(_projectDir)" />
<Message Text="Generated $(_projectFile)" />
</Target>

<Target Name="PublishTrimmedProjects"
DependsOnTargets="GenerateProjects">
<PropertyGroup>
<TestRestoreCommand>"$(TestDotNetPath)"</TestRestoreCommand>
<TestRestoreCommand>$(TestRestoreCommand) msbuild /t:PublishTrimmed</TestRestoreCommand>
<TestRestoreCommand>$(TestRestoreCommand) /nr:false</TestRestoreCommand>
<TestRestoreCommand>$(TestRestoreCommand) /warnaserror</TestRestoreCommand>
<TestRestoreCommand>$(TestRestoreCommand) -p:configuration=Release</TestRestoreCommand>
</PropertyGroup>

<Exec Command="$(TestRestoreCommand)" StandardOutputImportance="Low" WorkingDirectory="%(TestConsoleApps.ProjectDir)" />
</Target>

<Target Name="ExecuteApplications"
DependsOnTargets="PublishTrimmedProjects"
Inputs="%(TestConsoleApps.Identity)"
Outputs="_unused"
Condition="'$(ArchiveTests)' != 'true'">

<Message Importance="High" Text="[Trimming Tests] Running test: %(TestConsoleApps.ProjectCompileItems)..." />
<Exec IgnoreExitCode="true" Command="%(TestConsoleApps.TestCommand)" StandardOutputImportance="Low" WorkingDirectory="%(TestConsoleApps.TestExecutionDirectory)">
<Output TaskParameter="ExitCode" PropertyName="ExecutionExitCode" />
</Exec>

<Error Condition="'$(ExecutionExitCode)' != '100'" Text="Error: [Failed Test]: %(TestConsoleApps.ProjectCompileItems) The Command %(TestConsoleApps.TestCommand) return a non-success exit code." />
</Target>

<Target Name="Build" DependsOnTargets="ExecuteApplications" />

<!-- define test to do nothing, for this project Build does all the testing -->
<Target Name="Test" DependsOnTargets="Build" />
<Target Name="VSTest" DependsOnTargets="Build" />
</Project>
2 changes: 2 additions & 0 deletions src/libraries/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
<IsTestSupportProject>false</IsTestSupportProject>
<IsTestSupportProject Condition="($(MSBuildProjectFullPath.Contains('\tests\')) or $(MSBuildProjectFullPath.Contains('/tests/'))) and '$(IsTestProject)' != 'true'">true</IsTestSupportProject>

<IsTrimmingTestProject Condition="$(MSBuildProjectName.EndsWith('.TrimmingTests'))">true</IsTrimmingTestProject>

<!-- Treat test assemblies as non-shipping (do not publish or sign them). -->
<IsShipping Condition="'$(IsTestProject)' == 'true' or '$(IsTestSupportProject)' == 'true'">false</IsShipping>
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Import Project="$(RepositoryEngineeringDir)references.targets" />
<Import Project="$(RepositoryEngineeringDir)resolveContract.targets" />
<Import Project="$(RepositoryEngineeringDir)testing\tests.targets" Condition="'$(EnableTestSupport)' == 'true'" />
<Import Project="$(RepositoryEngineeringDir)testing\linker\trimmingTests.targets" Condition="'$(IsTrimmingTestProject)' == 'true'" />
<Import Project="$(RepositoryEngineeringDir)testing\runtimeConfiguration.targets" />
<Import Project="$(RepositoryEngineeringDir)testing\runsettings.targets" Condition="'$(EnableRunSettingsSupport)' == 'true'" />
<Import Project="$(RepositoryEngineeringDir)testing\coverage.targets" Condition="'$(EnableRunSettingsSupport)' == 'true' or '$(EnableCoverageSupport)' == 'true'" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections;

class Program
{
static int Main(string[] args)
{
var x = new[] { 5, 4, 3, 100, 2, 1 };
var y = new[] { "a", "b", "c", "h", "d", "e" };
// This will test that GenericArraySortHelper'2 called by reflection will be kept.
Array.Sort(x, y);
// This will test that GenericArraySortHelper'1 called by reflection will be kept.
Array.Sort(y);
return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project DefaultTargets="Build">
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
</Project>
2 changes: 1 addition & 1 deletion src/libraries/restore/runtime/runtime.depproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
</Target>

<Target Name="SetupRuntimePackNative"
Condition="'$(RuntimeFlavor)' == 'Mono'"
AfterTargets="AfterResolveReferences">
<ItemGroup>
<BinPlaceItem Include="@(RuntimeFiles)" />
Expand All @@ -90,6 +89,7 @@
<BinPlaceItem Include="@(MonoIncludeFiles)">
<DestinationSubDirectory>include/%(RecursiveDir)</DestinationSubDirectory>
</BinPlaceItem>
<BinPlaceItem Remove="@(BinPlaceItem)" Condition="'$(RuntimeFlavor)' != 'Mono' and '%(BinPlaceItem.Filename)' == 'System.Private.CoreLib'" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsMobile)' != 'true'">
<TestHostBinPlaceItem Include="@(RuntimeFiles)" />
Expand Down
10 changes: 9 additions & 1 deletion src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<CoverageReportInputPath>$(ArtifactsBinDir)\*.Tests\**\coverage.opencover.xml</CoverageReportInputPath>
<CoverageReportDir>$(ArtifactsDir)coverage</CoverageReportDir>
<EnableCoverageSupport>true</EnableCoverageSupport>

<TestAssemblies Condition="'$(TestAssemblies)' == ''">true</TestAssemblies>
<TestTrimming Condition="'$(TestTrimming)' == ''">false</TestTrimming>
</PropertyGroup>

<!-- Projects that don't support code coverage measurement. -->
Expand All @@ -17,7 +20,7 @@
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Transactions.Local\tests\System.Transactions.Local.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TestAssemblies)' == 'true'">
<!-- We currently only test with C# projects. -->
<ProjectReference Include="$(MSBuildThisFileDirectory)*\tests\**\*.Tests.csproj"
Exclude="@(ProjectExclusions)"
Expand All @@ -26,6 +29,11 @@
Condition="'$(BuildAllConfigurations)' == 'true'" />
</ItemGroup>

<ItemGroup Condition="'$(TestTrimming)' == 'true'">
<ProjectReference Include="$(MSBuildThisFileDirectory)*\tests\**\*.TrimmingTests.proj"
Exclude="@(ProjectExclusions)" />
</ItemGroup>

<Target Name="GenerateMergedCoverageReport"
Condition="'$(Coverage)' == 'true'"
AfterTargets="Test"
Expand Down

0 comments on commit 5182718

Please sign in to comment.