Skip to content

Commit

Permalink
test: add test calling out to bash
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcur committed Jun 30, 2024
1 parent 361ae84 commit b8e65a1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ mod poll;
mod ringbuffer;
use ringbuffer::{IoResult, Ringbuffer};

#[cfg(test)]
mod tests;

const DEFAULT_NUM_LINES: u16 = 24;
const DEFAULT_NUM_COLUMNS: u16 = 80;

Expand Down Expand Up @@ -344,8 +347,32 @@ fn from_read(read: &mut impl Read, lines: u16, columns: u16) -> anyhow::Result<S
}

fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let mut cli = Cli::parse();
let out = cli.out.take();
let screen = run(cli)?;

let fonts = &[
"ui-monospace",
"Consolas",
"Liberation Mono",
"Source Code Pro",
];

if let Some(out) = out {
let mut file = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(out)?;
write!(file, "{}", screen.to_svg(fonts))?;
} else {
println!("{}", screen.to_svg(fonts))
}

Ok(())
}

fn run(cli: Cli) -> anyhow::Result<Screen> {
if cli.interactive {
if cli.out.is_none() {
anyhow::bail!("`--interactive` is set but no SVG output file is specified in `--out`. See `termsnap --help`.");
Expand Down Expand Up @@ -433,23 +460,5 @@ fn main() -> anyhow::Result<()> {
}
};

let fonts = &[
"ui-monospace",
"Consolas",
"Liberation Mono",
"Source Code Pro",
];

if let Some(out) = cli.out {
let mut file = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(out)?;
write!(file, "{}", screen.to_svg(fonts))?;
} else {
println!("{}", screen.to_svg(fonts))
}

Ok(())
Ok(screen)
}
23 changes: 23 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use clap::Parser;

use super::{run, Cli};

#[test]
fn bash_echo() {
let cli = Cli::parse_from([
"termsnap",
"-l",
"20",
"-c",
"80",
"--",
"bash",
"-c",
"echo 'hello, world'",
]);

let screen = run(cli).unwrap();
let content: String = screen.cells().map(|c| c.c).collect();

assert!(content.starts_with("hello, world"));
}

0 comments on commit b8e65a1

Please sign in to comment.