From 7d52e434cb77b4586aa1874883122f3c4d721020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Z=C3=B6chbauer?= Date: Wed, 7 Feb 2024 21:04:04 +0100 Subject: [PATCH] Print buffer test tool (#11) An additional test helper to print snapshots on demand --- Cargo.lock | 54 ++++++++++++++++++- Cargo.toml | 20 +++---- src/app.rs | 6 ++- src/test_helper.rs | 49 +++++++++++++++-- src/view/block.rs | 2 +- ...view__block__tests__simple_block_test.snap | 3 +- src/widget/block.rs | 4 +- 7 files changed, 116 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab2c9df..7b1062b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,6 +190,27 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "either" version = "1.9.0" @@ -386,6 +407,17 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libredox" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +dependencies = [ + "bitflags 2.4.2", + "libc", + "redox_syscall", +] + [[package]] name = "linked-hash-map" version = "0.5.6" @@ -485,6 +517,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "overload" version = "0.1.1" @@ -621,6 +659,17 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_users" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -962,6 +1011,7 @@ dependencies = [ "anyhow", "bitflags 2.4.2", "crossterm", + "directories", "futures", "futures-task", "futures-util", @@ -986,9 +1036,9 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "unicode-segmentation" -version = "1.10.1" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" +checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" [[package]] name = "unicode-width" diff --git a/Cargo.toml b/Cargo.toml index a09e79c..d7d301c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,25 +6,25 @@ edition = "2021" [dependencies] # xilem_core = { path = "../xilem/xilem/crates/xilem_core" } xilem_core = { git = "https://github.com/Philipp-M/xilem.git", branch = "personal" } +anyhow = "1.0" +bitflags = "2.4" crossterm = "0.27" +directories = "5.0" +kurbo = "0.10" futures-task = "0.3" futures-util = "0.3" +ratatui = "0.26" tokio = { version = "1.35", features = ["full"] } -anyhow = "1.0" -tracing = "0.1.40" +tracing = "0.1" tracing-appender = "0.2" tracing-subscriber = "0.3" -kurbo = "0.10.4" -# update this when a new release occurs -ratatui = "0.26" -bitflags = "2.4.2" -unicode-segmentation = "1.10.1" -unicode-width = "0.1.11" +unicode-segmentation = "1.11" +unicode-width = "0.1" [dev-dependencies] futures = "0.3" -insta = "1.34.0" -rand = "0.8.5" +insta = "1.34" +rand = "0.8" [lints.clippy] dbg_macro = "warn" diff --git a/src/app.rs b/src/app.rs index 5ca14ae..6251bdd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -20,12 +20,13 @@ use crossterm::{ }; use crossterm::event::{poll, read, Event as CxEvent, KeyCode, KeyEvent}; +use directories::ProjectDirs; use ratatui::Terminal; #[cfg(not(test))] use std::io::stdout; -use std::{collections::HashSet, path::PathBuf, sync::Arc, time::Duration}; +use std::{collections::HashSet, sync::Arc, time::Duration}; use tokio::runtime::Runtime; use tracing_subscriber::{fmt::writer::MakeWriterExt, layer::SubscriberExt, Registry}; use xilem_core::{AsyncWake, Id, IdPath, MessageResult}; @@ -40,7 +41,8 @@ use std::io::{Stdout, Write}; // TODO less hardcoding and cross-platform support fn setup_logging(log_level: tracing::Level) -> Result { - let cache_dir = PathBuf::from(std::env::var_os("HOME").unwrap()).join(".cache/trui"); + let proj_dirs = ProjectDirs::from("", "", "trui").expect("Opening cache directory"); + let cache_dir = proj_dirs.cache_dir(); let tracing_file_appender = tracing_appender::rolling::never(cache_dir, "trui.log"); let (tracing_file_writer, guard) = tracing_appender::non_blocking(tracing_file_appender); diff --git a/src/test_helper.rs b/src/test_helper.rs index 2c9760e..50f2d20 100644 --- a/src/test_helper.rs +++ b/src/test_helper.rs @@ -1,11 +1,16 @@ +use std::env; +use std::io::stdout; use std::marker::PhantomData; use std::sync::Arc; +use crossterm::cursor::MoveToNextLine; +use crossterm::execute; +use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use ratatui::backend::TestBackend; use ratatui::layout::Size; -use ratatui::prelude::Buffer; +use ratatui::prelude::*; use ratatui::style::Style; -use ratatui::Terminal; +use ratatui::{Terminal, TerminalOptions, Viewport}; use tokio::sync::mpsc; use xilem_core::MessageResult; @@ -49,6 +54,9 @@ pub fn render_view( event_tx.blocking_send(Event::Quit).unwrap(); let _ = join_handle.join().unwrap(); + + print_buffer(&buffer).unwrap(); + buffer } @@ -56,7 +64,7 @@ pub fn render_view( /// /// * `buffer_size` - The terminal output buffer is set to that size. /// * `sut` - (system under test) The widget to render. -pub fn render_widget(buffer_size: Size, sut: &mut impl Widget) -> Terminal { +pub fn render_widget(buffer_size: Size, sut: &mut impl Widget) -> Buffer { let mut messages = vec![]; let mut cx_state = CxState::new(&mut messages); let mut widget_state = WidgetState::new(); @@ -89,7 +97,10 @@ pub fn render_widget(buffer_size: Size, sut: &mut impl Widget) -> Terminal std::io::Result<()> { + if env::var("DEBUG_SNAPSHOT").is_ok() { + enable_raw_mode()?; + execute!(stdout(), MoveToNextLine(0))?; + let mut terminal = Terminal::with_options( + CrosstermBackend::new(stdout()), + TerminalOptions { + viewport: Viewport::Inline(buffer.area.height), + }, + )?; + terminal.current_buffer_mut().clone_from(buffer); + terminal.flush()?; + execute!(stdout(), MoveToNextLine(0))?; + disable_raw_mode()?; + }; + Ok(()) +} diff --git a/src/view/block.rs b/src/view/block.rs index c61255d..a1ea40b 100644 --- a/src/view/block.rs +++ b/src/view/block.rs @@ -318,7 +318,7 @@ mod tests { #[test] fn simple_block_test() { - let sut = Arc::new(block("some text")); + let sut = Arc::new(block("some text".fg(Color::Cyan))); let buffer = render_view( Size { width: 15, diff --git a/src/view/snapshots/trui__view__block__tests__simple_block_test.snap b/src/view/snapshots/trui__view__block__tests__simple_block_test.snap index 64e2d5e..7acc0fa 100644 --- a/src/view/snapshots/trui__view__block__tests__simple_block_test.snap +++ b/src/view/snapshots/trui__view__block__tests__simple_block_test.snap @@ -12,6 +12,7 @@ Buffer { " ", ], styles: [ - x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, + x: 0, y: 0, fg: Cyan, bg: Reset, underline: Reset, modifier: NONE, + x: 9, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, ] } diff --git a/src/widget/block.rs b/src/widget/block.rs index 6b4b007..2e4a4b4 100644 --- a/src/widget/block.rs +++ b/src/widget/block.rs @@ -234,13 +234,13 @@ mod tests { false, ); - let terminal = render_widget( + let buffer = render_widget( Size { width: 15, height: 5, }, &mut sut, ); - insta::assert_debug_snapshot!(terminal.backend().buffer()); + insta::assert_debug_snapshot!(buffer); } }