Refactor test folder structure for cli tests and add file_read_text tests

pull/38/head
Chip Senkbeil 3 years ago
parent 8cdc9f271d
commit da08d2db4f
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -1 +0,0 @@
mod file_read_test;

@ -1,4 +1,4 @@
use crate::{fixtures::*, utils::FAILURE_LINE};
use crate::cli::{fixtures::*, utils::FAILURE_LINE};
use assert_cmd::Command;
use assert_fs::prelude::*;
use distant::ExitCode;
@ -8,18 +8,24 @@ use distant_core::{
};
use rstest::*;
const FILE_CONTENTS: &str = r#"
some text
on multiple lines
that is a file's contents
"#;
#[rstest]
fn should_print_out_file_contents(mut action_cmd: Command) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some\ntext\ncontent").unwrap();
file.write_str(FILE_CONTENTS).unwrap();
// distant action file-read {path}
action_cmd
.args(&["file-read", file.to_str().unwrap()])
.assert()
.success()
.stdout("some\ntext\ncontent\n")
.stdout(format!("{}\n", FILE_CONTENTS))
.stderr("");
}
@ -27,7 +33,7 @@ fn should_print_out_file_contents(mut action_cmd: Command) {
fn should_support_json_output(mut action_cmd: Command) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str("some\ntext\ncontent").unwrap();
file.write_str(FILE_CONTENTS).unwrap();
// distant action --format json file-read {path}
let cmd = action_cmd
@ -41,7 +47,7 @@ fn should_support_json_output(mut action_cmd: Command) {
assert_eq!(
res.payload[0],
ResponseData::Blob {
data: b"some\ntext\ncontent".to_vec()
data: FILE_CONTENTS.as_bytes().to_vec()
}
);
}

@ -0,0 +1,94 @@
use crate::cli::{fixtures::*, utils::FAILURE_LINE};
use assert_cmd::Command;
use assert_fs::prelude::*;
use distant::ExitCode;
use distant_core::{
data::{Error, ErrorKind},
Response, ResponseData,
};
use rstest::*;
const FILE_CONTENTS: &str = r#"
some text
on multiple lines
that is a file's contents
"#;
#[rstest]
fn should_print_out_file_contents(mut action_cmd: Command) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str(FILE_CONTENTS).unwrap();
// distant action file-read-text {path}
action_cmd
.args(&["file-read-text", file.to_str().unwrap()])
.assert()
.success()
.stdout(format!("{}\n", FILE_CONTENTS))
.stderr("");
}
#[rstest]
fn should_support_json_output(mut action_cmd: Command) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
file.write_str(FILE_CONTENTS).unwrap();
// distant action --format json file-read-text {path}
let cmd = action_cmd
.args(&["--format", "json"])
.args(&["file-read-text", file.to_str().unwrap()])
.assert()
.success()
.stderr("");
let res: Response = serde_json::from_slice(&cmd.get_output().stdout).unwrap();
assert_eq!(
res.payload[0],
ResponseData::Text {
data: FILE_CONTENTS.to_string()
}
);
}
#[rstest]
fn yield_an_error_when_fails(mut action_cmd: Command) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("missing-file");
// distant action file-read-text {path}
action_cmd
.args(&["file-read-text", file.to_str().unwrap()])
.assert()
.code(ExitCode::Software.to_i32())
.stdout("")
.stderr(FAILURE_LINE.clone());
}
#[rstest]
fn should_support_json_output_for_error(mut action_cmd: Command) {
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("missing-file");
// distant action --format json file-read-text {path}
let cmd = action_cmd
.args(&["--format", "json"])
.args(&["file-read-text", file.to_str().unwrap()])
.assert()
.code(ExitCode::Software.to_i32())
.stderr("");
let res: Response = serde_json::from_slice(&cmd.get_output().stdout).unwrap();
assert!(
matches!(
res.payload[0],
ResponseData::Error(Error {
kind: ErrorKind::NotFound,
..
})
),
"Unexpected response: {:?}",
res.payload[0]
);
}

@ -0,0 +1,2 @@
mod file_read;
mod file_read_text;

@ -0,0 +1 @@
mod cli;
Loading…
Cancel
Save