Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename public api create to create_dir #1512

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/services/webdav/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl oio::Write for WebdavWriter {
let status = resp.status();

match status {
StatusCode::CREATED | StatusCode::OK => {
StatusCode::CREATED | StatusCode::OK | StatusCode::NO_CONTENT => {
resp.into_body().consume().await?;
Ok(())
}
Expand Down
34 changes: 13 additions & 21 deletions src/types/operator/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,39 +270,31 @@ impl BlockingOperator {
///
/// # Examples
///
/// ## Create an empty file
///
/// ```no_run
/// # use std::io::Result;
/// # use opendal::BlockingOperator;
/// # use futures::TryStreamExt;
/// # fn test(op: BlockingOperator) -> Result<()> {
/// op.create("path/to/file")?;
/// # Ok(())
/// # }
/// ```
///
/// ## Create a dir
///
/// ```no_run
/// # use std::io::Result;
/// # use opendal::BlockingOperator;
/// # use futures::TryStreamExt;
/// # async fn test(op: BlockingOperator) -> Result<()> {
/// op.create("path/to/dir/")?;
/// op.create_dir("path/to/dir/")?;
/// # Ok(())
/// # }
/// ```
pub fn create(&self, path: &str) -> Result<()> {
pub fn create_dir(&self, path: &str) -> Result<()> {
let path = normalize_path(path);

if path.ends_with('/') {
self.inner()
.blocking_create(&path, OpCreate::new(EntryMode::DIR))?;
} else {
self.inner()
.blocking_create(&path, OpCreate::new(EntryMode::FILE))?;
};
if !validate_path(&path, EntryMode::DIR) {
return Err(
Error::new(ErrorKind::NotADirectory, "read path is not a directory")
.with_operation("create_dir")
.with_context("service", self.inner().info().scheme())
.with_context("path", &path),
);
}

self.inner()
.blocking_create(&path, OpCreate::new(EntryMode::DIR))?;

Ok(())
}
Expand Down
38 changes: 14 additions & 24 deletions src/types/operator/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,6 @@ impl Operator {
///
/// # Examples
///
/// ## Create an empty file
///
/// ```
/// # use std::io::Result;
/// # use opendal::Operator;
/// # use futures::TryStreamExt;
/// # #[tokio::main]
/// # async fn test(op: Operator) -> Result<()> {
/// op.create("path/to/file").await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Create a dir
///
/// ```
Expand All @@ -345,22 +332,25 @@ impl Operator {
/// # use futures::TryStreamExt;
/// # #[tokio::main]
/// # async fn test(op: Operator) -> Result<()> {
/// op.create("path/to/dir/").await?;
/// op.create_dir("path/to/dir/").await?;
/// # Ok(())
/// # }
/// ```
pub async fn create(&self, path: &str) -> Result<()> {
pub async fn create_dir(&self, path: &str) -> Result<()> {
let path = normalize_path(path);

let _ = if path.ends_with('/') {
self.inner()
.create(&path, OpCreate::new(EntryMode::DIR))
.await?
} else {
self.inner()
.create(&path, OpCreate::new(EntryMode::FILE))
.await?
};
if !validate_path(&path, EntryMode::DIR) {
return Err(
Error::new(ErrorKind::NotADirectory, "read path is not a directory")
.with_operation("create_dir")
.with_context("service", self.inner().info().scheme())
.with_context("path", &path),
);
}

self.inner()
.create(&path, OpCreate::new(EntryMode::DIR))
.await?;

Ok(())
}
Expand Down
6 changes: 5 additions & 1 deletion tests/behavior/blocking_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ pub fn test_scan(op: BlockingOperator) -> Result<()> {
"x/", "x/y", "x/x/", "x/x/y", "x/x/x/", "x/x/x/y", "x/x/x/x/",
];
for path in expected.iter() {
op.create(path)?;
if path.ends_with('/') {
op.create_dir(path)?;
} else {
op.write(path, "")?;
}
}

let w = op.scan("x/")?;
Expand Down
16 changes: 8 additions & 8 deletions tests/behavior/blocking_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ macro_rules! behavior_blocking_write_tests {
pub fn test_create_file(op: BlockingOperator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();

op.create(&path)?;
op.write(&path, "")?;

let meta = op.stat(&path)?;
assert_eq!(meta.mode(), EntryMode::FILE);
Expand All @@ -111,9 +111,9 @@ pub fn test_create_file(op: BlockingOperator) -> Result<()> {
pub fn test_create_file_existing(op: BlockingOperator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();

op.create(&path)?;
op.write(&path, "")?;

op.create(&path)?;
op.write(&path, "")?;

let meta = op.stat(&path)?;
assert_eq!(meta.mode(), EntryMode::FILE);
Expand All @@ -127,7 +127,7 @@ pub fn test_create_file_existing(op: BlockingOperator) -> Result<()> {
pub fn test_create_file_with_special_chars(op: BlockingOperator) -> Result<()> {
let path = format!("{} !@#$%^&()_+-=;',.txt", uuid::Uuid::new_v4());

op.create(&path)?;
op.write(&path, "")?;

let meta = op.stat(&path)?;
assert_eq!(meta.mode(), EntryMode::FILE);
Expand All @@ -141,7 +141,7 @@ pub fn test_create_file_with_special_chars(op: BlockingOperator) -> Result<()> {
pub fn test_create_dir(op: BlockingOperator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path)?;
op.create_dir(&path)?;

let meta = op.stat(&path)?;
assert_eq!(meta.mode(), EntryMode::DIR);
Expand All @@ -154,9 +154,9 @@ pub fn test_create_dir(op: BlockingOperator) -> Result<()> {
pub fn test_create_dir_existing(op: BlockingOperator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path)?;
op.create_dir(&path)?;

op.create(&path)?;
op.create_dir(&path)?;

let meta = op.stat(&path)?;
assert_eq!(meta.mode(), EntryMode::DIR);
Expand Down Expand Up @@ -227,7 +227,7 @@ pub fn test_stat(op: BlockingOperator) -> Result<()> {
pub fn test_stat_dir(op: BlockingOperator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path).expect("write must succeed");
op.create_dir(&path).expect("write must succeed");

let meta = op.stat(&path)?;
assert_eq!(meta.mode(), EntryMode::DIR);
Expand Down
26 changes: 17 additions & 9 deletions tests/behavior/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub async fn test_list_dir(op: Operator) -> Result<()> {

/// listing a directory, which contains more objects than a single page can take.
pub async fn test_list_rich_dir(op: Operator) -> Result<()> {
op.create("test_list_rich_dir/").await?;
op.create_dir("test_list_rich_dir/").await?;

let mut expected: Vec<String> = (0..=1000)
.map(|num| format!("test_list_rich_dir/file-{num}"))
Expand All @@ -128,7 +128,7 @@ pub async fn test_list_rich_dir(op: Operator) -> Result<()> {
expected
.iter()
.map(|v| async {
op.create(v).await.expect("create must succeed");
op.write(v, "").await.expect("create must succeed");
})
// Collect into a FuturesUnordered.
.collect::<FuturesUnordered<_>>()
Expand All @@ -155,7 +155,7 @@ pub async fn test_list_rich_dir(op: Operator) -> Result<()> {
pub async fn test_list_empty_dir(op: Operator) -> Result<()> {
let dir = format!("{}/", uuid::Uuid::new_v4());

op.create(&dir).await.expect("write must succeed");
op.create_dir(&dir).await.expect("write must succeed");

let mut obs = op.list(&dir).await?;
let mut objects = HashMap::new();
Expand Down Expand Up @@ -189,7 +189,7 @@ pub async fn test_list_non_exist_dir(op: Operator) -> Result<()> {
pub async fn test_list_sub_dir(op: Operator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path).await.expect("creat must succeed");
op.create_dir(&path).await.expect("creat must succeed");

let mut obs = op.list("/").await?;
let mut found = false;
Expand All @@ -216,9 +216,9 @@ pub async fn test_list_nested_dir(op: Operator) -> Result<()> {
let dir_name = format!("{}/", uuid::Uuid::new_v4());
let dir_path = format!("{dir}{dir_name}");

op.create(&dir).await.expect("creat must succeed");
op.create(&file_path).await.expect("creat must succeed");
op.create(&dir_path).await.expect("creat must succeed");
op.create_dir(&dir).await.expect("creat must succeed");
op.write(&file_path, "").await.expect("creat must succeed");
op.create_dir(&dir_path).await.expect("creat must succeed");

let mut obs = op.list(&dir).await?;
let mut objects = HashMap::new();
Expand Down Expand Up @@ -276,7 +276,11 @@ pub async fn test_scan(op: Operator) -> Result<()> {
"x/", "x/y", "x/x/", "x/x/y", "x/x/x/", "x/x/x/y", "x/x/x/x/",
];
for path in expected.iter() {
op.create(path).await?;
if path.ends_with('/') {
op.create_dir(path).await?;
} else {
op.write(path, "").await?;
}
}

let w = op.scan("x/").await?;
Expand All @@ -301,7 +305,11 @@ pub async fn test_remove_all(op: Operator) -> Result<()> {
"x/", "x/y", "x/x/", "x/x/y", "x/x/x/", "x/x/x/y", "x/x/x/x/",
];
for path in expected.iter() {
op.create(path).await?;
if path.ends_with('/') {
op.create_dir(path).await?;
} else {
op.write(path, "").await?;
}
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
}

op.remove_all("x/").await?;
Expand Down
24 changes: 12 additions & 12 deletions tests/behavior/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ macro_rules! behavior_write_tests {
pub async fn test_create_file(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();

op.create(&path).await?;
op.write(&path, "").await?;

let meta = op.stat(&path).await?;
assert_eq!(meta.mode(), EntryMode::FILE);
Expand All @@ -121,9 +121,9 @@ pub async fn test_create_file(op: Operator) -> Result<()> {
pub async fn test_create_file_existing(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();

op.create(&path).await?;
op.write(&path, "").await?;

op.create(&path).await?;
op.write(&path, "").await?;

let meta = op.stat(&path).await?;
assert_eq!(meta.mode(), EntryMode::FILE);
Expand All @@ -137,7 +137,7 @@ pub async fn test_create_file_existing(op: Operator) -> Result<()> {
pub async fn test_create_file_with_special_chars(op: Operator) -> Result<()> {
let path = format!("{} !@#$%^&()_+-=;',.txt", uuid::Uuid::new_v4());

op.create(&path).await?;
op.write(&path, "").await?;

let meta = op.stat(&path).await?;
assert_eq!(meta.mode(), EntryMode::FILE);
Expand All @@ -151,7 +151,7 @@ pub async fn test_create_file_with_special_chars(op: Operator) -> Result<()> {
pub async fn test_create_dir(op: Operator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path).await?;
op.create_dir(&path).await?;

let meta = op.stat(&path).await?;
assert_eq!(meta.mode(), EntryMode::DIR);
Expand All @@ -164,9 +164,9 @@ pub async fn test_create_dir(op: Operator) -> Result<()> {
pub async fn test_create_dir_existing(op: Operator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path).await?;
op.create_dir(&path).await?;

op.create(&path).await?;
op.create_dir(&path).await?;

let meta = op.stat(&path).await?;
assert_eq!(meta.mode(), EntryMode::DIR);
Expand Down Expand Up @@ -234,7 +234,7 @@ pub async fn test_stat(op: Operator) -> Result<()> {
pub async fn test_stat_dir(op: Operator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path).await.expect("write must succeed");
op.create_dir(&path).await.expect("write must succeed");

let meta = op.stat(&path).await?;
assert_eq!(meta.mode(), EntryMode::DIR);
Expand Down Expand Up @@ -590,7 +590,7 @@ pub async fn test_fuzz_part_reader(op: Operator) -> Result<()> {
pub async fn test_read_with_dir_path(op: Operator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path).await.expect("write must succeed");
op.create_dir(&path).await.expect("write must succeed");

let result = op.read(&path).await;
assert!(result.is_err());
Expand Down Expand Up @@ -641,7 +641,7 @@ pub async fn test_delete(op: Operator) -> Result<()> {
pub async fn test_delete_empty_dir(op: Operator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

op.create(&path).await.expect("create must succeed");
op.create_dir(&path).await.expect("create must succeed");

op.delete(&path).await?;

Expand Down Expand Up @@ -676,13 +676,13 @@ pub async fn test_delete_not_existing(op: Operator) -> Result<()> {
// Delete via stream.
pub async fn test_delete_stream(op: Operator) -> Result<()> {
let dir = uuid::Uuid::new_v4().to_string();
op.create(&format!("{dir}/"))
op.create_dir(&format!("{dir}/"))
.await
.expect("creat must succeed");

let expected: Vec<_> = (0..100).collect();
for path in expected.iter() {
op.create(&format!("{dir}/{path}")).await?;
op.write(&format!("{dir}/{path}"), "").await?;
}

op.with_limit(30)
Expand Down