Skip to content

Commit 40d3be7

Browse files
authored
Merge pull request #315 from andrewliebenow/remove-unnecessary-unsafe
Remove unnecessary unsafe code and dependency
2 parents a00830a + 33b14cb commit 40d3be7

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

Diff for: Cargo.lock

-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: text/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ regex.workspace = true
1515
chrono.workspace = true
1616
libc.workspace = true
1717
notify-debouncer-full = "0.3"
18-
ctor = "0.2"
1918
diff = "0.1"
2019
dirs = "5.0"
2120
deunicode = "1.6"

Diff for: text/tests/diff-tests.rs

+28-22
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ mod constants;
1313

1414
use constants::{EXIT_STATUS_DIFFERENCE, EXIT_STATUS_NO_DIFFERENCE};
1515
use plib::{run_test, TestPlan};
16+
use std::{collections::HashMap, path::PathBuf, process::Stdio, sync::LazyLock};
1617

1718
fn diff_test(args: &[&str], expected_output: &str, expected_diff_exit_status: u8) {
18-
let str_args: Vec<String> = args.iter().map(|s| String::from(*s)).collect();
19+
let str_args = args.iter().cloned().map(str::to_owned).collect();
1920

2021
run_test(TestPlan {
2122
cmd: String::from("diff"),
@@ -27,8 +28,6 @@ fn diff_test(args: &[&str], expected_output: &str, expected_diff_exit_status: u8
2728
});
2829
}
2930

30-
use std::{path::PathBuf, process::Stdio};
31-
3231
fn diff_base_path() -> PathBuf {
3332
PathBuf::from("tests").join("diff")
3433
}
@@ -74,14 +73,13 @@ fn f1_txt_with_eol_spaces_path() -> String {
7473
}
7574

7675
struct DiffTestHelper {
77-
pub key: String,
7876
content: String,
7977
file1_path: String,
8078
file2_path: String,
8179
}
8280

8381
impl DiffTestHelper {
84-
fn new(options: &str, file1_path: String, file2_path: String, key: String) -> Self {
82+
fn new(options: &str, file1_path: String, file2_path: String) -> Self {
8583
let args = format!(
8684
"run --release --bin diff --{} {} {}",
8785
options, file1_path, file2_path
@@ -99,7 +97,6 @@ impl DiffTestHelper {
9997
let content = String::from_utf8(output.stdout).expect("Failed to read output of Command!");
10098

10199
Self {
102-
key,
103100
file1_path,
104101
file2_path,
105102
content,
@@ -119,20 +116,7 @@ impl DiffTestHelper {
119116
}
120117
}
121118

122-
static mut DIFF_TEST_INPUT: Vec<DiffTestHelper> = vec![];
123-
124-
fn input_by_key(key: &str) -> &DiffTestHelper {
125-
unsafe {
126-
DIFF_TEST_INPUT
127-
.iter()
128-
.filter(|data| data.key == key)
129-
.nth(0)
130-
.unwrap()
131-
}
132-
}
133-
134-
#[ctor::ctor]
135-
fn diff_tests_setup() {
119+
fn get_diff_test_helper_hash_map() -> HashMap<String, DiffTestHelper> {
136120
let diff_test_helper_init_data = [
137121
("", f1_txt_path(), f2_txt_path(), "test_diff_normal"),
138122
(" -c", f1_txt_path(), f2_txt_path(), "test_diff_context3"),
@@ -210,14 +194,33 @@ fn diff_tests_setup() {
210194
),
211195
];
212196

213-
for row in diff_test_helper_init_data {
214-
unsafe { DIFF_TEST_INPUT.push(DiffTestHelper::new(row.0, row.1, row.2, row.3.to_string())) }
197+
let mut diff_test_helper_hash_map =
198+
HashMap::<String, DiffTestHelper>::with_capacity(diff_test_helper_init_data.len());
199+
200+
for (options, file1_path, file2_path, key) in diff_test_helper_init_data {
201+
let insert_option = diff_test_helper_hash_map.insert(
202+
key.to_owned(),
203+
DiffTestHelper::new(options, file1_path, file2_path),
204+
);
205+
206+
assert!(insert_option.is_none());
215207
}
208+
209+
diff_test_helper_hash_map
210+
}
211+
212+
fn input_by_key(key: &str) -> &'static DiffTestHelper {
213+
static DIFF_TEST_INPUT: LazyLock<HashMap<String, DiffTestHelper>> =
214+
LazyLock::new(get_diff_test_helper_hash_map);
215+
216+
// Initialized on first access
217+
DIFF_TEST_INPUT.get(key).unwrap()
216218
}
217219

218220
#[test]
219221
fn test_diff_normal() {
220222
let data = input_by_key("test_diff_normal");
223+
221224
diff_test(
222225
&[data.file1_path(), data.file2_path()],
223226
data.content(),
@@ -316,6 +319,7 @@ fn test_diff_unified10() {
316319
#[test]
317320
fn test_diff_file_directory() {
318321
let data = input_by_key("test_diff_file_directory");
322+
319323
diff_test(
320324
&[data.file1_path(), data.file2_path()],
321325
data.content(),
@@ -326,6 +330,7 @@ fn test_diff_file_directory() {
326330
#[test]
327331
fn test_diff_directories() {
328332
let data = input_by_key("test_diff_directories");
333+
329334
diff_test(
330335
&[data.file1_path(), data.file2_path()],
331336
data.content(),
@@ -391,6 +396,7 @@ fn test_diff_directories_recursive_unified() {
391396
#[test]
392397
fn test_diff_counting_eol_spaces() {
393398
let data = input_by_key("test_diff_counting_eol_spaces");
399+
394400
diff_test(
395401
&[data.file1_path(), data.file2_path()],
396402
data.content(),

0 commit comments

Comments
 (0)