Skip to content

Simplify test setup in scenario-tests and allow setting a TestRestoreConfigFile and TestDotNetRoot #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 22 additions & 56 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,39 @@

<Import Project="Sdk.targets" Sdk="Microsoft.DotNet.Arcade.Sdk" />

<PropertyGroup>
<TestRoot Condition="'$(TestRoot)' == ''">$(ArtifactsObjDir)generatedtests/</TestRoot>
<TestSdkVersion Condition="'$(TestSdkVersion)' == ''">$(NETCoreSdkVersion)</TestSdkVersion>
<!-- Allow executing the test host with a specific shared framework version (i.e. 10.0.0-ci).
This is necessary as i.e. in the VMR, the live built shared framework has a `-dev` or `-ci`
suffix which could be determined as "lower" than the toolset SDK's shared framework that the repo
builds with. As an example `10.0.0-ci` and `10.0.0-dev` are considered lower than `10.0.0-preview.1`
by the host (in accordance with semver2). -->
<TestFxVersionOverride Condition="'$(UseFxVersionOverride)' == 'true' and '$(TestFxVersionOverride)' == ''">$(BundledNETCoreAppPackageVersion)</TestFxVersionOverride>
<TestBinlogDir Condition="'$(TestBinlogDir)' == ''">$(ArtifactsTestResultsDir)</TestBinlogDir>
</PropertyGroup>

<Target Name="CleanTestRoot">
<!-- Clean the test root to avoid dirty content from a previous run -->
<RemoveDir Directories="$(TestRoot)" />
</Target>

<Target Name="CreateDirectoryBuildFiles">
<!-- Define Directory.Build.* files in the scenario tests artifacts directory to prevent the test run from
picking up the configuration from the VMR's Directory.Build.* files. We need an isolated configuration
from which to test. -->
<WriteLinesToFile File="$(TestRoot)Directory.Build.targets"
Lines="&lt;Project /&gt;"
Overwrite="true" />
<!-- Because the VMR (https://github.com/dotnet/dotnet) is configured to use CPM (https://github.com/dotnet/installer/pull/19286), it will
cause the scenario tests to fail with the following error:
Projects that use central package version management should not define the version on the PackageReference items but on the PackageVersion items: ...
To work around this, we explicitly disable CPM. Note that this can't be solved by including a stub Directory.Packages.props file because the default
behavior is to automatically default ManagePackageVersionsCentrally to true when it finds such a file.
https://github.com/NuGet/NuGet.Client/blob/ca13cf0a281b9774dd0238a43ab98c1927056cc2/src/NuGet.Core/NuGet.Build.Tasks/NuGet.props#L25-L33 -->
<PropertyGroup>
<_DirectoryBuildPropsContent>
&lt;Project&gt;
&lt;PropertyGroup&gt;
&lt;ManagePackageVersionsCentrally&gt;
false
&lt;/ManagePackageVersionsCentrally&gt;
&lt;/PropertyGroup&gt;
&lt;/Project&gt;
</_DirectoryBuildPropsContent>
</PropertyGroup>
<WriteLinesToFile File="$(TestRoot)Directory.Build.props"
Lines="$(_DirectoryBuildPropsContent)"
Overwrite="true" />
</Target>

<!-- Override the test target to run the test harness as a separate executable against dotnet SDK used to run these tests. -->
<Target Name="Test"
DependsOnTargets="CleanTestRoot;CreateDirectoryBuildFiles">
<Target Name="Test">
<PropertyGroup>
<TestArgs>--dotnet-root "$(DotNetRoot.TrimEnd('\'))"</TestArgs>
<TestArgs>$(TestArgs) --sdk-version $(TestSdkVersion)</TestArgs>
<TestRoot Condition="'$(TestRoot)' == ''">$([MSBuild]::NormalizeDirectory('$(ArtifactsObjDir)', 'generatedtests'))</TestRoot>
<TestBinlogDir Condition="'$(TestBinlogDir)' == ''">$(ArtifactsTestResultsDir)</TestBinlogDir>

<TestDotNetRoot Condition="'$(TestDotNetRoot)' == ''">$(DotNetRoot)</TestDotNetRoot>
<TestDotNetTool Condition="'$(OS)' == 'Windows_NT'">$([MSBuild]::NormalizePath('$(TestDotNetRoot)', 'dotnet.exe'))</TestDotNetTool>
<TestDotNetTool Condition="'$(OS)' != 'Windows_NT'">$([MSBuild]::NormalizePath('$(TestDotNetRoot)', 'dotnet'))</TestDotNetTool>

<TestArgs>--dotnet-root "$(TestDotNetRoot.TrimEnd('\'))"</TestArgs>
<TestArgs>$(TestArgs) --test-root "$(TestRoot.TrimEnd('\'))"</TestArgs>
<TestArgs>$(TestArgs) --binlog-dir "$(TestBinlogDir.TrimEnd('\'))"</TestArgs>
<TestArgs Condition="'$(TestSdkVersion)' != ''">$(TestArgs) --sdk-version $(TestSdkVersion)</TestArgs>
<TestArgs>$(TestArgs) $(AdditionalTestArgs)</TestArgs>

<_MSBuildSdksDir>$(DotNetRoot)sdk/$(TestSdkVersion)/Sdks</_MSBuildSdksDir>

<TestFxVersionOverrideCLI Condition="'$(TestFxVersionOverride)' != ''">--fx-version $(TestFxVersionOverride) </TestFxVersionOverrideCLI>
<!-- Allow executing the test host with a specific shared framework version (i.e. 10.0.0-ci).
This is necessary as i.e. in the VMR, the live built shared framework has a `-dev` or `-ci`
suffix which could be determined as "lower" than the toolset SDK's shared framework that the repo
builds with. As an example `10.0.0-ci` and `10.0.0-dev` are considered lower than `10.0.0-preview.1`
by the host (in accordance with semver2). -->
<TestFxVersionCLI Condition="'$(TestFxVersion)' != ''">--fx-version $(TestFxVersion) </TestFxVersionCLI>
</PropertyGroup>

<ItemGroup>
<_TestEnvVars Include="MSBuildSDKsPath=$(_MSBuildSdksDir)" />
<TestEnvironmentVariable Include="RestoreConfigFile=$(TestRestoreConfigFile)" Condition="'$(TestRestoreConfigFile)' != ''" />
</ItemGroup>

<Exec Command='"$(DotNetTool)" exec $(TestFxVersionOverrideCLI)"$(TargetPath)" $(TestArgs)'
EnvironmentVariables="@(_TestEnvVars)" />
<!-- Clean the test root to avoid dirty content from a previous run -->
<RemoveDir Directories="$(TestRoot)" />

<Exec Command='"$(TestDotNetTool)" exec $(TestFxVersionCLI)"$(TargetPath)" $(TestArgs)'
EnvironmentVariables="@(TestEnvironmentVariable)" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ private static void ConfigureProcess(Process process, string workingDirectory, s
process.StartInfo.EnvironmentVariables["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "1";
process.StartInfo.EnvironmentVariables["DOTNET_ROOT"] = dotnetRoot;
process.StartInfo.EnvironmentVariables["DOTNET_ROLL_FORWARD"] = "Major";
// Don't use the repo infrastructure
process.StartInfo.EnvironmentVariables["ImportDirectoryBuildProps"] = "false";
process.StartInfo.EnvironmentVariables["ImportDirectoryBuildTargets"] = "false";
process.StartInfo.EnvironmentVariables["ImportDirectoryPackagesProps"] = "false";

if (!string.IsNullOrEmpty(nugetPackagesDirectory))
{
Expand Down