From 239fd40afa265910f2f10cd8c007ff01f21e7652 Mon Sep 17 00:00:00 2001 From: artkonr Date: Wed, 28 Aug 2024 16:10:49 +0200 Subject: [PATCH] upgrade dependencies; adapt to new `Result` APIs --- pom.xml | 2 +- .../java/io/github/artkonr/process/Chain.java | 5 ++-- .../java/io/github/artkonr/process/Cmd.java | 25 +++++++++++-------- .../java/io/github/artkonr/process/Util.java | 4 +-- .../io/github/artkonr/process/CmdTest.java | 16 ++++++------ .../io/github/artkonr/process/UtilTest.java | 7 ++---- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index 7acff88..d664fd8 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ 17 17 UTF-8 - 1.5.0 + 1.7.1 1.18.32 5.10.2 diff --git a/src/main/java/io/github/artkonr/process/Chain.java b/src/main/java/io/github/artkonr/process/Chain.java index a86630c..ef1fc32 100644 --- a/src/main/java/io/github/artkonr/process/Chain.java +++ b/src/main/java/io/github/artkonr/process/Chain.java @@ -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; @@ -100,8 +99,8 @@ public Chain pipeTo(@NonNull Cmd command) { */ @Override public Result invoke() { - Result, IOException> invoked = Result - .wrap(IOException.class, () -> ProcessBuilder.startPipeline(pipeline)); + Result, Exception> invoked = Result + .wrap(() -> ProcessBuilder.startPipeline(pipeline)); int endI = pipeline.size() - 1; var fin = handle( diff --git a/src/main/java/io/github/artkonr/process/Cmd.java b/src/main/java/io/github/artkonr/process/Cmd.java index 5b163a0..d1a098b 100644 --- a/src/main/java/io/github/artkonr/process/Cmd.java +++ b/src/main/java/io/github/artkonr/process/Cmd.java @@ -3,7 +3,6 @@ import io.github.artkonr.result.Result; import lombok.NonNull; -import java.io.IOException; import static io.github.artkonr.process.Util.*; @@ -80,7 +79,7 @@ public Chain pipeTo(@NonNull Cmd command) { @Override public Result invoke() { return handle( - Result.wrap(IOException.class, handle::start), + Result.wrap(handle::start), getCmd(handle) ); } @@ -182,19 +181,26 @@ private Output(long pid, String cmd, int exitcode, Data stdout, Data stderr) { * @param cmd invoked command * @return invocation {@link Result} */ - static Result handle(Result result, + static Result handle(Result 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, @@ -202,8 +208,7 @@ static Result handle(Result read(BufferedReader reader) { + static Result read(BufferedReader reader) { try ( BufferedReader input = reader; ByteArrayOutputStream buf = new ByteArrayOutputStream(); @@ -54,7 +54,7 @@ static Result 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)); } } diff --git a/src/test/java/io/github/artkonr/process/CmdTest.java b/src/test/java/io/github/artkonr/process/CmdTest.java index 5ff8389..5c29fec 100644 --- a/src/test/java/io/github/artkonr/process/CmdTest.java +++ b/src/test/java/io/github/artkonr/process/CmdTest.java @@ -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 @@ -90,7 +90,7 @@ void handle__ok() { Process process = TestProcess.builder() .stdout("abc") .build(); - Result invoke = Result.ok(process); + Result invoke = Result.ok(process); Result result = Cmd.handle(invoke, "pwd"); assertTrue(result.isOk()); assertEquals(0, result.get().exitcode()); @@ -105,7 +105,7 @@ void handle__err__not_exited_normally() { .stdout("abc") .stderr("fail") .build(); - Result invoke = Result.ok(process); + Result invoke = Result.ok(process); Result result = Cmd.handle(invoke, "pwd"); assertTrue(result.isErr()); String msg = result.getErr().getMessage(); @@ -116,7 +116,7 @@ void handle__err__not_exited_normally() { @Test void handle__err__process_api_failed() { - Result invoke = Result.err(new IOException("fail")); + Result invoke = Result.err(new IOException("fail")); Result result = Cmd.handle(invoke, "pwd"); assertTrue(result.isErr()); assertNotNull(result.getErr().getCause()); @@ -129,10 +129,10 @@ void handle__err__read_stdout_failed() { .stdout("abc") .failure(TestProcess.Failure.STREAM_READ_ERR) .build(); - Result invoke = Result.ok(process); + Result invoke = Result.ok(process); Result 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 @@ -141,7 +141,7 @@ void handle__err__interrupted_when_waiting_for_exitcode() { .stdout("abc") .failure(TestProcess.Failure.WAIT_FOR_COMPLETION_INTERRUPTED) .build(); - Result invoke = Result.ok(process); + Result invoke = Result.ok(process); Result result = Cmd.handle(invoke, "pwd"); assertTrue(result.isErr()); assertNotNull(result.getErr().getCause()); @@ -154,7 +154,7 @@ void handle__err__generic_pojo_error() { .stdout("abc") .failure(TestProcess.Failure.GENERIC_ERROR_INJECTED) .build(); - Result invoke = Result.ok(process); + Result invoke = Result.ok(process); assertThrows(RuntimeException.class, () -> Cmd.handle(invoke, "pwd")); } diff --git a/src/test/java/io/github/artkonr/process/UtilTest.java b/src/test/java/io/github/artkonr/process/UtilTest.java index 9deb4d7..f8fd197 100644 --- a/src/test/java/io/github/artkonr/process/UtilTest.java +++ b/src/test/java/io/github/artkonr/process/UtilTest.java @@ -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); } @@ -57,7 +54,7 @@ void read__err__stream_read() { ); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - Result result = Util.read(reader); + Result result = Util.read(reader); assertTrue(result.isErr()); assertInstanceOf(CmdException.class, result.getErr()); assertNotNull(result.getErr().getCause()); @@ -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 result = Util.read(reader); + Result result = Util.read(reader); assertTrue(result.isErr()); assertInstanceOf(CmdException.class, result.getErr()); assertNotNull(result.getErr().getCause());