如何编译和测试仅安装的目标框架?

huangapple go评论106阅读模式
英文:

How to compile+test for only installed target frameworks?

问题

这是一个确切的重复问题:

但我正在寻找一个可行的解决方案,因为以下解决方案对我不起作用:

我需要支持6.0图像:

  1. docker run -t mcr.microsoft.com/dotnet/sdk:6.0 dotnet --list-sdks
  2. 6.0.410 [/usr/share/dotnet/sdk]
  3. docker run -t mcr.microsoft.com/dotnet/sdk:6.0 dotnet build --version
  4. .NETMSBuild版本为17.3.2+561848881
  5. 17.3.2.46306

以及7.0图像:

  1. docker run -t mcr.microsoft.com/dotnet/sdk:7.0 dotnet --list-sdks
  2. 7.0.305 [/usr/share/dotnet/sdk]
  3. docker run -t mcr.microsoft.com/dotnet/sdk:7.0 dotnet build --version
  4. .NETMSBuild版本为17.6.3+07e294721
  5. 17.6.3.22601

换句话说,我应该用什么替代以下天真的尝试?

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. <PropertyGroup Condition="'$(VisualStudioVersion)'=='16.0'">
  3. <TargetFramework>net6.0</TargetFramework>
  4. </PropertyGroup>
  5. <PropertyGroup Condition="'$(VisualStudioVersion)'=='17.0'">
  6. <TargetFrameworks>net7.0</TargetFrameworks>
  7. </PropertyGroup>

microsoft.com论坛上,我也找不到可行的解决方案:


从技术上讲,具有安装了net7.0的图像可以构建net6.0目标框架,但它未能通过测试步骤。复现问题的完整步骤:

  1. docker run -it mcr.microsoft.com/dotnet/sdk:7.0
  2. # cd /tmp
  3. # dotnet new sln -o unit-testing-using-dotnet-test
  4. # cd unit-testing-using-dotnet-test
  5. # dotnet new classlib -o PrimeService
  6. # mv PrimeService/Class1.cs PrimeService.cs
  7. # dotnet sln add ./PrimeService/PrimeService.csproj
  8. # dotnet new xunit -o PrimeService.Tests
  9. # dotnet add ./PrimeService.Tests/PrimeService.Tests.csproj reference ./PrimeService/PrimeService.csproj
  10. # dotnet sln add ./PrimeService.Tests/PrimeService.Tests.csproj

现在将其移动到net6.0并观察它可以构建,但测试不再有效:

  1. # sed -e 's/net7.0/net6.0/' -i ./PrimeService/PrimeService.csproj
  2. # sed -e 's/net7.0/net6.0/' -i ./PrimeService.Tests/PrimeService.Tests.csproj

给出:

  1. # dotnet build
  2. .NETMSBuild版本为17.6.3+07e294721
  3. 确定要还原的项目...
  4. 已还原 /tmp/unit-testing-using-dotnet-test/PrimeService/PrimeService.csproj(在696毫秒内)。
  5. 已还原 /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/PrimeService.Tests.csproj(在752毫秒内)。
  6. PrimeService -> /tmp/unit-testing-using-dotnet-test/PrimeService/bin/Debug/net6.0/PrimeService.dll
  7. PrimeService.Tests -> /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll
  8. 构建成功。
  9. 0个警告
  10. 0个错误
  11. 经过时间00:00:03.71

