@@ -12,12 +12,9 @@ use webdriver_downloader::prelude::*;
1212#[ cfg( all( feature = "geckodriver" , feature = "chromedriver" ) ) ]
1313compile_error ! ( "Only one of 'geckodriver' or 'chromedriver' features can be enabled at a time." ) ;
1414
15- // Define the WEBDRIVER_BIN constant based on the enabled feature
16- #[ cfg( feature = "geckodriver" ) ]
17- const WEBDRIVER_BIN : & str = "geckodriver" ;
18-
19- #[ cfg( feature = "chromedriver" ) ]
20- const WEBDRIVER_BIN : & str = "chromedriver" ;
15+ // Enforce that at least one driver feature is enabled
16+ #[ cfg( not( any( feature = "geckodriver" , feature = "chromedriver" ) ) ) ]
17+ compile_error ! ( "At least one of 'geckodriver' or 'chromedriver' features must be enabled." ) ;
2118
2219#[ cfg( target_os = "windows" ) ]
2320const DRIVER_EXT : & str = ".exe" ;
@@ -61,33 +58,33 @@ fn user_bin_dir() -> PathBuf {
6158
6259/// Check if a driver is already installed at the given path from environment
6360/// variable
64- fn is_webdriver_available ( env_var : & str , executable_name : & str ) -> bool {
61+ fn is_webdriver_available ( env_var : & str , bin_name : & str ) -> bool {
6562 // First check environment variable path
6663 if let Ok ( path) = env:: var ( env_var) {
67- let exe_path = if cfg ! ( windows) && !path. to_lowercase ( ) . ends_with ( ".exe" ) {
64+ let bin_path = if cfg ! ( target_os = " windows" ) && !path. to_lowercase ( ) . ends_with ( ".exe" ) {
6865 format ! ( "{path}{DRIVER_EXT}" )
6966 } else {
7067 path
7168 } ;
72- let exe = Path :: new ( & exe_path ) ;
69+ let exe = Path :: new ( & bin_path ) ;
7370 if exe. exists ( ) && exe. is_file ( ) {
74- println ! ( "{executable_name } found at path specified in {env_var}: {exe_path }" ) ;
71+ println ! ( "{bin_name } found at path specified in {env_var}: {bin_path }" ) ;
7572 return true ;
7673 }
7774 }
7875
7976 // Check if webdriver exists in user's bin directory
8077 let bin_dir = user_bin_dir ( ) ;
81- let bin_path = bin_dir. join ( format ! ( "{executable_name }{DRIVER_EXT}" ) ) ;
78+ let bin_path = bin_dir. join ( format ! ( "{bin_name }{DRIVER_EXT}" ) ) ;
8279 if bin_path. exists ( ) && bin_path. is_file ( ) {
8380 println ! (
8481 "{} found in user's bin directory: {}" ,
85- executable_name ,
82+ bin_name ,
8683 bin_path. display( )
8784 ) ;
8885 println ! (
8986 "cargo:rustc-env=WEBDRIVER_DOWNLOAD_PATH={}" ,
90- bin_dir . to_string_lossy( )
87+ bin_path . to_string_lossy( )
9188 ) ;
9289 return true ;
9390 }
@@ -165,7 +162,7 @@ fn setup_driver(config: &WebdriverDownloadConfig) -> Result<()> {
165162 webdriver_bin_dir. display( )
166163 )
167164 } ) ?;
168- let webdriver_bin = webdriver_bin_dir. join ( WEBDRIVER_BIN ) ;
165+ let webdriver_bin = webdriver_bin_dir. join ( config . driver_name ) ;
169166
170167 println ! (
171168 "cargo::rerun-if-changed={}" ,
@@ -199,12 +196,38 @@ fn setup_driver(config: &WebdriverDownloadConfig) -> Result<()> {
199196
200197 println ! (
201198 "cargo:rustc-env=WEBDRIVER_DOWNLOAD_PATH={}" ,
202- webdriver_bin_dir . to_string_lossy( )
199+ webdriver_bin . to_string_lossy( )
203200 ) ;
204201
205202 Ok ( ( ) )
206203}
207204
205+ #[ cfg( feature = "chromedriver" ) ]
206+ fn get_chrome_path ( ) -> Result < PathBuf > {
207+ if let Ok ( chrome_path) = env:: var ( CHROME_PATH_ENV ) {
208+ let path = PathBuf :: from ( & chrome_path) ;
209+ if path. exists ( ) {
210+ Ok ( path)
211+ } else {
212+ Err ( anyhow ! ( "Chrome not found on path: {}" , chrome_path) ) . with_context ( || {
213+ format ! ( "Please set {CHROME_PATH_ENV} to a valid Chrome installation" )
214+ } )
215+ }
216+ } else {
217+ let new_browser_path = os_specific:: chromedriver_for_testing:: default_browser_path ( ) ?;
218+ let old_browser_path = os_specific:: chromedriver_old:: default_browser_path ( ) ?;
219+ if new_browser_path. exists ( ) {
220+ Ok ( new_browser_path)
221+ } else if old_browser_path. exists ( ) {
222+ Ok ( old_browser_path)
223+ } else {
224+ Err ( anyhow ! ( "Chrome browser not detected" ) ) . with_context ( || {
225+ format ! ( "Use {CHROME_PATH_ENV} to point to a valid Chrome installation" )
226+ } )
227+ }
228+ }
229+ }
230+
208231#[ cfg( feature = "geckodriver" ) ]
209232fn get_firefox_path ( ) -> Result < PathBuf > {
210233 if let Ok ( firefox_path) = env:: var ( FIREFOX_PATH_ENV ) {
@@ -234,32 +257,6 @@ fn get_firefox_path() -> Result<PathBuf> {
234257 }
235258}
236259
237- #[ cfg( feature = "chromedriver" ) ]
238- fn get_chrome_path ( ) -> Result < PathBuf > {
239- if let Ok ( chrome_path) = env:: var ( CHROME_PATH_ENV ) {
240- let path = PathBuf :: from ( & chrome_path) ;
241- if path. exists ( ) {
242- Ok ( path)
243- } else {
244- Err ( anyhow ! ( "Chrome not found on path: {}" , chrome_path) ) . with_context ( || {
245- format ! ( "Please set {CHROME_PATH_ENV} to a valid Chrome installation" )
246- } )
247- }
248- } else {
249- let new_browser_path = os_specific:: chromedriver_for_testing:: default_browser_path ( ) ?;
250- let old_browser_path = os_specific:: chromedriver_old:: default_browser_path ( ) ?;
251- if new_browser_path. exists ( ) {
252- Ok ( new_browser_path)
253- } else if old_browser_path. exists ( ) {
254- Ok ( old_browser_path)
255- } else {
256- Err ( anyhow ! ( "Chrome browser not detected" ) ) . with_context ( || {
257- format ! ( "Use {CHROME_PATH_ENV} to point to a valid Chrome installation" )
258- } )
259- }
260- }
261- }
262-
263260fn get_browser_version ( path : & PathBuf ) -> Result < String > {
264261 let output = Command :: new ( path)
265262 . arg ( "--version" )
@@ -307,16 +304,15 @@ fn main() -> Result<()> {
307304 if cfg ! ( feature = "webdriver_download" ) {
308305 println ! ( "cargo:rerun-if-changed=src/lib.rs" ) ;
309306 let webdriver_bin_dir = user_bin_dir ( ) ;
310- let webdriver_bin = webdriver_bin_dir. join ( WEBDRIVER_BIN ) ;
311307 println ! (
312308 "cargo::rerun-if-changed={}" ,
313- webdriver_bin . to_string_lossy( )
309+ webdriver_bin_dir . to_string_lossy( )
314310 ) ;
315311
316312 #[ cfg( feature = "chromedriver" ) ]
317313 {
318314 let config = WebdriverDownloadConfig {
319- driver_name : WEBDRIVER_BIN ,
315+ driver_name : "chromedriver" ,
320316 path_env : CHROMEDRIVER_PATH_ENV ,
321317 get_browser_path : get_chrome_path,
322318 } ;
@@ -326,15 +322,33 @@ fn main() -> Result<()> {
326322 #[ cfg( feature = "geckodriver" ) ]
327323 {
328324 let config = WebdriverDownloadConfig {
329- driver_name : WEBDRIVER_BIN ,
325+ driver_name : "geckodriver" ,
330326 path_env : GECKODRIVER_PATH_ENV ,
331327 get_browser_path : get_firefox_path,
332328 } ;
333329 setup_driver ( & config) ?;
334330 }
331+
332+ #[ cfg( not( any( feature = "chromedriver" , feature = "geckodriver" ) ) ) ]
333+ {
334+ println ! ( "cargo::warning=No specific driver feature enabled, skipping driver setup" ) ;
335+ }
335336 } else {
336- let msg = format ! ( "'webdriver_download' feature disabled. Please install a {GECKODRIVER_PATH_ENV} or {CHROMEDRIVER_PATH_ENV} version manually and make the environment variable 'WEBDRIVER_PATH' point to it." ) ;
337- println ! ( "cargo::warning={msg}" ) ;
337+ #[ cfg( feature = "chromedriver" ) ]
338+ {
339+ let msg = format ! ( "'webdriver_download' feature disabled. Please install a {CHROMEDRIVER_PATH_ENV} version manually and make the environment variable 'WEBDRIVER_PATH' point to it." ) ;
340+ println ! ( "cargo::warning={msg}" ) ;
341+ }
342+ #[ cfg( feature = "geckodriver" ) ]
343+ {
344+ let msg = format ! ( "'webdriver_download' feature disabled. Please install a {} version manually and make the environment variable 'WEBDRIVER_PATH' point to it." ,
345+ GECKODRIVER_PATH_ENV ) ;
346+ println ! ( "cargo::warning={msg}" ) ;
347+ }
348+ #[ cfg( not( any( feature = "chromedriver" , feature = "geckodriver" ) ) ) ]
349+ {
350+ println ! ( "cargo::warning='webdriver_download' feature disabled and no driver feature enabled" ) ;
351+ }
338352 }
339353 Ok ( ( ) )
340354}
0 commit comments