diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b8587f..bf928f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ ## Unreleased -* No changes (CI release improvements) +* Ensure only one hosting server exists at a time within a test context. A server hosted in a previous step will now be closed when starting another. ## v0.17.1 (April 1, 2026) diff --git a/toolproof/src/civilization.rs b/toolproof/src/civilization.rs index 67514da..12497fa 100644 --- a/toolproof/src/civilization.rs +++ b/toolproof/src/civilization.rs @@ -37,13 +37,8 @@ pub struct Civilization<'u> { } impl<'u> Civilization<'u> { - pub async fn shutdown(self) { - for handle in &self.handles { - handle.stop(false).await; - } - for thread in &self.threads { - thread.abort(); - } + pub async fn shutdown(mut self) { + self.stop_servers().await; if let Some(BrowserWindow::Chrome(window)) = self.window { window @@ -55,6 +50,16 @@ impl<'u> Civilization<'u> { } impl<'u> Civilization<'u> { + pub async fn stop_servers(&mut self) { + for handle in self.handles.drain(..) { + handle.stop(false).await; + } + for thread in self.threads.drain(..) { + thread.abort(); + } + self.purge_port(); + } + pub fn ensure_port(&mut self) -> u16 { if self.assigned_server_port.is_none() { self.assigned_server_port = pick_unused_port(); diff --git a/toolproof/src/definitions/hosting/mod.rs b/toolproof/src/definitions/hosting/mod.rs index 7b4aa36..c877b87 100644 --- a/toolproof/src/definitions/hosting/mod.rs +++ b/toolproof/src/definitions/hosting/mod.rs @@ -14,6 +14,8 @@ mod host_dir { use super::*; async fn host(dir: &String, civ: &mut Civilization<'_>) -> Result<(), ToolproofStepError> { + civ.stop_servers().await; + let mut attempts = 0; let mut running = false; while !running && attempts < 5 { diff --git a/toolproof/test_suite/browser/serve_twice.toolproof.yml b/toolproof/test_suite/browser/serve_twice.toolproof.yml new file mode 100644 index 0000000..7fe17fd --- /dev/null +++ b/toolproof/test_suite/browser/serve_twice.toolproof.yml @@ -0,0 +1,25 @@ +name: Browser can serve a directory twice in one test + +steps: + - step: I have a "my_test.toolproof.yml" file with the content {yaml} + yaml: |- + name: Inner serve twice test + + steps: + - I have a "public/index.html" file with the content "

First

" + - I serve the directory "public" + - In my browser, I load "/" + - step: In my browser, I evaluate {js} + js: |- + toolproof.assert_eq(document.querySelector("p").innerText, "First"); + - I have a "public2/index.html" file with the content "

Second

" + - I serve the directory "public2" + - In my browser, I load "/" + - step: In my browser, I evaluate {js} + js: |- + toolproof.assert_eq(document.querySelector("p").innerText, "Second"); + - I run "%toolproof_path% --timeout 60" + - step: "stdout should contain 'Total passing tests: 1'" + - step: "stdout should contain 'Failing tests: 0'" + - step: "stdout should contain 'All tests passed'" + - stderr should be empty