Skip to content

Commit

Permalink
Append to log files instead of overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
triarius committed Sep 21, 2024
1 parent 6e7bee2 commit 14c4cbf
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 12 deletions.
74 changes: 72 additions & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ eyre = "0.6.12"
[dev-dependencies]
assert_cmd = "2.0.16"
pretty_assertions = "1.4.1"
tempfile = "3.12.0"

[lints.clippy]
all = "deny"
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::panic;
use crossbeam::channel::{bounded, select, Receiver, TrySendError};
use eyre::{eyre, Result};
use std::{
fs::File,
fs::OpenOptions,
io::{stderr, stdin, stdout, Read, Write},
path::Path,
process::{Command, Stdio},
Expand All @@ -18,7 +18,7 @@ fn io_streams<W: Write + Send + 'static>(
) -> Result<(Stdio, Vec<Box<dyn Write + Send>>)> {
match log_path {
Some(path) => {
let file = File::create(path)?;
let file = OpenOptions::new().create(true).append(true).open(path)?;
Ok((Stdio::piped(), vec![Box::new(writer), Box::new(file)]))
}
None => Ok((Stdio::inherit(), vec![Box::new(writer)])),
Expand Down Expand Up @@ -53,7 +53,10 @@ pub fn run(

match child.stdin.take() {
Some(child_in) => {
let in_file = File::create(stdin_log_path.unwrap())?;
let in_file = OpenOptions::new()
.create(true)
.append(true)
.open(stdin_log_path.unwrap())?;
let mut in_writers: Vec<Box<dyn Write + Send>> =
vec![Box::new(child_in), Box::new(in_file)];

Expand Down
89 changes: 82 additions & 7 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,90 @@
use assert_cmd::Command;
use eyre::{eyre, Result};
use pretty_assertions::assert_eq;
use std::{
fs::File,
io::{Read, Write},
path::Path,
};
use tempfile::tempdir;

#[test]
fn child_exit() {
let mut runner = Command::cargo_bin("runner").unwrap();
let assert = runner.args(["--", "echo", "hello"]).assert();
assert.success().stdout("hello\n");
fn child_exit() -> Result<()> {
Command::cargo_bin("runner")?
.args(["--", "echo", "hello"])
.assert()
.success()
.stdout("hello\n");
Ok(())
}

#[test]
fn stdin_close() {
fn stdin_close() -> Result<()> {
Command::cargo_bin("runner")?
.args(["--", "cat"])
.write_stdin("hello")
.assert()
.success()
.stdout("hello");
Ok(())
}

const TEMP_HEADER: &str = "Temporary File Header";

#[test]
fn child_exit_with_files() -> Result<()> {
let mut runner = Command::cargo_bin("runner").unwrap();
let assert = runner.args(["--", "cat"]).write_stdin("hello").assert();
assert.success().stdout("hello");
let dir = tempdir()?;

let in_file_path = dir.path().join("in.log");
let out_file_path = dir.path().join("out.log");
let err_file_path = dir.path().join("err.log");

[&in_file_path, &out_file_path, &err_file_path]
.into_iter()
.try_for_each(|p| create_temp_data_in_file(p))
.unwrap();

runner
.args([
"--in-file",
in_file_path
.to_str()
.ok_or_else(|| eyre!("invalid in-file path"))?,
"--out-file",
out_file_path
.to_str()
.ok_or_else(|| eyre!("invalid out-file path"))?,
"--err-file",
err_file_path
.to_str()
.ok_or_else(|| eyre!("invalid err-file path"))?,
"--",
"cat",
])
.write_stdin("hello world\n")
.assert()
.success()
.stdout("hello world\n");

let in_contents = read_file(&in_file_path)?;
let out_contents = read_file(&out_file_path)?;
let err_contents = read_file(&err_file_path)?;

assert_eq!(in_contents, format!("{TEMP_HEADER}\nhello world\n"));
assert_eq!(out_contents, format!("{TEMP_HEADER}\nhello world\n"));
assert_eq!(err_contents, format!("{TEMP_HEADER}\n"));
Ok(())
}

fn read_file(path: &Path) -> Result<String> {
let mut out = String::new();
File::open(path)?.read_to_string(&mut out)?;
Ok(out)
}

fn create_temp_data_in_file(path: &Path) -> Result<()> {
let mut f = File::create_new(path)?;
writeln!(f, "{TEMP_HEADER}")?;
Ok(())
}

0 comments on commit 14c4cbf

Please sign in to comment.