Skip to content

Commit

Permalink
[rust] Fix config setup in Selenium Manager (#12807)
Browse files Browse the repository at this point in the history
* [rust] Fix config setup in Selenium Manager

* [rust] Reuse logic for escaping paths for config cache path

* [rust] Revert canonicalice logic for cache path

* [rust] Update checksum in Cargo.Bazel.lock
  • Loading branch information
bonigarcia committed Sep 24, 2023
1 parent bf5d592 commit 553791c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "24231be79f3b3f23c29c0093148f7183cb564cd13ac0fe297c6e5efe9e6311a8",
"checksum": "437c56863604d3ef83e56701f4a5d8948553a0c963b7c970716fceabcc75ec10",
"crates": {
"addr2line 0.19.0": {
"name": "addr2line",
Expand Down
3 changes: 3 additions & 0 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ impl StringKey<'_> {
// to be discovered in the first place and stored globally (on CACHE_PATH)
return check_cache_path(result, default_value);
}
if !result.is_empty() {
return result;
}
}
default_value
}
Expand Down
36 changes: 24 additions & 12 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,18 +935,28 @@ pub trait SeleniumManager {
}

fn canonicalize_path(&self, path_buf: PathBuf) -> String {
let canon_path = path_buf_to_string(path_buf.as_path().canonicalize().unwrap_or(path_buf));
let mut canon_path = path_buf_to_string(
path_buf
.as_path()
.canonicalize()
.unwrap_or(path_buf.clone()),
);
if WINDOWS.is(self.get_os()) {
canon_path.replace(UNC_PREFIX, "")
} else {
canon_path
canon_path = canon_path.replace(UNC_PREFIX, "")
}
if !path_buf_to_string(path_buf.clone()).eq(&canon_path) {
self.get_logger().trace(format!(
"Path {} has been canonicalized to {}",
path_buf.display(),
canon_path
));
}
canon_path
}

fn get_escaped_path(&self, string_path: String) -> String {
let original_path = string_path.clone();
let mut escaped_path = string_path;
let path = Path::new(&original_path);
let mut escaped_path = string_path.clone();
let path = Path::new(&string_path);

if path.exists() {
escaped_path = self.canonicalize_path(path.to_path_buf());
Expand All @@ -957,14 +967,16 @@ pub trait SeleniumManager {
Command::new_single(format_one_arg(ESCAPE_COMMAND, escaped_path.as_str()));
escaped_path = run_shell_command("bash", "-c", escape_command).unwrap_or_default();
if escaped_path.is_empty() {
escaped_path = original_path.clone();
escaped_path = string_path.clone();
}
}
}
self.get_logger().trace(format!(
"Original path: {} - Escaped path: {}",
original_path, escaped_path
));
if !string_path.eq(&escaped_path) {
self.get_logger().trace(format!(
"Path {} has been escaped to {}",
string_path, escaped_path
));
}
escaped_path
}

Expand Down
14 changes: 10 additions & 4 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::path::PathBuf;
use std::path::Path;
use std::process::exit;

use clap::Parser;
Expand Down Expand Up @@ -135,8 +135,14 @@ fn main() {
let trace = cli.trace || BooleanKey("trace", false).get_value();
let log = Logger::create(&cli.output, debug, trace);
let grid = cli.grid;
let browser_name: String = cli.browser.unwrap_or_default();
let driver_name: String = cli.driver.unwrap_or_default();
let mut browser_name: String = cli.browser.unwrap_or_default();
let mut driver_name: String = cli.driver.unwrap_or_default();
if browser_name.is_empty() {
browser_name = StringKey(vec!["browser"], "").get_value();
}
if driver_name.is_empty() {
driver_name = StringKey(vec!["driver"], "").get_value();
}

let mut selenium_manager: Box<dyn SeleniumManager> = if !browser_name.is_empty() {
get_manager_by_browser(browser_name).unwrap_or_else(|err| {
Expand Down Expand Up @@ -224,7 +230,7 @@ fn main() {
});
}

fn log_driver_and_browser_path(log: &Logger, driver_path: &PathBuf, browser_path: &str) {
fn log_driver_and_browser_path(log: &Logger, driver_path: &Path, browser_path: &str) {
if driver_path.exists() {
log.info(format!("{}{}", DRIVER_PATH, driver_path.display()));
} else {
Expand Down
56 changes: 56 additions & 0 deletions rust/tests/config_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 assert_cmd::Command;
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};

use crate::common::{assert_browser, assert_driver};
use rstest::rstest;
use tempfile::Builder;

mod common;

#[rstest]
#[case("chrome")]
#[case("firefox")]
#[case("edge")]
fn config_test(#[case] browser_name: String) {
let tmp_dir = Builder::new().prefix("sm-config-test").tempdir().unwrap();
let config_path = tmp_dir.path().join("se-config.toml");
let config_file = File::create(config_path.as_path()).unwrap();
let mut writer = BufWriter::new(config_file);
writer
.write_all(format!(r#"browser="{}""#, browser_name).as_bytes())
.unwrap();
writer.flush().unwrap();

let mut cmd = Command::new(env!("CARGO_BIN_EXE_selenium-manager"));
cmd.args([
"--output",
"json",
"--cache-path",
tmp_dir.path().to_str().unwrap(),
])
.assert()
.success()
.code(0);

assert_driver(&mut cmd);
assert_browser(&mut cmd);
}

0 comments on commit 553791c

Please sign in to comment.