Skip to content

Commit 9c4dba9

Browse files
benhillisBen HillisCopilot
authored
Refactor tests: use TAEF metadata for WSL version filtering (#40129)
Replace runtime WSL2_TEST_ONLY()/WSL1_TEST_ONLY() skip macros with TAEF metadata-based test selection. Tests that don't apply to the current WSL version are now excluded by /select: queries at selection time rather than skipped at runtime, eliminating hundreds of 'skipped' results from test output. Changes: - Add WSL2_TEST_METHOD, WSL1_TEST_METHOD, WSLC_TEST_METHOD macros in Common.h that tag tests with WSLVersion metadata property - Convert ~430 test methods across 26 files to use new macros - Update run-tests.ps1 to auto-add /select: version filter - Update CloudTest XML configs with version selection queries - Remove WSL2_TEST_ONLY() from composite macros in NetworkTests.cpp - Update test README with new macro documentation Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 01706c9 commit 9c4dba9

30 files changed

Lines changed: 611 additions & 1373 deletions

cloudtest/TestGroup-wslc.xml.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
</Setup>
2323

2424
<TestJob Name="CloudTest.Taef" TimeoutMins="120">
25-
<Execution Type="TAEF" Path="[WorkingDirectory]\wsltests.dll" Args="/p:bugReportDirectory=[LoggingDirectory]\BugReportOutput /errorOnCrash /testmode:etwlogger /EtwLogger:WPRProfileFile=[WorkingDirectory]\wsl.wprp /EtwLogger:WPRProfile=WSL /EtwLogger:SavePoint=ExecutionComplete /EtwLogger:RecordingScope=Execution /p:SetupScript=.\test-setup.ps1 /p:Package=[WorkingDirectory]\Microsoft.WSL_${PACKAGE_VERSION}_x64_ARM64.msixbundle /p:Version=2 /p:AllowUnsigned=${ALLOW_UNSIGNED_PACKAGE} /p:UnitTestsPath=[WorkingDirectory]\unit_tests /p:DistroPath=[WorkingDirectory]\test_distro.tar.xz /p:TestDataPath=[WorkingDirectory]\test_data /p:DistroName=test_distro /logOutput:High /p:RedirectStdout=[LoggingDirectory]\stdout.txt /p:RedirectStderr=[LoggingDirectory]\stderr.txt /p:KernelLogs=[LoggingDirectory]\dmesg.txt /p:DumpFolder=[LoggingDirectory] /p:WerReport /p:LogDmesg /p:PipelineBuildId=${PIPELINE_BUILD_ID} /p:DumpTool=DumpTool.exe /select:&quot;@TestCategory='WSLC'&quot;" />
25+
<Execution Type="TAEF" Path="[WorkingDirectory]\wsltests.dll" Args="/p:bugReportDirectory=[LoggingDirectory]\BugReportOutput /errorOnCrash /testmode:etwlogger /EtwLogger:WPRProfileFile=[WorkingDirectory]\wsl.wprp /EtwLogger:WPRProfile=WSL /EtwLogger:SavePoint=ExecutionComplete /EtwLogger:RecordingScope=Execution /p:SetupScript=.\test-setup.ps1 /p:Package=[WorkingDirectory]\Microsoft.WSL_${PACKAGE_VERSION}_x64_ARM64.msixbundle /p:Version=2 /p:AllowUnsigned=${ALLOW_UNSIGNED_PACKAGE} /p:UnitTestsPath=[WorkingDirectory]\unit_tests /p:DistroPath=[WorkingDirectory]\test_distro.tar.xz /p:TestDataPath=[WorkingDirectory]\test_data /p:DistroName=test_distro /logOutput:High /p:RedirectStdout=[LoggingDirectory]\stdout.txt /p:RedirectStderr=[LoggingDirectory]\stderr.txt /p:KernelLogs=[LoggingDirectory]\dmesg.txt /p:DumpFolder=[LoggingDirectory] /p:WerReport /p:LogDmesg /p:PipelineBuildId=${PIPELINE_BUILD_ID} /p:DumpTool=DumpTool.exe /select:&quot;@TestCategory='WSLC' and (@WSLVersion='2' or not(@WSLVersion='*'))&quot;" />
2626
</TestJob>
2727
</TestJobGroup>

cloudtest/TestGroup.xml.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
</Setup>
2323

2424
<TestJob Name="CloudTest.Taef" TimeoutMins="240">
25-
<Execution Type="TAEF" Path="[WorkingDirectory]\wsltests.dll" Args="/p:bugReportDirectory=[LoggingDirectory]\BugReportOutput /errorOnCrash /testmode:etwlogger /EtwLogger:WPRProfileFile=[WorkingDirectory]\wsl.wprp /EtwLogger:WPRProfile=WSL /EtwLogger:SavePoint=ExecutionComplete /EtwLogger:RecordingScope=Execution /p:SetupScript=.\test-setup.ps1 /p:Package=[WorkingDirectory]\Microsoft.WSL_${PACKAGE_VERSION}_x64_ARM64.msixbundle /p:Version=${version} /p:AllowUnsigned=${ALLOW_UNSIGNED_PACKAGE} /p:UnitTestsPath=[WorkingDirectory]\unit_tests /p:DistroPath=[WorkingDirectory]\test_distro.tar.xz /p:TestDataPath=[WorkingDirectory]\test_data /p:DistroName=test_distro /logOutput:High /p:RedirectStdout=[LoggingDirectory]\stdout.txt /p:RedirectStderr=[LoggingDirectory]\stderr.txt /p:KernelLogs=[LoggingDirectory]\dmesg.txt /p:DumpFolder=[LoggingDirectory] /p:WerReport /p:LogDmesg /p:PipelineBuildId=${PIPELINE_BUILD_ID} /p:DumpTool=DumpTool.exe /select:&quot;not(@TestCategory='WSLC')&quot;" />
25+
<Execution Type="TAEF" Path="[WorkingDirectory]\wsltests.dll" Args="/p:bugReportDirectory=[LoggingDirectory]\BugReportOutput /errorOnCrash /testmode:etwlogger /EtwLogger:WPRProfileFile=[WorkingDirectory]\wsl.wprp /EtwLogger:WPRProfile=WSL /EtwLogger:SavePoint=ExecutionComplete /EtwLogger:RecordingScope=Execution /p:SetupScript=.\test-setup.ps1 /p:Package=[WorkingDirectory]\Microsoft.WSL_${PACKAGE_VERSION}_x64_ARM64.msixbundle /p:Version=${version} /p:AllowUnsigned=${ALLOW_UNSIGNED_PACKAGE} /p:UnitTestsPath=[WorkingDirectory]\unit_tests /p:DistroPath=[WorkingDirectory]\test_distro.tar.xz /p:TestDataPath=[WorkingDirectory]\test_data /p:DistroName=test_distro /logOutput:High /p:RedirectStdout=[LoggingDirectory]\stdout.txt /p:RedirectStderr=[LoggingDirectory]\stderr.txt /p:KernelLogs=[LoggingDirectory]\dmesg.txt /p:DumpFolder=[LoggingDirectory] /p:WerReport /p:LogDmesg /p:PipelineBuildId=${PIPELINE_BUILD_ID} /p:DumpTool=DumpTool.exe /select:&quot;not(@TestCategory='WSLC') and (@WSLVersion='${version}' or not(@WSLVersion='*'))&quot;" />
2626
</TestJob>
2727
</TestJobGroup>

test/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ Below is a brief overview:
8181
8282
### Writing the Test
8383
84+
For tests that only apply to a specific WSL version, use the version-specific test method macros instead of `TEST_METHOD`:
85+
86+
- `WSL2_TEST_METHOD(Name)` — test only runs on WSL2
87+
- `WSL1_TEST_METHOD(Name)` — test only runs on WSL1
88+
- `WSLC_TEST_METHOD(Name)` — test only runs on WSL2 (for use in WSLC test classes)
89+
- `TEST_METHOD(Name)` — test runs on both WSL1 and WSL2
90+
91+
These macros use TAEF metadata properties to tag tests with their required WSL version.
92+
When tests are run via `run-tests.ps1` or CloudTest, a `/select:` query automatically
93+
filters out tests that don't match the target version—so they don't appear in results at all
94+
(no "skipped" noise).
95+
8496
For example, consider the file below, named `ExampleTest.cpp`:
8597
8698
```cpp
@@ -96,13 +108,19 @@ For example, consider the file below, named `ExampleTest.cpp`:
96108
{
97109
TEST_CLASS(ExampleTest) // define this as a test class
98110
99-
// add tests via test methods of the test class
100-
TEST_METHOD(HelloWorldTest) // ExampleTest::ExampleTest::HelloWorldTest
111+
// runs on both WSL1 and WSL2
112+
TEST_METHOD(HelloWorldTest)
101113
{
102114
std::wstring outputExpected = L"Linux on Windows Rocks!\n";
103115
auto [output, __] = LxsstuLaunchWslAndCaptureOutput(L"echo Linux on Windows Rocks!"); // from /test/Common.h
104116
VERIFY_ARE_EQUAL(output, outputExpected); // TAEF test method that passes if both are equal, and fails otherwise.
105117
}
118+
119+
// only runs on WSL2
120+
WSL2_TEST_METHOD(Wsl2OnlyTest)
121+
{
122+
// ...
123+
}
106124
};
107125
} //namespace ExampleTest
108126
```

test/windows/Common.h

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,29 @@ using namespace std::chrono_literals;
3838

3939
#define LXSST_REMOVE_DISTRO_CONF_COMMAND_LINE L"-u root -e rm /etc/wsl.conf"
4040

41-
#define WSL1_TEST_ONLY() \
42-
if (LxsstuVmMode()) \
43-
{ \
44-
LogSkipped("This test is only applicable to WSL1"); \
45-
return; \
46-
}
47-
48-
#define WSL2_TEST_ONLY() \
49-
if (!LxsstuVmMode()) \
50-
{ \
51-
LogSkipped("This test is only applicable to WSL2"); \
52-
return; \
53-
}
41+
//
42+
// Test method declaration macros that tag tests with TAEF metadata for version-based selection.
43+
// Use these instead of TEST_METHOD() for tests that only apply to a specific WSL version.
44+
// When run via run-tests.ps1 or CloudTest, inapplicable tests are excluded from the run
45+
// entirely (no "skipped" noise) via TAEF /select: queries.
46+
//
47+
#define WSL1_TEST_METHOD(_name) \
48+
TAEF_BEGIN_TEST_METHOD_PROPERTIES_IN_CLASS_SCOPE(_name) \
49+
TEST_METHOD_PROPERTY(L"WSLVersion", L"1") \
50+
TAEF_END_TEST_METHOD_PROPERTIES_IN_CLASS_SCOPE() \
51+
TEST_METHOD(_name)
52+
53+
#define WSL2_TEST_METHOD(_name) \
54+
TAEF_BEGIN_TEST_METHOD_PROPERTIES_IN_CLASS_SCOPE(_name) \
55+
TEST_METHOD_PROPERTY(L"WSLVersion", L"2") \
56+
TAEF_END_TEST_METHOD_PROPERTIES_IN_CLASS_SCOPE() \
57+
TEST_METHOD(_name)
58+
59+
#define WSLC_TEST_METHOD(_name) \
60+
TAEF_BEGIN_TEST_METHOD_PROPERTIES_IN_CLASS_SCOPE(_name) \
61+
TEST_METHOD_PROPERTY(L"WSLVersion", L"2") \
62+
TAEF_END_TEST_METHOD_PROPERTIES_IN_CLASS_SCOPE() \
63+
TEST_METHOD(_name)
5464

5565
// macro for skipping tests that are currently failing due to not yet being fully implemented
5666
#define SKIP_TEST_NOT_IMPL() \

test/windows/DrvFsTests.cpp

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ class DrvFsTests
258258

259259
void DrvfsMountElevated(DrvFsMode Mode)
260260
{
261-
WSL2_TEST_ONLY();
262261
WINDOWS_11_TEST_ONLY(); // TODO: Enable on Windows 10 when virtio support is added
263262
SKIP_TEST_ARM64();
264263

@@ -270,7 +269,6 @@ class DrvFsTests
270269

271270
void DrvfsMountElevatedDifferentConsole(DrvFsMode Mode)
272271
{
273-
WSL2_TEST_ONLY();
274272
WINDOWS_11_TEST_ONLY(); // TODO: Enable on Windows 10 when virtio support is added
275273
SKIP_TEST_ARM64();
276274

@@ -282,7 +280,6 @@ class DrvFsTests
282280

283281
void DrvfsMountNonElevated(DrvFsMode Mode)
284282
{
285-
WSL2_TEST_ONLY();
286283
WINDOWS_11_TEST_ONLY(); // TODO: Enable on Windows 10 when virtio support is added
287284
SKIP_TEST_ARM64();
288285

@@ -296,7 +293,6 @@ class DrvFsTests
296293

297294
void DrvfsMountNonElevatedDifferentConsole(DrvFsMode Mode)
298295
{
299-
WSL2_TEST_ONLY();
300296
WINDOWS_11_TEST_ONLY(); // TODO: Enable on Windows 10 when virtio support is added
301297
SKIP_TEST_ARM64();
302298

@@ -310,7 +306,6 @@ class DrvFsTests
310306

311307
void DrvfsMountElevatedSystemDistroEnabled(DrvFsMode Mode)
312308
{
313-
WSL2_TEST_ONLY();
314309
WINDOWS_11_TEST_ONLY(); // TODO: Enable on Windows 10 when virtio support is added
315310
SKIP_TEST_ARM64();
316311

@@ -322,7 +317,6 @@ class DrvFsTests
322317

323318
void DrvfsMountNonElevatedSystemDistroEnabled(DrvFsMode Mode)
324319
{
325-
WSL2_TEST_ONLY();
326320
WINDOWS_11_TEST_ONLY(); // TODO: Enable on Windows 10 when virtio support is added
327321
SKIP_TEST_ARM64();
328322

@@ -383,8 +377,6 @@ class DrvFsTests
383377

384378
void DrvFsMountUnicodePath(DrvFsMode Mode)
385379
{
386-
WSL2_TEST_ONLY();
387-
388380
// Create a Windows directory with unicode characters
389381
constexpr auto unicodeDir = L"C:\\drvfs-测试-テスト";
390382
auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { std::filesystem::remove_all(unicodeDir); });
@@ -1149,63 +1141,53 @@ class WSL1 : public DrvFsTests
11491141
return true;
11501142
}
11511143

1152-
TEST_METHOD(DrvFsDisableQueryByName)
1144+
WSL1_TEST_METHOD(DrvFsDisableQueryByName)
11531145
{
1154-
WSL1_TEST_ONLY();
11551146
VERIFY_NO_THROW(DrvFsCommon(LX_DRVFS_DISABLE_QUERY_BY_NAME));
11561147
}
11571148

1158-
TEST_METHOD(DrvFsDisableQueryByNameAndStatInfo)
1149+
WSL1_TEST_METHOD(DrvFsDisableQueryByNameAndStatInfo)
11591150
{
1160-
WSL1_TEST_ONLY();
11611151
VERIFY_NO_THROW(DrvFsCommon(LX_DRVFS_DISABLE_QUERY_BY_NAME_AND_STAT_INFO));
11621152
}
11631153

1164-
TEST_METHOD(VfsAccessDrvFs)
1154+
WSL1_TEST_METHOD(VfsAccessDrvFs)
11651155
{
1166-
WSL1_TEST_ONLY();
11671156
DrvFsTests::VfsAccessDrvFs();
11681157
}
11691158

1170-
TEST_METHOD(FsCommonDrvFs)
1159+
WSL1_TEST_METHOD(FsCommonDrvFs)
11711160
{
1172-
WSL1_TEST_ONLY();
11731161
DrvFsTests::FsCommonDrvFs();
11741162
}
11751163

1176-
TEST_METHOD(DrvFs)
1164+
WSL1_TEST_METHOD(DrvFs)
11771165
{
1178-
WSL1_TEST_ONLY();
11791166
DrvFsTests::DrvFs(DrvFsMode::WSL1);
11801167
}
11811168

1182-
TEST_METHOD(DrvFsFat)
1169+
WSL1_TEST_METHOD(DrvFsFat)
11831170
{
1184-
WSL1_TEST_ONLY();
11851171
DrvFsTests::DrvFsFat(DrvFsMode::WSL1);
11861172
}
11871173

1188-
TEST_METHOD(DrvFsSmb)
1174+
WSL1_TEST_METHOD(DrvFsSmb)
11891175
{
1190-
WSL1_TEST_ONLY();
11911176
DrvFsTests::DrvFsSmb(DrvFsMode::WSL1);
11921177
}
11931178

1194-
TEST_METHOD(DrvFsMetadata)
1179+
WSL1_TEST_METHOD(DrvFsMetadata)
11951180
{
1196-
WSL1_TEST_ONLY();
11971181
DrvFsTests::DrvFsMetadata(DrvFsMode::WSL1);
11981182
}
11991183

1200-
TEST_METHOD(XattrDrvFs)
1184+
WSL1_TEST_METHOD(XattrDrvFs)
12011185
{
1202-
WSL1_TEST_ONLY();
12031186
DrvFsTests::XattrDrvFs(DrvFsMode::WSL1);
12041187
}
12051188

1206-
TEST_METHOD(WslPath)
1189+
WSL1_TEST_METHOD(WslPath)
12071190
{
1208-
WSL1_TEST_ONLY();
12091191
DrvFsTests::WslPath(DrvFsMode::WSL1);
12101192
}
12111193
};
@@ -1242,99 +1224,83 @@ class WSL1 : public DrvFsTests
12421224
return true; \
12431225
} \
12441226
\
1245-
TEST_METHOD(VfsAccessDrvFs) \
1227+
WSL2_TEST_METHOD(VfsAccessDrvFs) \
12461228
{ \
1247-
WSL2_TEST_ONLY(); \
12481229
DrvFsTests::VfsAccessDrvFs(); \
12491230
} \
12501231
\
1251-
TEST_METHOD(FsCommonDrvFs) \
1232+
WSL2_TEST_METHOD(FsCommonDrvFs) \
12521233
{ \
1253-
WSL2_TEST_ONLY(); \
12541234
DrvFsTests::FsCommonDrvFs(); \
12551235
} \
12561236
\
1257-
TEST_METHOD(DrvFs) \
1237+
WSL2_TEST_METHOD(DrvFs) \
12581238
{ \
1259-
WSL2_TEST_ONLY(); \
12601239
DrvFsTests::DrvFs(DrvFsMode::##_mode##); \
12611240
} \
12621241
\
1263-
TEST_METHOD(DrvFsFat) \
1242+
WSL2_TEST_METHOD(DrvFsFat) \
12641243
{ \
1265-
WSL2_TEST_ONLY(); \
12661244
DrvFsTests::DrvFsFat(DrvFsMode::##_mode##); \
12671245
} \
12681246
\
1269-
TEST_METHOD(DrvFsSmb) \
1247+
WSL2_TEST_METHOD(DrvFsSmb) \
12701248
{ \
1271-
WSL2_TEST_ONLY(); \
12721249
DrvFsTests::DrvFsSmb(DrvFsMode::##_mode##); \
12731250
} \
12741251
\
1275-
TEST_METHOD(DrvFsMetadata) \
1252+
WSL2_TEST_METHOD(DrvFsMetadata) \
12761253
{ \
1277-
WSL2_TEST_ONLY(); \
12781254
DrvFsTests::DrvFsMetadata(DrvFsMode::##_mode##); \
12791255
} \
12801256
\
1281-
TEST_METHOD(DrvfsMountElevated) \
1257+
WSL2_TEST_METHOD(DrvfsMountElevated) \
12821258
{ \
1283-
WSL2_TEST_ONLY(); \
12841259
DrvFsTests::DrvfsMountElevated(DrvFsMode::##_mode##); \
12851260
} \
12861261
\
1287-
TEST_METHOD(DrvfsMountElevatedDifferentConsole) \
1262+
WSL2_TEST_METHOD(DrvfsMountElevatedDifferentConsole) \
12881263
{ \
1289-
WSL2_TEST_ONLY(); \
12901264
DrvFsTests::DrvfsMountElevatedDifferentConsole(DrvFsMode::##_mode##); \
12911265
} \
12921266
\
1293-
TEST_METHOD(DrvfsMountNonElevated) \
1267+
WSL2_TEST_METHOD(DrvfsMountNonElevated) \
12941268
{ \
1295-
WSL2_TEST_ONLY(); \
12961269
DrvFsTests::DrvfsMountNonElevated(DrvFsMode::##_mode##); \
12971270
} \
12981271
\
1299-
TEST_METHOD(DrvfsMountNonElevatedDifferentConsole) \
1272+
WSL2_TEST_METHOD(DrvfsMountNonElevatedDifferentConsole) \
13001273
{ \
1301-
WSL2_TEST_ONLY(); \
13021274
DrvFsTests::DrvfsMountNonElevatedDifferentConsole(DrvFsMode::##_mode##); \
13031275
} \
13041276
\
1305-
TEST_METHOD(DrvfsMountElevatedSystemDistroEnabled) \
1277+
WSL2_TEST_METHOD(DrvfsMountElevatedSystemDistroEnabled) \
13061278
{ \
1307-
WSL2_TEST_ONLY(); \
13081279
DrvFsTests::DrvfsMountElevatedSystemDistroEnabled(DrvFsMode::##_mode##); \
13091280
} \
13101281
\
1311-
TEST_METHOD(DrvfsMountNonElevatedSystemDistroEnabled) \
1282+
WSL2_TEST_METHOD(DrvfsMountNonElevatedSystemDistroEnabled) \
13121283
{ \
1313-
WSL2_TEST_ONLY(); \
13141284
DrvFsTests::DrvfsMountNonElevatedSystemDistroEnabled(DrvFsMode::##_mode##); \
13151285
} \
13161286
\
1317-
TEST_METHOD(XattrDrvFs) \
1287+
WSL2_TEST_METHOD(XattrDrvFs) \
13181288
{ \
1319-
WSL2_TEST_ONLY(); \
13201289
DrvFsTests::XattrDrvFs(DrvFsMode::##_mode##); \
13211290
} \
13221291
\
1323-
TEST_METHOD(DrvFsReFs) \
1292+
WSL2_TEST_METHOD(DrvFsReFs) \
13241293
{ \
1325-
WSL2_TEST_ONLY(); \
13261294
DrvFsTests::DrvFsReFs(DrvFsMode::##_mode##); \
13271295
} \
13281296
\
1329-
TEST_METHOD(WslPath) \
1297+
WSL2_TEST_METHOD(WslPath) \
13301298
{ \
1331-
WSL2_TEST_ONLY(); \
13321299
DrvFsTests::WslPath(DrvFsMode::##_mode##); \
13331300
} \
13341301
\
1335-
TEST_METHOD(DrvFsMountUnicodePath) \
1302+
WSL2_TEST_METHOD(DrvFsMountUnicodePath) \
13361303
{ \
1337-
WSL2_TEST_ONLY(); \
13381304
DrvFsTests::DrvFsMountUnicodePath(DrvFsMode::##_mode##); \
13391305
} \
13401306
}

0 commit comments

Comments
 (0)