Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add end to end tests #64

Merged
merged 9 commits into from
Jan 7, 2025
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
109 changes: 67 additions & 42 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::Error;
use crossterm::event::KeyEvent;
use fancy_regex::Regex as FancyRegex;
use ignore::WalkState;
use log::warn;
use parking_lot::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::crossterm::event::{KeyCode, KeyEventKind, KeyModifiers};
use regex::Regex;
use std::{
collections::HashMap,
Expand All @@ -23,13 +24,47 @@ use tokio::{
};

use crate::{
event::{AppEvent, BackgroundProcessingEvent, ReplaceResult, SearchResult},
fields::{CheckboxField, Field, FieldError, TextField},
parsed_fields::{ParsedFields, SearchType},
utils::relative_path_from,
EventHandlingResult,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReplaceResult {
Success,
Error(String),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SearchResult {
pub path: PathBuf,
pub line_number: usize,
pub line: String,
pub replacement: String,
pub included: bool,
pub replace_result: Option<ReplaceResult>,
}

#[derive(Debug)]
pub enum AppEvent {
Rerender,
PerformSearch,
}

#[derive(Debug)]
pub enum BackgroundProcessingEvent {
AddSearchResult(SearchResult),
SearchCompleted,
ReplacementCompleted(ReplaceState),
}

#[derive(Debug, PartialEq, Eq)]
pub enum EventHandlingResult {
Rerender,
Exit,
None,
}

#[derive(Debug, Eq, PartialEq)]
pub struct SearchState {
pub results: Vec<SearchResult>,
Expand Down Expand Up @@ -392,7 +427,7 @@ pub struct App {
const BINARY_EXTENSIONS: &[&str] = &["png", "gif", "jpg", "jpeg", "ico", "svg", "pdf"];

impl App {
pub fn new(
fn new(
directory: Option<PathBuf>,
include_hidden: bool,
advanced_regex: bool,
Expand All @@ -415,6 +450,16 @@ impl App {
}
}

pub fn new_with_receiver(
directory: Option<PathBuf>,
include_hidden: bool,
advanced_regex: bool,
) -> (Self, UnboundedReceiver<AppEvent>) {
let (app_event_sender, app_event_receiver) = mpsc::unbounded_channel();
let app = Self::new(directory, include_hidden, advanced_regex, app_event_sender);
(app, app_event_receiver)
}

pub fn cancel_search(&mut self) {
if let Screen::SearchProgressing(SearchInProgressState { handle, .. }) =
&mut self.current_screen
Expand Down Expand Up @@ -474,10 +519,7 @@ impl App {

pub async fn handle_app_event(&mut self, event: AppEvent) -> EventHandlingResult {
match event {
AppEvent::Rerender => EventHandlingResult {
exit: false,
rerender: true,
},
AppEvent::Rerender => EventHandlingResult::Rerender,
AppEvent::PerformSearch => self.perform_search_if_valid(),
}
}
Expand Down Expand Up @@ -506,10 +548,7 @@ impl App {
}
};

EventHandlingResult {
exit: false,
rerender: true,
}
EventHandlingResult::Rerender
}

pub fn trigger_replacement(&mut self) {
Expand Down Expand Up @@ -586,9 +625,10 @@ impl App {
search_in_progress_state.last_render = Instant::now();
}
}
EventHandlingResult {
exit: false,
rerender,
if rerender {
EventHandlingResult::Rerender
} else {
EventHandlingResult::None
}
}
BackgroundProcessingEvent::SearchCompleted => {
Expand All @@ -597,17 +637,11 @@ impl App {
{
self.current_screen = Screen::SearchComplete(search_state);
}
EventHandlingResult {
exit: false,
rerender: true,
}
EventHandlingResult::Rerender
}
BackgroundProcessingEvent::ReplacementCompleted(replace_state) => {
self.current_screen = Screen::Results(replace_state);
EventHandlingResult {
exit: false,
rerender: true,
}
EventHandlingResult::Rerender
}
}
}
Expand Down Expand Up @@ -683,30 +717,21 @@ impl App {
false
}

pub fn handle_key_events(&mut self, key: &KeyEvent) -> anyhow::Result<EventHandlingResult> {
pub fn handle_key_event(&mut self, key: &KeyEvent) -> anyhow::Result<EventHandlingResult> {
if key.kind == KeyEventKind::Release {
return Ok(EventHandlingResult {
exit: false,
rerender: true,
});
return Ok(EventHandlingResult::Rerender);
}

match (key.code, key.modifiers) {
(KeyCode::Esc, _) | (KeyCode::Char('c'), KeyModifiers::CONTROL)
if !self.search_fields.show_error_popup =>
{
self.reset();
return Ok(EventHandlingResult {
exit: true,
rerender: true,
});
return Ok(EventHandlingResult::Exit);
}
(KeyCode::Char('r'), KeyModifiers::CONTROL) => {
self.reset();
return Ok(EventHandlingResult {
exit: false,
rerender: true,
});
return Ok(EventHandlingResult::Rerender);
}
(_, _) => {}
}
Expand All @@ -719,9 +744,10 @@ impl App {
Screen::PerformingReplacement(_) => false,
Screen::Results(replace_state) => replace_state.handle_key_results(key),
};
Ok(EventHandlingResult {
exit,
rerender: true,
Ok(if exit {
EventHandlingResult::Exit
} else {
EventHandlingResult::Rerender
})
}

Expand Down Expand Up @@ -920,7 +946,6 @@ mod tests {
use rand::Rng;

use super::*;
use crate::EventHandler;

fn random_num() -> usize {
let mut rng = rand::thread_rng();
Expand Down Expand Up @@ -1052,8 +1077,8 @@ mod tests {
}

fn build_test_app(results: Vec<SearchResult>) -> App {
let event_handler = EventHandler::new();
let mut app = App::new(None, false, false, event_handler.app_event_sender);
let (app_event_sender, _) = mpsc::unbounded_channel();
let mut app = App::new(None, false, false, app_event_sender);
app.current_screen = Screen::SearchComplete(SearchState {
results,
selected: 0,
Expand Down
Loading
Loading