use std::env;
use std::path::{Path, PathBuf};
use crate::platform::Platform;
pub(crate) fn default_cache_dir() -> PathBuf {
dirs::cache_dir()
.unwrap_or_else(env::temp_dir)
.join("zendriver/chrome")
}
pub(crate) fn binary_path(cache_dir: &Path, version: &str, platform: Platform) -> PathBuf {
cache_dir.join(version).join(binary_subpath(platform))
}
pub(crate) fn binary_subpath(platform: Platform) -> PathBuf {
match platform {
Platform::LinuxX64 => PathBuf::from("chrome-linux64").join("chrome"),
Platform::MacX64 => PathBuf::from("chrome-mac-x64")
.join("Google Chrome for Testing.app")
.join("Contents")
.join("MacOS")
.join("Google Chrome for Testing"),
Platform::MacArm64 => PathBuf::from("chrome-mac-arm64")
.join("Google Chrome for Testing.app")
.join("Contents")
.join("MacOS")
.join("Google Chrome for Testing"),
Platform::Win32 => PathBuf::from("chrome-win32").join("chrome.exe"),
Platform::Win64 => PathBuf::from("chrome-win64").join("chrome.exe"),
}
}
#[cfg(test)]
#[allow(clippy::panic, clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn default_cache_dir_ends_with_zendriver_chrome() {
let p = default_cache_dir();
assert!(
p.ends_with("zendriver/chrome"),
"expected suffix zendriver/chrome, got {}",
p.display()
);
}
#[test]
fn binary_path_linux_layout() {
let root = Path::new("/tmp/cache");
let p = binary_path(root, "120.0.6099.234", Platform::LinuxX64);
assert_eq!(
p,
Path::new("/tmp/cache/120.0.6099.234/chrome-linux64/chrome")
);
}
#[test]
fn binary_path_mac_arm64_layout() {
let root = Path::new("/tmp/cache");
let p = binary_path(root, "120.0.6099.234", Platform::MacArm64);
assert_eq!(
p,
Path::new(
"/tmp/cache/120.0.6099.234/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing"
)
);
}
#[test]
fn binary_path_win64_layout() {
let root = Path::new("/tmp/cache");
let p = binary_path(root, "120.0.6099.234", Platform::Win64);
assert_eq!(
p,
Path::new("/tmp/cache/120.0.6099.234/chrome-win64/chrome.exe")
);
}
}