Skip to content

Commit

Permalink
Adding Record and Delete snapshots for a module from Project View
Browse files Browse the repository at this point in the history
  • Loading branch information
thsaravana committed Mar 13, 2024
1 parent fdc402b commit c062d36
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.2

- Record and Delete snapshots for a test class or package from the Project View under the More Run/Debug menu
- Record and Delete snapshots for a test class, package or module from the Project View under the More Run/Debug menu

## 1.1

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Features
- View previously-recorded golden snapshots for the currently opened test class
- View golden snapshots of the current focussed test method
- View failure diffs for the current test class or method
- Record, Verify and Delete snapshots for individual tests or for entire test class
- Record, Verify and Delete snapshots for individual tests or for entire test class, package or module
- Zoom options for Actual Size and Fit to Window
- Fully supported for test files written in Java or Kotlin

Expand Down
21 changes: 5 additions & 16 deletions src/main/kotlin/com/getyourguide/paparazzi/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.getyourguide.paparazzi

import com.getyourguide.paparazzi.service.Snapshot
import com.intellij.execution.executors.DefaultRunExecutor
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.editor.CaretModel
import com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings
import com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode
import com.intellij.openapi.externalSystem.task.TaskCallback
import com.intellij.openapi.externalSystem.util.ExternalSystemUtil
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.TextEditor
import com.intellij.openapi.project.Project
Expand All @@ -25,7 +20,6 @@ import com.intellij.util.concurrency.AppExecutorUtil
import org.jetbrains.kotlin.idea.util.projectStructure.getModule
import org.jetbrains.kotlin.idea.util.projectStructure.getModuleDir
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.plugins.gradle.util.GradleConstants
import org.jetbrains.uast.UClass
import org.jetbrains.uast.UMethod
import org.jetbrains.uast.getContainingUFile
Expand Down Expand Up @@ -67,17 +61,12 @@ internal fun VirtualFile.methods(project: Project): List<String> {
}

internal fun Project.modulePath(file: VirtualFile): String? {
basePath?.let { projectPath ->
val relativePath = FileUtil.getRelativePath(projectPath, file.path, File.separatorChar)
val moduleName = relativePath?.split(File.separator)?.firstOrNull()
if (moduleName != null) {
return projectPath + File.separator + moduleName
return modules.asSequence().map { it.getModuleDir() }.firstOrNull { file.path.startsWith(it) }
?: basePath?.let { projectPath ->
val relativePath = FileUtil.getRelativePath(projectPath, file.path, File.separatorChar)
val moduleName = relativePath?.split(File.separator)?.firstOrNull()
if (moduleName != null) projectPath + File.separator + moduleName else null
}
}
return modules
.asSequence()
.map { it.getModuleDir() }
.firstOrNull { file.path.startsWith(it) }
}

internal inline fun <reified T> nonBlocking(crossinline asyncAction: () -> T, crossinline uiThreadAction: (T) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiRecursiveElementVisitor
Expand All @@ -18,7 +19,8 @@ abstract class GroupAction(name: String, icon: Icon) : AnAction(name, null, icon

override fun update(e: AnActionEvent) {
super.update(e)
e.presentation.isVisible = getPaparazziClass(e) != null || getPaparazziDirectory(e) != null
e.presentation.isVisible =
getPaparazziClass(e) != null || getPaparazziDirectory(e) != null || getPaparazziModule(e) != null
}

override fun getActionUpdateThread() = ActionUpdateThread.BGT
Expand All @@ -42,6 +44,12 @@ abstract class GroupAction(name: String, icon: Icon) : AnAction(name, null, icon
return if (found) psiDirectory else null
}

protected fun getPaparazziModule(e: AnActionEvent): PsiDirectory? {
val psiDirectory = LangDataKeys.IDE_VIEW.getData(e.dataContext)?.directories?.firstOrNull() ?: return null
ModuleUtilCore.findModuleForPsiElement(psiDirectory) ?: return null
return psiDirectory
}

private fun PsiDirectory.isPackage(): Boolean {
return getPackage()?.qualifiedName?.isNotEmpty() == true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ class GroupDeleteAction : GroupAction(ACTION_NAME, AllIcons.Actions.GC) {
val snapshots = fileInfo.allSnapshots()
val files = snapshots.map { it.file }
deleteSnapshots(project, files)
} else {
val psiDirectory = getPaparazziDirectory(e)
if (psiDirectory != null) {
val fileInfoList = getPaparazziFileInfo(psiDirectory, project)
val snapshots = fileInfoList.flatMap { it.allSnapshots() }.map { it.file }
deleteSnapshots(project, snapshots)
}
return
}
val psiDirectory = getPaparazziDirectory(e)
if (psiDirectory != null) {
val fileInfoList = getPaparazziFileInfo(psiDirectory, project)
val snapshots = fileInfoList.flatMap { it.allSnapshots() }.map { it.file }
deleteSnapshots(project, snapshots)
return
}
val module = getPaparazziModule(e)
if (module != null) {
val fileInfoList = getPaparazziFileInfo(module, project)
val snapshots = fileInfoList.flatMap { it.allSnapshots() }.map { it.file }
deleteSnapshots(project, snapshots)
return
}

}

private fun getPaparazziFileInfo(psiDirectory: PsiDirectory, project: Project): List<FileInfo> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ class GroupRecordAction : GroupAction(ACTION_NAME, AllIcons.Debugger.Db_set_brea
val psiClass = uClass.javaPsi
val testName = getQualifiedTestName(psiClass, null)
runRecordTask(project, testName, modulePath, RecordTaskCallback(project, psiClass, null))
} else {
val psiDirectory = getPaparazziDirectory(e)
if (psiDirectory != null) {
val modulePath = project.modulePath(psiDirectory.virtualFile) ?: return
val psiPackage = psiDirectory.getPackage() ?: return
val testName = psiPackage.qualifiedName + ".*"
runRecordTask(project, testName, modulePath)
}
return
}
val psiDirectory = getPaparazziDirectory(e)
if (psiDirectory != null) {
val modulePath = project.modulePath(psiDirectory.virtualFile) ?: return
val psiPackage = psiDirectory.getPackage() ?: return
val testName = psiPackage.qualifiedName + ".*"
runRecordTask(project, testName, modulePath)
return
}
val module = getPaparazziModule(e)
if (module != null) {
val modulePath = project.modulePath(module.virtualFile) ?: return
runRecordTask(project, null, modulePath)
return
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
<li>View previously-recorded golden snapshots for the current test file opened in the editor</li>
<li>View golden snapshots of the current focussed test method</li>
<li>View failure diffs for the current test class or method</li>
<li>Record, Verify and Delete snapshots for individual tests or for entire test class</li>
<li>Record, Verify and Delete snapshots for individual tests or for entire test class, package or module</li>
<li>Zoom options for Actual Size and Fit to Window</li>
<li>Fully supported for test files written in Java or Kotlin</li>
</ul>
]]></description>
<change-notes><![CDATA[
<h3>1.2</h3>
<ul>
<li>Record and Delete snapshots for a test class or package from the Project View under the More Run/Debug menu</li>
<li>Record and Delete snapshots for a test class, package or module from the Project View under the More Run/Debug menu</li>
</ul>
<h3>1.1</h3>
<ul>
Expand Down

0 comments on commit c062d36

Please sign in to comment.