1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:17:36 +00:00

LibCore+Tests: Unify process handling into Command class

The Test262RunnerHandler class in test-test262 was made in order to
spawn a subprocess, connect to its input/output error pipes, and obtain
its return value.

Later on, a copy of this implementation was added to TestSed with
modifications, such as adding support for reading from the output pipes
as well.

Unify these two implementations into a new Core::Command class. This new
implementation is more closely modeled from the TestSed implementation
due to the extra functionality it implemented.
This commit is contained in:
Shannon Booth 2023-07-17 16:39:07 +12:00 committed by Sam Atkins
parent cb920b23cc
commit 5dd93474ee
4 changed files with 160 additions and 249 deletions

View file

@ -1,5 +1,7 @@
/*
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,10 +12,13 @@
#include <AK/LexicalPath.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <LibCore/File.h>
#include <LibCore/Forward.h>
#include <spawn.h>
namespace Core {
// FIXME: Unify this and the below 'command' functions with Command class below
struct CommandResult {
int exit_code { 0 };
ByteBuffer output;
@ -23,4 +28,38 @@ struct CommandResult {
ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<DeprecatedString> const& arguments, Optional<LexicalPath> chdir);
ErrorOr<CommandResult> command(DeprecatedString const& command_string, Optional<LexicalPath> chdir);
class Command {
public:
struct ProcessOutputs {
ByteBuffer standard_output;
ByteBuffer standard_error;
};
static ErrorOr<OwnPtr<Command>> create(StringView command, char const* const arguments[]);
Command(pid_t pid, NonnullOwnPtr<Core::File> stdin_file, NonnullOwnPtr<Core::File> stdout_file, NonnullOwnPtr<Core::File> stderr_file);
ErrorOr<void> write(StringView input);
bool write_lines(Span<DeprecatedString> lines);
ErrorOr<ProcessOutputs> read_all();
enum class ProcessResult {
Running,
DoneWithZeroExitCode,
Failed,
FailedFromTimeout,
Unknown,
};
ErrorOr<ProcessResult> status(int options = 0);
private:
pid_t m_pid { -1 };
NonnullOwnPtr<Core::File> m_stdin;
NonnullOwnPtr<Core::File> m_stdout;
NonnullOwnPtr<Core::File> m_stderr;
};
}