|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use crate::api; |
| 4 | +use crate::api::client; |
| 5 | +use crate::error::OxenError; |
| 6 | +use crate::model::RemoteRepository; |
| 7 | +use crate::view::entries::PaginatedDirEntriesResponse; |
| 8 | +use crate::view::PaginatedDirEntries; |
| 9 | + |
| 10 | +pub async fn list( |
| 11 | + remote_repo: &RemoteRepository, |
| 12 | + workspace_id: impl AsRef<str>, |
| 13 | + directory: impl AsRef<Path>, |
| 14 | + page: usize, |
| 15 | + page_size: usize, |
| 16 | +) -> Result<PaginatedDirEntries, OxenError> { |
| 17 | + let workspace_id = workspace_id.as_ref(); |
| 18 | + let path_str = directory.as_ref().to_str().unwrap(); |
| 19 | + let uri = if path_str.is_empty() || path_str == "." { |
| 20 | + format!("/workspaces/{workspace_id}/ls?page={page}&page_size={page_size}") |
| 21 | + } else { |
| 22 | + format!("/workspaces/{workspace_id}/ls/{path_str}?page={page}&page_size={page_size}") |
| 23 | + }; |
| 24 | + let url = api::endpoint::url_from_repo(remote_repo, &uri)?; |
| 25 | + log::debug!("workspaces::ls url: {url}"); |
| 26 | + |
| 27 | + let client = client::new_for_url(&url)?; |
| 28 | + let res = client.get(&url).send().await?; |
| 29 | + let body = client::parse_json_body(&url, res).await?; |
| 30 | + let response: PaginatedDirEntriesResponse = serde_json::from_str(&body).map_err(|err| { |
| 31 | + OxenError::basic_str(format!( |
| 32 | + "api::workspaces::ls error parsing response from {url}\n\nErr {err:?}\n\n{body}" |
| 33 | + )) |
| 34 | + })?; |
| 35 | + Ok(response.entries) |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod tests { |
| 40 | + use crate::config::UserConfig; |
| 41 | + use crate::constants::DEFAULT_BRANCH_NAME; |
| 42 | + use crate::error::OxenError; |
| 43 | + use crate::test; |
| 44 | + use crate::view::entries::EMetadataEntry; |
| 45 | + use crate::{api, constants}; |
| 46 | + |
| 47 | + #[tokio::test] |
| 48 | + async fn test_workspace_ls_root() -> Result<(), OxenError> { |
| 49 | + test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move { |
| 50 | + let workspace_id = UserConfig::identifier()?; |
| 51 | + let _workspace = |
| 52 | + api::client::workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, &workspace_id) |
| 53 | + .await?; |
| 54 | + |
| 55 | + let result = api::client::workspaces::ls::list( |
| 56 | + &remote_repo, |
| 57 | + &workspace_id, |
| 58 | + "", |
| 59 | + constants::DEFAULT_PAGE_NUM, |
| 60 | + constants::DEFAULT_PAGE_SIZE, |
| 61 | + ) |
| 62 | + .await?; |
| 63 | + |
| 64 | + assert!(!result.entries.is_empty(), "Should return entries at root"); |
| 65 | + |
| 66 | + Ok(remote_repo) |
| 67 | + }) |
| 68 | + .await |
| 69 | + } |
| 70 | + |
| 71 | + #[tokio::test] |
| 72 | + async fn test_workspace_ls_with_additions() -> Result<(), OxenError> { |
| 73 | + test::run_remote_repo_test_bounding_box_csv_pushed(|local_repo, remote_repo| async move { |
| 74 | + let workspace_id = UserConfig::identifier()?; |
| 75 | + let _workspace = |
| 76 | + api::client::workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, &workspace_id) |
| 77 | + .await?; |
| 78 | + |
| 79 | + // Upload a file to the workspace |
| 80 | + let test_file = local_repo.path.join("new_file.txt"); |
| 81 | + crate::util::fs::write_to_path(&test_file, "new content")?; |
| 82 | + api::client::workspaces::files::upload_single_file( |
| 83 | + &remote_repo, |
| 84 | + &workspace_id, |
| 85 | + "", |
| 86 | + test_file, |
| 87 | + ) |
| 88 | + .await?; |
| 89 | + |
| 90 | + let result = api::client::workspaces::ls::list( |
| 91 | + &remote_repo, |
| 92 | + &workspace_id, |
| 93 | + "", |
| 94 | + constants::DEFAULT_PAGE_NUM, |
| 95 | + constants::DEFAULT_PAGE_SIZE, |
| 96 | + ) |
| 97 | + .await?; |
| 98 | + |
| 99 | + // Find the added file |
| 100 | + let added = result |
| 101 | + .entries |
| 102 | + .iter() |
| 103 | + .find(|e| e.filename() == "new_file.txt"); |
| 104 | + assert!(added.is_some(), "Should find newly added file"); |
| 105 | + |
| 106 | + if let Some(EMetadataEntry::WorkspaceMetadataEntry(ws_entry)) = added { |
| 107 | + assert!(ws_entry.changes.is_some(), "Added file should have changes"); |
| 108 | + } |
| 109 | + |
| 110 | + Ok(remote_repo) |
| 111 | + }) |
| 112 | + .await |
| 113 | + } |
| 114 | + |
| 115 | + #[tokio::test] |
| 116 | + async fn test_workspace_ls_with_removals() -> Result<(), OxenError> { |
| 117 | + test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move { |
| 118 | + let workspace_id = UserConfig::identifier()?; |
| 119 | + let _workspace = |
| 120 | + api::client::workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, &workspace_id) |
| 121 | + .await?; |
| 122 | + |
| 123 | + // Remove a file from the workspace |
| 124 | + let rm_path = std::path::Path::new("annotations") |
| 125 | + .join("train") |
| 126 | + .join("bounding_box.csv"); |
| 127 | + api::client::workspaces::changes::rm(&remote_repo, &workspace_id, &rm_path).await?; |
| 128 | + |
| 129 | + let result = api::client::workspaces::ls::list( |
| 130 | + &remote_repo, |
| 131 | + &workspace_id, |
| 132 | + "annotations/train", |
| 133 | + constants::DEFAULT_PAGE_NUM, |
| 134 | + constants::DEFAULT_PAGE_SIZE, |
| 135 | + ) |
| 136 | + .await?; |
| 137 | + |
| 138 | + // The removed file should NOT appear in the listing |
| 139 | + let removed = result |
| 140 | + .entries |
| 141 | + .iter() |
| 142 | + .find(|e| e.filename() == "bounding_box.csv"); |
| 143 | + assert!( |
| 144 | + removed.is_none(), |
| 145 | + "Removed file should not appear in workspace ls" |
| 146 | + ); |
| 147 | + |
| 148 | + Ok(remote_repo) |
| 149 | + }) |
| 150 | + .await |
| 151 | + } |
| 152 | +} |
0 commit comments