if there's a way to create or get an instance of CommitWorkflowHandler in the test code.

Answered

Hello,

I am currently developing a plugin that automatically generates commit messages using ChatGPT for the Changes checked in the commit tab.

During the implementation process, I've added a feature that displays a warning notification if the plugin's icon is clicked without any Changes being checked.

To create an integration test for this feature, I wanted to set up a DataContext in the test, referring to this post

However, I couldn't find a way to insert an instance of AbstractCommitWorkflowHandler into the DataContext.

Is there any way to use an actual implementation of VcsDataKeys.COMMIT_WORKFLOW_HANDLER in the DataContext?

I've already written all the unit tests, but I'd like to create test code that integrates with the actual commit workflow.

Thank you for your assistance.

here's my test code

class NotificationTest : BasePlatformTestCase() {
    private lateinit var action: GenerateCommitMessageAction
    private val notifiedMessages = mutableListOf<Triple<String, String, NotificationType>>()

    override fun setUp() {
        super.setUp()
        action = GenerateCommitMessageAction()


        project.messageBus.connect().subscribe(Notifications.TOPIC, object : Notifications {
            override fun notify(notification: Notification) {
                println("Notification received: ${notification.title}, ${notification.content}")
                notifiedMessages.add(Triple(notification.title, notification.content, notification.type))
            }
        })
    }

    fun testNoChangesDetected() {
    	//how can i get instance of CommitWorkflowHandler?
        val context = SimpleDataContext.getProjectContext(project)
        val event = AnActionEvent.createFromAnAction(
            action,
            null,
            ActionPlaces.VCS_HISTORY_TOOLBAR_PLACE,
            context
        )

        WriteCommandAction.runWriteCommandAction(project) {
            println("Action performed")
            action.actionPerformed(event)
        }
        println("After action performed")


        PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()



        println("Captured notifications: ${notifiedMessages.size}")
        assertTruenotifiedMessages.isNotEmpty())

        notifiedMessages.forEach { (title, content, type) ->
            println("Received notification: $title, $content, $type")
        }


        val (title, content, type) = notifiedMessages.last()
        assertEquals("MessageMaker: No Changes Detected", title)
        assertEquals("There are no changes to generate a commit message for.", content)
        assertEquals(NotificationType.WARNING, type)
    }

    override fun tearDown() {
        notifiedMessages.clear()
        super.tearDown()
    }
}

 

0
3 comments

Hi,

There are a couple of CommitWorkflowHandler implementations. I also don't understand your test scenario: what implementation is expected, how you use it (if you use it explicitly), in which place it is called, etc. Please clarify in more detail.

1
@Service(Service.Level.PROJECT)
class CommitManager {
    fun getCheckedChanges(event: AnActionEvent): List<Change> {
        val handler = getHandler(event) ?: throw RuntimeException()
//        val handler = getHandler(event) ?: throw NoChangesException()

        CustomLogger.info(handler.toString())

        return handler.ui.getIncludedChanges()
            .takeIf { it.isNotEmpty() }
            ?: throw NoChangesException()
    }

    private fun getHandler(event: AnActionEvent) =
        event.getData(VcsDataKeys.COMMIT_WORKFLOW_HANDLER) as? AbstractCommitWorkflowHandler<*, *>
}


Oh, I understand. Let me explain in detail. Here's my test scenario:

  1. GenerateCommitMessageAction is the action triggered when the "this icon" in the image is clicked. This action performs the task of generating a commit message based on the checked Changes.
  2. In the current test, I'm trying to verify the Notification that occurs when the icon is clicked without any Changes being checked.
  3. The current implementation to retrieve checked Changes involves getting AbstractCommitWorkflowHandler from the DataContext and then calling handler.ui.getIncludedChanges(), as shown in the code above.
  4. My problem is that I'm unable to retrieve AbstractCommitWorkflowHandler in the test environment.
  5. From what I've found, I know that SimpleDataContext can be used to add necessary data, but I couldn't find a way to create an instance of AbstractCommitWorkflowHandler.

For these reasons, I'm curious about how to configure a DataContext containing AbstractCommitWorkflowHandler in the test environment. 

thanks for help.

0

Hi,

The simplest approach would be to use a mock. Testing a “real” implementation is complex here, because, to get data from “real” implementation, a lot of UI classes would need to be initialized, which requires a lot of non-trivial code required for waiting for the UI to load.

0

Please sign in to leave a comment.