Skip to content

Commit 88674fd

Browse files
committed
Add snapshot test using insta
1 parent 0a09c13 commit 88674fd

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ which = "7.0"
6868

6969
[dev-dependencies]
7070
env_logger = "0.11"
71+
git2-testing = { path = "./git2-testing" }
72+
insta = { version = "1.41.0", features = ["filters"] }
7173
pretty_assertions = "1.4"
7274
tempfile = "3"
7375

src/main.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ fn shutdown_terminal() {
305305
}
306306
}
307307

308-
fn draw(terminal: &mut Terminal, app: &App) -> io::Result<()> {
308+
fn draw<B: ratatui::backend::Backend>(
309+
terminal: &mut ratatui::Terminal<B>,
310+
app: &App,
311+
) -> io::Result<()> {
309312
if app.requires_redraw() {
310313
terminal.clear()?;
311314
}
@@ -408,3 +411,70 @@ fn set_panic_handler() -> Result<()> {
408411

409412
Ok(())
410413
}
414+
415+
#[cfg(test)]
416+
mod tests {
417+
use std::{cell::RefCell, path::PathBuf};
418+
419+
use asyncgit::sync::RepoPath;
420+
use crossbeam_channel::unbounded;
421+
use git2_testing::repo_init;
422+
use insta::assert_snapshot;
423+
use ratatui::{backend::TestBackend, Terminal};
424+
425+
use crate::{
426+
app::App, draw, input::Input, keys::KeyConfig,
427+
ui::style::Theme,
428+
};
429+
430+
// Macro recommended by: https://insta.rs/docs/cmd/
431+
macro_rules! apply_common_filters {
432+
{} => {
433+
let mut settings = insta::Settings::clone_current();
434+
// MacOS Temp Folder
435+
settings.add_filter(r"/var/folders/\S+?/T/\S+", "[TEMP_FILE]");
436+
// Linux Temp Folder
437+
settings.add_filter(r"/tmp/\.tmp\S+", "[TEMP_FILE]");
438+
// Windows Temp folder
439+
settings.add_filter(r"\b[A-Z]:\\.*\\Local\\Temp\\\S+", "[TEMP_FILE]");
440+
// Convert Windows paths to Unix paths
441+
settings.add_filter(r"\\\\?([\w\d.])", "/$1");
442+
let _bound = settings.bind_to_scope();
443+
}
444+
}
445+
446+
#[test]
447+
fn app_starts() {
448+
apply_common_filters!();
449+
450+
let (temp_dir, _repo) = repo_init();
451+
let path: RepoPath = temp_dir.path().to_str().unwrap().into();
452+
453+
let (tx_git, _rx_git) = unbounded();
454+
let (tx_app, _rx_app) = unbounded();
455+
456+
let input = Input::new();
457+
458+
let theme = Theme::init(&PathBuf::new());
459+
let key_config = KeyConfig::init()
460+
.map_err(|e| eprintln!("KeyConfig loading error: {e}"))
461+
.unwrap_or_default();
462+
463+
let app = App::new(
464+
RefCell::new(path),
465+
tx_git,
466+
tx_app,
467+
input.clone(),
468+
theme,
469+
key_config,
470+
)
471+
.unwrap();
472+
473+
let mut terminal =
474+
Terminal::new(TestBackend::new(120, 40)).unwrap();
475+
476+
let _ = draw(&mut terminal, &app);
477+
478+
assert_snapshot!(terminal.backend());
479+
}
480+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
source: src/main.rs
3+
expression: terminal.backend()
4+
snapshot_kind: text
5+
---
6+
" Status [1] | Log [2] | Files [3] | Stashing [4] | Stashes [5] [TEMP_FILE] "
7+
" ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── "
8+
"┌Unstaged Changes──────────────────────────────────────────┐┌Diff: ────────────────────────────────────────────────────┐"
9+
"│Loading ... ││ │"
10+
"│ ││ │"
11+
"│ ││ │"
12+
"│ ││ │"
13+
"│ ││ │"
14+
"│ ││ │"
15+
"│ ││ │"
16+
"│ ││ │"
17+
"│ ││ │"
18+
"│ ││ │"
19+
"│ ││ │"
20+
"│ ││ │"
21+
"│ ││ │"
22+
"│ ││ │"
23+
"│ ││ │"
24+
"│ ││ │"
25+
"│ ││ │"
26+
"│ ││ │"
27+
"│ ││ │"
28+
"│ ││ │"
29+
"└──────────────────────────────────────────────────{master}┘│ │"
30+
"┌Staged Changes────────────────────────────────────────────┐│ │"
31+
"│Loading ... ││ │"
32+
"│ ││ │"
33+
"│ ││ │"
34+
"│ ││ │"
35+
"│ ││ │"
36+
"│ ││ │"
37+
"│ ││ │"
38+
"│ ││ │"
39+
"│ ││ │"
40+
"│ ││ │"
41+
"│ ││ │"
42+
"│ ││ │"
43+
"│ ││ │"
44+
"└──────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────┘"
45+
" "

0 commit comments

Comments
 (0)