Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d903ea6

Browse files
committed
Fixed tests
1 parent 6c63bf5 commit d903ea6

6 files changed

Lines changed: 144 additions & 86 deletions

File tree

src/interactive/app/eventloop.rs

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crossbeam::channel::Receiver;
1010
use crosstermion::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
1111
use crosstermion::input::Event;
1212
use dua::{
13-
traverse::{EntryData, ProcessEventResult, RunningTraversal, Traversal},
13+
traverse::{EntryData, TraversalProcessingEvent, RunningTraversal, Traversal},
1414
WalkOptions, WalkResult,
1515
};
1616
use std::path::PathBuf;
@@ -70,7 +70,7 @@ impl AppState {
7070
walk_options: &WalkOptions,
7171
input: Vec<PathBuf>,
7272
) -> Result<()> {
73-
let running_traversal = RunningTraversal::new(traversal.root_index, walk_options, input)?;
73+
let running_traversal = RunningTraversal::start(traversal.root_index, walk_options, input)?;
7474
self.running_traversal = Some(running_traversal);
7575
Ok(())
7676
}
@@ -104,48 +104,71 @@ impl AppState {
104104
self.refresh_screen(window, traversal, display, terminal)?;
105105

106106
loop {
107-
if let Some(running_traversal) = &mut self.running_traversal {
108-
crossbeam::select! {
109-
recv(events) -> event => {
110-
let Ok(event) = event else {
111-
continue;
112-
};
113-
let result = self.process_event(
114-
window,
115-
traversal,
116-
display,
117-
terminal,
118-
event)?;
119-
if let Some(processing_result) = result {
120-
return Ok(processing_result);
121-
}
122-
},
123-
recv(&running_traversal.event_rx) -> event => {
124-
let Ok(event) = event else {
125-
continue;
126-
};
127-
128-
let result = running_traversal.process_event(traversal, event);
129-
if result != ProcessEventResult::NoOp {
130-
if result == ProcessEventResult::Finished {
131-
self.is_scanning = false;
132-
self.running_traversal = None;
133-
}
134-
self.update_state(traversal);
135-
self.refresh_screen(window, traversal, display, terminal)?;
107+
if let Some(result) = self.process_event(
108+
window,
109+
traversal,
110+
display,
111+
terminal,
112+
&events,
113+
)? {
114+
return Ok(result);
115+
}
116+
}
117+
}
118+
119+
pub fn process_event<B>(
120+
&mut self,
121+
window: &mut MainWindow,
122+
traversal: &mut Traversal,
123+
display: &mut DisplayOptions,
124+
terminal: &mut Terminal<B>,
125+
events: &Receiver<Event>,
126+
) -> Result<Option<ProcessingResult>>
127+
where
128+
B: Backend,
129+
{
130+
if let Some(running_traversal) = &mut self.running_traversal {
131+
crossbeam::select! {
132+
recv(events) -> event => {
133+
let Ok(event) = event else {
134+
return Ok(Some(ProcessingResult::ExitRequested(WalkResult { num_errors: 0 })));
135+
};
136+
let result = self.process_terminal_event(
137+
window,
138+
traversal,
139+
display,
140+
terminal,
141+
event)?;
142+
if let Some(processing_result) = result {
143+
return Ok(Some(processing_result));
144+
}
145+
},
146+
recv(&running_traversal.event_rx) -> event => {
147+
let Ok(event) = event else {
148+
return Ok(None);
149+
};
150+
151+
let result = running_traversal.process_event(traversal, event);
152+
if result != TraversalProcessingEvent::None {
153+
if result == TraversalProcessingEvent::Finished {
154+
self.is_scanning = false;
155+
self.running_traversal = None;
136156
}
157+
self.update_state(traversal);
158+
self.refresh_screen(window, traversal, display, terminal)?;
137159
}
138160
}
139-
} else {
140-
let Ok(event) = events.recv() else {
141-
continue;
142-
};
143-
let result = self.process_event(window, traversal, display, terminal, event)?;
144-
if let Some(processing_result) = result {
145-
return Ok(processing_result);
146-
}
161+
}
162+
} else {
163+
let Ok(event) = events.recv() else {
164+
return Ok(Some(ProcessingResult::ExitRequested(WalkResult { num_errors: 0 })));
165+
};
166+
let result = self.process_terminal_event(window, traversal, display, terminal, event)?;
167+
if let Some(processing_result) = result {
168+
return Ok(Some(processing_result));
147169
}
148170
}
171+
Ok(None)
149172
}
150173

151174
fn update_state(&mut self, traversal: &Traversal) {
@@ -165,7 +188,7 @@ impl AppState {
165188
self.reset_message(); // force "scanning" to appear
166189
}
167190

168-
fn process_event<B>(
191+
fn process_terminal_event<B>(
169192
&mut self,
170193
window: &mut MainWindow,
171194
traversal: &mut Traversal,

src/interactive/app/terminal_app.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,29 @@ impl TerminalApp {
103103
ProcessingResult::ExitRequested(res) => Ok(res),
104104
}
105105
}
106+
107+
pub fn run_until_traversed<B>(
108+
&mut self,
109+
terminal: &mut Terminal<B>,
110+
events: Receiver<Event>,
111+
) -> Result<WalkResult>
112+
where
113+
B: Backend
114+
{
115+
while self.state.running_traversal.is_some() {
116+
match self.state.process_event(
117+
&mut self.window,
118+
&mut self.traversal,
119+
&mut self.display,
120+
terminal,
121+
&events,
122+
)? {
123+
Some(ProcessingResult::ExitRequested(res)) => {
124+
return Ok(res);
125+
}
126+
_ => {}
127+
}
128+
}
129+
Ok(WalkResult { num_errors: 0 })
130+
}
106131
}

src/interactive/app/tests/journeys_readonly.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ fn init_from_pdu_results() -> Result<()> {
2727
fn simple_user_journey_read_only() -> Result<()> {
2828
let long_root = "sample-02/dir";
2929
let short_root = "sample-01";
30-
let (mut terminal, mut app, traversal_events) =
30+
let (mut terminal, mut app) =
3131
initialized_app_and_terminal_from_fixture(&[short_root, long_root])?;
32-
33-
// TODO: process traversal events before continuing?
34-
// let (key_send, key_receive) = crossbeam::channel::bounded(0);
35-
32+
3633
// POST-INIT
3734
// after initialization, we expect that...
3835
{
@@ -70,28 +67,28 @@ fn simple_user_journey_read_only() -> Result<()> {
7067
// SORTING
7168
{
7269
// when hitting the M key
73-
app.process_events(&mut terminal, into_codes("m"), traversal_events)?;
70+
app.process_events(&mut terminal, into_codes("m"))?;
7471
assert_eq!(
7572
app.state.sorting,
7673
SortMode::MTimeDescending,
7774
"it sets the sort mode to descending by mtime"
7875
);
7976
// when hitting the M key again
80-
app.process_events(&mut terminal, into_codes("m"), traversal_events)?;
77+
app.process_events(&mut terminal, into_codes("m"))?;
8178
assert_eq!(
8279
app.state.sorting,
8380
SortMode::MTimeAscending,
8481
"it sets the sort mode to ascending by mtime"
8582
);
8683
// when hitting the C key
87-
app.process_events(&mut terminal, into_codes("c"), traversal_events)?;
84+
app.process_events(&mut terminal, into_codes("c"))?;
8885
assert_eq!(
8986
app.state.sorting,
9087
SortMode::CountDescending,
9188
"it sets the sort mode to descending by count"
9289
);
9390
// when hitting the C key again
94-
app.process_events(&mut terminal, into_codes("c"), traversal_events)?;
91+
app.process_events(&mut terminal, into_codes("c"))?;
9592
assert_eq!(
9693
app.state.sorting,
9794
SortMode::CountAscending,
@@ -103,7 +100,7 @@ fn simple_user_journey_read_only() -> Result<()> {
103100
"it recomputes the cached items"
104101
);
105102
// when hitting the S key
106-
app.process_events(&mut terminal, into_codes("s"), traversal_events)?;
103+
app.process_events(&mut terminal, into_codes("s"))?;
107104
assert_eq!(
108105
app.state.sorting,
109106
SortMode::SizeDescending,
@@ -115,14 +112,14 @@ fn simple_user_journey_read_only() -> Result<()> {
115112
"it recomputes the cached items"
116113
);
117114
// when hitting the S key again
118-
app.process_events(&mut terminal, into_codes("s"), traversal_events)?;
115+
app.process_events(&mut terminal, into_codes("s"))?;
119116
assert_eq!(
120117
app.state.sorting,
121118
SortMode::SizeAscending,
122119
"it sets the sort mode to ascending by size"
123120
);
124121
// hit the S key again to get Descending - the rest depends on it
125-
app.process_events(&mut terminal, into_codes("s"), traversal_events)?;
122+
app.process_events(&mut terminal, into_codes("s"))?;
126123
assert_eq!(app.state.sorting, SortMode::SizeDescending,);
127124

128125
assert_eq!(
@@ -135,35 +132,35 @@ fn simple_user_journey_read_only() -> Result<()> {
135132
// Entry-Navigation
136133
{
137134
// when hitting the j key
138-
app.process_events(&mut terminal, into_codes("j"), traversal_events)?;
135+
app.process_events(&mut terminal, into_codes("j"))?;
139136
assert_eq!(
140137
node_by_name(&app, fixture_str(long_root)),
141138
node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
142139
"it moves the cursor down and selects the next item based on the current sort mode"
143140
);
144141
// when hitting it while there is nowhere to go
145-
app.process_events(&mut terminal, into_codes("j"), traversal_events)?;
142+
app.process_events(&mut terminal, into_codes("j"))?;
146143
assert_eq!(
147144
node_by_name(&app, fixture_str(long_root)),
148145
node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
149146
"it stays at the previous position"
150147
);
151148
// when hitting the k key
152-
app.process_events(&mut terminal, into_codes("k"), traversal_events)?;
149+
app.process_events(&mut terminal, into_codes("k"))?;
153150
assert_eq!(
154151
node_by_name(&app, fixture_str(short_root)),
155152
node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
156153
"it moves the cursor up and selects the next item based on the current sort mode"
157154
);
158155
// when hitting the k key again
159-
app.process_events(&mut terminal, into_codes("k"), traversal_events)?;
156+
app.process_events(&mut terminal, into_codes("k"))?;
160157
assert_eq!(
161158
node_by_name(&app, fixture_str(short_root)),
162159
node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
163160
"it stays at the current cursor position as there is nowhere to go"
164161
);
165162
// when hitting the o key with a directory selected
166-
app.process_events(&mut terminal, into_codes("o"), traversal_events)?;
163+
app.process_events(&mut terminal, into_codes("o"))?;
167164
{
168165
let new_root_idx = index_by_name(&app, fixture_str(short_root));
169166
assert_eq!(
@@ -178,7 +175,7 @@ fn simple_user_journey_read_only() -> Result<()> {
178175
);
179176

180177
// when hitting the u key while inside a sub-directory
181-
app.process_events(&mut terminal, into_codes("u"), traversal_events)?;
178+
app.process_events(&mut terminal, into_codes("u"))?;
182179
{
183180
assert_eq!(
184181
app.traversal.root_index,
@@ -194,7 +191,7 @@ fn simple_user_journey_read_only() -> Result<()> {
194191
}
195192
// when hitting the u key while inside of the root directory
196193
// We are moving the cursor down just to have a non-default selection
197-
app.process_events(&mut terminal, into_codes("ju"), traversal_events)?;
194+
app.process_events(&mut terminal, into_codes("ju"))?;
198195
{
199196
assert_eq!(
200197
app.traversal.root_index,
@@ -212,9 +209,9 @@ fn simple_user_journey_read_only() -> Result<()> {
212209
// Deletion
213210
{
214211
// when hitting the 'd' key (also move cursor back to start)
215-
app.process_events(&mut terminal, into_codes("k"), traversal_events)?;
212+
app.process_events(&mut terminal, into_codes("k"))?;
216213
let previously_selected_index = *app.state.navigation().selected.as_ref().unwrap();
217-
app.process_events(&mut terminal, into_codes("d"), traversal_events)?;
214+
app.process_events(&mut terminal, into_codes("d"))?;
218215
{
219216
assert_eq!(
220217
Some(1),
@@ -236,7 +233,7 @@ fn simple_user_journey_read_only() -> Result<()> {
236233

237234
// when hitting the 'd' key again
238235
{
239-
app.process_events(&mut terminal, into_codes("d"), traversal_events)?;
236+
app.process_events(&mut terminal, into_codes("d"))?;
240237

241238
assert_eq!(
242239
Some(2),
@@ -253,7 +250,7 @@ fn simple_user_journey_read_only() -> Result<()> {
253250

254251
// when hitting the 'd' key once again
255252
{
256-
app.process_events(&mut terminal, into_codes("d"), traversal_events)?;
253+
app.process_events(&mut terminal, into_codes("d"))?;
257254

258255
assert_eq!(
259256
Some(1),
@@ -270,7 +267,7 @@ fn simple_user_journey_read_only() -> Result<()> {
270267
}
271268
// when hitting the spacebar (after moving up to the first entry)
272269
{
273-
app.process_events(&mut terminal, into_codes("k "), traversal_events)?;
270+
app.process_events(&mut terminal, into_codes("k "))?;
274271

275272
assert_eq!(
276273
None,
@@ -289,7 +286,7 @@ fn simple_user_journey_read_only() -> Result<()> {
289286
// Marking
290287
{
291288
// select something
292-
app.process_events(&mut terminal, into_codes(" j "), traversal_events)?;
289+
app.process_events(&mut terminal, into_codes(" j "))?;
293290
assert_eq!(
294291
Some(false),
295292
app.window.mark_pane.as_ref().map(|p| p.has_focus()),
@@ -306,7 +303,6 @@ fn simple_user_journey_read_only() -> Result<()> {
306303
app.process_events(
307304
&mut terminal,
308305
into_keys(Some(KeyCode::Tab)),
309-
traversal_events,
310306
)?;
311307
{
312308
assert_eq!(

src/interactive/app/tests/journeys_with_writes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use pretty_assertions::assert_eq;
99
#[test]
1010
#[cfg(not(target_os = "windows"))] // it stopped working here, don't know if it's truly broken or if it's the test. Let's wait for windows users to report.
1111
fn basic_user_journey_with_deletion() -> Result<()> {
12+
use crate::interactive::app::tests::utils::{into_keys, into_events};
13+
1214
let fixture = WritableFixture::from("sample-02");
1315
let (mut terminal, mut app) = initialized_app_and_terminal_from_paths(&[fixture.root.clone()])?;
1416

@@ -26,11 +28,11 @@ fn basic_user_journey_with_deletion() -> Result<()> {
2628
// When selecting the marker window and pressing the combination to delete entries
2729
app.process_events(
2830
&mut terminal,
29-
vec![
31+
into_events(vec![
3032
Event::Key(KeyCode::Tab.into()),
3133
Event::Key(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL)),
3234
]
33-
.into_iter(),
35+
.into_iter()),
3436
)?;
3537
assert!(
3638
app.window.mark_pane.is_none(),

0 commit comments

Comments
 (0)