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

[rust] Include mirror arguments to change default online repository URLs (#11687) #12914

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,28 @@ impl ChromeManager {
}

fn create_latest_release_url(&self) -> String {
format!("{}{}", DRIVER_URL, LATEST_RELEASE)
format!(
"{}{}",
self.get_driver_mirror_url_or_default(DRIVER_URL),
LATEST_RELEASE
)
}

fn create_latest_release_with_version_url(&self) -> String {
format!(
"{}{}_{}",
DRIVER_URL,
self.get_driver_mirror_url_or_default(DRIVER_URL),
LATEST_RELEASE,
self.get_major_browser_version()
)
}

fn create_cft_url(&self, endpoint: &str) -> String {
format!("{}{}", CFT_URL, endpoint)
format!(
"{}{}",
self.get_browser_mirror_url_or_default(CFT_URL),
endpoint
)
}

fn request_driver_version_from_latest(&self, driver_url: String) -> Result<String, Error> {
Expand Down Expand Up @@ -365,7 +373,10 @@ impl SeleniumManager for ChromeManager {
};
Ok(format!(
"{}{}/{}_{}.zip",
DRIVER_URL, driver_version, self.driver_name, driver_label
self.get_driver_mirror_url_or_default(DRIVER_URL),
driver_version,
self.driver_name,
driver_label
))
}

Expand Down
9 changes: 9 additions & 0 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ pub const CONFIG_FILE: &str = "se-config.toml";
pub const ENV_PREFIX: &str = "SE_";
pub const VERSION_PREFIX: &str = "-version";
pub const PATH_PREFIX: &str = "-path";
pub const MIRROR_PREFIX: &str = "-mirror-url";
pub const CACHE_PATH_KEY: &str = "cache-path";

pub struct ManagerConfig {
pub cache_path: String,
pub browser_version: String,
pub driver_version: String,
pub browser_path: String,
pub driver_mirror_url: String,
pub browser_mirror_url: String,
pub os: String,
pub arch: String,
pub proxy: String,
Expand Down Expand Up @@ -86,6 +89,8 @@ impl ManagerConfig {
let browser_version_label = concat(browser_name, VERSION_PREFIX);
let driver_version_label = concat(driver_name, VERSION_PREFIX);
let browser_path_label = concat(browser_name, PATH_PREFIX);
let driver_mirror_label = concat(driver_name, MIRROR_PREFIX);
let browser_mirror_label = concat(browser_name, MIRROR_PREFIX);

ManagerConfig {
cache_path,
Expand All @@ -94,6 +99,10 @@ impl ManagerConfig {
driver_version: StringKey(vec!["driver-version", &driver_version_label], "")
.get_value(),
browser_path: StringKey(vec!["browser-path", &browser_path_label], "").get_value(),
driver_mirror_url: StringKey(vec!["driver-mirror-url", &driver_mirror_label], "")
.get_value(),
browser_mirror_url: StringKey(vec!["browser-mirror-url", &browser_mirror_label], "")
.get_value(),
os: StringKey(vec!["os"], self_os).get_value(),
arch: StringKey(vec!["arch"], self_arch.as_str()).get_value(),
proxy: StringKey(vec!["proxy"], "").get_value(),
Expand Down
19 changes: 13 additions & 6 deletions rust/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub const WEBVIEW2_NAME: &str = "webview2";
const DRIVER_URL: &str = "https://msedgedriver.azureedge.net/";
const LATEST_STABLE: &str = "LATEST_STABLE";
const LATEST_RELEASE: &str = "LATEST_RELEASE";
const BROWSER_URL: &str = "https://edgeupdates.microsoft.com/api/products";
const BROWSER_URL: &str = "https://edgeupdates.microsoft.com/api/products/";
const MIN_EDGE_VERSION_DOWNLOAD: i32 = 113;
const EDGE_WINDOWS_AND_LINUX_APP_NAME: &str = "msedge";
const EDGE_MACOS_APP_NAME: &str = "Microsoft Edge.app/Contents/MacOS/Microsoft Edge";
Expand Down Expand Up @@ -182,7 +182,11 @@ impl SeleniumManager for EdgeManager {
|| major_browser_version.is_empty()
|| self.is_browser_version_unstable()
{
let latest_stable_url = format!("{}{}", DRIVER_URL, LATEST_STABLE);
let latest_stable_url = format!(
"{}{}",
self.get_driver_mirror_url_or_default(DRIVER_URL),
LATEST_STABLE
);
self.log.debug(format!(
"Reading {} latest version from {}",
&self.driver_name, latest_stable_url
Expand All @@ -201,7 +205,7 @@ impl SeleniumManager for EdgeManager {
}
let driver_url = format!(
"{}{}_{}_{}",
DRIVER_URL,
self.get_driver_mirror_url_or_default(DRIVER_URL),
LATEST_RELEASE,
major_browser_version,
self.get_os().to_uppercase()
Expand Down Expand Up @@ -256,7 +260,9 @@ impl SeleniumManager for EdgeManager {
};
Ok(format!(
"{}{}/edgedriver_{}.zip",
DRIVER_URL, driver_version, driver_label
self.get_driver_mirror_url_or_default(DRIVER_URL),
driver_version,
driver_label
))
}

Expand Down Expand Up @@ -320,10 +326,11 @@ impl SeleniumManager for EdgeManager {
let is_fixed_browser_version = !self.is_empty(browser_version)
&& !self.is_stable(browser_version)
&& !self.is_unstable(browser_version);
let browser_url = self.get_browser_mirror_url_or_default(BROWSER_URL);
let edge_updates_url = if is_fixed_browser_version {
format!("{}?view=enterprise", BROWSER_URL)
format!("{}?view=enterprise", browser_url)
} else {
BROWSER_URL.to_string()
browser_url
};
self.get_logger().debug(format!(
"Checking {} releases on {}",
Expand Down
20 changes: 16 additions & 4 deletions rust/src/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ impl SeleniumManager for FirefoxManager {
_ => {
self.assert_online_or_err(OFFLINE_REQUEST_ERR_MSG)?;

let latest_url = format!("{}{}", DRIVER_URL, LATEST_RELEASE);
let latest_url = format!(
"{}{}",
self.get_driver_mirror_url_or_default(DRIVER_URL),
LATEST_RELEASE
);
let driver_version =
read_redirect_from_link(self.get_http_client(), latest_url, self.get_logger())?;

Expand Down Expand Up @@ -260,7 +264,11 @@ impl SeleniumManager for FirefoxManager {
};
Ok(format!(
"{}download/v{}/{}-v{}-{}",
DRIVER_URL, driver_version, self.driver_name, driver_version, driver_label
self.get_driver_mirror_url_or_default(DRIVER_URL),
driver_version,
self.driver_name,
driver_version,
driver_label
))
}

Expand Down Expand Up @@ -403,7 +411,11 @@ impl SeleniumManager for FirefoxManager {
}

for version in firefox_versions.iter().rev() {
let release_url = format_two_args("{}{}/", BROWSER_URL, version);
let release_url = format_two_args(
"{}{}/",
&self.get_browser_mirror_url_or_default(BROWSER_URL),
version,
);
self.get_logger()
.trace(format!("Checking release URL: {}", release_url));
let content = read_content_from_link(self.get_http_client(), release_url)?;
Expand Down Expand Up @@ -501,7 +513,7 @@ impl SeleniumManager for FirefoxManager {
let browser_version = self.get_browser_version();
Ok(format!(
"{}{}/{}/{}/{}{}.{}",
BROWSER_URL,
self.get_browser_mirror_url_or_default(BROWSER_URL),
browser_version,
platform_label,
language,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl SeleniumManager for GridManager {
let release_version = self.get_selenium_release_version()?;
Ok(format!(
"{}download/{}/{}-{}.{}",
DRIVER_URL,
self.get_driver_mirror_url_or_default(DRIVER_URL),
release_version,
GRID_RELEASE,
self.get_driver_version(),
Expand Down
2 changes: 1 addition & 1 deletion rust/src/iexplorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl SeleniumManager for IExplorerManager {
let release_version = self.get_selenium_release_version()?;
Ok(format!(
"{}download/{}/{}{}.zip",
DRIVER_URL,
self.get_driver_mirror_url_or_default(DRIVER_URL),
release_version,
IEDRIVER_RELEASE,
self.get_driver_version()
Expand Down
41 changes: 41 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,47 @@ pub trait SeleniumManager {
}
}

fn get_driver_mirror_url(&self) -> &str {
self.get_config().driver_mirror_url.as_str()
}

fn set_driver_mirror_url(&mut self, mirror_url: String) {
if !mirror_url.is_empty() {
self.get_config_mut().driver_mirror_url = mirror_url;
}
}

fn get_browser_mirror_url(&self) -> &str {
self.get_config().browser_mirror_url.as_str()
}

fn set_browser_mirror_url(&mut self, mirror_url: String) {
if !mirror_url.is_empty() {
self.get_config_mut().browser_mirror_url = mirror_url;
}
}

fn get_driver_mirror_url_or_default<'a>(&'a self, default_url: &'a str) -> String {
self.get_url_or_default(self.get_driver_mirror_url(), default_url)
}

fn get_browser_mirror_url_or_default<'a>(&'a self, default_url: &'a str) -> String {
self.get_url_or_default(self.get_browser_mirror_url(), default_url)
}

fn get_url_or_default<'a>(&'a self, value_url: &'a str, default_url: &'a str) -> String {
let url = if value_url.is_empty() {
default_url
} else {
value_url
};
if !url.ends_with('/') {
format!("{}/", url)
} else {
url.to_string()
}
}

fn canonicalize_path(&self, path_buf: PathBuf) -> String {
let mut canon_path = path_to_string(&path_buf);
if WINDOWS.is(self.get_os()) || canon_path.starts_with(UNC_PREFIX) {
Expand Down
10 changes: 10 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ struct Cli {
#[clap(long, value_parser)]
browser_path: Option<String>,

/// Mirror for driver repositories (e.g., https://registry.npmmirror.com/-/binary/chromedriver/)
#[clap(long, value_parser)]
driver_mirror_url: Option<String>,

/// Mirror for browser repositories
#[clap(long, value_parser)]
browser_mirror_url: Option<String>,

/// Output type: LOGGER (using INFO, WARN, etc.), JSON (custom JSON notation), or SHELL (Unix-like)
#[clap(long, value_parser, default_value = "LOGGER")]
output: String,
Expand Down Expand Up @@ -179,6 +187,8 @@ fn main() {
selenium_manager.set_browser_version(cli.browser_version.unwrap_or_default());
selenium_manager.set_driver_version(cli.driver_version.unwrap_or_default());
selenium_manager.set_browser_path(cli.browser_path.unwrap_or_default());
selenium_manager.set_driver_mirror_url(cli.driver_mirror_url.unwrap_or_default());
selenium_manager.set_browser_mirror_url(cli.browser_mirror_url.unwrap_or_default());
selenium_manager.set_os(cli.os.unwrap_or_default());
selenium_manager.set_arch(cli.arch.unwrap_or_default());
selenium_manager.set_ttl(cli.ttl);
Expand Down
42 changes: 42 additions & 0 deletions rust/tests/mirror_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::common::assert_driver;
use assert_cmd::Command;

mod common;

#[test]
fn mirror_test() {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_selenium-manager"));
cmd.args([
"--browser",
"chrome",
"--driver-mirror-url",
"https://registry.npmmirror.com/-/binary/chromedriver/",
"--browser-version",
"112",
"--avoid-browser-download",
"--output",
"json",
])
.assert()
.success()
.code(0);

assert_driver(&mut cmd);
}
Loading