test: make file based testing more robust with unique identifiers

This commit is contained in:
lalvarezt 2025-07-16 09:43:48 +02:00 committed by Alex Pasmantier
parent 14b91f5e55
commit 5d7ef02f00
6 changed files with 43 additions and 23 deletions

3
.gitignore vendored
View File

@ -29,3 +29,6 @@ Cargo.lock
.vscode/
**/node_modules/
# test artifacts
target_dir/

View File

@ -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'));

View File

@ -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'));

View File

@ -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'));

View File

@ -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'));

View File

@ -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,