From ad099ba9af9c50d80b3b964134b31d79a5c81c05 Mon Sep 17 00:00:00 2001 From: arctic_hen7 Date: Mon, 4 Jul 2022 14:15:51 +1000 Subject: [PATCH] test: added tests for custom server integration This involved adding the infrastructure for integration-locked example testing. --- .github/workflows/ci.yml | 2 ++ bonnie.toml | 3 ++- examples/core/basic/src/templates/index.rs | 2 +- examples/core/custom_server/tests/main.rs | 15 +++++++++------ scripts/test.rs | 11 ++++++++++- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1777c63d89..6c004d4308 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,6 +56,8 @@ jobs: type: core - name: index_view type: core + - name: custom_server + type: core steps: - uses: actions/checkout@v2 - run: cargo install bonnie wasm-pack rust-script diff --git a/bonnie.toml b/bonnie.toml index aa5be02b01..ff969d603d 100644 --- a/bonnie.toml +++ b/bonnie.toml @@ -152,7 +152,8 @@ test.cmd = [ "bonnie test example-all-integrations core rx_state --headless", "bonnie test example-all-integrations core index_view --headless", "bonnie test example-all-integrations core set_headers --headless", - "bonnie test example-all-integrations core static_content --headless" + "bonnie test example-all-integrations core static_content --headless", + "bonnie test example-all-integrations core custom_server --headless" ] test.desc = "runs all tests headlessly (assumes geckodriver running in background)" test.subcommands.core.cmd = "cargo test" diff --git a/examples/core/basic/src/templates/index.rs b/examples/core/basic/src/templates/index.rs index d51ebd2fd4..10082b7f02 100644 --- a/examples/core/basic/src/templates/index.rs +++ b/examples/core/basic/src/templates/index.rs @@ -34,6 +34,6 @@ pub async fn get_build_state( _locale: String, ) -> RenderFnResultWithCause { Ok(IndexPageState { - greeting: "Hello Worlds!".to_string(), + greeting: "Hello World!".to_string(), }) } diff --git a/examples/core/custom_server/tests/main.rs b/examples/core/custom_server/tests/main.rs index 7029e4c2b2..2b336084ff 100644 --- a/examples/core/custom_server/tests/main.rs +++ b/examples/core/custom_server/tests/main.rs @@ -8,14 +8,10 @@ async fn main(c: &mut Client) -> Result<(), fantoccini::error::CmdError> { let url = c.current_url().await?; assert!(url.as_ref().starts_with("http://localhost:8080")); - // The greeting was passed through using build state wait_for_checkpoint!("initial_state_present", 0, c); wait_for_checkpoint!("page_visible", 0, c); let greeting = c.find(Locator::Css("p")).await?.text().await?; assert_eq!(greeting, "Hello World!"); - // For some reason, retrieving the inner HTML or text of a `` doens't work - let title = c.find(Locator::Css("title")).await?.html(false).await?; - assert!(title.contains("Index Page")); // Go to `/about` c.find(Locator::Id("about-link")).await?.click().await?; @@ -26,11 +22,18 @@ async fn main(c: &mut Client) -> Result<(), fantoccini::error::CmdError> { // Make sure the hardcoded text there exists let text = c.find(Locator::Css("p")).await?.text().await?; assert_eq!(text, "About."); - let title = c.find(Locator::Css("title")).await?.html(false).await?; - assert!(title.contains("About Page")); // Make sure we get initial state if we refresh c.refresh().await?; wait_for_checkpoint!("initial_state_present", 0, c); + // Check the API routes (we test with a server) + // The echo route should echo back everything we give it + c.goto("http://localhost:8080/api/echo/test").await?; + let text = c.find(Locator::Css("pre")).await?.text().await?; + assert_eq!(text, "test"); + c.goto("http://localhost:8080/api/echo/blah").await?; + let text = c.find(Locator::Css("pre")).await?.text().await?; + assert_eq!(text, "blah"); + Ok(()) } diff --git a/scripts/test.rs b/scripts/test.rs index 37bba720dc..266c8c1986 100644 --- a/scripts/test.rs +++ b/scripts/test.rs @@ -67,12 +67,21 @@ fn real_main() -> i32 { .spawn() .expect("couldn't start test server (command execution failed)"); + // Check if this example is locked to a single integration (in which case we shouldn't apply the user's integration setting) + let integration_locked = std::fs::metadata(format!("examples/{}/{}/.integration_locked", &category, &example)).is_ok(); + let cargo_features = if integration_locked { + println!("The given example is locked to a specific integration. The provided integration may not be used."); + String::new() + } else { + format!("--features 'perseus-integration/{}'", &integration) + }; + // We want to get an exit code from actually running the tests (which interact with the server we're running in the background), which we'll return from this process let exit_code = { let output = Command::new(shell_exec) .current_dir(&format!("examples/{}/{}", &category, &example)) // TODO Confirm that this syntax works on Windows - .args([shell_param, &format!("cargo test --features 'perseus-integration/{}' -- --test-threads 1", &integration)]) + .args([shell_param, &format!("cargo test {} -- --test-threads 1", &cargo_features)]) .envs(if is_headless { vec![("PERSEUS_RUN_WASM_TESTS", "true"), ("PERSEUS_RUN_WASM_TESTS_HEADLESS", "true")] } else {