Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ regex.workspace = true
chrono.workspace = true
libc.workspace = true
notify-debouncer-full = "0.3"
ctor = "0.2"
diff = "0.1"
dirs = "5.0"
deunicode = "1.6"
Expand Down
50 changes: 28 additions & 22 deletions text/tests/diff-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ mod constants;

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

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

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

use std::{path::PathBuf, process::Stdio};

fn diff_base_path() -> PathBuf {
PathBuf::from("tests").join("diff")
}
Expand Down Expand Up @@ -74,14 +73,13 @@ fn f1_txt_with_eol_spaces_path() -> String {
}

struct DiffTestHelper {
pub key: String,
content: String,
file1_path: String,
file2_path: String,
}

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

Self {
key,
file1_path,
file2_path,
content,
Expand All @@ -119,20 +116,7 @@ impl DiffTestHelper {
}
}

static mut DIFF_TEST_INPUT: Vec<DiffTestHelper> = vec![];

fn input_by_key(key: &str) -> &DiffTestHelper {
unsafe {
DIFF_TEST_INPUT
.iter()
.filter(|data| data.key == key)
.nth(0)
.unwrap()
}
}

#[ctor::ctor]
fn diff_tests_setup() {
fn get_diff_test_helper_hash_map() -> HashMap<String, DiffTestHelper> {
let diff_test_helper_init_data = [
("", f1_txt_path(), f2_txt_path(), "test_diff_normal"),
(" -c", f1_txt_path(), f2_txt_path(), "test_diff_context3"),
Expand Down Expand Up @@ -210,14 +194,33 @@ fn diff_tests_setup() {
),
];

for row in diff_test_helper_init_data {
unsafe { DIFF_TEST_INPUT.push(DiffTestHelper::new(row.0, row.1, row.2, row.3.to_string())) }
let mut diff_test_helper_hash_map =
HashMap::<String, DiffTestHelper>::with_capacity(diff_test_helper_init_data.len());

for (options, file1_path, file2_path, key) in diff_test_helper_init_data {
let insert_option = diff_test_helper_hash_map.insert(
key.to_owned(),
DiffTestHelper::new(options, file1_path, file2_path),
);

assert!(insert_option.is_none());
}

diff_test_helper_hash_map
}

fn input_by_key(key: &str) -> &'static DiffTestHelper {
static DIFF_TEST_INPUT: LazyLock<HashMap<String, DiffTestHelper>> =
LazyLock::new(get_diff_test_helper_hash_map);

// Initialized on first access
DIFF_TEST_INPUT.get(key).unwrap()
}

#[test]
fn test_diff_normal() {
let data = input_by_key("test_diff_normal");

diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
Expand Down Expand Up @@ -316,6 +319,7 @@ fn test_diff_unified10() {
#[test]
fn test_diff_file_directory() {
let data = input_by_key("test_diff_file_directory");

diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
Expand All @@ -326,6 +330,7 @@ fn test_diff_file_directory() {
#[test]
fn test_diff_directories() {
let data = input_by_key("test_diff_directories");

diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
Expand Down Expand Up @@ -391,6 +396,7 @@ fn test_diff_directories_recursive_unified() {
#[test]
fn test_diff_counting_eol_spaces() {
let data = input_by_key("test_diff_counting_eol_spaces");

diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
Expand Down