-
Notifications
You must be signed in to change notification settings - Fork 335
Infrastructure: allow passing stdin to subprocesses. #1134
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7c40ea3
Initial attempt at getting process stdin overridable, so that we can …
BillyONeal 3439c70
Use longer example buffer to avoid complicated buffer management in r…
BillyONeal 1b40487
Change Windows to issue a single large OVERLAPPED write for the stdin…
BillyONeal da28c17
Refactor all windows handle management bits to avoid needing a millio…
BillyONeal 38f7eeb
Also get rid of unnecessary movability on the POSIX version of Anonym…
BillyONeal 69341d1
Replace some unreachables with debug printing.
BillyONeal 003eeb3
Clarify that we expect the APC to close the handle all the time.
BillyONeal f5767f8
Warnings found bugs :)
BillyONeal c78bf7f
Merge remote-tracking branch 'origin/main' into process-stdin
BillyONeal c680e21
Hook up missing tests
BillyONeal 9e860b1
Whoops resurrect ChildStdinTracker
BillyONeal e48b7ba
format
BillyONeal fe4d71b
Add C (void) to main.
BillyONeal 81ee9a9
Merge remote-tracking branch 'origin/main' into process-stdin
BillyONeal 6a0476c
Fix https://github.com/microsoft/vcpkg-tool/pull/1144 with stdin rath…
BillyONeal b49e713
Add current process ID to the pipe name.
BillyONeal c72c5ee
clang-format
BillyONeal 90b4331
Merge remote-tracking branch 'origin/main' into process-stdin
BillyONeal 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #if defined(_WIN32) | ||
| #include <Windows.h> | ||
| int main() { CloseHandle(GetStdHandle(STD_INPUT_HANDLE)); } | ||
| #else | ||
| #include <unistd.h> | ||
| int main(void) { close(STDIN_FILENO); } | ||
| #endif |
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,18 @@ | ||
| #include <stdio.h> | ||
| #if defined(_WIN32) | ||
| #include <Windows.h> | ||
| #else // ^^^ _WIN32 // !_WIN32 | ||
| #include <unistd.h> | ||
| #endif // ^^^ !_WIN32 | ||
|
|
||
| int main() | ||
| { | ||
| const char content[] = "hello world"; | ||
| fwrite(content, 1, sizeof(content) - 1, stdout); | ||
| fflush(stdout); | ||
| #if defined(_WIN32) | ||
| CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE)); | ||
| #else // ^^^ _WIN32 // !_WIN32 | ||
| close(STDOUT_FILENO); | ||
| #endif // ^^^ !_WIN32 | ||
| } | ||
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,38 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| // This program reads stdin and asserts that it is the expected value repeated | ||
| // until stdin ends | ||
|
|
||
| int main(int argc, char** argv) | ||
| { | ||
| char buffer[20]; | ||
| // The repeated string 'example' is intentionally a prime length to make hitting buffering edge | ||
| // cases more likely | ||
| const char expected[] = "exampleexampleexampleexamp"; | ||
| size_t offset = 0; // always between 0 and 6 | ||
| for (;;) | ||
| { | ||
| size_t read_amount = fread(buffer, 1, sizeof(buffer), stdin); | ||
| if (argc > 1) | ||
| { | ||
| puts(argv[1]); | ||
| fflush(stdout); | ||
| } | ||
| if (read_amount == 0) | ||
| { | ||
| if (feof(stdin)) | ||
| { | ||
| puts("success"); | ||
| return 0; | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| if (memcmp(buffer, expected + offset, read_amount) != 0) | ||
| { | ||
| return 2; | ||
| } | ||
| offset = (offset + read_amount) % 7; | ||
| } | ||
| } |
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,76 @@ | ||
| #include <catch2/catch.hpp> | ||
|
|
||
| #include <vcpkg/base/strings.h> | ||
| #include <vcpkg/base/system.process.h> | ||
|
|
||
| using namespace vcpkg; | ||
|
|
||
| TEST_CASE ("captures-output", "[system.process]") | ||
| { | ||
| auto test_program = Path(get_exe_path_of_current_process().parent_path()) / "reads-stdin"; | ||
| Command cmd{test_program}; | ||
| cmd.string_arg("this is printed when something is read"); | ||
| static constexpr std::size_t minimum_size = 1'000'000; // to exceed OS pipe buffer size | ||
| constexpr StringLiteral example = "example"; | ||
| constexpr auto examples = (minimum_size / example.size()) + 1; | ||
| std::string input; | ||
| constexpr auto input_size = examples * example.size(); | ||
| for (std::size_t idx = 0; idx < examples; ++idx) | ||
| { | ||
| input.append(example.data(), example.size()); | ||
| } | ||
|
|
||
| std::string expected; | ||
| constexpr StringLiteral repeat = "this is printed when something is read"; | ||
| constexpr auto repeats = (input_size / 20) + (input_size % 20 != 0) + 1; | ||
| for (std::size_t idx = 0; idx < repeats; ++idx) | ||
| { | ||
| expected.append(repeat.data(), repeat.size()); | ||
| #if defined(_WIN32) | ||
| expected.push_back('\r'); | ||
| #endif // ^^^ _WIN32 | ||
| expected.push_back('\n'); | ||
| } | ||
|
|
||
| expected.append("success"); | ||
| #if defined(_WIN32) | ||
| expected.push_back('\r'); | ||
| #endif // ^^^ _WIN32 | ||
| expected.push_back('\n'); | ||
|
|
||
| auto run = cmd_execute_and_capture_output( | ||
| cmd, default_working_directory, default_environment, Encoding::Utf8, EchoInDebug::Hide, input) | ||
| .value_or_exit(VCPKG_LINE_INFO); | ||
| REQUIRE(run.exit_code == 0); | ||
| REQUIRE(run.output == expected); | ||
| } | ||
|
|
||
| TEST_CASE ("no closes-stdin crash", "[system.process]") | ||
| { | ||
| auto test_program = Path(get_exe_path_of_current_process().parent_path()) / "closes-stdin"; | ||
| Command cmd{test_program}; | ||
| auto run = cmd_execute_and_capture_output(cmd, | ||
| default_working_directory, | ||
| default_environment, | ||
| Encoding::Utf8, | ||
| EchoInDebug::Hide, | ||
| "this is some input that will be intentionally not read") | ||
| .value_or_exit(VCPKG_LINE_INFO); | ||
| REQUIRE(run.exit_code == 0); | ||
| REQUIRE(run.output.empty()); | ||
| } | ||
|
|
||
| TEST_CASE ("no closes-stdout crash", "[system.process]") | ||
| { | ||
| auto test_program = Path(get_exe_path_of_current_process().parent_path()) / "closes-stdout"; | ||
| Command cmd{test_program}; | ||
| auto run = cmd_execute_and_capture_output(cmd, | ||
| default_working_directory, | ||
| default_environment, | ||
| Encoding::Utf8, | ||
| EchoInDebug::Hide, | ||
| "this is some input that will be read") | ||
| .value_or_exit(VCPKG_LINE_INFO); | ||
| REQUIRE(run.exit_code == 0); | ||
| REQUIRE(run.output == "hello world"); | ||
| } |
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.