Fix append_file and append_file_text not creating a file when missing

pull/96/head
Chip Senkbeil 3 years ago
parent dba8a80dd8
commit 4957651696
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -182,6 +182,7 @@ async fn file_write(path: PathBuf, data: impl AsRef<[u8]>) -> Result<Outgoing, S
async fn file_append(path: PathBuf, data: impl AsRef<[u8]>) -> Result<Outgoing, ServerError> {
let mut file = tokio::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.await?;
@ -1042,6 +1043,40 @@ mod tests {
file.assert(predicate::path::missing());
}
#[tokio::test]
async fn file_append_should_create_file_if_missing() {
let (conn_id, state, tx, mut rx) = setup(1);
// Don't create the file directly, but define path
// where the file should be
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileAppend {
path: file.path().to_path_buf(),
data: b"some extra contents".to_vec(),
}],
);
process(conn_id, state, req, tx).await.unwrap();
let res = rx.recv().await.unwrap();
assert_eq!(res.payload.len(), 1, "Wrong payload size");
assert!(
matches!(res.payload[0], ResponseData::Ok),
"Unexpected response: {:?}",
res.payload[0]
);
// Yield to allow chance to finish appending to file
tokio::time::sleep(Duration::from_millis(50)).await;
// Also verify that we actually did create to the file
file.assert("some extra contents");
}
#[tokio::test]
async fn file_append_should_send_ok_when_successful() {
let (conn_id, state, tx, mut rx) = setup(1);
@ -1107,6 +1142,40 @@ mod tests {
file.assert(predicate::path::missing());
}
#[tokio::test]
async fn file_append_text_should_create_file_if_missing() {
let (conn_id, state, tx, mut rx) = setup(1);
// Don't create the file directly, but define path
// where the file should be
let temp = assert_fs::TempDir::new().unwrap();
let file = temp.child("test-file");
let req = Request::new(
"test-tenant",
vec![RequestData::FileAppendText {
path: file.path().to_path_buf(),
text: "some extra contents".to_string(),
}],
);
process(conn_id, state, req, tx).await.unwrap();
let res = rx.recv().await.unwrap();
assert_eq!(res.payload.len(), 1, "Wrong payload size");
assert!(
matches!(res.payload[0], ResponseData::Ok),
"Unexpected response: {:?}",
res.payload[0]
);
// Yield to allow chance to finish appending to file
tokio::time::sleep(Duration::from_millis(50)).await;
// Also verify that we actually did create to the file
file.assert("some extra contents");
}
#[tokio::test]
async fn file_append_text_should_send_ok_when_successful() {
let (conn_id, state, tx, mut rx) = setup(1);

@ -197,6 +197,11 @@ pub struct Ssh2Session {
impl Ssh2Session {
/// Connect to a remote TCP server using SSH
pub fn connect(host: impl AsRef<str>, opts: Ssh2SessionOpts) -> io::Result<Self> {
debug!(
"Establishing ssh connection to {} using {:?}",
host.as_ref(),
opts
);
let mut config = WezConfig::new();
config.add_default_config_files();
@ -255,6 +260,7 @@ impl Ssh2Session {
.map_err(|x| io::Error::new(io::ErrorKind::InvalidData, x))?;
// Establish a connection
trace!("WezSession::connect({:?})", config);
let (session, events) =
WezSession::connect(config).map_err(|x| io::Error::new(io::ErrorKind::Other, x))?;

Loading…
Cancel
Save