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

Add method to export trash path (issue #38) #67

Closed
Closed
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
15 changes: 15 additions & 0 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ impl TrashContext {
}
Ok(())
}

/// Retrieves the path of the Trash directory.
///
/// # Example
///
/// ```
/// use std::fs::File;
/// use trash::{ delete, trash_path };
/// File::create("mv_to_trash").unwrap();
/// delete("mv_to_trash").unwrap();
/// assert!(trash_path().unwrap().join("files").join("mv_to_trash").exists())
/// ```
pub fn trash_path(&self) -> Result<PathBuf, Error> {
home_trash()
}
}

pub fn list() -> Result<Vec<TrashItem>, Error> {
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ impl TrashContext {
}
}

/// Convenience method for `DEFAULT_TRASH_CTX.trash_path()`
///
/// See: [`TrashContext::trash_path`](TrashContext::trash_path)
pub fn trash_path() -> Result<PathBuf, Error> {
DEFAULT_TRASH_CTX.trash_path()
}

/// Convenience method for `DEFAULT_TRASH_CTX.delete()`.
///
/// See: [`TrashContext::delete`](TrashContext::delete)
Expand Down
16 changes: 15 additions & 1 deletion tests/trash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use log::trace;

use serial_test::serial;
use trash::{delete, delete_all};
use trash::{delete, delete_all, trash_path};

mod util {
use std::sync::atomic::{AtomicI64, Ordering};
Expand Down Expand Up @@ -40,6 +40,20 @@ fn test_delete_file() {
trace!("Finished test_delete_file");
}

#[test]
#[serial]
fn test_trash_path() {
init_logging();
trace!("Started test_trash_path");

let path = PathBuf::from(get_unique_name());
File::create(&path).unwrap();

delete(&path).unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adjust delete so that it returns the path to the deleted file to make it possible to assert that it lives within the trash_path().

assert!(trash_path().unwrap().join("files").join(path).exists());
trace!("Finished test_trash_path");
}

#[test]
#[serial]
fn test_delete_folder() {
Expand Down