Skip to content

Commit

Permalink
test(term): handle not tty case
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Jun 9, 2024
1 parent d005d0d commit b3ada7c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
[workflow-ci-url]: https://github.com/ymgyt/syndicationd/actions/workflows/ci.yaml
[workflow-release-badge]: https://img.shields.io/github/actions/workflow/status/ymgyt/syndicationd/release.yml?style=for-the-badge&logo=github&label=Release
[workflow-release-url]: https://github.com/ymgyt/syndicationd/actions/workflows/release.yml
[coverage-badge]: https://img.shields.io/codecov/c/github/ymgyt/syndicationd?token=W1A93WSPEE&style=for-the-badge&logo=codecov
[coverage-badge]: https://img.shields.io/codecov/c/github/ymgyt/syndicationd?token=W1A93WSPEE&style=for-the-badge&logo=codecov&color=brightgreen
[coverage-url]: https://app.codecov.io/github/ymgyt/syndicationd
[grafana-badge]: https://img.shields.io/badge/Grafana-Dashboard-orange?style=for-the-badge&logo=grafana
[grafana-url]: https://ymgyt.grafana.net/public-dashboards/863ebddd82c44ddd9a28a68eaac848ff?orgId=1&refresh=1h&from=now-1h&to=now
Expand Down
2 changes: 1 addition & 1 deletion codecov.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# https://docs.codecov.com/docs/codecovyml-reference
coverage:
round: down
range: 50..70
range: 50..80
status: # https://docs.codecov.com/docs/commit-status
project:
default:
Expand Down
16 changes: 14 additions & 2 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,25 @@ impl Application {

self.event_loop(input).await;

self.cleanup()
self.cleanup().ok();

Ok(())
}

/// Initialize application.
/// Setup terminal and handle cache.
async fn init(&mut self) -> anyhow::Result<()> {
self.terminal.init()?;
match self.terminal.init() {
Ok(()) => Ok(()),
Err(err) => {
if self.flags.contains(Should::Quit) {
tracing::warn!("Failed to init terminal: {err}");
Ok(())
} else {
Err(err)
}
}
}?;

match self.restore_credential().await {
Ok(cred) => self.handle_initial_credential(cred),
Expand Down
5 changes: 2 additions & 3 deletions crates/synd_term/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::{future, path::PathBuf, time::Duration};

use anyhow::Context as _;
use crossterm::event::EventStream;
use futures_util::TryFutureExt;
use synd_term::{
application::{Application, Cache, Config},
cli::{self, ApiOptions, Args, FeedOptions, Palette},
client::Client,
config::{self, Categories},
terminal::Terminal,
terminal::{self, Terminal},
ui::theme::Theme,
};
use tracing::error;
Expand Down Expand Up @@ -120,7 +119,7 @@ async fn main() {
std::process::exit(exit_code);
};

let mut event_stream = EventStream::new();
let mut event_stream = terminal::event_stream();

if let Err(err) = future::ready(build_app(
endpoint,
Expand Down
26 changes: 21 additions & 5 deletions crates/synd_term/src/terminal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Result;
use crossterm::{
event::EventStream,
terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use futures_util::{future::Either, stream, Stream};
use ratatui::Frame;
use std::io;
use std::io::{self, IsTerminal};

#[cfg(not(feature = "integration"))]
mod backend;
Expand Down Expand Up @@ -33,7 +34,7 @@ impl Terminal {
}

/// Initialize terminal
pub fn init(&mut self) -> Result<()> {
pub fn init(&mut self) -> io::Result<()> {
terminal::enable_raw_mode()?;
crossterm::execute!(io::stdout(), EnterAlternateScreen)?;

Expand All @@ -50,13 +51,13 @@ impl Terminal {
}

/// Reset terminal
pub fn restore(&mut self) -> Result<()> {
pub fn restore(&mut self) -> io::Result<()> {
Self::restore_backend()?;
self.backend.show_cursor()?;
Ok(())
}

fn restore_backend() -> Result<()> {
fn restore_backend() -> io::Result<()> {
terminal::disable_raw_mode()?;
io::stdout().execute(LeaveAlternateScreen)?;
Ok(())
Expand All @@ -79,3 +80,18 @@ impl Terminal {
self.backend.backend().buffer()
}
}

pub fn event_stream() -> impl Stream<Item = std::io::Result<crossterm::event::Event>> + Unpin {
// When tests are run with nix(crane), /dev/tty is not available
// In such cases, executing `EventStream::new()` will cause a panic.
// Currently, this issue only arises during testing with nix, so an empty stream that does not panic is returned
// https://github.com/crossterm-rs/crossterm/blob/fce58c879a748f3159216f68833100aa16141ab0/src/terminal/sys/file_descriptor.rs#L74
// https://github.com/crossterm-rs/crossterm/blob/fce58c879a748f3159216f68833100aa16141ab0/src/event/read.rs#L39
let is_terminal = std::io::stdout().is_terminal();

if is_terminal {
Either::Left(EventStream::new())
} else {
Either::Right(stream::empty())
}
}
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
cargoNextestExtraArgs = "--features integration";
CARGO_PROFILE = "";
RUST_LOG = "synd,integration=debug";
RUST_BACKTRACE = "1";
});

audit = craneLib.cargoAudit {
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fmt: fmt-toml

# Run linter
lint:
cargo clippy --all-features
cargo clippy --all-features --tests

# Format toml files
fmt-toml:
Expand Down

0 comments on commit b3ada7c

Please sign in to comment.