From 6b818b927c5e4f60c30ffb175cdc99e27482e289 Mon Sep 17 00:00:00 2001 From: LM Date: Fri, 18 Jul 2025 01:40:19 +0200 Subject: [PATCH] test: make file based testing more robust with unique identifiers (#649) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 📺 PR Description update all file based tests to follow a naming convention in a dedicated testing folder to prevent clashes. Make sure all needed files are in place before running the test ## Checklist - [x] my commits **and PR title** follow the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/#summary) format - [ ] if this is a new feature, I have added tests to consolidate the feature and prevent regressions - [ ] if this is a bug fix, I have added a test that reproduces the bug (if applicable) - [x] I have added a reasonable amount of documentation to the code where appropriate --- .gitignore | 3 +++ tests/cli/cli_config.rs | 11 +++++++---- tests/cli/cli_input.rs | 22 +++++++++++++++------- tests/cli/cli_monitoring.rs | 13 ++++++++----- tests/cli/cli_ui_behavior.rs | 15 ++++++++------- tests/common/mod.rs | 2 ++ 6 files changed, 43 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 9800ba6..5818ed1 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ Cargo.lock .vscode/ **/node_modules/ + +# test artifacts +target_dir/ diff --git a/tests/cli/cli_config.rs b/tests/cli/cli_config.rs index 7d9ea16..47decaa 100644 --- a/tests/cli/cli_config.rs +++ b/tests/cli/cli_config.rs @@ -4,19 +4,22 @@ //! ensuring that users can customize their setup and work in different directories. use super::common::*; +use std::path::Path; /// Tests that the PATH positional argument correctly sets the working directory. #[test] fn test_path_as_positional_argument_sets_working_directory() { let mut tester = PtyTester::new(); - let tmp_dir = std::env::temp_dir(); + let tmp_dir = Path::new(TARGET_DIR); // Create initial files to be detected - std::fs::write(tmp_dir.join("file1.txt"), "").unwrap(); + std::fs::write(tmp_dir.join("UNIQUE16CHARIDfile.txt"), "").unwrap(); // Starts the files channel in the specified temporary directory let cmd = tv_local_config_and_cable_with_args(&[ "files", + "--input", + "UNIQUE16CHARID", tmp_dir.to_str().unwrap(), ]); let mut child = tester.spawn_command_tui(cmd); @@ -25,8 +28,8 @@ fn test_path_as_positional_argument_sets_working_directory() { tester.assert_tui_frame_contains( "╭───────────────────────── files ──────────────────────────╮", ); - // Verify that the test file `file1.txt` is present - tester.assert_tui_frame_contains("file1.txt"); + // Verify that the test file is present + tester.assert_tui_frame_contains("UNIQUE16CHARIDfile.txt"); // Send Ctrl+C to exit cleanly tester.send(&ctrl('c')); diff --git a/tests/cli/cli_input.rs b/tests/cli/cli_input.rs index 5464888..3d6a4a3 100644 --- a/tests/cli/cli_input.rs +++ b/tests/cli/cli_input.rs @@ -85,19 +85,23 @@ fn test_multiple_keybindings_override() { #[test] fn test_exact_matching_enabled() { let mut tester = PtyTester::new(); + let tmp_dir = Path::new(TARGET_DIR); + + // Create initial file to be detected + std::fs::write(tmp_dir.join("UNIQUE16CHARIDfile.txt"), "").unwrap(); // This enables exact substring matching instead of the default fuzzy matching let cmd = tv_local_config_and_cable_with_args(&[ "files", "--exact", "--input", - "file1", - Path::new(TARGET_DIR).to_str().unwrap(), + "UNIQUE16CHARIDfile", + tmp_dir.to_str().unwrap(), ]); let mut child = tester.spawn_command_tui(cmd); // Verify the TUI started successfully with exact matching enabled - tester.assert_tui_frame_contains("file1.txt"); + tester.assert_tui_frame_contains("UNIQUE16CHARIDfile.txt"); // Send Ctrl+C to exit the application tester.send(&ctrl('c')); @@ -107,21 +111,25 @@ fn test_exact_matching_enabled() { #[test] fn test_exact_matching_enabled_fails() { let mut tester = PtyTester::new(); + let tmp_dir = Path::new(TARGET_DIR); + + // Create initial file to be detected + std::fs::write(tmp_dir.join("UNIQUE16CHARIDfile.txt"), "").unwrap(); // This enables exact substring matching instead of the default fuzzy matching let cmd = tv_local_config_and_cable_with_args(&[ "files", "--exact", "--input", - "fl1", - Path::new(TARGET_DIR).to_str().unwrap(), + "UNIQUE16CHARIDfl", + tmp_dir.to_str().unwrap(), ]); let mut child = tester.spawn_command_tui(cmd); // Verify the TUI started successfully with exact matching enabled and no results - tester.assert_tui_frame_contains("│> fl1"); + tester.assert_tui_frame_contains("│> UNIQUE16CHARIDfl"); tester.assert_tui_frame_contains("0 / 0"); - tester.assert_not_tui_frame_contains("file1.txt"); + tester.assert_not_tui_frame_contains("UNIQUE16CHARIDfile.txt"); // Send Ctrl+C to exit the application tester.send(&ctrl('c')); diff --git a/tests/cli/cli_monitoring.rs b/tests/cli/cli_monitoring.rs index f6460b8..056245b 100644 --- a/tests/cli/cli_monitoring.rs +++ b/tests/cli/cli_monitoring.rs @@ -4,15 +4,16 @@ //! ensuring users can enable real-time data updates. use super::common::*; +use std::path::Path; /// Tests that --watch enables live monitoring with automatic source command re-execution. #[test] fn test_watch_reloads_source_command() { let mut tester = PtyTester::new(); - let tmp_dir = std::env::temp_dir(); + let tmp_dir = Path::new(TARGET_DIR); // Create initial file to be detected - std::fs::write(tmp_dir.join("file1.txt"), "").unwrap(); + std::fs::write(tmp_dir.join("UNIQUE16CHARIDfile.txt"), "").unwrap(); // This monitors the temp directory and updates every 0.5 seconds let cmd = tv_local_config_and_cable_with_args(&[ @@ -20,21 +21,23 @@ fn test_watch_reloads_source_command() { "0.5", "--source-command", "ls", + "--input", + "UNIQUE16CHARID", tmp_dir.to_str().unwrap(), ]); let mut child = tester.spawn_command_tui(cmd); // Verify the initial file is detected - tester.assert_tui_frame_contains("file1.txt"); + tester.assert_tui_frame_contains("UNIQUE16CHARIDfile.txt"); // Create a second file - std::fs::write(tmp_dir.join("control.txt"), "").unwrap(); + std::fs::write(tmp_dir.join("UNIQUE16CHARIDcontrol.txt"), "").unwrap(); // Wait longer than watch interval std::thread::sleep(std::time::Duration::from_millis(2000)); // Verify the new file appears after the watch interval - tester.assert_tui_frame_contains("control.txt"); + tester.assert_tui_frame_contains("UNIQUE16CHARIDcontrol.txt"); // Send Ctrl+C to exit tester.send(&ctrl('c')); diff --git a/tests/cli/cli_ui_behavior.rs b/tests/cli/cli_ui_behavior.rs index 19ef6e9..9065f6c 100644 --- a/tests/cli/cli_ui_behavior.rs +++ b/tests/cli/cli_ui_behavior.rs @@ -5,6 +5,7 @@ //! These are integration tests that combine CLI setup with interactive behavior. use super::common::*; +use std::path::Path; /// Tests that the toggle preview keybinding functionality works correctly. #[test] @@ -132,32 +133,32 @@ fn test_scroll_preview_keybindings() { #[test] fn test_reload_source_keybinding() { let mut tester = PtyTester::new(); - let tmp_dir = std::env::temp_dir(); + let tmp_dir = Path::new(TARGET_DIR); // Create initial file to be detected - std::fs::write(tmp_dir.join("file1.txt"), "").unwrap(); + std::fs::write(tmp_dir.join("UNIQUE16CHARIDfile.txt"), "").unwrap(); // Start with the files channel let cmd = tv_local_config_and_cable_with_args(&[ "files", "--input", - ".txt", + "UNIQUE16CHARID", tmp_dir.to_str().unwrap(), ]); let mut child = tester.spawn_command_tui(cmd); // Verify the initial file appears in the TUI - tester.assert_tui_frame_contains("file1.txt"); + tester.assert_tui_frame_contains("UNIQUE16CHARIDfile.txt"); // add another file to be detected - std::fs::write(tmp_dir.join("control.txt"), "").unwrap(); + std::fs::write(tmp_dir.join("UNIQUE16CHARIDcontrol.txt"), "").unwrap(); // Send Ctrl+R to reload the source command tester.send(&ctrl('r')); // Verify the new file appears in the TUI as well as the existing one - tester.assert_tui_frame_contains("control.txt"); - tester.assert_tui_frame_contains("file1.txt"); + tester.assert_tui_frame_contains("UNIQUE16CHARIDcontrol.txt"); + tester.assert_tui_frame_contains("UNIQUE16CHARIDfile.txt"); // Send Ctrl+C to exit tester.send(&ctrl('c')); diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 645cf42..dcac6d1 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -18,6 +18,8 @@ pub const DEFAULT_CABLE_DIR: &str = "./cable/unix"; #[cfg(windows)] pub const DEFAULT_CABLE_DIR: &str = "./cable/windows"; +pub const TARGET_DIR: &str = "./tests/target_dir"; + pub const DEFAULT_PTY_SIZE: PtySize = PtySize { rows: 30, cols: 120,