-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Initialize environment and add E2E tests with formatting #14475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AmelBawa-msft
wants to merge
23
commits into
feature/wsl-for-apps
Choose a base branch
from
user/amelbawa/env
base: feature/wsl-for-apps
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+649
−10
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
fbc2aeb
Init env
AmelBawa-msft 596b4dd
Added E2E tests
AmelBawa-msft e802f4f
Clang format
AmelBawa-msft d7fd8cd
WIP
AmelBawa-msft dee2dac
Add envfile support
AmelBawa-msft 52e9363
Init tests
AmelBawa-msft de32712
Added UT
AmelBawa-msft e7b0303
Added more UT
AmelBawa-msft 9064bed
Added more E2E Tests
AmelBawa-msft a72c357
Added more E2E Tests
AmelBawa-msft 6eba41c
Code enhancement
AmelBawa-msft a8aefdd
Added more E2E Tests
AmelBawa-msft 6381793
Added more E2E Tests
AmelBawa-msft 6af19b1
Added more E2E Tests
AmelBawa-msft 289c031
Clang format
AmelBawa-msft 106b9c1
Resolve conflicts
AmelBawa-msft 5e8904b
Resolve copilot comment
AmelBawa-msft 302eed9
Merge branch 'feature/wsl-for-apps' into user/amelbawa/env
AmelBawa-msft 9a508a6
Resolve conflicts
AmelBawa-msft 5997a19
Merge branch 'user/amelbawa/env' of https://github.com/microsoft/wsl …
AmelBawa-msft 098a9b5
Resolve conflicts
AmelBawa-msft fa688d4
Addressed comments
AmelBawa-msft 1e5b7c2
Fix test
AmelBawa-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /*++ | ||
|
|
||
| Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| Module Name: | ||
|
|
||
| WSLCCLIEnvVarParserUnitTests.cpp | ||
|
|
||
| Abstract: | ||
|
|
||
| This file contains unit tests for WSLC CLI environment variable parsing and validation. | ||
|
|
||
| --*/ | ||
|
|
||
| #include "precomp.h" | ||
| #include "windows/Common.h" | ||
| #include "WSLCCLITestHelpers.h" | ||
| #include "ContainerModel.h" | ||
|
|
||
| #include <filesystem> | ||
| #include <fstream> | ||
|
|
||
| using namespace wsl::windows::wslc; | ||
|
|
||
| namespace WSLCCLIEnvVarParserUnitTests { | ||
|
|
||
| class WSLCCLIEnvVarParserUnitTests | ||
| { | ||
| WSL_TEST_CLASS(WSLCCLIEnvVarParserUnitTests) | ||
|
|
||
| TEST_METHOD_SETUP(TestMethodSetup) | ||
| { | ||
| EnvTestFile = wsl::windows::common::filesystem::GetTempFilename(); | ||
| return true; | ||
| } | ||
|
|
||
| TEST_METHOD_CLEANUP(TestMethodCleanup) | ||
| { | ||
| DeleteFileW(EnvTestFile.c_str()); | ||
| return true; | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_ValidEnvVars) | ||
| { | ||
| const auto parsed = models::EnvironmentVariable::Parse(L"FOO=bar"); | ||
| VERIFY_IS_TRUE(parsed.has_value()); | ||
| VERIFY_ARE_EQUAL(L"FOO=bar", parsed.value()); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_UsesProcessEnvWhenValueMissing) | ||
| { | ||
| constexpr const auto key = L"WSLC_TEST_ENV_FROM_PROCESS"; | ||
| VERIFY_IS_TRUE(SetEnvironmentVariableW(key, L"process_value")); | ||
|
|
||
| auto cleanup = wil::scope_exit([&] { SetEnvironmentVariableW(key, nullptr); }); | ||
|
|
||
| const auto parsed = models::EnvironmentVariable::Parse(key); | ||
| VERIFY_IS_TRUE(parsed.has_value()); | ||
| VERIFY_ARE_EQUAL(L"WSLC_TEST_ENV_FROM_PROCESS=process_value", parsed.value()); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_NulloptForWhitespaceOrUnsetVar) | ||
| { | ||
| const auto whitespaceOnly = models::EnvironmentVariable::Parse(L" \t "); | ||
| VERIFY_IS_FALSE(whitespaceOnly.has_value()); | ||
|
|
||
| SetEnvironmentVariableA("WSLC_TEST_ENV_UNSET", nullptr); | ||
AmelBawa-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const auto missingFromProcess = models::EnvironmentVariable::Parse(L"WSLC_TEST_ENV_UNSET"); | ||
| VERIFY_IS_FALSE(missingFromProcess.has_value()); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_InvalidKeysThrow) | ||
| { | ||
| VERIFY_THROWS(models::EnvironmentVariable::Parse(L"=value"), std::exception); | ||
| VERIFY_THROWS(models::EnvironmentVariable::Parse(L"BAD KEY=value"), std::exception); | ||
| VERIFY_THROWS(models::EnvironmentVariable::Parse(L"BAD\tKEY=value"), std::exception); | ||
| VERIFY_THROWS(models::EnvironmentVariable::Parse(L"BAD\nKEY=value"), std::exception); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_ParseFileParsesAndSkipsExpectedLines) | ||
| { | ||
| constexpr const auto key = L"WSLC_TEST_ENV_FROM_FILE"; | ||
| VERIFY_IS_TRUE(SetEnvironmentVariableW(key, L"file_process_value") == TRUE); | ||
|
|
||
| auto envCleanup = wil::scope_exit([&] { SetEnvironmentVariableW(key, nullptr); }); | ||
|
|
||
| std::ofstream file(EnvTestFile); | ||
| VERIFY_IS_TRUE(file.is_open()); | ||
| file << "# comment\n"; | ||
| file << "\n"; | ||
| file << "KEY1=VALUE1\n"; | ||
| file << " KEY2=VALUE2\n"; | ||
| file << "WSLC_TEST_ENV_FROM_FILE\n"; | ||
| file << "WSLC_TEST_ENV_DOES_NOT_EXIST\n"; | ||
| file.close(); | ||
|
|
||
| const auto parsed = models::EnvironmentVariable::ParseFile(EnvTestFile.wstring()); | ||
|
|
||
| VERIFY_ARE_EQUAL(3U, parsed.size()); | ||
| VERIFY_ARE_EQUAL(L"KEY1=VALUE1", parsed[0]); | ||
| VERIFY_ARE_EQUAL(L"KEY2=VALUE2", parsed[1]); | ||
| VERIFY_ARE_EQUAL(L"WSLC_TEST_ENV_FROM_FILE=file_process_value", parsed[2]); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_ParseFileThrowsWhenMissing) | ||
| { | ||
| VERIFY_THROWS(models::EnvironmentVariable::ParseFile(L"ENV_FILE_NOT_FOUND"), std::exception); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_ExplicitEmptyValueIsValid) | ||
| { | ||
| const auto parsed = models::EnvironmentVariable::Parse(L"FOO="); | ||
| VERIFY_IS_TRUE(parsed.has_value()); | ||
| VERIFY_ARE_EQUAL(L"FOO=", parsed.value()); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_MultipleEqualsPreservedInValue) | ||
| { | ||
| const auto parsed = models::EnvironmentVariable::Parse(L"FOO=a=b=c"); | ||
| VERIFY_IS_TRUE(parsed.has_value()); | ||
| VERIFY_ARE_EQUAL(L"FOO=a=b=c", parsed.value()); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_EmptyInputReturnsNullopt) | ||
| { | ||
| const auto parsed = models::EnvironmentVariable::Parse(L""); | ||
| VERIFY_IS_FALSE(parsed.has_value()); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_UsesProcessEnvWhenValueIsExplicitlyEmpty) | ||
| { | ||
| constexpr const auto key = L"WSLC_TEST_ENV_EMPTY_VALUE"; | ||
| VERIFY_IS_TRUE(SetEnvironmentVariableW(key, L"")); | ||
|
|
||
| auto cleanup = wil::scope_exit([&] { SetEnvironmentVariableW(key, nullptr); }); | ||
|
|
||
| const auto parsed = models::EnvironmentVariable::Parse(key); | ||
| VERIFY_IS_TRUE(parsed.has_value()); | ||
| VERIFY_ARE_EQUAL(L"WSLC_TEST_ENV_EMPTY_VALUE=", parsed.value()); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_ParseFilePreservesTrailingWhitespaceInValue) | ||
| { | ||
| std::ofstream file(EnvTestFile); | ||
| VERIFY_IS_TRUE(file.is_open()); | ||
| file << "KEY=value \n"; | ||
| file.close(); | ||
|
|
||
| const auto parsed = models::EnvironmentVariable::ParseFile(EnvTestFile.wstring()); | ||
|
|
||
| VERIFY_ARE_EQUAL(1U, parsed.size()); | ||
| VERIFY_ARE_EQUAL(L"KEY=value ", parsed[0]); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_ParseFileThrowsOnInvalidLine) | ||
| { | ||
| std::ofstream file(EnvTestFile); | ||
| VERIFY_IS_TRUE(file.is_open()); | ||
| file << "BAD KEY=value\n"; | ||
| file.close(); | ||
|
|
||
| VERIFY_THROWS(models::EnvironmentVariable::ParseFile(EnvTestFile.wstring()), std::exception); | ||
| } | ||
|
|
||
| TEST_METHOD(WSLCCLIEnvVarParser_ParseFileEmptyFileReturnsEmpty) | ||
| { | ||
| std::ofstream file(EnvTestFile); | ||
| VERIFY_IS_TRUE(file.is_open()); | ||
| file.close(); | ||
|
|
||
| const auto parsed = models::EnvironmentVariable::ParseFile(EnvTestFile.wstring()); | ||
| VERIFY_ARE_EQUAL(0U, parsed.size()); | ||
| } | ||
|
|
||
| private: | ||
| std::filesystem::path EnvTestFile; | ||
| }; | ||
|
|
||
| } // namespace WSLCCLIEnvVarParserUnitTests | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.