Skip to content

Commit

Permalink
Add method to export trash path (issue Byron#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanPabloMiceli committed May 3, 2023
1 parent eef463a commit 2db7fb0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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();
assert!(trash_path().unwrap().join("files").join(path).exists());
trace!("Finished test_trash_path");
}

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

0 comments on commit 2db7fb0

Please sign in to comment.