Skip to content

Commit 553791c

Browse files
authored
[rust] Fix config setup in Selenium Manager (#12807)
* [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
1 parent bf5d592 commit 553791c

File tree

5 files changed

+94
-17
lines changed

5 files changed

+94
-17
lines changed

rust/Cargo.Bazel.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"checksum": "24231be79f3b3f23c29c0093148f7183cb564cd13ac0fe297c6e5efe9e6311a8",
2+
"checksum": "437c56863604d3ef83e56701f4a5d8948553a0c963b7c970716fceabcc75ec10",
33
"crates": {
44
"addr2line 0.19.0": {
55
"name": "addr2line",

rust/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ impl StringKey<'_> {
185185
// to be discovered in the first place and stored globally (on CACHE_PATH)
186186
return check_cache_path(result, default_value);
187187
}
188+
if !result.is_empty() {
189+
return result;
190+
}
188191
}
189192
default_value
190193
}

rust/src/lib.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -935,18 +935,28 @@ pub trait SeleniumManager {
935935
}
936936

937937
fn canonicalize_path(&self, path_buf: PathBuf) -> String {
938-
let canon_path = path_buf_to_string(path_buf.as_path().canonicalize().unwrap_or(path_buf));
938+
let mut canon_path = path_buf_to_string(
939+
path_buf
940+
.as_path()
941+
.canonicalize()
942+
.unwrap_or(path_buf.clone()),
943+
);
939944
if WINDOWS.is(self.get_os()) {
940-
canon_path.replace(UNC_PREFIX, "")
941-
} else {
942-
canon_path
945+
canon_path = canon_path.replace(UNC_PREFIX, "")
943946
}
947+
if !path_buf_to_string(path_buf.clone()).eq(&canon_path) {
948+
self.get_logger().trace(format!(
949+
"Path {} has been canonicalized to {}",
950+
path_buf.display(),
951+
canon_path
952+
));
953+
}
954+
canon_path
944955
}
945956

946957
fn get_escaped_path(&self, string_path: String) -> String {
947-
let original_path = string_path.clone();
948-
let mut escaped_path = string_path;
949-
let path = Path::new(&original_path);
958+
let mut escaped_path = string_path.clone();
959+
let path = Path::new(&string_path);
950960

951961
if path.exists() {
952962
escaped_path = self.canonicalize_path(path.to_path_buf());
@@ -957,14 +967,16 @@ pub trait SeleniumManager {
957967
Command::new_single(format_one_arg(ESCAPE_COMMAND, escaped_path.as_str()));
958968
escaped_path = run_shell_command("bash", "-c", escape_command).unwrap_or_default();
959969
if escaped_path.is_empty() {
960-
escaped_path = original_path.clone();
970+
escaped_path = string_path.clone();
961971
}
962972
}
963973
}
964-
self.get_logger().trace(format!(
965-
"Original path: {} - Escaped path: {}",
966-
original_path, escaped_path
967-
));
974+
if !string_path.eq(&escaped_path) {
975+
self.get_logger().trace(format!(
976+
"Path {} has been escaped to {}",
977+
string_path, escaped_path
978+
));
979+
}
968980
escaped_path
969981
}
970982

rust/src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::path::PathBuf;
18+
use std::path::Path;
1919
use std::process::exit;
2020

2121
use clap::Parser;
@@ -135,8 +135,14 @@ fn main() {
135135
let trace = cli.trace || BooleanKey("trace", false).get_value();
136136
let log = Logger::create(&cli.output, debug, trace);
137137
let grid = cli.grid;
138-
let browser_name: String = cli.browser.unwrap_or_default();
139-
let driver_name: String = cli.driver.unwrap_or_default();
138+
let mut browser_name: String = cli.browser.unwrap_or_default();
139+
let mut driver_name: String = cli.driver.unwrap_or_default();
140+
if browser_name.is_empty() {
141+
browser_name = StringKey(vec!["browser"], "").get_value();
142+
}
143+
if driver_name.is_empty() {
144+
driver_name = StringKey(vec!["driver"], "").get_value();
145+
}
140146

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

227-
fn log_driver_and_browser_path(log: &Logger, driver_path: &PathBuf, browser_path: &str) {
233+
fn log_driver_and_browser_path(log: &Logger, driver_path: &Path, browser_path: &str) {
228234
if driver_path.exists() {
229235
log.info(format!("{}{}", DRIVER_PATH, driver_path.display()));
230236
} else {

rust/tests/config_tests.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use assert_cmd::Command;
19+
use std::env;
20+
use std::fs::File;
21+
use std::io::{BufWriter, Write};
22+
23+
use crate::common::{assert_browser, assert_driver};
24+
use rstest::rstest;
25+
use tempfile::Builder;
26+
27+
mod common;
28+
29+
#[rstest]
30+
#[case("chrome")]
31+
#[case("firefox")]
32+
#[case("edge")]
33+
fn config_test(#[case] browser_name: String) {
34+
let tmp_dir = Builder::new().prefix("sm-config-test").tempdir().unwrap();
35+
let config_path = tmp_dir.path().join("se-config.toml");
36+
let config_file = File::create(config_path.as_path()).unwrap();
37+
let mut writer = BufWriter::new(config_file);
38+
writer
39+
.write_all(format!(r#"browser="{}""#, browser_name).as_bytes())
40+
.unwrap();
41+
writer.flush().unwrap();
42+
43+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_selenium-manager"));
44+
cmd.args([
45+
"--output",
46+
"json",
47+
"--cache-path",
48+
tmp_dir.path().to_str().unwrap(),
49+
])
50+
.assert()
51+
.success()
52+
.code(0);
53+
54+
assert_driver(&mut cmd);
55+
assert_browser(&mut cmd);
56+
}

0 commit comments

Comments
 (0)