diff --git a/distant-core/src/server/distant/handler.rs b/distant-core/src/server/distant/handler.rs index 8a7f74b..ae9cda1 100644 --- a/distant-core/src/server/distant/handler.rs +++ b/distant-core/src/server/distant/handler.rs @@ -182,6 +182,7 @@ async fn file_write(path: PathBuf, data: impl AsRef<[u8]>) -> Result) -> Result { 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); diff --git a/distant-ssh2/src/lib.rs b/distant-ssh2/src/lib.rs index abd4b41..23d66e9 100644 --- a/distant-ssh2/src/lib.rs +++ b/distant-ssh2/src/lib.rs @@ -197,6 +197,11 @@ pub struct Ssh2Session { impl Ssh2Session { /// Connect to a remote TCP server using SSH pub fn connect(host: impl AsRef, opts: Ssh2SessionOpts) -> io::Result { + 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))?;