change: Prevent group removal while it has tasks.

This commit is contained in:
Arne Beer 2021-07-24 19:44:40 +02:00
parent 91391ce867
commit 9a5df3bf75
No known key found for this signature in database
GPG key ID: CC9408F679023B65
4 changed files with 39 additions and 3 deletions

View file

@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [1.0.0] -
A lot of things happened during this release.
Even though a few new features were added, the main effort went into increasing stability and inter-version compatibility.
Even though quite a few new features were added, the main effort went into increasing stability and inter-version compatibility.
The goal of this release is to push the code quality, error handling, test coverage and stability to a level that justifies a v1.0 release. \
Since this project follows semantic versioning, this includes no breaking changes and backward compatibility on version upgrades. \
@ -47,6 +47,7 @@ I want this project to move forward.
- Reworked shutdown, restoration and cleanup logic.
- Rename `Index` to `Id` in `pueue status` to free up screen space.
- Remove `Exitcode` column in `pueue status` and include exitcode into `Failed` status to free up screen space.
- You can no longer remove groups, if there are still tasks assigned to that group.
### Datastructures

View file

@ -43,6 +43,13 @@ pub fn group(message: GroupMessage, sender: &Sender<Message>, state: &SharedStat
return create_failure_message(format!("You cannot delete the default group"));
}
// Make sure there are no tasks in that group.
if state.tasks.iter().any(|(_, task)| task.group == group) {
return create_failure_message(format!(
"You cannot remove a group, if there're still tasks in it."
));
}
// Propagate the message to the TaskHandler, which is responsible for actually
// manipulating our internal data
let result = sender.send(Message::Group(GroupMessage::Remove(group.clone())));

View file

@ -37,7 +37,14 @@ impl TaskHandler {
error!("Group \"{}\" to be remove doesn't exists", group);
}
if !state.groups.contains_key(&group) {}
// Make sure there are no tasks in that group.
if state.tasks.iter().any(|(_, task)| task.group == group) {
error!(
"Tried to remove group \"{}\", while it still contained tasks.",
group
);
return;
}
if let Err(error) = state.remove_group(&group) {
error!("Error while removing group: \"{}\"", error);

View file

@ -46,7 +46,7 @@ async fn test_cannot_delete_default() -> Result<()> {
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
/// Users cannot delete the default group.
/// Users cannot delete a non-existing group.
async fn test_cannot_delete_non_existing() -> Result<()> {
let (settings, tempdir) = base_setup()?;
let shared = &settings.shared;
@ -57,3 +57,24 @@ async fn test_cannot_delete_non_existing() -> Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
/// Groups with tasks shouldn't be able to be removed.
async fn test_cannot_delete_group_with_tasks() -> Result<()> {
let (settings, tempdir) = base_setup()?;
let shared = &settings.shared;
let _pid = boot_daemon(tempdir.path())?;
// Add a new group
let add_message = Message::Group(GroupMessage::Add("testgroup".to_string()));
assert_success(send_message(shared, add_message.clone()).await?);
// Add a task
assert_success(fixtures::add_task_to_group(shared, "sleep 10", "testgroup").await?);
// We shouldn't be capable of removing that group
let pause_message = Message::Group(GroupMessage::Remove("testgroup".to_string()));
assert_failure(send_message(shared, pause_message).await?);
Ok(())
}