Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated deps, fixed CI commands, fixed clippy lints #419

Merged
merged 6 commits into from Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -9,16 +9,18 @@ permissions:

env:
RUSTFLAGS: -Dwarnings # Turn warnings into errors
RUST_CI: true

jobs:
check:
name: cargo check
name: cargo clippy && cargo fmt && cargo test
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
- run: cargo check --all-features
- run: cargo fmt --all -- --check
- run: cargo test google.com --all-features --all-targets
- run: cargo clippy --all-features --all-targets
8 changes: 4 additions & 4 deletions Cargo.toml
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
anyhow = "1"
base64 = "0.21"
derive_builder = "0.12"
directories = { version = "4.0", optional = true }
directories = { version = "5.0", optional = true }
log = "0.4"
rand = "0.8"
regex = "1"
Expand All @@ -23,13 +23,13 @@ tempfile = "3"
thiserror = "1"
ureq = { version = "2.5", optional = true }
walkdir = { version = "2", optional = true }
tungstenite = "0.18"
tungstenite = "0.20"
url = "2.3"
which = "4.0"
zip = { version = "0.6.3", optional = true }

[target.'cfg(windows)'.dependencies]
winreg = "0.10"
winreg = "0.51"

[dev-dependencies]
chrono = { version = "0.4", default_features = false, features = ["clock"] }
Expand All @@ -51,5 +51,5 @@ default = ["offline"]
fetch = ["ureq", "directories", "zip", "walkdir"]
nightly = []
offline = ["auto_generate_cdp/offline"]
rustls = ["tungstenite/rustls"]
rustls = ["tungstenite/rustls", "tungstenite/rustls-tls-native-roots"]
native-tls = ["tungstenite/native-tls"]
77 changes: 39 additions & 38 deletions src/browser/fetcher.rs
Expand Up @@ -74,24 +74,25 @@ impl Default for FetcherOptions {
}

