Skip to content

Commit cd0f8d8

Browse files
committed
separate webdriver launch from other logic
Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
1 parent a2badab commit cd0f8d8

6 files changed

Lines changed: 444 additions & 410 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ jobs:
4444
components: clippy
4545
targets: wasm32-unknown-unknown
4646
# lint plotly_static for all features
47+
- run: cargo check -p plotly_static
48+
- run: cargo test -p plotly_static build_without_driver_feature_returns_error
4749
- run: cargo clippy -p plotly_static --features geckodriver,webdriver_download -- -D warnings -A deprecated
4850
- run: cargo clippy -p plotly_static --features chromedriver,webdriver_download -- -D warnings -A deprecated
4951
# lint the main library workspace for non-wasm target

‎plotly_static/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ serde_json = "1.0"
6363

6464
### Feature Flags
6565

66-
- To use static export, enable exactly one of the driver features below.
66+
- To use static export at runtime, enable exactly one of the driver features below.
6767
- `chromedriver`: Use Chromedriver and Chrome/Chromium browser for rendering and export
6868
- `geckodriver`: Use Geckodriver Firefox browser for rendering for rendering and export
6969
- `webdriver_download`: Auto-download the chosen WebDriver binary

‎plotly_static/build.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn setup_driver(config: &WebdriverDownloadConfig) -> Result<()> {
200200
Ok(())
201201
}
202202

203-
#[cfg(all(feature = "chromedriver", not(feature = "geckodriver")))]
203+
#[cfg(feature = "chromedriver")]
204204
fn get_chrome_path() -> Result<PathBuf> {
205205
if let Ok(chrome_path) = env::var(BROWSER_BIN_PATH_ENV) {
206206
let path = PathBuf::from(&chrome_path);

‎plotly_static/src/lib.rs‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
//!
6262
//! ### Driver Features
6363
//!
64-
//! To use static export, enable one of the following features:
64+
//! For compile time only, no specific feature must be provided. However, to use static export at runtime, one of the below features must be enabled.
6565
//!
6666
//! - `chromedriver`: Use Chrome/Chromium for rendering
6767
//! - `geckodriver`: Use Firefox for rendering
@@ -304,6 +304,9 @@ use crate::template::{image_export_js_script, pdf_export_js_script};
304304
mod template;
305305
mod webdriver;
306306

307+
#[cfg(not(any(feature = "chromedriver", feature = "geckodriver")))]
308+
const DRIVER_FEATURE_REQUIRED: &str = "Static image export at runtime requires enabling either the 'chromedriver' or 'geckodriver' feature.";
309+
307310
/// Supported image formats for static image export.
308311
///
309312
/// This enum defines all the image formats that can be exported from Plotly
@@ -649,6 +652,7 @@ impl StaticExporterBuilder {
649652
}
650653

651654
/// Create a new WebDriver instance based on the spawn_webdriver flag
655+
#[cfg(any(feature = "chromedriver", feature = "geckodriver"))]
652656
fn create_webdriver(&self) -> Result<WebDriver> {
653657
let port = self.webdriver_port;
654658
let in_async = tokio::runtime::Handle::try_current().is_ok();
@@ -689,16 +693,22 @@ impl StaticExporterBuilder {
689693
/// .expect("Failed to build AsyncStaticExporter");
690694
/// ```
691695
pub fn build_async(&self) -> Result<AsyncStaticExporter> {
692-
let wd = self.create_webdriver()?;
693-
Ok(AsyncStaticExporter {
694-
webdriver_port: self.webdriver_port,
695-
webdriver_url: self.webdriver_url.clone(),
696-
webdriver: wd,
697-
offline_mode: self.offline_mode,
698-
pdf_export_timeout: self.pdf_export_timeout,
699-
webdriver_browser_caps: self.webdriver_browser_caps.clone(),
700-
webdriver_client: None,
701-
})
696+
#[cfg(not(any(feature = "chromedriver", feature = "geckodriver")))]
697+
return Err(anyhow!(DRIVER_FEATURE_REQUIRED));
698+
699+
#[cfg(any(feature = "chromedriver", feature = "geckodriver"))]
700+
{
701+
let wd = self.create_webdriver()?;
702+
Ok(AsyncStaticExporter {
703+
webdriver_port: self.webdriver_port,
704+
webdriver_url: self.webdriver_url.clone(),
705+
webdriver: wd,
706+
offline_mode: self.offline_mode,
707+
pdf_export_timeout: self.pdf_export_timeout,
708+
webdriver_browser_caps: self.webdriver_browser_caps.clone(),
709+
webdriver_client: None,
710+
})
711+
}
702712
}
703713
}
704714

@@ -779,7 +789,7 @@ impl StaticExporter {
779789
/// # Examples
780790
///
781791
/// ```no_run
782-
///
792+
///
783793
/// // This example requires a running WebDriver (chromedriver/geckodriver) and a browser.
784794
/// // It cannot be run as a doc test.
785795
///
@@ -842,7 +852,7 @@ impl StaticExporter {
842852
/// # Examples
843853
///
844854
/// ```no_run
845-
///
855+
///
846856
/// // This example requires a running WebDriver (chromedriver/geckodriver) and a browser.
847857
/// // It cannot be run as a doc test.
848858
/// use plotly_static::{StaticExporterBuilder, ImageFormat};
@@ -1128,9 +1138,7 @@ impl AsyncStaticExporter {
11281138
fn build_webdriver_caps(&self) -> Result<Capabilities> {
11291139
#[cfg(not(any(feature = "chromedriver", feature = "geckodriver")))]
11301140
{
1131-
Err(anyhow!(
1132-
"Static image export requires enabling either the 'chromedriver' or 'geckodriver' feature."
1133-
))
1141+
Err(anyhow!(DRIVER_FEATURE_REQUIRED))
11341142
}
11351143
#[cfg(any(feature = "chromedriver", feature = "geckodriver"))]
11361144
{
@@ -1348,6 +1356,15 @@ mod tests {
13481356
let _ = env_logger::try_init();
13491357
}
13501358

1359+
#[test]
1360+
#[cfg(not(any(feature = "chromedriver", feature = "geckodriver")))]
1361+
fn build_without_driver_feature_returns_error() {
1362+
match StaticExporterBuilder::default().build_async() {
1363+
Err(e) => assert_eq!(e.to_string(), DRIVER_FEATURE_REQUIRED),
1364+
Ok(_) => panic!("expected build to fail without a driver feature"),
1365+
}
1366+
}
1367+
13511368
// Helper to generate unique ports for parallel tests
13521369
#[cfg(not(feature = "debug"))]
13531370
fn get_unique_port() -> u32 {

0 commit comments

Comments
 (0)