Skip to content

Commit ce9f2cd

Browse files
authored
Filter src by framework for tests build (#4244)
* Separate build and test into parallel jobs * Filter source projects by framework for tests build
1 parent 9059721 commit ce9f2cd

3 files changed

Lines changed: 31 additions & 20 deletions

File tree

.github/workflows/New-FrameworkFilteredSolution.ps1 renamed to .github/workflows/New-FilteredSolution.ps1

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
<#
55
.SYNOPSIS
6-
Generates a filtered .slnx solution file that only includes test projects supporting a given target framework.
6+
Generates a filtered .slnx solution file by removing projects that don't match the specified criteria.
77
88
.DESCRIPTION
9-
Parses a .slnx solution file and queries each test project's TargetFrameworks using MSBuild.
10-
Removes test projects that don't support the specified target framework, writes the result
11-
to a temporary or specified output path, and prints the output path.
12-
13-
This is useful for running `dotnet test --solution` with MTP (Microsoft Testing Platform),
14-
which requires all test projects in the solution to support the requested target framework.
9+
Parses a .slnx solution file and applies one or more filters:
10+
- Removes projects that don't support the specified target framework (via MSBuild query).
11+
- Optionally removes all sample projects (under samples/).
12+
- Optionally filters test projects by name pattern (e.g., only *UnitTests*).
13+
Writes the filtered solution to the specified output path and prints the path.
1514
1615
.PARAMETER Solution
1716
Path to the source .slnx solution file.
@@ -34,16 +33,16 @@
3433
3534
.EXAMPLE
3635
# Generate a filtered solution and run tests
37-
$filtered = ./eng/New-FilteredSolution.ps1 -Solution ./agent-framework-dotnet.slnx -TargetFramework net472
36+
$filtered = .github/workflows/New-FilteredSolution.ps1 -Solution dotnet/agent-framework-dotnet.slnx -TargetFramework net472
3837
dotnet test --solution $filtered --no-build -f net472
3938
4039
.EXAMPLE
4140
# Generate a solution with only unit test projects
42-
./eng/New-FilteredSolution.ps1 -Solution ./agent-framework-dotnet.slnx -TargetFramework net10.0 -TestProjectNameFilter "*UnitTests*" -OutputPath filtered-unit.slnx
41+
.github/workflows/New-FilteredSolution.ps1 -Solution dotnet/agent-framework-dotnet.slnx -TargetFramework net10.0 -TestProjectNameFilter "*UnitTests*" -OutputPath filtered-unit.slnx
4342
4443
.EXAMPLE
4544
# Inline usage with dotnet test (PowerShell)
46-
dotnet test --solution (./eng/New-FilteredSolution.ps1 -Solution ./agent-framework-dotnet.slnx -TargetFramework net472) --no-build -f net472
45+
dotnet test --solution (.github/workflows/New-FilteredSolution.ps1 -Solution dotnet/agent-framework-dotnet.slnx -TargetFramework net472) --no-build -f net472
4746
#>
4847

4948
[CmdletBinding()]
@@ -91,16 +90,17 @@ if ($ExcludeSamples) {
9190
Write-Host "Removed $($sampleProjects.Count) sample project(s)." -ForegroundColor Yellow
9291
}
9392

94-
# Find all Project elements with paths containing "tests/"
95-
$testProjects = $slnx.SelectNodes("//Project[contains(@Path, 'tests/')]")
93+
# Filter all remaining projects by target framework
94+
$allProjects = $slnx.SelectNodes("//Project")
9695

97-
foreach ($proj in $testProjects) {
96+
foreach ($proj in $allProjects) {
9897
$projRelPath = $proj.GetAttribute("Path")
9998
$projFullPath = Join-Path $solutionDir $projRelPath
10099
$projFileName = Split-Path $projRelPath -Leaf
100+
$isTestProject = $projRelPath -like "*tests/*"
101101

102-
# Filter by project name pattern if specified
103-
if ($TestProjectNameFilter -and ($projFileName -notlike $TestProjectNameFilter)) {
102+
# Filter test projects by name pattern if specified
103+
if ($isTestProject -and $TestProjectNameFilter -and ($projFileName -notlike $TestProjectNameFilter)) {
104104
Write-Verbose "Removing (name filter): $projRelPath"
105105
$removed += $projRelPath
106106
$proj.ParentNode.RemoveChild($proj) | Out-Null
@@ -139,7 +139,7 @@ if ($removed.Count -gt 0) {
139139
Write-Host " - $r" -ForegroundColor Yellow
140140
}
141141
}
142-
Write-Host "Kept $($kept.Count) test project(s)." -ForegroundColor Green
142+
Write-Host "Kept $($kept.Count) project(s)." -ForegroundColor Green
143143

144144
# Output the path for piping
145145
Write-Output $OutputPath

.github/workflows/dotnet-build-and-test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172
- name: Generate test solution (no samples)
173173
shell: pwsh
174174
run: |
175-
.github/workflows/New-FrameworkFilteredSolution.ps1 `
175+
.github/workflows/New-FilteredSolution.ps1 `
176176
-Solution dotnet/agent-framework-dotnet.slnx `
177177
-TargetFramework ${{ matrix.targetFramework }} `
178178
-Configuration ${{ matrix.configuration }} `
@@ -193,10 +193,10 @@ jobs:
193193
Configuration = "${{ matrix.configuration }}"
194194
Verbose = $true
195195
}
196-
.github/workflows/New-FrameworkFilteredSolution.ps1 @commonArgs `
196+
.github/workflows/New-FilteredSolution.ps1 @commonArgs `
197197
-TestProjectNameFilter "*UnitTests*" `
198198
-OutputPath dotnet/filtered-unit.slnx
199-
.github/workflows/New-FrameworkFilteredSolution.ps1 @commonArgs `
199+
.github/workflows/New-FilteredSolution.ps1 @commonArgs `
200200
-TestProjectNameFilter "*IntegrationTests*" `
201201
-OutputPath dotnet/filtered-integration.slnx
202202

dotnet/.github/skills/build-and-test/SKILL.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,18 @@ Tests use the [Microsoft Testing Platform](https://learn.microsoft.com/dotnet/co
101101

102102
```bash
103103
# Run all unit tests across the solution, ignoring projects with no matching tests
104-
dotnet test --solution ./agent-framework-dotnet.slnx --no-build -f net10.0
104+
dotnet test --solution ./agent-framework-dotnet.slnx --no-build -f net10.0 --ignore-exit-code 8
105+
```
106+
107+
- **Running tests with `--solution` for a specific TFM** requires all projects in the solution to support that TFM. Not all projects target every framework (e.g., some are `net10.0`-only). Use `.github/workflows/New-FilteredSolution.ps1` to generate a filtered solution:
108+
109+
```powershell
110+
# Generate a filtered solution for net472 and run tests
111+
$filtered = .github/workflows/New-FilteredSolution.ps1 -Solution dotnet/agent-framework-dotnet.slnx -TargetFramework net472
112+
dotnet test --solution $filtered --no-build -f net472 --ignore-exit-code 8
113+
114+
# Exclude samples and keep only unit test projects
115+
.github/workflows/New-FilteredSolution.ps1 -Solution dotnet/agent-framework-dotnet.slnx -TargetFramework net10.0 -ExcludeSamples -TestProjectNameFilter "*UnitTests*" -OutputPath dotnet/filtered-unit.slnx
105116
```
106117

107118
```bash

0 commit comments

Comments
 (0)