Skip to content

Commit bf93ee9

Browse files
committed
Add snapshot test using insta
1 parent e08d954 commit bf93ee9

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
@@ -302,7 +302,10 @@ fn shutdown_terminal() {
302302
}
303303
}
304304

305-
fn draw(terminal: &mut Terminal, app: &App) -> io::Result<()> {
305+
fn draw<B: ratatui::backend::Backend>(
306+
terminal: &mut ratatui::Terminal<B>,
307+
app: &App,
308+
) -> io::Result<()> {
306309
if app.requires_redraw() {
307310
terminal.clear()?;
308311
}
@@ -418,3 +421,70 @@ fn set_panic_handlers() -> Result<()> {
418421

419422
Ok(())
420423
}
424+
425+
#[cfg(test)]
426+
mod tests {
427+
use std::{cell::RefCell, path::PathBuf};
428+
429+
use asyncgit::sync::RepoPath;
430+
use crossbeam_channel::unbounded;
431+
use git2_testing::repo_init;
432+
use insta::assert_snapshot;
433+
use ratatui::{backend::TestBackend, Terminal};
434+
435+
use crate::{
436+
app::App, draw, input::Input, keys::KeyConfig,
437+
ui::style::Theme,
438+
};
439+
440+
// Macro recommended by: https://insta.rs/docs/cmd/
441+
macro_rules! apply_common_filters {
442+
{} => {
443+
let mut settings = insta::Settings::clone_current();
444+
// MacOS Temp Folder
445+
settings.add_filter(r"/var/folders/\S+?/T/\S+", "[TEMP_FILE]");
446+
// Linux Temp Folder
447+
settings.add_filter(r"/tmp/\.tmp\S+", "[TEMP_FILE]");
448+
// Windows Temp folder
449+
settings.add_filter(r"\b[A-Z]:\\.*\\Local\\Temp\\\S+", "[TEMP_FILE]");
450+
// Convert Windows paths to Unix paths
451+
settings.add_filter(r"\\\\?([\w\d.])", "/$1");
452+
let _bound = settings.bind_to_scope();
453+
}
454+
}
455+
456+
#[test]
457+
fn app_starts() {
458+
apply_common_filters!();
459+
460+
let (temp_dir, _repo) = repo_init();
461+
let path: RepoPath = temp_dir.path().to_str().unwrap().into();
462+
463+
let (tx_git, _rx_git) = unbounded();
464+
let (tx_app, _rx_app) = unbounded();
465+
466+
let input = Input::new();
467+
468+
let theme = Theme::init(&PathBuf::new());
469+
let key_config = KeyConfig::init()
470+
.map_err(|e| eprintln!("KeyConfig loading error: {e}"))
471+
.unwrap_or_default();
472+
473+
let app = App::new(
474+
RefCell::new(path),
475+
tx_git,
476+
tx_app,
477+
input.clone(),
478+
theme,
479+
key_config,
480+
)
481+
.unwrap();
482+
483+
let mut terminal =
484+
Terminal::new(TestBackend::new(120, 40)).unwrap();
485+
486+
let _ = draw(&mut terminal, &app);
487+
488+
assert_snapshot!(terminal.backend());
489+
}
490+
}
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)