Skip to content

Commit

Permalink
Fix for test failing on some Linux environments (#35)
Browse files Browse the repository at this point in the history
* Fix for test failing on some Linux environments

In particular when there is no desktop environment installed.
Like in a CI oriented system.

* Fix clippy warnings

* Update changelog
  • Loading branch information
ArturKovacs committed Aug 18, 2021
1 parent bd8679c commit 9da7b59
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Dates are specified in YYYY-MM-DD format.

## Changed
- Fix failing to delete files on some freedesktop (eg Linux) systems when the home was not mounted at the root.
- Fix for test failing on Linux environments that don't have a desktop environment (more specifically don't have a tool like `gio`)

# v2.0.1 on 2021-05-02

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ log = "0.4"
chrono = "0.4.9"
rand = "0.8.3"
once_cell = "1.7.2"
env_logger = "0.8"
env_logger = "0.9"

[build-dependencies]
windows = "0.9.0"
Expand Down
67 changes: 55 additions & 12 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,20 +653,25 @@ mod tests {
collections::{hash_map::Entry, HashMap},
env,
ffi::OsString,
fmt,
fs::File,
path::{Path, PathBuf},
process::Command,
};

use log::warn;

use crate::{
canonicalize_paths,
canonicalize_paths, delete_all,
os_limited::{list, purge_all},
tests::get_unique_name,
Error,
};

#[test]
fn test_list() {
crate::tests::init_logging();

let file_name_prefix = get_unique_name();
let batches: usize = 2;
let files_per_batch: usize = 3;
Expand All @@ -676,7 +681,17 @@ mod tests {
for path in names.iter() {
File::create(path).unwrap();
}
delete_all_using_system_program(&names).unwrap();
// eprintln!("Deleting {:?}", names);
let result = delete_all_using_system_program(&names);
if let Err(SystemTrashError::NoTrashProgram) = &result {
// For example may be the case on build systems that don't have a destop environment
warn!(
"No system default trashing utility was found, using this crate's implementation"
);
delete_all(&names).unwrap();
} else {
result.unwrap();
}
}
let items = list().unwrap();
let items: HashMap<_, Vec<_>> = items
Expand Down Expand Up @@ -708,13 +723,37 @@ mod tests {
//////////////////////////////////////////////////////////////////////////////////////
/// System
//////////////////////////////////////////////////////////////////////////////////////
static DEFAULT_TRASH: &str = "gio";

#[derive(Debug)]
pub enum SystemTrashError {
NoTrashProgram,
Other(Error),
}
impl fmt::Display for SystemTrashError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SystemTrashError during a `trash` operation: {:?}", self)
}
}
impl std::error::Error for SystemTrashError {}

fn is_program_in_path(program: &str) -> bool {
if let Some(path_vars) = std::env::var_os("PATH") {
for path in std::env::split_paths(&path_vars) {
let full_path = path.join(program);
if full_path.is_file() {
return true;
}
}
}
false
}

/// This is based on the electron library's implementation.
/// See: https://github.com/electron/electron/blob/34c4c8d5088fa183f56baea28809de6f2a427e02/shell/common/platform_util_linux.cc#L96
pub fn delete_all_canonicalized_using_system_program(
full_paths: Vec<PathBuf>,
) -> Result<(), Error> {
) -> Result<(), SystemTrashError> {
static DEFAULT_TRASH: &str = "gio";
let trash = {
// Determine desktop environment and set accordingly.
let desktop_env = get_desktop_environment();
Expand Down Expand Up @@ -743,28 +782,32 @@ mod tests {
argv.push(full_path.into());
}
}

if !is_program_in_path(trash) {
return Err(SystemTrashError::NoTrashProgram);
}
// Execute command
let mut command = Command::new(trash);
command.args(argv);
let result =
command.output().map_err(|e| Error::Unknown { description: format!("{}", e) })?;
let result = command.output().map_err(|e| {
SystemTrashError::Other(Error::Unknown {
description: format!("Tried executing: {:?} - Error was: {}", command, e),
})
})?;
if !result.status.success() {
let stderr = String::from_utf8_lossy(&result.stderr);
return Err(Error::Unknown {
return Err(SystemTrashError::Other(Error::Unknown {
description: format!("Used '{}', stderr: {}", trash, stderr),
});
}));
}

Ok(())
}

pub fn delete_all_using_system_program<I, T>(paths: I) -> Result<(), Error>
pub fn delete_all_using_system_program<I, T>(paths: I) -> Result<(), SystemTrashError>
where
I: IntoIterator<Item = T>,
T: AsRef<Path>,
{
let full_paths = canonicalize_paths(paths)?;
let full_paths = canonicalize_paths(paths).map_err(|e| SystemTrashError::Other(e))?;
delete_all_canonicalized_using_system_program(full_paths)
}

Expand Down

0 comments on commit 9da7b59

Please sign in to comment.