impl FetcherOptions {
#[must_use]
pub fn with_revision(mut self, revision: Revision) -> Self {
self.revision = revision;
self
}

#[must_use]
pub fn with_install_dir<P: Into<PathBuf>>(mut self, install_dir: Option<P>) -> Self {
match install_dir {
Some(dir) => self.install_dir = Some(dir.into()),
None => self.install_dir = None,
}
self
}

#[must_use]
pub fn with_allow_download(mut self, allow_download: bool) -> Self {
self.allow_download = allow_download;
self
}

#[must_use]
pub fn with_allow_standard_dirs(mut self, allow_standard_dirs: bool) -> Self {
self.allow_standard_dirs = allow_standard_dirs;
self
Expand All @@ -104,8 +105,8 @@ pub struct Fetcher {
}

impl Fetcher {
pub fn new(options: FetcherOptions) -> Result<Self> {
Ok(Self { options })
pub fn new(options: FetcherOptions) -> Self {
Self { options }
}

// look for good existing installation, if none exists then download and install
Expand All @@ -123,7 +124,7 @@ impl Fetcher {
if self.options.allow_download {
let zip_path = self.download(&rev)?;

self.unzip(zip_path)?;
Fetcher::unzip(zip_path)?;

// look again
return self.chrome_path(&rev);
Expand Down Expand Up @@ -170,7 +171,7 @@ impl Fetcher {
// find full path to chrome executable from base_path
fn chrome_path(&self, revision: &str) -> Result<PathBuf> {
let mut path = self.base_path(revision)?;
path.push(archive_name(revision)?);
path.push(archive_name(revision));

#[cfg(target_os = "linux")]
{
Expand All @@ -193,18 +194,18 @@ impl Fetcher {

// download a .zip of the revision we want
fn download(&self, revision: &str) -> Result<PathBuf> {
let url = dl_url(revision)?;
info!("Chrome download url: {}", url);
let url = dl_url(revision);
info!("Chrome download url: {url}");
let total = get_size(&url)?;
info!("Total size of download: {} MiB", total);
info!("Total size of download: {total} MiB");

let mut path: PathBuf = if let Some(mut dir) = self.options.install_dir.clone() {
// we have a preferred install location
dir.push(format!("{}-{}", PLATFORM, revision));
dir.push(format!("{PLATFORM}-{revision}"));
dir
} else if self.options.allow_standard_dirs {
let mut dir = get_project_dirs()?.data_dir().to_path_buf();
dir.push(format!("{}-{}", PLATFORM, revision));
dir.push(format!("{PLATFORM}-{revision}"));
dir
} else {
// No preferred install dir and not allowed to use standard dirs.
Expand All @@ -219,7 +220,7 @@ impl Fetcher {
)
.map_err(|_err| anyhow!("Could not create directory at {:?}", path.parent()))?;

println!("{:?}", path);
println!("{path:?}");

info!("Creating file for download: {}", &path.display());
let mut file = OpenOptions::new().create(true).write(true).open(&path)?;
Expand Down Expand Up @@ -248,7 +249,7 @@ impl Fetcher {
}

#[cfg(not(target_os = "macos"))]
fn do_unzip<P: AsRef<Path>>(&self, zip_path: P, extract_path: &Path) -> Result<()> {
fn do_unzip<P: AsRef<Path>>(zip_path: P, extract_path: &Path) -> Result<()> {
let mut archive = zip::ZipArchive::new(File::open(zip_path.as_ref())?)?;

for i in 0..archive.len() {
Expand All @@ -261,7 +262,7 @@ impl Fetcher {
trace!("File {} comment: {}", i, comment);
}

if (&*file.name()).ends_with('/') {
if (file.name()).ends_with('/') {
trace!(
"File {} extracted to \"{}\"",
i,
Expand All @@ -277,7 +278,7 @@ impl Fetcher {
);
if let Some(p) = out_path.parent() {
if !p.exists() {
fs::create_dir_all(&p)?;
fs::create_dir_all(p)?;
}
}
let mut out_file = BufWriter::new(File::create(&out_path)?);
Expand All @@ -296,7 +297,7 @@ impl Fetcher {
Ok(())
}
// unzip the downloaded file and do all the needed file manipulation
fn unzip<P: AsRef<Path>>(&self, zip_path: P) -> Result<PathBuf> {
fn unzip<P: AsRef<Path>>(zip_path: P) -> Result<PathBuf> {
let mut extract_path: PathBuf = zip_path
.as_ref()
.parent()
Expand All @@ -317,7 +318,7 @@ impl Fetcher {
extract_path.display()
);

self.do_unzip(zip_path.as_ref(), &extract_path)?;
Fetcher::do_unzip(zip_path.as_ref(), &extract_path)?;

info!("Cleaning up");
if fs::remove_file(&zip_path).is_err() {
Expand Down Expand Up @@ -345,85 +346,85 @@ fn get_project_dirs() -> Result<ProjectDirs> {
}
}

fn dl_url<R>(revision: R) -> Result<String>
fn dl_url<R>(revision: R) -> String
where
R: AsRef<str>,
{
#[cfg(target_os = "linux")]
{
Ok(format!(
format!(
"{}/chromium-browser-snapshots/Linux_x64/{}/{}.zip",
DEFAULT_HOST,
revision.as_ref(),
archive_name(revision.as_ref())?
))
archive_name(revision.as_ref())
)
}

#[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
{
Ok(format!(
format!(
"{}/chromium-browser-snapshots/Mac/{}/{}.zip",
DEFAULT_HOST,
revision.as_ref(),
archive_name(revision.as_ref())?
))
)
}

#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
{
Ok(format!(
format!(
"{}/chromium-browser-snapshots/Mac_Arm/{}/{}.zip",
DEFAULT_HOST,
revision.as_ref(),
archive_name(revision.as_ref())?
))
)
}

#[cfg(windows)]
{
Ok(format!(
format!(
"{}/chromium-browser-snapshots/Win_x64/{}/{}.zip",
DEFAULT_HOST,
revision.as_ref(),
archive_name(revision.as_ref())?
))
)
}
}

fn archive_name<R: AsRef<str>>(revision: R) -> Result<&'static str> {
fn archive_name<R: AsRef<str>>(revision: R) -> &'static str {
#[cfg(target_os = "linux")]
{
drop(revision);

Ok("chrome-linux")
"chrome-linux"
}

#[cfg(target_os = "macos")]
{
drop(revision);

Ok("chrome-mac")
"chrome-mac"
}

#[cfg(windows)]
{
// Windows archive name changed at r591479.
if revision.as_ref().parse::<u32>()? > 591_479 {
Ok("chrome-win")
"chrome-win"
} else {
Ok("chrome-win32")
"chrome-win32"
}
}
}

// Returns the latest chrome revision for the current platform.
// This function will panic on unsupported platforms.
fn latest_revision() -> Result<String> {
let mut url = format!("{}/chromium-browser-snapshots", DEFAULT_HOST);
let mut url = format!("{DEFAULT_HOST}/chromium-browser-snapshots");

#[cfg(target_os = "linux")]
{
url = format!("{}/Linux_x64/LAST_CHANGE", url);
url = format!("{url}/Linux_x64/LAST_CHANGE");
ureq::get(&url)
.call()?
.into_string()
Expand All @@ -432,7 +433,7 @@ fn latest_revision() -> Result<String> {

#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
{
url = format!("{}/Mac_Arm/LAST_CHANGE", url);
url = format!("{url}/Mac_Arm/LAST_CHANGE");
ureq::get(&url)
.call()?
.into_string()
Expand All @@ -441,7 +442,7 @@ fn latest_revision() -> Result<String> {

#[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
{
url = format!("{}/Mac/LAST_CHANGE", url);
url = format!("{url}/Mac/LAST_CHANGE");
ureq::get(&url)
.call()?
.into_string()
Expand All @@ -450,7 +451,7 @@ fn latest_revision() -> Result<String> {

#[cfg(windows)]
{
url = format!("{}/Win_x64/LAST_CHANGE", url);
url = format!("{url}/Win_x64/LAST_CHANGE");
ureq::get(&url)
.call()?
.into_string()
Expand Down
2 changes: 1 addition & 1 deletion src/browser/mod.rs
Expand Up @@ -312,7 +312,7 @@ impl Browser {
trace!("Starting browser's event handling loop");
loop {
match shutdown_rx.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => {
Ok(()) | Err(TryRecvError::Disconnected) => {
info!("Browser event loop received shutdown message");
break;
}
Expand Down
8 changes: 5 additions & 3 deletions src/browser/process.rs
Expand Up @@ -10,6 +10,8 @@ use std::{
#[cfg(test)]
use std::cell::RefCell;

use derive_builder::Builder;

use anyhow::{anyhow, Result};
use log::*;
use rand::seq::SliceRandom;
Expand Down Expand Up @@ -68,7 +70,7 @@ struct TemporaryProcess(Child, Option<tempfile::TempDir>);
impl Drop for TemporaryProcess {
fn drop(&mut self) {
info!("Killing Chrome. PID: {}", self.0.id());
self.0.kill().and_then(|_| self.0.wait()).ok();
self.0.kill().and_then(|()| self.0.wait()).ok();
if let Some(dir) = self.1.take() {
if let Err(e) = dir.close() {
warn!("Failed to close temporary directory: {}", e);
Expand Down Expand Up @@ -230,7 +232,7 @@ impl Process {
if launch_options.path.is_none() {
#[cfg(feature = "fetch")]
{
let fetch = Fetcher::new(launch_options.fetcher_options.clone())?;
let fetch = Fetcher::new(launch_options.fetcher_options.clone());
launch_options.path = Some(fetch.fetch()?);
}
#[cfg(not(feature = "fetch"))]
Expand Down Expand Up @@ -536,7 +538,7 @@ mod tests {
// if we do this after it fails on windows because chrome can stay running
// for a bit.
let mut installed_dir = tests_temp_dir.clone();
installed_dir.push(format!("{}-{}", PLATFORM, CUR_REV));
installed_dir.push(format!("{PLATFORM}-{CUR_REV}"));

if installed_dir.exists() {
info!("Deleting pre-existing install at {:?}", &installed_dir);
Expand Down
2 changes: 1 addition & 1 deletion src/browser/tab/mod.rs
Expand Up @@ -916,7 +916,7 @@ impl Tab {
code: code.clone(),
windows_virtual_key_code: Some(definiton.key_code),
native_virtual_key_code: Some(definiton.key_code),
modifiers: modifiers.clone(),
modifiers,
timestamp: None,
unmodified_text: None,
key_identifier: None,
Expand Down
2 changes: 1 addition & 1 deletion src/browser/transport/mod.rs
Expand Up @@ -238,7 +238,7 @@ impl Transport {
// hence need for Connection Shutdown
loop {
match shutdown_rx.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => {
Ok(()) | Err(TryRecvError::Disconnected) => {
info!("Transport incoming message loop loop received shutdown message");
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/browser/transport/waiting_call_registry.rs
Expand Up @@ -66,7 +66,7 @@ impl WaitingCallRegistry {
pub fn cancel_outstanding_method_calls(&self) {
trace!("Cancelling outstanding method calls");
let calls = self.calls.lock().unwrap();
for (call_id, sender) in calls.iter() {
for (call_id, sender) in &*calls {
trace!(
"Telling waiting method call {:?} that the connection closed",
call_id
Expand Down