但:

  1. # dotnet test
  2. 确定要还原的项目...
  3. 所有项目都已为还原而升级。
  4. PrimeService -> /tmp/unit-testing-using-dotnet-test/PrimeService/bin/Debug/net6.0/PrimeService.dll
  5. PrimeService.Tests -> /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll
  6. /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll的测试运行(.NETCoreApp,版本=v6.0
  7. MicrosoftR)测试执行命令行工具版本17.6.0x64
  8. 版权所有(cMicrosoft Corporation。保留所有权利。
  9. 开始测试执行,请稍候...
  10. 共有1个测试文件与指定的模式匹配。
  11. 为/source(s)/tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll'的测试宿主进程以错误退出:您必须安装或更新.NET才能运行此应用程序。
  12. 应用程序:/tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/testhost.dll
  13. 架构:x64
  14. Framework:'Microsoft.NETCore.App',版本'6.0.0'(x64)
  15. .NET位置:/usr/share/dotnet/
  16. 找到以下框架:
  17. 7.0.8位于[/usr/share/dotnet/shared/Microsoft.NETCore.App]
  18. 了解框架解析:
  19. https://aka.ms/dotnet/app-launch-failed
  20. 要安装丢失的框架,请下载:
  21. https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=6.0.0&arch=x64&rid=debian.11-x64
  22. 。请检查诊断日志以获取更多信息。
  23. 测试运行已中止。
英文:

This is an exact duplicate of:

But I am looking for a working solution, since the following does not work for me:

I need to support 6.0 image:

  1. % docker run -t mcr.microsoft.com/dotnet/sdk:6.0 dotnet --list-sdks
  2. 6.0.410 [/usr/share/dotnet/sdk]
  3. % docker run -t mcr.microsoft.com/dotnet/sdk:6.0 dotnet build --version
  4. MSBuild version 17.3.2+561848881 for .NET
  5. 17.3.2.46306

as well as 7.0 image:

  1. % docker run -t mcr.microsoft.com/dotnet/sdk:7.0 dotnet --list-sdks
  2. 7.0.305 [/usr/share/dotnet/sdk]
  3. % docker run -t mcr.microsoft.com/dotnet/sdk:7.0 dotnet build --version
  4. MSBuild version 17.6.3+07e294721 for .NET
  5. 17.6.3.22601

In other word, what should I replace the following naive attempt with ?

  1. &lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
  2. &lt;PropertyGroup Condition=&quot;&#39;$(VisualStudioVersion)&#39;==&#39;16.0&#39;&quot;&gt;
  3. &lt;TargetFramework&gt;net6.0&lt;/TargetFramework&gt;
  4. &lt;/PropertyGroup&gt;
  5. &lt;PropertyGroup Condition=&quot;&#39;$(VisualStudioVersion)&#39;==&#39;17.0&#39;&quot;&gt;
  6. &lt;TargetFrameworks&gt;net7.0&lt;/TargetFrameworks&gt;
  7. &lt;/PropertyGroup&gt;

I could not find no working solution either on microsoft.com forum:


Technically the image with net7.0 installed can build net6.0 target framework, however it is failing the test step. Full steps to reproduce issue:

  1. % docker run -it mcr.microsoft.com/dotnet/sdk:7.0
  2. # cd /tmp
  3. # dotnet new sln -o unit-testing-using-dotnet-test
  4. # cd unit-testing-using-dotnet-test
  5. # dotnet new classlib -o PrimeService
  6. # mv PrimeService/Class1.cs PrimeService.cs
  7. # dotnet sln add ./PrimeService/PrimeService.csproj
  8. # dotnet new xunit -o PrimeService.Tests
  9. # dotnet add ./PrimeService.Tests/PrimeService.Tests.csproj reference ./PrimeService/PrimeService.csproj
  10. # dotnet sln add ./PrimeService.Tests/PrimeService.Tests.csproj

now move it to net6.0 and observe it builds fine, but test do not work anymore:

  1. # sed -e &#39;s/net7.0/net6.0/&#39; -i ./PrimeService/PrimeService.csproj
  2. # sed -e &#39;s/net7.0/net6.0/&#39; -i ./PrimeService.Tests/PrimeService.Tests.csproj

gives:

  1. # dotnet build
  2. MSBuild version 17.6.3+07e294721 for .NET
  3. Determining projects to restore...
  4. Restored /tmp/unit-testing-using-dotnet-test/PrimeService/PrimeService.csproj (in 696 ms).
  5. Restored /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/PrimeService.Tests.csproj (in 752 ms).
  6. PrimeService -&gt; /tmp/unit-testing-using-dotnet-test/PrimeService/bin/Debug/net6.0/PrimeService.dll
  7. PrimeService.Tests -&gt; /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll
  8. Build succeeded.
  9. 0 Warning(s)
  10. 0 Error(s)
  11. Time Elapsed 00:00:03.71

but:

  1. # dotnet test
  2. Determining projects to restore...
  3. All projects are up-to-date for restore.
  4. PrimeService -&gt; /tmp/unit-testing-using-dotnet-test/PrimeService/bin/Debug/net6.0/PrimeService.dll
  5. PrimeService.Tests -&gt; /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll
  6. Test run for /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll (.NETCoreApp,Version=v6.0)
  7. Microsoft (R) Test Execution Command Line Tool Version 17.6.0 (x64)
  8. Copyright (c) Microsoft Corporation. All rights reserved.
  9. Starting test execution, please wait...
  10. A total of 1 test files matched the specified pattern.
  11. Testhost process for source(s) &#39;/tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll&#39; exited with error: You must install or update .NET to run this application.
  12. App: /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/testhost.dll
  13. Architecture: x64
  14. Framework: &#39;Microsoft.NETCore.App&#39;, version &#39;6.0.0&#39; (x64)
  15. .NET location: /usr/share/dotnet/
  16. The following frameworks were found:
  17. 7.0.8 at [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  18. Learn about framework resolution:
  19. https://aka.ms/dotnet/app-launch-failed
  20. To install missing framework, download:
  21. https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=6.0.0&arch=x64&rid=debian.11-x64
  22. . Please check the diagnostic logs for more information.
  23. Test Run Aborted.

答案1

得分: 1

我找到了一个小技巧,你只需要在项目文件中有一个 PropertyGroup,核心思想是使用 -p 参数来替换 Targetframeworks 属性的值,然后使用 for 来获取每个 SDK 版本并构建每个版本。

  1. for /f "eol= tokens=1,2 delims=. " %i in ('dotnet --list-sdks') do
  2. dotnet build -p:TargetFrameworks=netcoreapp%i.%j
英文:

I find a small trick, you need only one PropertyGroup in the project file, the core idea is to use the -p argument to replace the value of the Targetframeworks property, and use for to obtain each SDK versions and build each one.

  1. for /f &quot;eol= tokens=1,2 delims=. &quot; %i in (&#39;dotnet --list-sdks&#39;) do
  2. dotnet build -p:TargetFrameworks=netcoreapp%i.%j

答案2

得分: 0

感谢@shingo的建议,我能够提供以下的gitlab-ci配置:

  1. build:
  2. stage: build
  3. extends: .build-matrix
  4. image: $IMAGE
  5. script:
  6. - |
  7. for sdk in $(dotnet --list-sdks | cut -f1-2 -d'.'); do
  8. dotnet build -p:TargetFrameworks=net${sdk}
  9. done

同样的配置也适用于测试部分:

  1. tests:
  2. stage: test
  3. extends: .build-matrix
  4. image: $IMAGE
  5. script:
  6. - |
  7. for sdk in $(dotnet --list-sdks | cut -f1-2 -d'.'); do
  8. dotnet test -p:TargetFrameworks=net${sdk}
  9. done
英文:

Thanks to @shingo suggestion I was able to come up with the following in my gitlab-ci:

  1. build:
  2. stage: build
  3. extends: .build-matrix
  4. image: $IMAGE
  5. script:
  6. - |
  7. for sdk in $(dotnet --list-sdks | cut -f1-2 -d&#39;.&#39;); do
  8. dotnet build -p:TargetFrameworks=net${sdk}
  9. done

and same goes for test:

  1. tests:
  2. stage: test
  3. extends: .build-matrix
  4. image: $IMAGE
  5. script:
  6. - |
  7. for sdk in $(dotnet --list-sdks | cut -f1-2 -d&#39;.&#39;); do
  8. dotnet test -p:TargetFrameworks=net${sdk}
  9. done

huangapple
  • 本文由 发表于 2023年7月4日 22:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定