guide: keep serving when stdout is closed #43

Merged
jcoffey-dev merged 1 commits from fix/guide-stdout into main 2026-10-30 21:01:00 +00:00
3 changed files with 56 additions and 9 deletions

No files matched your search

+8 -4
View File
@@ -16,7 +16,7 @@ pub mod form;
mod http;
use std::fs;
use std::io::{BufRead, BufReader, Read};
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
@@ -134,10 +134,14 @@ pub fn run(config: GuideConfig) -> Result<(), Error> {
});
let url = format!("http://127.0.0.1:{port}/{}/", guide.token);
println!("The guide is at {url}");
println!("Keep this running while you use it. Ctrl-C ends it.");
// Said, not needed: with stdout closed the guide still serves.
let say = |line: &str| {
let _ = writeln!(std::io::stdout(), "{line}");
};
say(&format!("The guide is at {url}"));
say("Keep this running while you use it. Ctrl-C ends it.");
if config.open && !open_browser(&url) {
println!("Open that address in a browser on this machine.");
say("Open that address in a browser on this machine.");
}
for stream in listener.incoming() {
let Ok(stream) = stream else { continue };
+25 -4
View File
@@ -10,13 +10,15 @@
use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream;
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::process::{Child, ChildStdout, Command, Stdio};
use std::time::{Duration, Instant};
use serde_json::Value;
pub struct Guide {
child: Child,
/// Kept open: the guide may still be printing.
_out: Option<BufReader<ChildStdout>>,
pub host: String,
pub token: String,
}
@@ -37,9 +39,8 @@ pub fn start(bin: &str, dir: &Path) -> Guide {
.spawn()
.unwrap();
let mut line = String::new();
BufReader::new(child.stdout.take().unwrap())
.read_line(&mut line)
.unwrap();
let mut out = BufReader::new(child.stdout.take().unwrap());
out.read_line(&mut line).unwrap();
let url = line
.trim()
.strip_prefix("The guide is at http://")
@@ -49,6 +50,26 @@ pub fn start(bin: &str, dir: &Path) -> Guide {
host: host.to_owned(),
token: token.to_owned(),
child,
_out: Some(out),
}
}
/// A guide whose stdout is closed before it prints anything, on `port`;
/// the token is read from nowhere, so only its refusals can be checked.
pub fn start_unheard(bin: &str, dir: &Path, port: u16) -> Guide {
let (reader, writer) = std::io::pipe().unwrap();
drop(reader);
let child = Command::new(bin)
.args(["guide", "--no-open", "--port", &port.to_string()])
.arg(dir)
.stdout(writer)
.spawn()
.unwrap();
Guide {
host: format!("127.0.0.1:{port}"),
token: String::new(),
child,
_out: None,
}
}
+23 -1
View File
@@ -13,7 +13,7 @@ mod integration;
use std::fs;
use integration::guide::{get, post, send, settled, start};
use integration::guide::{get, post, send, settled, start, start_unheard};
use serde_json::{Value, json};
use tempfile::TempDir;
@@ -144,3 +144,25 @@ fn a_maildir_move_previews_runs_and_reports() {
serde_json::from_str(&fs::read_to_string(root.join("guide.json")).unwrap()).unwrap();
assert_eq!(saved["form"]["target"]["url"], "http://127.0.0.1:9");
}
#[test]
fn a_closed_stdout_doesnt_end_the_guide() {
let dir = TempDir::new().unwrap();
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let guide = start_unheard(BIN, dir.path(), port);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
while std::net::TcpStream::connect(&guide.host).is_err() {
assert!(
std::time::Instant::now() < deadline,
"the guide never listened"
);
std::thread::sleep(std::time::Duration::from_millis(50));
}
std::thread::sleep(std::time::Duration::from_millis(300));
let head = format!("GET /x/ HTTP/1.1\r\nHost: {}", guide.host);
assert_eq!(send(&guide, &head, "").0, 404);
}