Skip to content

Conversation

@beena352
Copy link

Summary of the Pull Request:

This PR adds automated test coverage for the wsladiag CLI tool under test/windows.
The new tests exercise the supported commands and common error paths to ensure argument parsing, exit codes, and output behavior remain correct.

  • Specifically, the tests cover:
  • --help and -h behavior
  • list command output (both with and without active sessions)
  • shell command error paths (missing session name, invalid session name, verbose vs non-verbose)
  • Unknown and empty commands

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

A new test file (WsladiagTests.cpp) is added to the Windows test suite. The tests invoke wsladiag.exe directly and validate:

  • Correct exit codes for valid and invalid commands
  • Whether output is written to stdout vs stderr as expected
  • That help text and error messages are shown consistently
  • That non-interactive error paths (e.g., invalid session name) fail fast and do not hang

The tests intentionally avoid interactive shell success cases and distro-dependent scenarios to keep the suite stable and fast. Assertions use substring matching rather than exact formatting to reduce flakiness.

Validation Steps Performed

Built the solution and confirmed wsladiag.exe is generated correctly

  • Ran the new test suite locally and verified all tests pass
  • Tested edge cases manually (e.g., running wsladiag with no arguments, invalid commands)
  • Verified test behavior in both scenarios: when WSLA sessions exist and when none exist
  • Confirmed tests fail appropriately when wsladiag.exe is not available


const bool noSessions = (out.find(L"No WSLA sessions found.") != std::wstring::npos);

const bool hasTable = (out.find(L"WSLA session") != std::wstring::npos) && (out.find(L"ID") != std::wstring::npos) &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend having a utility method to verify the complete output. Something like:

ValidateOutput("list", "No WSLA sessions found.\r\n");

For instance. That would make writing future tests easier

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Same for below tests)


static std::wstring BuildWsladiagCmd(const std::wstring& args)
{
const auto exePath = wsl::windows::common::wslutil::GetBasePath() / L"wsladiag.exe";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetBasePath() won't work here since it will look up the current process's executable folder.

To get the installation folder where wsladiag.exe is, use : wsl::windows::common::wslutil::GetMsiPackagePath()

@beena352 beena352 force-pushed the user/beenachauhan/test_wsladiag branch from 15256c5 to 5e41ecf Compare December 24, 2025 21:29
catch (...)
{
const auto hr = wil::ResultFromCaughtException();
if (hr == E_INVALIDARG)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend against this, because the service API might return that, and in that case we wouldn't want to show the usage.

To show command line parsing errors, I would recommend using something similar to this:

auto strings = wsl::windows::common::wslutil::ErrorToString(context->ReportedError().value());

For the user errors to work, we'd need to explicitely enable them, like this:

    wsl::windows::common::EnableContextualizedErrors(false);

And then create an execution context that we initialize in main (probably with new entrypoint value for wsladiag):

wsl::windows::common::ExecutionContext context{Context::WslaDiag};

Then we can use context to actually get the error string when an exception is thrown

// Initialize the tests
TEST_CLASS_SETUP(TestClassSetup)
{
VERIFY_ARE_EQUAL(LxsstuInitialize(FALSE), TRUE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specific to WSL and isn't needed for WSLA tests

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for LxsstuUninitialize(FALSE); below

// Validate that list command output shows either no sessions message or session table
static void ValidateListOutput(const std::wstring& out)
{
const bool noSessions = out.find(L"No WSLA sessions found.") != std::wstring::npos;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if I was unclear in my previous comment. What I was suggesting was to have a method that takes the expected output as a parameter and do the validation. Something like:

void ValidateWslaDiagOutput(const std::string& Cmd, const std::string& ExpectedOutput);

So we can something like:

ValidateWslaDiagOutput("list", "<expected output>");

TEST_METHOD(UnknownCommand_ShowsUsage)
{
auto [out, err, code] = RunWsladiag(L"blah");
VERIFY_ARE_NOT_EQUAL(0, code);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend validating that the error code is 1 explicitly, since that's what we expect

@beena352 beena352 force-pushed the user/beenachauhan/test_wsladiag branch 2 times, most recently from f08388e to 87b8180 Compare December 30, 2025 23:50
{
wsl::windows::common::EnableContextualizedErrors(false);

std::optional<ExecutionContext> context;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: context is always set to the same value, so it doesn't need to be an std::optional

auto [out, err, code] = RunWsladiag(L"blah");
VERIFY_ARE_EQUAL(1, code);

const std::wstring combined = out + err;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd recommend validating both fds separately. For instance error messages should always be on stderr, not stdout

static void ValidateWslaDiagOutput(const std::wstring& cmd, const std::wstring& expectedSubstring)
{
auto [out, err, code] = RunWsladiag(cmd);
const std::wstring combined = out + err;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I'd recommend having two parameters, one for each output so we can validate them separately.

I'll also recommend validating the entire strings, that way we'll catch if anything unexpected is printed

@beena352 beena352 force-pushed the user/beenachauhan/test_wsladiag branch from 87b8180 to c5a97ae Compare January 2, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants