Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .github/instructions/uitests.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This document provides specific guidance for GitHub Copilot when writing UI test
- Include one functional `[Category(UITestCategories.XYZ)]` attribute per test
- CollectionView tests use `[ShardedTestCategory(UITestCategories.CollectionView, shard: N)]`, which exposes both the `CollectionView` umbrella and one numbered CI shard category
- Shell tests use `[ShardedTestCategory(UITestCategories.Shell, shard: N)]`; CI uses `Shell1` and `Shell2` on Android, iOS, Windows, and MacCatalyst, while local umbrella runs continue to use `Shell`
- Layout tests use `[ShardedTestCategory(UITestCategories.Layout, shard: N)]`; CI uses `Layout1`, `Layout2`, and `Layout3` on every platform, while local umbrella runs continue to use `Layout`
- Omitted `shard` defaults to shard 1 so a new test cannot silently cease running; the rebalancing skill assigns the durable shard
- Put `ShardedTestCategory` on each test method; never put it on the class
- A fixture split across shards must initialize every shard in `FixtureSetup`; never use an `Order(1)` test as setup for later tests
Expand Down Expand Up @@ -150,7 +151,7 @@ public class Issue12345 : _IssuesUITest
public Issue12345(TestDevice device) : base(device) { }

[Test]
[Category(UITestCategories.Layout)] // Pick the most appropriate category
[ShardedTestCategory(UITestCategories.Layout, shard: 1)]
public void ButtonClickUpdatesLabel()
{
// Wait for element to be ready
Expand Down
28 changes: 26 additions & 2 deletions .github/skills/rebalance-ui-test-categories/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: rebalance-ui-test-categories
description: Rebalances a UI-test umbrella category into additive method-level CI shards using historical Azure DevOps test durations.
metadata:
author: dotnet-maui
version: "1.0"
compatibility: Requires PowerShell 7 and Azure CLI with the azure-devops extension.
version: "1.1"
compatibility: Requires PowerShell 7, the restored UITest.Analyzers Roslyn dependency, and Azure CLI with the azure-devops extension.
---

# Rebalance UI Test Categories
Expand All @@ -16,6 +16,10 @@ matrix legs and each leg must fit a target duration.

- Historical Azure test results are required. Never substitute source test
counts for timing evidence.
- Inventory, category edits, and mandatory validation use the existing analyzer
dependency's Roslyn lexer to ignore comments and literals consistently. The
lexical view preserves character offsets and line breaks and visits every
preprocessor branch; edits leave comment and literal contents unchanged.
- Use one method-level
`[ShardedTestCategory(UITestCategories.Category, shard: N)]` attribute. It
exposes both the umbrella and exactly one numbered shard category to NUnit.
Expand All @@ -32,9 +36,23 @@ matrix legs and each leg must fit a target duration.
- Applying a category maintains the analyzer's reusable shard-prefix set,
preserving previously registered umbrellas, and performs mandatory
plan-to-source validation after editing.
- Platform-alternative declarations share one logical method ID and historical
weight only when their possible platform sets are disjoint. The inventory
proves simple `ANDROID`, `IOS`, `MACCATALYST`, and `WINDOWS` disjunctions,
including nested `#if`/`#elif`/`#else` branches. Other conditions are treated
conservatively as potentially active on every platform; overlapping or
unproven duplicate declarations fail.
- Schema 2 plans record every declaration's relative file, source line, and
possible platforms. All declarations receive the logical method's shard and
are validated against the source; none are removed or counted twice for
timing. Schema 1 plans remain applicable only to methods without alternatives.
- Before splitting methods from one fixture across shards, ensure fixture setup
independently reaches the state required by every selected method. Never rely
on an `Order(1)` test to initialize the remaining shard.
- For a fixture with implicit state dependencies but no `Order` attributes, pass
its fully qualified class name through `-CohesiveFixture`. Unknown names fail.
This uses the existing ordered-fixture grouping and scheduling mechanism,
preserving test bodies and ordering without introducing setup or test changes.
- Dedicated configuration stages that use `testConfigurationArgs`, such as
`ios_ui_tests_mono_cv1`, reuse the numbered shard filters but remain outside
ordinary category-matrix projection data. Their samples and overhead are
Expand All @@ -43,6 +61,12 @@ matrix legs and each leg must fit a target duration.

## Commands

Restore the existing C# lexer dependency before gathering or applying:

```bash
dotnet restore src/TestUtils/src/UITest.Analyzers/UITest.Analyzers.csproj
```

Gather, plan, report, and apply in one operation:

```powershell
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Maui.UITestCategories
{
public static class CSharpCodeMasker
{
public static string Mask(string text)
{
// Lex every platform branch without evaluating its preprocessor condition.
var lexicalInput = Regex.Replace(text, @"(?m)^[ \t]*#", match =>
match.Value.Substring(0, match.Length - 1) + " ");
var characters = text.ToCharArray();
foreach (var token in SyntaxFactory.ParseTokens(
lexicalInput, options: new CSharpParseOptions(LanguageVersion.Preview)))
{
var kind = token.Kind().ToString();
if (kind.EndsWith("Token", StringComparison.Ordinal) &&
(kind.Contains("String") || kind == "CharacterLiteralToken"))
{
MaskSpan(characters, token.Span.Start, token.Span.Length);
}
foreach (var trivia in token.LeadingTrivia)
MaskComment(characters, trivia);
foreach (var trivia in token.TrailingTrivia)
MaskComment(characters, trivia);
}
return new string(characters);
}

static void MaskComment(char[] characters, SyntaxTrivia trivia)
{
if (trivia.IsKind(SyntaxKind.SingleLineCommentTrivia) ||
trivia.IsKind(SyntaxKind.MultiLineCommentTrivia) ||
trivia.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia) ||
trivia.IsKind(SyntaxKind.MultiLineDocumentationCommentTrivia))
{
MaskSpan(characters, trivia.Span.Start, trivia.Span.Length);
}
}

static void MaskSpan(char[] characters, int start, int length)
{
for (var index = start; index < start + length; index++)
if (characters[index] != '\r' && characters[index] != '\n')
characters[index] = ' ';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ param(
[int]$MinimumShards = 1,
[ValidateSet('Fail', 'ClassPlatformMax')]
[string]$UnmeasuredTestPolicy = 'Fail',
[string[]]$CohesiveFixture = @(),
[switch]$Apply,
[string]$RepositoryRoot
)
Expand Down Expand Up @@ -75,7 +76,8 @@ if ($Mode -in @('Plan', 'All')) {
}
$plan = New-UITestShardPlan -Category $Category -Inventory $inventory -Evidence $evidence `
-TargetMinutes $TargetMinutes -SafetyMarginMinutes $SafetyMarginMinutes `
-MaxShards $MaxShards -MinimumShards $MinimumShards -UnmeasuredTestPolicy $UnmeasuredTestPolicy
-MaxShards $MaxShards -MinimumShards $MinimumShards -UnmeasuredTestPolicy $UnmeasuredTestPolicy `
-CohesiveFixture $CohesiveFixture
Write-JsonFile -Value $plan -Path $OutputPath
Write-Host "Planned $(@($plan.assignments).Count) tests across $($plan.shardCount) shards; worst projection $($plan.maxProjectedMinutes)m -> $OutputPath"
}
Expand Down
Loading
Loading