From dd856639bfdc040bd8406c237fad1d7263a9f737 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Mon, 30 Nov 2026 20:03:58 -0600 Subject: [PATCH] Docs(example): Run the first Java program why: A reader following the published BOM should get a runnable first example. what: Exercise a real shell from the README and run the displayed source in a fresh JVM with an explicitly selected core JAR. --- CHANGELOG.md | 3 + README.md | 83 +++++++++++++++---- docs/README.md | 8 ++ docs/build.gradle.kts | 4 + .../docs/DocumentationSnippetsTest.java | 35 ++++++++ 5 files changed, 119 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df95979db..c6c72e5e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,9 @@ release that first shipped the module. ### Documented +- **The Java quick start now runs an interactive shell against the published + core.** Its complete main program prints the reply and exit status. + - **The Kotlin and Scala quick starts now run as standalone programs.** Each includes its build file and starts a private tmux session using the published libraries. (#203) diff --git a/README.md b/README.md index 97615e16d..9c37b0d6b 100644 --- a/README.md +++ b/README.md @@ -69,27 +69,82 @@ dependencies { -[`Server.start`][server-start] starts a private tmux server that closing the -[`Server`][server] stops. Make a [`Session`][session], [`Window`][window], and -split [`Pane`][pane], run a command, and leave nothing behind: +Save this interactive test as `Hello.java`. It types a name into a shell and +prints the reply and its exit status. It uses the published core, JDK 25 or +newer, and tmux 3.3a or newer on `PATH`. ```java -import io.github.libtmux.Pane; -import io.github.libtmux.PaneRun; import io.github.libtmux.Server; -import io.github.libtmux.Session; -import io.github.libtmux.Window; +import io.github.libtmux.ServerEndpoint; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.time.Duration; -try (Server server = Server.start()) { - Session session = server.newSession("demo"); - Window window = session.newWindow("build"); - Pane pane = window.split(); +class Hello { + public static void main(String[] args) + throws IOException, InterruptedException { + var root = Path.of(args.length == 0 + ? "/tmp/libtmux-java-dev" : args[0]); + Files.createDirectories(root); + var dir = Files.createTempDirectory(root, "hi-"); + var socket = dir.resolve("s"); + try (var server = Server.builder().configFile(Path.of("/dev/null")) + .endpoint(ServerEndpoint.socketPath(socket)).build()) { + try { + var pane = server.newSession(s -> s.named("demo") + .env("ENV", "/dev/null").env("PS1", "ready>") + .running("sh", "-i")).activePane().orElseThrow(); + var wait = Duration.ofSeconds(5); + var prompt = pane.watchText("ready>", wait).output(); + if (!prompt.contains("ready>")) { + throw new IllegalStateException("Shell prompt missing"); + } + pane.sendLine("read -r name"); + pane.sendLine("libtmux"); + var reply = pane.run("printf 'hello from %s\\n' \"$name\"", + wait); + System.out.println(String.join("\n", reply.output())); + System.out.println("exit " + reply.exitStatus().orElseThrow()); + } finally { + if (server.isAlive()) { + server.killServer(); + } + Files.deleteIfExists(socket); + Files.delete(dir); + } + } + } +} +``` + +In the directory containing `Hello.java`, get the published JAR with Maven: + +```console +$ mvn -q dependency:copy -Dartifact=io.github.libtmux:libtmux:0.1.2 \ + -DoutputDirectory=. +``` - PaneRun ran = pane.run("echo hello from libtmux", Duration.ofSeconds(30)); +Run the source file. Each run owns a new socket directory under +`/tmp/libtmux-java-dev/` and removes it before returning. + +```console +$ java -cp libtmux-0.1.2.jar Hello.java +``` + +```text +hello from libtmux +exit 0 +``` + +From a source checkout, [`Server.start`][server-start] also creates the +private socket and removes it on close: + +```java +import io.github.libtmux.Server; - ran.output(); // → [hello from libtmux] - ran.exitStatus().getAsInt(); // → 0 +try (var server = Server.start()) { + server.newSession("demo").name(); // → demo } ``` diff --git a/docs/README.md b/docs/README.md index 2a083fb47..5c8653362 100644 --- a/docs/README.md +++ b/docs/README.md @@ -118,6 +118,14 @@ guide and README, and checks the claims around it. $ ./gradlew :docs:test ``` +The first Java example in the root README also runs as a complete program in a +fresh JVM with only the core JAR. To exercise a published copy, pass its path: + +```console +$ ./gradlew :docs:test --tests '*firstJavaExampleRunsWithOnlyTheCoreJar' \ + -PlibtmuxReadmeJar=/path/to/libtmux.jar +``` + One case per snippet, named for the file and line it came from, so a failure says where to look. One tmux server per case, from [`libtmux-junit5`](../libtmux-junit5/), so a snippet that makes a session gets a diff --git a/docs/build.gradle.kts b/docs/build.gradle.kts index 89b1836cd..4e92e1a5e 100644 --- a/docs/build.gradle.kts +++ b/docs/build.gradle.kts @@ -77,6 +77,10 @@ tasks.withType().configureEach { val fieldCatalog = rootProject.file("libtmux/src/main/resources/META-INF/io.github.libtmux/field-catalog.tsv") + val core = providers.gradleProperty("libtmuxReadmeJar").map { rootProject.file(it) } + .orElse(project(":libtmux").tasks.named("jar").flatMap { it.archiveFile }.map { it.asFile }) + inputs.file(core).withPropertyName("readmeCoreJar").withPathSensitivity(PathSensitivity.NONE) + doFirst { systemProperty("libtmux.docs.readmeCore", core.get().absolutePath) } inputs.files(classpath) inputs.files(documents).withPropertyName("documentation").withPathSensitivity(PathSensitivity.RELATIVE) inputs.files(sources).withPropertyName("sources").withPathSensitivity(PathSensitivity.RELATIVE) diff --git a/docs/src/test/java/io/github/libtmux/docs/DocumentationSnippetsTest.java b/docs/src/test/java/io/github/libtmux/docs/DocumentationSnippetsTest.java index c036dd75d..8d48884c9 100644 --- a/docs/src/test/java/io/github/libtmux/docs/DocumentationSnippetsTest.java +++ b/docs/src/test/java/io/github/libtmux/docs/DocumentationSnippetsTest.java @@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -118,6 +119,40 @@ void everyJavaFenceIsOneTheExtractorTakes() throws IOException { } } + @Test + @Timeout(60) + void firstJavaExampleRunsWithOnlyTheCoreJar(TmuxSocketPath fixture, @TempDir Path out) throws Exception { + String source = Documentation.snippetsIn(ROOT, ROOT.resolve("README.md")) + .getFirst() + .code(); + Path program = Files.writeString(out.resolve("Hello.java"), source); + Path transcript = out.resolve("output"); + Path root = Files.createDirectory(fixture.path().resolveSibling("hello")); + ProcessBuilder command = new ProcessBuilder( + Path.of(System.getProperty("java.home"), "bin", "java").toString(), + "-cp", + System.getProperty("libtmux.docs.readmeCore"), + program.toString(), + root.toString()) + .redirectErrorStream(true) + .redirectOutput(transcript.toFile()); + Path binary = Path.of(System.getProperty("libtmux.tmux", "tmux")); + Path binaries = binary.getParent(); + if (binary.isAbsolute() && binaries != null) { + command.environment().merge("PATH", binaries.toString(), (path, dir) -> dir + ":" + path); + } + Process child = command.start(); + try { + assertTrue(child.waitFor(45, TimeUnit.SECONDS), "README program timed out"); + String output = Files.readString(transcript); + assertEquals(0, child.exitValue(), output); + assertEquals(List.of("hello from libtmux", "exit 0"), output.lines().toList()); + Files.delete(root); + } finally { + child.destroyForcibly(); + } + } + /** * A snippet that hangs would otherwise hang the build. Generous next to the slowest snippet, * about 2 seconds, since a slow runner still has to pass.