Skip to content

Commit

Permalink
Set tracing output to stderr and exit with non-zero code on error (#138)
Browse files Browse the repository at this point in the history
Previously, the error was caught, printed, and ignored so the command always exits with a success (exit code `0`).

* Add integration test for the URL command
  • Loading branch information
cdunster authored Oct 22, 2024
1 parent 9f306ef commit 462e1e5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

- Set tracing writer to write to `stderr` instead of `stdout` [#138](https://github.com/holochain/lair/pull/138)
- The `lair-keystore` binary now exits with an error (exit code `1`) if and error occurs [#138](https://github.com/holochain/lair/pull/138)

## 0.5.2

- enables some basic tracing [#135](https://github.com/holochain/lair/pull/135)
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/lair_keystore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pretty_assertions = { workspace = true }
sqlformat = { workspace = true }

[dev-dependencies]
assert_cmd = { workspace = true }
criterion = { workspace = true }
tempdir = { workspace = true }

Expand Down
7 changes: 3 additions & 4 deletions crates/lair_keystore/src/bin/lair-keystore-bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ async fn exec() -> LairResult<()> {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_writer(std::io::stderr)
.compact()
.finish(),
)
Expand Down Expand Up @@ -223,8 +224,6 @@ async fn exec() -> LairResult<()> {
}

#[tokio::main(flavor = "multi_thread")]
async fn main() {
if let Err(e) = exec().await {
eprintln!("{e}");
}
async fn main() -> LairResult<()> {
exec().await
}
29 changes: 29 additions & 0 deletions crates/lair_keystore/tests/client_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mod common;

use assert_cmd::Command;
use common::create_config;
use lair_keystore::dependencies::*;
use std::{fs::File, io::Write};

#[tokio::test(flavor = "multi_thread")]
async fn test_url_command_only_outputs_connection_url() {
let tmp_dir = tempdir::TempDir::new("lair keystore test").unwrap();
let passphrase = sodoken::BufRead::from(&b"passphrase"[..]);

let config = create_config(&tmp_dir, passphrase.clone()).await;
let config_path = tmp_dir.path().join("lair-keystore-config.yaml");
let mut config_file =
File::create_new(config_path).expect("test config file already exists");

config_file
.write_all(config.to_string().as_bytes())
.expect("failed to write config to file");

let mut cmd = Command::cargo_bin("lair-keystore").unwrap();
cmd.env("RUST_LOG", "trace"); // Enable all logging levels to make sure nothing gets printed to stdout
cmd.arg(format!("--lair-root={}", tmp_dir.path().display()));
cmd.arg("url");

let expected_stdout = format!("{}\n", config.connection_url);
cmd.assert().success().stdout(expected_stdout);
}

0 comments on commit 462e1e5

Please sign in to comment.