Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions src/windows/wslc/commands/RootCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Module Name:
#include "ImageCommand.h"
#include "SessionCommand.h"
#include "SettingsCommand.h"
#include "VersionCommand.h"

using namespace wsl::windows::wslc::execution;

Expand Down Expand Up @@ -45,6 +46,7 @@ std::vector<std::unique_ptr<Command>> RootCommand::GetCommands() const
commands.push_back(std::make_unique<ContainerRunCommand>(FullName()));
commands.push_back(std::make_unique<ContainerStartCommand>(FullName()));
commands.push_back(std::make_unique<ContainerStopCommand>(FullName()));
commands.push_back(std::make_unique<VersionCommand>(FullName()));
return commands;
}

Expand Down
34 changes: 34 additions & 0 deletions src/windows/wslc/commands/VersionCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*++

Copyright (c) Microsoft. All rights reserved.

Module Name:

VersionCommand.cpp

Abstract:

Implementation of the version command.

--*/
#include "VersionCommand.h"

using namespace wsl::windows::wslc::execution;

namespace wsl::windows::wslc {
std::wstring VersionCommand::ShortDescription() const
{
return {L"Show version information."};
}

std::wstring VersionCommand::LongDescription() const
{
return {L"Show version information for this tool."};
}

void VersionCommand::ExecuteInternal(CLIExecutionContext& context) const
{
UNREFERENCED_PARAMETER(context);
wsl::windows::common::wslutil::PrintMessage(std::format(L"{} v{}", s_ExecutableName, WSL_PACKAGE_VERSION));
}
} // namespace wsl::windows::wslc
30 changes: 30 additions & 0 deletions src/windows/wslc/commands/VersionCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
VersionCommand.h
Abstract:
Declaration of the VersionCommand.
--*/
#pragma once
#include "Command.h"

namespace wsl::windows::wslc {
struct VersionCommand final : public Command
{
constexpr static std::wstring_view CommandName = L"version";
VersionCommand(const std::wstring& parent) : Command(CommandName, parent)
{
}
std::wstring ShortDescription() const override;
std::wstring LongDescription() const override;

protected:
void ExecuteInternal(CLIExecutionContext& context) const override;
};
} // namespace wsl::windows::wslc
4 changes: 4 additions & 0 deletions test/windows/wslc/CommandLineTestCases.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ COMMAND_LINE_TEST_CASE(L"image list -q", L"list", true)
COMMAND_LINE_TEST_CASE(L"image pull ubuntu", L"pull", true)
COMMAND_LINE_TEST_CASE(L"pull ubuntu", L"pull", true)

// Version command tests
COMMAND_LINE_TEST_CASE(L"version", L"version", true)
COMMAND_LINE_TEST_CASE(L"version --help", L"version", true)
COMMAND_LINE_TEST_CASE(L"version extraarg", L"version", false)
// Settings command
COMMAND_LINE_TEST_CASE(L"settings", L"settings", true)
COMMAND_LINE_TEST_CASE(L"settings reset", L"reset", true)
Expand Down
43 changes: 43 additions & 0 deletions test/windows/wslc/WSLCCLICommandUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Module Name:
#include "RootCommand.h"
#include "ContainerCommand.h"
#include "SessionCommand.h"
#include "VersionCommand.h"

using namespace wsl::windows::wslc;
using namespace WSLCTestHelpers;
Expand Down Expand Up @@ -95,6 +96,48 @@ class WSLCCLICommandUnitTests
VERIFY_IS_NOT_NULL(subcmd.get());
}
}

// Test: Verify VersionCommand has the correct name
TEST_METHOD(VersionCommand_HasCorrectName)
{
auto cmd = VersionCommand(L"wslc");
VERIFY_ARE_EQUAL(std::wstring_view(L"version"), cmd.Name());
}

// Test: Verify VersionCommand has no subcommands
TEST_METHOD(VersionCommand_HasNoSubcommands)
{
auto cmd = VersionCommand(L"wslc");
VERIFY_ARE_EQUAL(0u, cmd.GetCommands().size());
}

// Test: Verify VersionCommand has no arguments (only the auto-added --help)
TEST_METHOD(VersionCommand_HasNoArguments)
{
auto cmd = VersionCommand(L"wslc");
VERIFY_ARE_EQUAL(0u, cmd.GetArguments().size());
// Test out that auto added help command is the only one
VERIFY_ARE_EQUAL(1u, cmd.GetAllArguments().size());
}

// Test: Verify RootCommand contains VersionCommand as a subcommand
TEST_METHOD(RootCommand_ContainsVersionCommand)
{
auto root = RootCommand();
auto subcommands = root.GetCommands();

bool found = false;
for (const auto& subcmd : subcommands)
{
if (subcmd->Name() == VersionCommand::CommandName)
{
found = true;
break;
}
}

VERIFY_IS_TRUE(found, L"RootCommand should contain VersionCommand");
}
};

} // namespace WSLCCLICommandUnitTests
12 changes: 12 additions & 0 deletions test/windows/wslc/e2e/WSLCE2EGlobalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class WSLCE2EGlobalTests
RunWslcAndVerify(L"INVALID_CMD", {.Stdout = GetHelpMessage(), .Stderr = L"Unrecognized command: 'INVALID_CMD'\r\n", .ExitCode = 1});
}

TEST_METHOD(WSLCE2E_VersionCommand)
{
WSL2_TEST_ONLY();
RunWslcAndVerify(L"version", {.Stdout = GetVersionMessage(), .Stderr = L"", .ExitCode = 0});
}

private:
std::wstring GetHelpMessage() const
{
Expand All @@ -56,6 +62,11 @@ class WSLCE2EGlobalTests
return output.str();
}

std::wstring GetVersionMessage() const
{
return std::format(L"wslc v{}\r\n", WSL_PACKAGE_VERSION);
}

std::wstring GetDescription() const
{
return L"WSLC is the Windows Subsystem for Linux Container CLI tool. It enables management and interaction with WSL "
Expand Down Expand Up @@ -91,6 +102,7 @@ class WSLCE2EGlobalTests
<< L" run Run a container.\r\n"
<< L" start Start a container.\r\n"
<< L" stop Stop containers.\r\n"
<< L" version Show version information.\r\n"
<< L"\r\n"
<< L"For more details on a specific command, pass it the help argument. [-h]\r\n\r\n";
return commands.str();
Expand Down
Loading