Skip to content

Commit ae00dac

Browse files
committed
Add snapshot test using insta
1 parent 90a2269 commit ae00dac

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
@@ -66,6 +66,8 @@ which = "6.0"
6666

6767
[dev-dependencies]
6868
env_logger = "0.11"
69+
git2-testing = { path = "./git2-testing" }
70+
insta = { version = "1.41.0", features = ["filters"] }
6971
pretty_assertions = "1.4"
7072
tempfile = "3"
7173

src/main.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,10 @@ fn shutdown_terminal() {
300300
}
301301
}
302302

303-
fn draw(terminal: &mut Terminal, app: &App) -> io::Result<()> {
303+
fn draw<B: ratatui::backend::Backend>(
304+
terminal: &mut ratatui::Terminal<B>,
305+
app: &App,
306+
) -> io::Result<()> {
304307
if app.requires_redraw() {
305308
terminal.clear()?;
306309
}
@@ -397,3 +400,70 @@ fn set_panic_handlers() -> Result<()> {
397400

398401
Ok(())
399402
}
403+
404+
#[cfg(test)]
405+
mod tests {
406+
use std::{cell::RefCell, path::PathBuf};
407+
408+
use asyncgit::sync::RepoPath;
409+
use crossbeam_channel::unbounded;
410+
use git2_testing::repo_init;
411+
use insta::assert_snapshot;
412+
use ratatui::{backend::TestBackend, Terminal};
413+
414+
use crate::{
415+
app::App, draw, input::Input, keys::KeyConfig,
416+
ui::style::Theme,
417+
};
418+
419+
// Macro recommended by: https://insta.rs/docs/cmd/
420+
macro_rules! apply_common_filters {
421+
{} => {
422+
let mut settings = insta::Settings::clone_current();
423+
// MacOS Temp Folder
424+
settings.add_filter(r"/var/folders/\S+?/T/\S+", "[TEMP_FILE]");
425+
// Linux Temp Folder
426+
settings.add_filter(r"/tmp/\.tmp\S+", "[TEMP_FILE]");
427+
// Windows Temp folder
428+
settings.add_filter(r"\b[A-Z]:\\.*\\Local\\Temp\\\S+", "[TEMP_FILE]");
429+
// Convert Windows paths to Unix paths
430+
settings.add_filter(r"\\\\?([\w\d.])", "/$1");
431+
let _bound = settings.bind_to_scope();
432+
}
433+
}
434+
435+
#[test]
436+
fn app_starts() {
437+
apply_common_filters!();
438+
439+
let (temp_dir, _repo) = repo_init();
440+
let path: RepoPath = temp_dir.path().to_str().unwrap().into();
441+
442+
let (tx_git, _rx_git) = unbounded();
443+
let (tx_app, _rx_app) = unbounded();
444+
445+
let input = Input::new();
446+
447+
let theme = Theme::init(&PathBuf::new());
448+
let key_config = KeyConfig::init()
449+
.map_err(|e| eprintln!("KeyConfig loading error: {e}"))
450+
.unwrap_or_default();
451+
452+
let app = App::new(
453+
RefCell::new(path),
454+
tx_git,
455+
tx_app,
456+
input.clone(),
457+
theme,
458+
key_config,
459+
)
460+
.unwrap();
461+
462+
let mut terminal =
463+
Terminal::new(TestBackend::new(120, 40)).unwrap();
464+
465+
let _ = draw(&mut terminal, &app);
466+
467+
assert_snapshot!(terminal.backend());
468+
}
469+
}
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)