Skip to content

Commit

Permalink
[rust] Avoid unwrap when creating cache path
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Aug 8, 2023
1 parent 694f67e commit f4fa3fa
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 75 deletions.
39 changes: 20 additions & 19 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,20 @@ impl ChromeManager {
}
}

fn get_browser_path_in_cache(&self) -> PathBuf {
self.get_cache_path()
fn get_browser_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
Ok(self
.get_cache_path()?
.join(self.get_browser_name())
.join(self.get_platform_label())
.join(self.get_browser_version())
.join(self.get_browser_version()))
}

fn get_browser_binary_path_in_cache(&self) -> PathBuf {
let browser_in_cache = self.get_browser_path_in_cache();
fn get_browser_binary_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let browser_in_cache = self.get_browser_path_in_cache()?;
if MACOS.is(self.get_os()) {
browser_in_cache.join(CFT_MACOS_APP_NAME)
Ok(browser_in_cache.join(CFT_MACOS_APP_NAME))
} else {
browser_in_cache.join(self.get_browser_name_with_extension())
Ok(browser_in_cache.join(self.get_browser_name_with_extension()))
}
}
}
Expand Down Expand Up @@ -400,7 +401,7 @@ impl SeleniumManager for ChromeManager {
fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -445,7 +446,7 @@ impl SeleniumManager for ChromeManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}
Ok(driver_version)
}
Expand All @@ -456,7 +457,7 @@ impl SeleniumManager for ChromeManager {
let browser_name = self.browser_name;
let browser_version;
let major_browser_version = self.get_major_browser_version();
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

// First, browser version is checked in the local metadata
match get_browser_version_from_metadata(
Expand Down Expand Up @@ -484,7 +485,7 @@ impl SeleniumManager for ChromeManager {
&browser_version,
browser_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}
}
}
Expand Down Expand Up @@ -534,17 +535,17 @@ impl SeleniumManager for ChromeManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch_folder = self.get_platform_label();
compose_driver_path_in_cache(
self.get_cache_path(),
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
os,
arch_folder,
driver_version,
)
))
}

fn get_config(&self) -> &ManagerConfig {
Expand All @@ -570,7 +571,7 @@ impl SeleniumManager for ChromeManager {
fn download_browser(&mut self) -> Result<Option<PathBuf>, Box<dyn Error>> {
let browser_version;
let browser_name = self.browser_name;
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);
let major_browser_version = self.get_major_browser_version();
let major_browser_version_int = major_browser_version.parse::<i32>().unwrap_or_default();

Expand Down Expand Up @@ -622,7 +623,7 @@ impl SeleniumManager for ChromeManager {
&browser_version,
browser_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}
}
}
Expand All @@ -632,7 +633,7 @@ impl SeleniumManager for ChromeManager {
));

// Checking if browser version is in the cache
let browser_binary_path = self.get_browser_binary_path_in_cache();
let browser_binary_path = self.get_browser_binary_path_in_cache()?;
if browser_binary_path.exists() {
self.get_logger().debug(format!(
"{} {} already in the cache",
Expand Down Expand Up @@ -661,7 +662,7 @@ impl SeleniumManager for ChromeManager {

uncompress(
&driver_zip_file,
&self.get_browser_path_in_cache(),
&self.get_browser_path_in_cache()?,
self.get_logger(),
None,
)?;
Expand Down
12 changes: 6 additions & 6 deletions rust/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl SeleniumManager for EdgeManager {

fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let mut major_browser_version = self.get_major_browser_version();
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -191,7 +191,7 @@ impl SeleniumManager for EdgeManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}

Ok(driver_version)
Expand Down Expand Up @@ -230,7 +230,7 @@ impl SeleniumManager for EdgeManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch = self.get_arch();
Expand All @@ -251,13 +251,13 @@ impl SeleniumManager for EdgeManager {
} else {
"linux64"
};
compose_driver_path_in_cache(
self.get_cache_path(),
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
os,
arch_folder,
driver_version,
)
))
}

fn get_config(&self) -> &ManagerConfig {
Expand Down
14 changes: 8 additions & 6 deletions rust/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ impl BrowserPath {
}
}

pub fn create_parent_path_if_not_exists(path: &Path) {
pub fn create_parent_path_if_not_exists(path: &Path) -> Result<(), Box<dyn Error>> {
if let Some(p) = path.parent() {
create_path_if_not_exists(p);
create_path_if_not_exists(p)?;
}
Ok(())
}

pub fn create_path_if_not_exists(path: &Path) {
pub fn create_path_if_not_exists(path: &Path) -> Result<(), Box<dyn Error>> {
if !path.exists() {
fs::create_dir_all(path).unwrap();
fs::create_dir_all(path)?;
}
Ok(())
}

pub fn uncompress(
Expand Down Expand Up @@ -132,7 +134,7 @@ pub fn unzip(
None => continue,
};
if single_file.is_none() {
create_path_if_not_exists(target);
create_path_if_not_exists(target)?;
out_path = target.join(path);
}

Expand All @@ -148,7 +150,7 @@ pub fn unzip(
out_path.display(),
file.size()
));
create_parent_path_if_not_exists(out_path.as_path());
create_parent_path_if_not_exists(out_path.as_path())?;

let mut outfile = File::create(&out_path)?;
io::copy(&mut file, &mut outfile)?;
Expand Down
12 changes: 6 additions & 6 deletions rust/src/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl SeleniumManager for FirefoxManager {
fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -161,7 +161,7 @@ impl SeleniumManager for FirefoxManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}

Ok(driver_version)
Expand Down Expand Up @@ -211,7 +211,7 @@ impl SeleniumManager for FirefoxManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch = self.get_arch();
Expand Down Expand Up @@ -241,13 +241,13 @@ impl SeleniumManager for FirefoxManager {
} else {
"linux64"
};
compose_driver_path_in_cache(
self.get_cache_path(),
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
os,
arch_folder,
driver_version,
)
))
}

