use assert_fs::prelude::*;
use indoc::indoc;
use predicates::prelude::*;
use rstest::*;
use crate::common::fixtures::*;
const FILE_CONTENTS: &str = indoc! {r#"
some text
on multiple lines
that is a file's contents
"#};
const APPENDED_FILE_CONTENTS: &str = indoc! {r#"
even more
file contents
"#};
#[rstest]
#[test_log::test]
fn should_support_writing_stdin_to_file(ctx: DistantManagerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
ctx.new_assert_cmd(["fs", "write"])
.args([file.to_str().unwrap()])
.write_stdin(FILE_CONTENTS)
.assert()
.success()
.stdout("")
.stderr("");
std::thread::sleep(std::time::Duration::from_millis(100));
file.assert(FILE_CONTENTS);
}
#[rstest]
#[test_log::test]
fn should_support_appending_stdin_to_file(ctx: DistantManagerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str(FILE_CONTENTS).unwrap();
ctx.new_assert_cmd(["fs", "write"])
.args(["--append", file.to_str().unwrap()])
.write_stdin(APPENDED_FILE_CONTENTS)
.assert()
.success()
.stdout("")
.stderr("");
std::thread::sleep(std::time::Duration::from_millis(100));
file.assert(format!("{}{}", FILE_CONTENTS, APPENDED_FILE_CONTENTS));
}
#[rstest]
#[test_log::test]
fn should_support_writing_argument_to_file(ctx: DistantManagerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
ctx.new_assert_cmd(["fs", "write"])
.args([file.to_str().unwrap(), "--"])
.arg(FILE_CONTENTS)
.assert()
.success()
.stdout("")
.stderr("");
std::thread::sleep(std::time::Duration::from_millis(100));
file.assert(FILE_CONTENTS);
}
#[rstest]
#[test_log::test]
fn should_support_appending_argument_to_file(ctx: DistantManagerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str(FILE_CONTENTS).unwrap();
ctx.new_assert_cmd(["fs", "write"])
.args(["--append", file.to_str().unwrap(), "--"])
.arg(APPENDED_FILE_CONTENTS)
.assert()
.success()
.stdout("")
.stderr("");
std::thread::sleep(std::time::Duration::from_millis(100));
file.assert(format!("{}{}", FILE_CONTENTS, APPENDED_FILE_CONTENTS));
}
#[rstest]
#[test_log::test]
fn yield_an_error_when_fails(ctx: DistantManagerCtx) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("missing-dir").child("missing-file");
ctx.new_assert_cmd(["fs", "write"])
.args([file.to_str().unwrap(), "--"])
.arg(FILE_CONTENTS)
.assert()
.code(1)
.stdout("")
.stderr(predicates::str::is_empty().not());
file.assert(predicates::path::missing());
}