Skip to content

Commit

Permalink
fix cancellation of cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
zhislin committed Oct 11, 2024
1 parent 66cdabe commit cdebe59
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.jaksonlin.pitestintellij.commands


class CommandCancellationException(message: String) : Exception(message) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ class PrepareEnvironmentCommand(project: Project, context: PitestContext) : Pite
private fun collectTargetClassThatWeTest(sourceRoots:List<String>) {
// The user input dialog and file operations don't need to be in ReadAction
val targetClass = showInputDialog("Please enter the name of the class that you want to test", "Enter target class")
// when user input content but cancel, targetClass is not null we should break the process
if (targetClass.isNullOrBlank()) {
return
throw CommandCancellationException("User cancelled the operation")
}
val targetClassInfo = FileUtils.findTargetClassFile(sourceRoots, targetClass)
if (targetClassInfo == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.github.jaksonlin.pitestintellij.services

import com.github.jaksonlin.pitestintellij.commands.*
import com.github.jaksonlin.pitestintellij.context.PitestContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.ui.Messages

@Service(Service.Level.APP)
class PitestService {
Expand All @@ -23,9 +25,22 @@ class PitestService {

object : Task.Backgroundable(targetProject, "Running pitest", true) {
override fun run(indicator: ProgressIndicator) {
for (command in commands) {
if (indicator.isCanceled) { break }
command.execute()
try {
for (command in commands) {
if (indicator.isCanceled) {
Messages.showInfoMessage("Pitest run was canceled", "Canceled")
break
}
command.execute()
}
} catch(e: CommandCancellationException) {
ApplicationManager.getApplication().invokeLater {
Messages.showInfoMessage("Pitest run was canceled", "Canceled")
}
} catch (e: Exception) {
ApplicationManager.getApplication().invokeLater {
Messages.showErrorDialog("Error executing Pitest command: ${e.message}", "Error")
}
}
}
}.queue()
Expand Down

0 comments on commit cdebe59

Please sign in to comment.