fn get_config(&self) -> &ManagerConfig {
Expand Down
11 changes: 6 additions & 5 deletions rust/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl SeleniumManager for GridManager {
fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -156,7 +156,7 @@ impl SeleniumManager for GridManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}

Ok(driver_version)
Expand Down Expand Up @@ -187,14 +187,15 @@ impl SeleniumManager for GridManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let browser_name = self.get_browser_name();
let driver_name = self.get_driver_name();
let driver_version = self.get_driver_version();
self.get_cache_path()
Ok(self
.get_cache_path()?
.join(browser_name)
.join(driver_version)
.join(format!("{driver_name}-{driver_version}.{GRID_EXTENSION}"))
.join(format!("{driver_name}-{driver_version}.{GRID_EXTENSION}")))
}

fn get_config(&self) -> &ManagerConfig {
Expand Down
12 changes: 6 additions & 6 deletions rust/src/iexplorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl SeleniumManager for IExplorerManager {
fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -164,7 +164,7 @@ impl SeleniumManager for IExplorerManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}

Ok(driver_version)
Expand Down Expand Up @@ -194,20 +194,20 @@ impl SeleniumManager for IExplorerManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let _minor_driver_version = self
.get_minor_version(driver_version)
.unwrap_or_default()
.parse::<i32>()
.unwrap_or_default();
compose_driver_path_in_cache(
self.get_cache_path(),
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
"Windows",
"win32",
driver_version,
)
))
}

fn get_config(&self) -> &ManagerConfig {
Expand Down
23 changes: 12 additions & 11 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub trait SeleniumManager {

fn get_driver_url(&mut self) -> Result<String, Box<dyn Error>>;

fn get_driver_path_in_cache(&self) -> PathBuf;
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>>;

fn get_config(&self) -> &ManagerConfig;

Expand All @@ -144,18 +144,18 @@ pub trait SeleniumManager {
download_to_tmp_folder(self.get_http_client(), driver_url, self.get_logger())?;

if self.is_grid() {
let driver_path_in_cache = Self::get_driver_path_in_cache(self);
create_parent_path_if_not_exists(&driver_path_in_cache);
let driver_path_in_cache = self.get_driver_path_in_cache()?;
create_parent_path_if_not_exists(&driver_path_in_cache)?;
Ok(fs::rename(driver_zip_file, driver_path_in_cache)?)
} else {
let driver_path_in_cache = Self::get_driver_path_in_cache(self);
let driver_path_in_cache = self.get_driver_path_in_cache()?;
let driver_name_with_extension = self.get_driver_name_with_extension();
uncompress(
Ok(uncompress(
&driver_zip_file,
&driver_path_in_cache,
self.get_logger(),
Some(driver_name_with_extension),
)
)?)
}
}

Expand Down Expand Up @@ -525,7 +525,7 @@ pub trait SeleniumManager {
}

// If driver was not in the PATH, try to find it in the cache
let driver_path = self.get_driver_path_in_cache();
let driver_path = self.get_driver_path_in_cache()?;
if driver_path.exists() {
if !self.is_safari() {
self.get_logger().debug(format!(
Expand Down Expand Up @@ -854,11 +854,11 @@ pub trait SeleniumManager {
}
}

fn get_cache_path(&self) -> PathBuf {
fn get_cache_path(&self) -> Result<PathBuf, Box<dyn Error>> {
let path = Path::new(&self.get_config().cache_path);
create_path_if_not_exists(path);
create_path_if_not_exists(path)?;
let canon_path = self.canonicalize_path(path.to_path_buf());
Path::new(&canon_path).to_path_buf()
Ok(Path::new(&canon_path).to_path_buf())
}

fn set_cache_path(&mut self, cache_path: String) {
Expand Down Expand Up @@ -917,7 +917,8 @@ pub fn get_manager_by_driver(
}
}

pub fn clear_cache(log: &Logger, cache_path: PathBuf) {
pub fn clear_cache(log: &Logger, path: &str) {
let cache_path = Path::new(path).to_path_buf();
if cache_path.exists() {
log.debug(format!("Clearing cache at: {}", cache_path.display()));
fs::remove_dir_all(&cache_path).unwrap_or_else(|err| {
Expand Down
Loading

0 comments on commit f4fa3fa

Please sign in to comment.