Skip to content

Commit

Permalink
Fix tests on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasschafer committed Nov 30, 2024
1 parent dc86c18 commit dbae897
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ jobs:
- name: Lint
run: cargo clippy

- name: Run tests
- name: Run tests (Windows)
if: matrix.os == 'windows-latest'
run: cargo test --verbose -- --test-threads=1

- name: Run tests (Unix)
if: matrix.os != 'windows-latest'
run: cargo test --verbose
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

use clap::Parser;
use event::EventHandlingResult;
use logging::setup_logging;
use log::LevelFilter;
use logging::{setup_logging, DEFAULT_LOG_LEVEL};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use std::{io, str::FromStr};
use tui::Tui;
use utils::validate_directory;

Expand Down
51 changes: 34 additions & 17 deletions tests/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use scooter::{
App, EventHandler, ReplaceResult, ReplaceState, Screen, SearchFields, SearchResult, SearchState,
};
use std::cmp::max;
use std::fs::{create_dir_all, File};
use std::fs::{self, create_dir_all, File};
use std::io::Write;
use std::mem;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -130,14 +130,37 @@ macro_rules! create_test_files {
let path = [temp_dir.path().to_str().unwrap(), $name].join("/");
let path = Path::new(&path);
create_dir_all(path.parent().unwrap()).unwrap();
let mut file = File::create(path).unwrap();
file.write_all(contents.as_bytes()).unwrap();
file.sync_all().unwrap();
{
let mut file = File::create(path).unwrap();
file.write_all(contents.as_bytes()).unwrap();
file.sync_all().unwrap();
}
)+

#[cfg(windows)]
sleep(Duration::from_millis(100));

temp_dir
}
};
}
fn collect_files(dir: &Path, base: &Path, files: &mut Vec<String>) {
for entry in fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_file() {
let rel_path = path
.strip_prefix(base)
.unwrap()
.to_str()
.unwrap()
.to_string()
.replace("\\", "/");
files.push(rel_path);
} else if path.is_dir() {
collect_files(&path, base, files);
}
}
}

macro_rules! assert_test_files {
($temp_dir:expr, $($name:expr => {$($line:expr),+ $(,)?}),+ $(,)?) => {
Expand Down Expand Up @@ -166,18 +189,6 @@ macro_rules! assert_test_files {
let mut expected_files: Vec<String> = vec![$($name.to_string()),+];
expected_files.sort();

fn collect_files(dir: &Path, base: &Path, files: &mut Vec<String>) {
for entry in fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_file() {
let rel_path = path.strip_prefix(base).unwrap().to_str().unwrap().to_string();
files.push(rel_path);
} else if path.is_dir() {
collect_files(&path, base, files);
}
}
}

let mut actual_files = Vec::new();
collect_files(
$temp_dir.path(),
Expand Down Expand Up @@ -209,16 +220,22 @@ where
}

async fn process_bp_events(app: &mut App) {
let timeout = Duration::from_secs(5);
let start = Instant::now();

while let Some(event) = app.background_processing_recv().await {
app.handle_background_processing_event(event);
if start.elapsed() > timeout {
panic!("Couldn't process background events in a reasonable time");
}
}
}

macro_rules! wait_for_screen {
($app:expr, $variant:path) => {
wait_until(
|| matches!($app.current_screen, $variant(_)),
Duration::from_millis(500),
Duration::from_secs(1),
)
};
}
Expand Down

0 comments on commit dbae897

Please sign in to comment.