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 snapshot test using insta #2411

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ which = "7.0"

[dev-dependencies]
env_logger = "0.11"
git2-testing = { path = "./git2-testing" }
insta = { version = "1.41.0", features = ["filters"] }
pretty_assertions = "1.4"
tempfile = "3"

Expand Down
259 changes: 259 additions & 0 deletions src/gitui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
use std::{cell::RefCell, time::Instant};

use anyhow::Result;
use asyncgit::{
sync::{utils::repo_work_dir, RepoPath},
AsyncGitNotification,
};
use crossbeam_channel::{never, tick, unbounded, Receiver};
use scopetime::scope_time;

#[cfg(test)]
use crossterm::event::{KeyCode, KeyModifiers};

use crate::{
app::{App, QuitState},
draw,
input::{Input, InputEvent, InputState},
keys::KeyConfig,
select_event,
spinner::Spinner,
ui::style::Theme,
watcher::RepoWatcher,
AsyncAppNotification, AsyncNotification, QueueEvent, Updater,
SPINNER_INTERVAL, TICK_INTERVAL,
};

pub struct Gitui {
app: crate::app::App,
rx_input: Receiver<InputEvent>,
rx_git: Receiver<AsyncGitNotification>,
rx_app: Receiver<AsyncAppNotification>,
rx_ticker: Receiver<Instant>,
rx_watcher: Receiver<()>,
}

impl Gitui {
pub(crate) fn new(
path: RepoPath,
theme: Theme,
key_config: &KeyConfig,
updater: Updater,
) -> Result<Self, anyhow::Error> {
let (tx_git, rx_git) = unbounded();
let (tx_app, rx_app) = unbounded();

let input = Input::new();

let (rx_ticker, rx_watcher) = match updater {
Updater::NotifyWatcher => {
let repo_watcher =
RepoWatcher::new(repo_work_dir(&path)?.as_str());

(never(), repo_watcher.receiver())
}
Updater::Ticker => (tick(TICK_INTERVAL), never()),
};

let app = App::new(
RefCell::new(path),
tx_git,
tx_app,
input.clone(),
theme,
key_config.clone(),
)?;

Ok(Self {
app,
rx_input: input.receiver(),
rx_git,
rx_app,
rx_ticker,
rx_watcher,
})
}

pub(crate) fn run_main_loop<B: ratatui::backend::Backend>(
&mut self,
terminal: &mut ratatui::Terminal<B>,
) -> Result<QuitState, anyhow::Error> {
let spinner_ticker = tick(SPINNER_INTERVAL);
let mut spinner = Spinner::default();

self.app.update()?;

loop {
let event = select_event(
&self.rx_input,
&self.rx_git,
&self.rx_app,
&self.rx_ticker,
&self.rx_watcher,
&spinner_ticker,
)?;

{
if matches!(event, QueueEvent::SpinnerUpdate) {
spinner.update();
spinner.draw(terminal)?;
continue;
}

scope_time!("loop");

match event {
QueueEvent::InputEvent(ev) => {
if matches!(
ev,
InputEvent::State(InputState::Polling)
) {
//Note: external ed closed, we need to re-hide cursor
terminal.hide_cursor()?;
}
self.app.event(ev)?;
}
QueueEvent::Tick | QueueEvent::Notify => {
self.app.update()?;
}
QueueEvent::AsyncEvent(ev) => {
if !matches!(
ev,
AsyncNotification::Git(
AsyncGitNotification::FinishUnchanged
)
) {
self.app.update_async(ev)?;
}
}
QueueEvent::SpinnerUpdate => unreachable!(),
}

self.draw(terminal)?;

spinner.set_state(self.app.any_work_pending());
spinner.draw(terminal)?;

if self.app.is_quit() {
break;
}
}
}

Ok(self.app.quit_state())
}

fn draw<B: ratatui::backend::Backend>(
&self,
terminal: &mut ratatui::Terminal<B>,
) -> std::io::Result<()> {
draw(terminal, &self.app)
}

#[cfg(test)]
fn update_async(&mut self, event: crate::AsyncNotification) {
self.app.update_async(event).unwrap();
}

#[cfg(test)]
fn input_event(
&mut self,
code: KeyCode,
modifiers: KeyModifiers,
) {
let event = crossterm::event::KeyEvent::new(code, modifiers);
self.app
.event(crate::input::InputEvent::Input(
crossterm::event::Event::Key(event),
))
.unwrap();
}

#[cfg(test)]
fn update(&mut self) {
self.app.update().unwrap();
}
}

#[cfg(test)]
mod tests {
use std::{path::PathBuf, thread::sleep, time::Duration};

use asyncgit::{sync::RepoPath, AsyncGitNotification};
use crossterm::event::{KeyCode, KeyModifiers};
use git2_testing::repo_init;
use insta::assert_snapshot;
use ratatui::{backend::TestBackend, Terminal};

use crate::{
gitui::Gitui, keys::KeyConfig, ui::style::Theme,
AsyncNotification, Updater,
};

// Macro adapted from: https://insta.rs/docs/cmd/
macro_rules! apply_common_filters {
{} => {
let mut settings = insta::Settings::clone_current();
// MacOS Temp Folder
settings.add_filter(r" *\[…\]\S+?/T/\S+", "[TEMP_FILE]");
// Linux Temp Folder
settings.add_filter(r" */tmp/\.tmp\S+", "[TEMP_FILE]");
// Windows Temp folder
settings.add_filter(r" *\[…\].*/Local/Temp/\S+", "[TEMP_FILE]");
// Commit ids that follow a vertical bar
settings.add_filter(r"│[a-z0-9]{7} ", "│[AAAAA] ");
let _bound = settings.bind_to_scope();
}
}

#[test]
fn gitui_starts() {
apply_common_filters!();

let (temp_dir, _repo) = repo_init();
let path: RepoPath = temp_dir.path().to_str().unwrap().into();

let theme = Theme::init(&PathBuf::new());
let key_config = KeyConfig::default();

let mut gitui =
Gitui::new(path, theme, &key_config, Updater::Ticker)
.unwrap();

let mut terminal =
Terminal::new(TestBackend::new(120, 40)).unwrap();

gitui.draw(&mut terminal).unwrap();

sleep(Duration::from_millis(500));

assert_snapshot!("app_loading", terminal.backend());

let event =
AsyncNotification::Git(AsyncGitNotification::Status);
gitui.update_async(event);

sleep(Duration::from_millis(500));

gitui.draw(&mut terminal).unwrap();

assert_snapshot!("app_loading_finished", terminal.backend());

gitui.input_event(KeyCode::Char('2'), KeyModifiers::empty());
gitui.input_event(
key_config.keys.tab_log.code,
key_config.keys.tab_log.modifiers,
);

sleep(Duration::from_millis(500));

gitui.update();

gitui.draw(&mut terminal).unwrap();

assert_snapshot!(
"app_log_tab_showing_one_commit",
terminal.backend()
);
}
}
Loading
Loading