Skip to content
Draft
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
83 changes: 69 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,82 @@ dependencies {

</details>

[`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
}
```

Expand Down
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ tasks.withType<Test>().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>("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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
Loading