mirror of https://github.com/chipsenkbeil/distant
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.4 KiB
Rust
163 lines
4.4 KiB
Rust
use crate::cli::{
|
|
fixtures::*,
|
|
utils::{random_tenant, FAILURE_LINE},
|
|
};
|
|
use assert_cmd::Command;
|
|
use assert_fs::prelude::*;
|
|
use distant::ExitCode;
|
|
use distant_core::{
|
|
data::{Error, ErrorKind},
|
|
Request, RequestData, Response, ResponseData,
|
|
};
|
|
use predicates::prelude::*;
|
|
use rstest::*;
|
|
|
|
#[rstest]
|
|
fn should_report_ok_when_done(mut action_cmd: Command) {
|
|
let temp = assert_fs::TempDir::new().unwrap();
|
|
let dir = temp.child("dir");
|
|
|
|
// distant action dir-create {path}
|
|
action_cmd
|
|
.args(&["dir-create", dir.to_str().unwrap()])
|
|
.assert()
|
|
.success()
|
|
.stdout("")
|
|
.stderr("");
|
|
|
|
dir.assert(predicate::path::exists());
|
|
dir.assert(predicate::path::is_dir());
|
|
}
|
|
|
|
#[rstest]
|
|
fn should_support_creating_missing_parent_directories_if_specified(mut action_cmd: Command) {
|
|
let temp = assert_fs::TempDir::new().unwrap();
|
|
let dir = temp.child("dir1").child("dir2");
|
|
|
|
// distant action dir-create {path}
|
|
action_cmd
|
|
.args(&["dir-create", "--all", dir.to_str().unwrap()])
|
|
.assert()
|
|
.success()
|
|
.stdout("")
|
|
.stderr("");
|
|
|
|
dir.assert(predicate::path::exists());
|
|
dir.assert(predicate::path::is_dir());
|
|
}
|
|
|
|
#[rstest]
|
|
fn yield_an_error_when_fails(mut action_cmd: Command) {
|
|
let temp = assert_fs::TempDir::new().unwrap();
|
|
let dir = temp.child("missing-dir").child("dir");
|
|
|
|
// distant action dir-create {path}
|
|
action_cmd
|
|
.args(&["dir-create", dir.to_str().unwrap()])
|
|
.assert()
|
|
.code(ExitCode::Software.to_i32())
|
|
.stdout("")
|
|
.stderr(FAILURE_LINE.clone());
|
|
|
|
dir.assert(predicate::path::missing());
|
|
}
|
|
|
|
#[rstest]
|
|
fn should_support_json_output(mut action_cmd: Command) {
|
|
let temp = assert_fs::TempDir::new().unwrap();
|
|
let dir = temp.child("dir");
|
|
|
|
let req = Request {
|
|
id: rand::random(),
|
|
tenant: random_tenant(),
|
|
payload: vec![RequestData::DirCreate {
|
|
path: dir.to_path_buf(),
|
|
all: false,
|
|
}],
|
|
};
|
|
|
|
// distant action --format json --interactive
|
|
let cmd = action_cmd
|
|
.args(&["--format", "json"])
|
|
.arg("--interactive")
|
|
.write_stdin(format!("{}\n", serde_json::to_string(&req).unwrap()))
|
|
.assert()
|
|
.success()
|
|
.stderr("");
|
|
|
|
let res: Response = serde_json::from_slice(&cmd.get_output().stdout).unwrap();
|
|
assert_eq!(res.payload[0], ResponseData::Ok);
|
|
|
|
dir.assert(predicate::path::exists());
|
|
dir.assert(predicate::path::is_dir());
|
|
}
|
|
|
|
#[rstest]
|
|
fn should_support_json_creating_missing_parent_directories_if_specified(mut action_cmd: Command) {
|
|
let temp = assert_fs::TempDir::new().unwrap();
|
|
let dir = temp.child("dir1").child("dir2");
|
|
|
|
let req = Request {
|
|
id: rand::random(),
|
|
tenant: random_tenant(),
|
|
payload: vec![RequestData::DirCreate {
|
|
path: dir.to_path_buf(),
|
|
all: true,
|
|
}],
|
|
};
|
|
|
|
// distant action --format json --interactive
|
|
let cmd = action_cmd
|
|
.args(&["--format", "json"])
|
|
.arg("--interactive")
|
|
.write_stdin(format!("{}\n", serde_json::to_string(&req).unwrap()))
|
|
.assert()
|
|
.success()
|
|
.stderr("");
|
|
|
|
let res: Response = serde_json::from_slice(&cmd.get_output().stdout).unwrap();
|
|
assert_eq!(res.payload[0], ResponseData::Ok);
|
|
|
|
dir.assert(predicate::path::exists());
|
|
dir.assert(predicate::path::is_dir());
|
|
}
|
|
|
|
#[rstest]
|
|
fn should_support_json_output_for_error(mut action_cmd: Command) {
|
|
let temp = assert_fs::TempDir::new().unwrap();
|
|
let dir = temp.child("missing-dir").child("dir");
|
|
|
|
let req = Request {
|
|
id: rand::random(),
|
|
tenant: random_tenant(),
|
|
payload: vec![RequestData::DirCreate {
|
|
path: dir.to_path_buf(),
|
|
all: false,
|
|
}],
|
|
};
|
|
|
|
// distant action --format json --interactive
|
|
let cmd = action_cmd
|
|
.args(&["--format", "json"])
|
|
.arg("--interactive")
|
|
.write_stdin(format!("{}\n", serde_json::to_string(&req).unwrap()))
|
|
.assert()
|
|
.success()
|
|
.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]
|
|
);
|
|
|
|
dir.assert(predicate::path::missing());
|
|
}
|