Skip to content
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

upgrade dependencies; adapt to new Result APIs #3

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.result>1.5.0</version.result>
<version.result>1.7.1</version.result>
<version.lombok>1.18.32</version.lombok>
<version.junit>5.10.2</version.junit>
</properties>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/github/artkonr/process/Chain.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.github.artkonr.result.TakeFrom;
import lombok.NonNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -100,8 +99,8 @@ public Chain pipeTo(@NonNull Cmd command) {
*/
@Override
public Result<io.github.artkonr.process.Output, CmdException> invoke() {
Result<List<Process>, IOException> invoked = Result
.wrap(IOException.class, () -> ProcessBuilder.startPipeline(pipeline));
Result<List<Process>, Exception> invoked = Result
.wrap(() -> ProcessBuilder.startPipeline(pipeline));

int endI = pipeline.size() - 1;
var fin = handle(
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/io/github/artkonr/process/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.artkonr.result.Result;
import lombok.NonNull;

import java.io.IOException;

import static io.github.artkonr.process.Util.*;

Expand Down Expand Up @@ -80,7 +79,7 @@ public Chain pipeTo(@NonNull Cmd command) {
@Override
public Result<io.github.artkonr.process.Output, CmdException> invoke() {
return handle(
Result.wrap(IOException.class, handle::start),
Result.wrap(handle::start),
getCmd(handle)
);
}
Expand Down Expand Up @@ -182,28 +181,34 @@ private Output(long pid, String cmd, int exitcode, Data stdout, Data stderr) {
* @param cmd invoked command
* @return invocation {@link Result}
*/
static Result<io.github.artkonr.process.Output, CmdException> handle(Result<Process, IOException> result,
static Result<io.github.artkonr.process.Output, CmdException> handle(Result<Process, Exception> result,
String cmd) {
return result
.mapErr(CmdException::wrap)
.flatMap(process -> Result
.wrap(InterruptedException.class, process::waitFor)
.mapErr(CmdException::wrap)
.flatMap(exitcode -> read(process.inputReader())
.upcast()
.flatMap(exitCode ->
read(process.inputReader())
.fuse(read(process.errorReader()))
.map(fuse -> Output.from(process.pid(), cmd, exitcode, fuse.left(), fuse.right()))
.map(fuse -> Output.from(
process.pid(),
cmd,
exitCode,
fuse.left(),
fuse.right()
))
)
)
.taint(
.mapErr(ex -> new CmdException("command failed", ex))
.fork(
output -> !output.exitedNormally(),
output -> CmdException.errorExitCode(
0,
output.command(),
output.exitcode(),
output.stderr().encode().orElse("n/a")
)
)
.mapErr(CmdException::wrap);
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/artkonr/process/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static byte[] readByteArray(byte[] bytes) {
* @param reader reader
* @return byte array wrapped in a {@link Result}
*/
static Result<byte[], CmdException> read(BufferedReader reader) {
static Result<byte[], Exception> read(BufferedReader reader) {
try (
BufferedReader input = reader;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
Expand All @@ -54,7 +54,7 @@ static Result<byte[], CmdException> read(BufferedReader reader) {
writer.flush();
return Result.ok(readByteArray(buf.toByteArray()));
} catch (IOException ex) {
return Result.err(new CmdException("failed to read input", ex));
return Result.err(new CmdException("failed to read stdout/stderr", ex));
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/test/java/io/github/artkonr/process/CmdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void pipeTo__text_command__ok() {

@Test
void pipeTo__text_command__null_arg() {
assertThrows(IllegalArgumentException.class, () -> Cmd.from("pwd").pipeTo(null, null));
assertThrows(IllegalArgumentException.class, () -> Cmd.from("pwd").pipeTo(null, (String[]) null));
}

@Test
Expand All @@ -90,7 +90,7 @@ void handle__ok() {
Process process = TestProcess.builder()
.stdout("abc")
.build();
Result<Process, IOException> invoke = Result.ok(process);
Result<Process, Exception> invoke = Result.ok(process);
Result<Output, CmdException> result = Cmd.handle(invoke, "pwd");
assertTrue(result.isOk());
assertEquals(0, result.get().exitcode());
Expand All @@ -105,7 +105,7 @@ void handle__err__not_exited_normally() {
.stdout("abc")
.stderr("fail")
.build();
Result<Process, IOException> invoke = Result.ok(process);
Result<Process, Exception> invoke = Result.ok(process);
Result<Output, CmdException> result = Cmd.handle(invoke, "pwd");
assertTrue(result.isErr());
String msg = result.getErr().getMessage();
Expand All @@ -116,7 +116,7 @@ void handle__err__not_exited_normally() {

@Test
void handle__err__process_api_failed() {
Result<Process, IOException> invoke = Result.err(new IOException("fail"));
Result<Process, Exception> invoke = Result.err(new IOException("fail"));
Result<Output, CmdException> result = Cmd.handle(invoke, "pwd");
assertTrue(result.isErr());
assertNotNull(result.getErr().getCause());
Expand All @@ -129,10 +129,10 @@ void handle__err__read_stdout_failed() {
.stdout("abc")
.failure(TestProcess.Failure.STREAM_READ_ERR)
.build();
Result<Process, IOException> invoke = Result.ok(process);
Result<Process, Exception> invoke = Result.ok(process);
Result<Output, CmdException> result = Cmd.handle(invoke, "pwd");
assertTrue(result.isErr());
assertEquals("failed to read stdout", result.getErr().getCause().getMessage());
assertEquals("failed to read stdout/stderr", result.getErr().getCause().getMessage());
}

@Test
Expand All @@ -141,7 +141,7 @@ void handle__err__interrupted_when_waiting_for_exitcode() {
.stdout("abc")
.failure(TestProcess.Failure.WAIT_FOR_COMPLETION_INTERRUPTED)
.build();
Result<Process, IOException> invoke = Result.ok(process);
Result<Process, Exception> invoke = Result.ok(process);
Result<Output, CmdException> result = Cmd.handle(invoke, "pwd");
assertTrue(result.isErr());
assertNotNull(result.getErr().getCause());
Expand All @@ -154,7 +154,7 @@ void handle__err__generic_pojo_error() {
.stdout("abc")
.failure(TestProcess.Failure.GENERIC_ERROR_INJECTED)
.build();
Result<Process, IOException> invoke = Result.ok(process);
Result<Process, Exception> invoke = Result.ok(process);
assertThrows(RuntimeException.class, () -> Cmd.handle(invoke, "pwd"));
}

Expand Down
7 changes: 2 additions & 5 deletions src/test/java/io/github/artkonr/process/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ void readByteArray__ok__empty_stream() {

@Test
void readByteArray__ok__all_whitespace() {
InputStream stream = new ByteArrayInputStream(new byte[]{ 10, 10, 10 });
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

byte[] result = Util.readByteArray(new byte[]{ 10, 10, 10 });
assertArrayEquals(new byte[0], result);
}
Expand All @@ -57,7 +54,7 @@ void read__err__stream_read() {
);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

Result<byte[], CmdException> result = Util.read(reader);
Result<byte[], Exception> result = Util.read(reader);
assertTrue(result.isErr());
assertInstanceOf(CmdException.class, result.getErr());
assertNotNull(result.getErr().getCause());
Expand All @@ -71,7 +68,7 @@ void read__err__stream_close() {
InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
BufferedReader reader = new BrokenReader(stream, thrown);

Result<byte[], CmdException> result = Util.read(reader);
Result<byte[], Exception> result = Util.read(reader);
assertTrue(result.isErr());
assertInstanceOf(CmdException.class, result.getErr());
assertNotNull(result.getErr().getCause());
Expand Down
Loading