-
Notifications
You must be signed in to change notification settings - Fork 1.7k
CLI: Make image save output optional #40194
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
4
commits into
feature/wsl-for-apps
Choose a base branch
from
user/amelbawa/image-save-output
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
184c191
Image save optional output
AmelBawa-msft 6395742
Clang format
AmelBawa-msft 9ff15e2
Update test/windows/wslc/e2e/WSLCE2EImageSaveTests.cpp
AmelBawa-msft cb72bef
Fix terminal detection in CI for WSLCE2E_Image_Save_ToTerminal_Fail test
Copilot 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,6 +163,63 @@ void RunWslcAndVerify(const std::wstring& cmd, const WSLCExecutionResult& expect | |
| RunWslc(cmd, elevationType).Verify(expected); | ||
| } | ||
|
|
||
| WSLCExecutionResult RunWslcAndRedirectToFile(const std::wstring& commandLine, std::optional<std::filesystem::path> outputPath, ElevationType elevationType) | ||
| { | ||
| auto cmd = L"\"" + GetWslcPath() + L"\" " + commandLine; | ||
|
AmelBawa-msft marked this conversation as resolved.
|
||
| wsl::windows::common::SubProcess process(nullptr, cmd.c_str()); | ||
|
|
||
| // If running non-elevated we need to keep the token alive until it completes. | ||
| wil::unique_handle nonElevatedToken; | ||
| if (elevationType == ElevationType::NonElevated) | ||
| { | ||
| nonElevatedToken = GetNonElevatedPrimaryToken(); | ||
| process.SetToken(nonElevatedToken.get()); | ||
| } | ||
|
|
||
| auto [parentStderrRead, childStderrWrite] = wsl::windows::common::wslutil::OpenAnonymousPipe(0, true, false); | ||
| THROW_IF_WIN32_BOOL_FALSE(SetHandleInformation(childStderrWrite.get(), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)); | ||
|
|
||
| wil::unique_hfile redirectedStdout; | ||
| HANDLE stdoutHandle = nullptr; | ||
|
|
||
| std::wstring effectiveCommandLine = commandLine; | ||
| if (outputPath.has_value()) | ||
| { | ||
| SECURITY_ATTRIBUTES securityAttributes{}; | ||
| securityAttributes.nLength = sizeof(securityAttributes); | ||
| securityAttributes.bInheritHandle = TRUE; | ||
| redirectedStdout.reset(CreateFileW( | ||
| outputPath->c_str(), GENERIC_WRITE, FILE_SHARE_READ, &securityAttributes, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr)); | ||
| THROW_LAST_ERROR_IF(!redirectedStdout); | ||
| stdoutHandle = redirectedStdout.get(); | ||
| effectiveCommandLine = std::format(L"{} > \"{}\"", commandLine, outputPath->wstring()); | ||
| } | ||
| else | ||
| { | ||
| // Open CONOUT$ so the child process receives a real console handle regardless of | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's clever ! I think this should be OK in most cases, but this might fail if the tests are run in a console-less context. If that happens, we can always revisit |
||
| // how the test runner has configured its own stdout (e.g. piped in CI). This | ||
| // makes IsConsoleHandle() return true inside wslc, which is the condition under | ||
| // test in WSLCE2E_Image_Save_ToTerminal_Fail. | ||
| SECURITY_ATTRIBUTES securityAttributes{}; | ||
| securityAttributes.nLength = sizeof(securityAttributes); | ||
| securityAttributes.bInheritHandle = TRUE; | ||
| redirectedStdout.reset( | ||
| CreateFileW(L"CONOUT$", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &securityAttributes, OPEN_EXISTING, 0, nullptr)); | ||
| THROW_LAST_ERROR_IF(!redirectedStdout); | ||
| stdoutHandle = redirectedStdout.get(); | ||
| } | ||
|
|
||
| process.SetStdHandles(nullptr, stdoutHandle, childStderrWrite.get()); | ||
|
|
||
| const auto processHandle = process.Start(); | ||
| childStderrWrite.reset(); | ||
|
|
||
| const auto exitCode = wsl::windows::common::SubProcess::GetExitCode(processHandle.get()); | ||
| const auto stdErrOutput = wsl::shared::string::MultiByteToWide(ReadToString(parentStderrRead.get())); | ||
|
|
||
| return {.CommandLine = std::move(effectiveCommandLine), .Stdout = L"", .Stderr = stdErrOutput, .ExitCode = exitCode}; | ||
| } | ||
|
|
||
| std::wstring GetWslcHeader() | ||
| { | ||
| std::wstringstream header; | ||
|
|
||
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
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.