Initialize environment and add E2E tests with formatting#14475
Initialize environment and add E2E tests with formatting#14475AmelBawa-msft wants to merge 23 commits intofeature/wsl-for-appsfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds initial support for passing environment variables from the wslc CLI into container creation/run flows (via -e/--env) and introduces E2E coverage for the new CLI behavior.
Changes:
- Extend
ContainerOptionsto carry environment variables and populate it from CLI args (-e/--env). - Plumb
ContainerOptions.EnvironmentVariablesintoWSLAContainerLauncherduring container creation/run. - Add E2E tests validating
container runenvironment variable propagation.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
test/windows/wslc/e2e/WSLCE2EContainerCreateTests.cpp |
Adds E2E coverage for container run -e/--env and test setup/cleanup env initialization. |
src/windows/wslc/tasks/ContainerTasks.cpp |
Parses -e/--env arguments into ContainerOptions.EnvironmentVariables. |
src/windows/wslc/services/ContainerService.cpp |
Passes EnvironmentVariables into WSLAContainerLauncher for container creation. |
src/windows/wslc/services/ContainerModel.h |
Extends ContainerOptions with an environment variables list. |
There was a problem hiding this comment.
Pull request overview
Adds WSLC support for passing environment variables into containers (via -e/--env and --env-file) and validates the behavior through new unit and end-to-end tests.
Changes:
- Extend
container run/createargument definitions to accept repeated-eand--env-file. - Parse/validate env entries (including key-only resolution from host env) and plumb them through
ContainerOptionsintoWSLAContainerLauncher. - Add new unit tests for env parsing and E2E coverage for env/env-file scenarios and precedence.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/windows/wslc/e2e/WSLCE2EContainerCreateTests.cpp | Adds E2E coverage for -e and --env-file behavior (including precedence and error cases). |
| test/windows/wslc/WSLCCLIEnvVarParserUnitTests.cpp | New unit tests for env var parsing and env-file parsing behavior. |
| src/windows/wslc/tasks/ContainerTasks.cpp | Parses --env-file and -e args into ContainerOptions.EnvironmentVariables. |
| src/windows/wslc/services/ContainerService.cpp | Passes environment variables to WSLAContainerLauncher during container create/run. |
| src/windows/wslc/services/ContainerModel.h | Adds ContainerOptions.EnvironmentVariables and declares EnvironmentVariable parsing helpers. |
| src/windows/wslc/services/ContainerModel.cpp | Implements env parsing and env-file parsing/validation. |
| src/windows/wslc/commands/ContainerRunCommand.cpp | Updates container run argument definitions to allow repeated env/env-file. |
| src/windows/wslc/commands/ContainerCreateCommand.cpp | Updates container create argument definitions to allow repeated env/env-file. |
| EnsureImageIsLoaded(AlpineImage); | ||
| EnsureImageIsLoaded(DebianImage); | ||
|
|
||
| VERIFY_IS_TRUE(::SetEnvironmentVariableW(HostEnvVariableName.c_str(), HostEnvVariableValue.c_str())); |
There was a problem hiding this comment.
nit:: I recommend using ScopedEnvVariable instead
There was a problem hiding this comment.
Reopening since this isn't resolved on the latest iteration
…into user/amelbawa/env
|
|
||
| static inline bool IsSpace(wchar_t ch) | ||
| { | ||
| return std::iswspace(ch) != 0; |
There was a problem hiding this comment.
Could we use iswpace directly instead ?
| if (!value.has_value()) | ||
| { | ||
| wil::unique_hglobal_string envValue; | ||
| auto hr = wil::GetEnvironmentVariableW(key.c_str(), envValue); |
There was a problem hiding this comment.
wil::GetEnvironmentVariableW can be templated on std::wstring to avoid the need for copy below
| while (std::getline(file, line)) | ||
| { | ||
| // Remove leading whitespace | ||
| line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](unsigned char ch) { return !std::isspace(ch); })); |
There was a problem hiding this comment.
This looks like it will only remove the first whitespace character. Instead we should probably loop and delete the first character as long as line is not empty and line[0] is a space
| continue; | ||
| } | ||
|
|
||
| auto envVar = Parse(wsl::shared::string::MultiByteToWide(line)); |
There was a problem hiding this comment.
I recommend using std::string in Parse as well, since that's what the service will expect
| EnsureImageIsLoaded(AlpineImage); | ||
| EnsureImageIsLoaded(DebianImage); | ||
|
|
||
| VERIFY_IS_TRUE(::SetEnvironmentVariableW(HostEnvVariableName.c_str(), HostEnvVariableValue.c_str())); |
There was a problem hiding this comment.
Reopening since this isn't resolved on the latest iteration
| TEST_METHOD(WSLCCLIEnvVarParser_UsesProcessEnvWhenValueMissing) | ||
| { | ||
| constexpr const auto key = L"WSLC_TEST_ENV_FROM_PROCESS"; | ||
| VERIFY_IS_TRUE(SetEnvironmentVariableW(key, L"process_value")); |
There was a problem hiding this comment.
I recommend using ScopedEnvVariable here as well
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_InvalidKeysThrow) | ||
| { | ||
| VERIFY_THROWS(models::EnvironmentVariable::Parse(L"=value"), std::exception); |
There was a problem hiding this comment.
nit: I recommend validating that the error message is what we expect
Summary of the Pull Request
-eoption inwslc run/create/execPR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed