term_transcript/lib.rs
1//! Snapshot testing for CLI / REPL applications, in a fun way.
2//!
3//! # What it does
4//!
5//! This crate allows to:
6//!
7//! - Create [`Transcript`]s of interacting with a terminal, capturing both the output text
8//! and [ANSI-compatible color info][SGR].
9//! - Save these transcripts in the [SVG] format, so that they can be easily embedded as images
10//! into HTML / Markdown documents. (Output format customization
11//! [is also supported](svg::Template#customization) via [Handlebars] templates.)
12//! - Parse transcripts from SVG
13//! - Test that a parsed transcript actually corresponds to the terminal output (either as text
14//! or text + colors).
15//!
16//! The primary use case is easy to create and maintain end-to-end tests for CLI / REPL apps.
17//! Such tests can be embedded into a readme file.
18//!
19//! # Design decisions
20//!
21//! - **Static capturing.** Capturing dynamic interaction with the terminal essentially
22//! requires writing / hacking together a new terminal, which looks like an overkill
23//! for the motivating use case (snapshot testing).
24//!
25//! - **(Primarily) static SVGs.** Animated SVGs create visual noise and make simple things
26//! (e.g., copying text from an SVG) harder than they should be.
27//!
28//! - **Self-contained tests.** Unlike generic snapshot files, [`Transcript`]s contain
29//! both user inputs and outputs. This allows using them as images with little additional
30//! explanation.
31//!
32//! # Limitations
33//!
34//! - Terminal coloring only works with ANSI escape codes. (Since ANSI escape codes
35//! are supported even on Windows nowadays, this shouldn't be a significant problem.)
36//! - ANSI escape sequences other than [SGR] ones are either dropped (in case of [CSI]
37//! and OSC sequences), or lead to a [`TermError::Ansi`] error.
38//! - By default, the crate exposes APIs to perform capture via OS pipes.
39//! Since the terminal is not emulated in this case, programs dependent on [`isatty`] checks
40//! or getting term size can produce different output than if launched in an actual shell
41//! (no coloring, no line wrapping etc.).
42//! - It is possible to capture output from a pseudo-terminal (PTY) using the `portable-pty`
43//! crate feature. However, since most escape sequences are dropped, this is still not a good
44//! option to capture complex outputs (e.g., ones moving cursor).
45//!
46//! # Alternatives / similar tools
47//!
48//! - [`insta`](https://crates.io/crates/insta) is a generic snapshot testing library, which
49//! is amazing in general, but *kind of* too low-level for E2E CLI testing.
50//! - [`rexpect`](https://crates.io/crates/rexpect) allows testing CLI / REPL applications
51//! by scripting interactions with them in tests. It works in Unix only.
52//! - [`trybuild`](https://crates.io/crates/trybuild) snapshot-tests output
53//! of a particular program (the Rust compiler).
54//! - [`trycmd`](https://crates.io/crates/trycmd) snapshot-tests CLI apps using
55//! a text-based format.
56//! - Tools like [`termtosvg`](https://github.com/nbedos/termtosvg) and
57//! [Asciinema](https://asciinema.org/) allow recording terminal sessions and save them to SVG.
58//! The output of these tools is inherently *dynamic* (which, e.g., results in animated SVGs).
59//! This crate [intentionally chooses](#design-decisions) a simpler static format, which
60//! makes snapshot testing easier.
61//!
62//! # Crate features
63//!
64//! ## `portable-pty`
65//!
66//! *(Off by default)*
67//!
68//! Allows using pseudo-terminal (PTY) to capture terminal output rather than pipes.
69//! Uses [the eponymous crate][`portable-pty`] under the hood.
70//!
71//! ## `svg`
72//!
73//! *(On by default)*
74//!
75//! Exposes [the eponymous module](svg) that allows rendering [`Transcript`]s
76//! into the SVG format.
77//!
78//! ## `font-subset`
79//!
80//! *(Off by default)*
81//!
82//! Enables subsetting and embedding OpenType fonts into snapshots. Requires the `svg` feature.
83//!
84//! ## `test`
85//!
86//! *(On by default)*
87//!
88//! Exposes [the eponymous module](crate::test) that allows parsing [`Transcript`]s
89//! from SVG files and testing them.
90//!
91//! ## `pretty_assertions`
92//!
93//! *(On by default)*
94//!
95//! Uses [the eponymous crate][`pretty_assertions`] when testing SVG files.
96//! Only really makes sense together with the `test` feature.
97//!
98//! ## `tracing`
99//!
100//! *(Off by default)*
101//!
102//! Uses [the eponymous facade][`tracing`] to trace main operations, which could be useful
103//! for debugging. Tracing is mostly performed on the `DEBUG` level.
104//!
105//! [SVG]: https://developer.mozilla.org/en-US/docs/Web/SVG
106//! [SGR]: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
107//! [CSI]: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences
108//! [`isatty`]: https://man7.org/linux/man-pages/man3/isatty.3.html
109//! [Handlebars]: https://handlebarsjs.com/
110//! [`pretty_assertions`]: https://docs.rs/pretty_assertions/
111//! [`portable-pty`]: https://docs.rs/portable-pty/
112//! [`tracing`]: https://docs.rs/tracing/
113//!
114//! # Examples
115//!
116//! Creating a terminal [`Transcript`] and rendering it to SVG.
117//!
118//! ```
119//! use term_transcript::{
120//! svg::{Template, TemplateOptions}, ShellOptions, Transcript, UserInput,
121//! };
122//! # use std::str;
123//!
124//! # fn main() -> anyhow::Result<()> {
125//! let transcript = Transcript::from_inputs(
126//! &mut ShellOptions::default(),
127//! vec![UserInput::command(r#"echo "Hello world!""#)],
128//! )?;
129//! let mut writer = vec![];
130//! // ^ Any `std::io::Write` implementation will do, such as a `File`.
131//! Template::default().render(&transcript, &mut writer)?;
132//! println!("{}", str::from_utf8(&writer)?);
133//! # Ok(())
134//! # }
135//! ```
136//!
137//! Snapshot testing. See the [`test` module](crate::test) for more examples.
138//!
139//! ```no_run
140//! use term_transcript::{test::TestConfig, ShellOptions};
141//!
142//! #[test]
143//! fn echo_works() {
144//! TestConfig::new(ShellOptions::default()).test(
145//! "tests/__snapshots__/echo.svg",
146//! &[r#"echo "Hello world!""#],
147//! );
148//! }
149//! ```
150
151// Documentation settings.
152#![doc(html_root_url = "https://docs.rs/term-transcript/0.5.0-beta.1")]
153#![cfg_attr(docsrs, feature(doc_cfg))]
154
155#[cfg(feature = "portable-pty")]
156pub use self::pty::{PtyCommand, PtyShell};
157pub use self::{
158 shell::{ShellOptions, StdShell},
159 types::{ExitStatus, Interaction, TermError, Transcript, UserInput},
160};
161
162#[cfg(feature = "portable-pty")]
163mod pty;
164mod shell;
165//mod style;
166#[cfg(feature = "svg")]
167#[cfg_attr(docsrs, doc(cfg(feature = "svg")))]
168pub mod svg;
169#[cfg(feature = "test")]
170#[cfg_attr(docsrs, doc(cfg(feature = "test")))]
171pub mod test;
172pub mod traits;
173mod types;
174mod utils;
175
176#[cfg(doctest)]
177doc_comment::doctest!("../README.md");