Skip to content

Commit c062d36

Browse files
committed
Adding Record and Delete snapshots for a module from Project View
1 parent fdc402b commit c062d36

7 files changed

Lines changed: 49 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 1.2
44

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

77
## 1.1
88

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Features
1616
- View previously-recorded golden snapshots for the currently opened test class
1717
- View golden snapshots of the current focussed test method
1818
- View failure diffs for the current test class or method
19-
- Record, Verify and Delete snapshots for individual tests or for entire test class
19+
- Record, Verify and Delete snapshots for individual tests or for entire test class, package or module
2020
- Zoom options for Actual Size and Fit to Window
2121
- Fully supported for test files written in Java or Kotlin
2222

src/main/kotlin/com/getyourguide/paparazzi/Utils.kt

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.getyourguide.paparazzi
22

33
import com.getyourguide.paparazzi.service.Snapshot
4-
import com.intellij.execution.executors.DefaultRunExecutor
54
import com.intellij.openapi.application.ModalityState
65
import com.intellij.openapi.application.ReadAction
76
import com.intellij.openapi.editor.CaretModel
8-
import com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings
9-
import com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode
10-
import com.intellij.openapi.externalSystem.task.TaskCallback
11-
import com.intellij.openapi.externalSystem.util.ExternalSystemUtil
127
import com.intellij.openapi.fileEditor.FileEditor
138
import com.intellij.openapi.fileEditor.TextEditor
149
import com.intellij.openapi.project.Project
@@ -25,7 +20,6 @@ import com.intellij.util.concurrency.AppExecutorUtil
2520
import org.jetbrains.kotlin.idea.util.projectStructure.getModule
2621
import org.jetbrains.kotlin.idea.util.projectStructure.getModuleDir
2722
import org.jetbrains.kotlin.psi.psiUtil.parents
28-
import org.jetbrains.plugins.gradle.util.GradleConstants
2923
import org.jetbrains.uast.UClass
3024
import org.jetbrains.uast.UMethod
3125
import org.jetbrains.uast.getContainingUFile
@@ -67,17 +61,12 @@ internal fun VirtualFile.methods(project: Project): List<String> {
6761
}
6862

6963
internal fun Project.modulePath(file: VirtualFile): String? {
70-
basePath?.let { projectPath ->
71-
val relativePath = FileUtil.getRelativePath(projectPath, file.path, File.separatorChar)
72-
val moduleName = relativePath?.split(File.separator)?.firstOrNull()
73-
if (moduleName != null) {
74-
return projectPath + File.separator + moduleName
64+
return modules.asSequence().map { it.getModuleDir() }.firstOrNull { file.path.startsWith(it) }
65+
?: basePath?.let { projectPath ->
66+
val relativePath = FileUtil.getRelativePath(projectPath, file.path, File.separatorChar)
67+
val moduleName = relativePath?.split(File.separator)?.firstOrNull()
68+
if (moduleName != null) projectPath + File.separator + moduleName else null
7569
}
76-
}
77-
return modules
78-
.asSequence()
79-
.map { it.getModuleDir() }
80-
.firstOrNull { file.path.startsWith(it) }
8170
}
8271

8372
internal inline fun <reified T> nonBlocking(crossinline asyncAction: () -> T, crossinline uiThreadAction: (T) -> Unit) {

src/main/kotlin/com/getyourguide/paparazzi/actions/group/GroupAction.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
55
import com.intellij.openapi.actionSystem.AnAction
66
import com.intellij.openapi.actionSystem.AnActionEvent
77
import com.intellij.openapi.actionSystem.LangDataKeys
8+
import com.intellij.openapi.module.ModuleUtilCore
89
import com.intellij.psi.PsiDirectory
910
import com.intellij.psi.PsiFile
1011
import com.intellij.psi.PsiRecursiveElementVisitor
@@ -18,7 +19,8 @@ abstract class GroupAction(name: String, icon: Icon) : AnAction(name, null, icon
1819

1920
override fun update(e: AnActionEvent) {
2021
super.update(e)
21-
e.presentation.isVisible = getPaparazziClass(e) != null || getPaparazziDirectory(e) != null
22+
e.presentation.isVisible =
23+
getPaparazziClass(e) != null || getPaparazziDirectory(e) != null || getPaparazziModule(e) != null
2224
}
2325

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

47+
protected fun getPaparazziModule(e: AnActionEvent): PsiDirectory? {
48+
val psiDirectory = LangDataKeys.IDE_VIEW.getData(e.dataContext)?.directories?.firstOrNull() ?: return null
49+
ModuleUtilCore.findModuleForPsiElement(psiDirectory) ?: return null
50+
return psiDirectory
51+
}
52+
4553
private fun PsiDirectory.isPackage(): Boolean {
4654
return getPackage()?.qualifiedName?.isNotEmpty() == true
4755
}

src/main/kotlin/com/getyourguide/paparazzi/actions/group/GroupDeleteAction.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,23 @@ class GroupDeleteAction : GroupAction(ACTION_NAME, AllIcons.Actions.GC) {
2626
val snapshots = fileInfo.allSnapshots()
2727
val files = snapshots.map { it.file }
2828
deleteSnapshots(project, files)
29-
} else {
30-
val psiDirectory = getPaparazziDirectory(e)
31-
if (psiDirectory != null) {
32-
val fileInfoList = getPaparazziFileInfo(psiDirectory, project)
33-
val snapshots = fileInfoList.flatMap { it.allSnapshots() }.map { it.file }
34-
deleteSnapshots(project, snapshots)
35-
}
29+
return
30+
}
31+
val psiDirectory = getPaparazziDirectory(e)
32+
if (psiDirectory != null) {
33+
val fileInfoList = getPaparazziFileInfo(psiDirectory, project)
34+
val snapshots = fileInfoList.flatMap { it.allSnapshots() }.map { it.file }
35+
deleteSnapshots(project, snapshots)
36+
return
3637
}
38+
val module = getPaparazziModule(e)
39+
if (module != null) {
40+
val fileInfoList = getPaparazziFileInfo(module, project)
41+
val snapshots = fileInfoList.flatMap { it.allSnapshots() }.map { it.file }
42+
deleteSnapshots(project, snapshots)
43+
return
44+
}
45+
3746
}
3847

3948
private fun getPaparazziFileInfo(psiDirectory: PsiDirectory, project: Project): List<FileInfo> {

src/main/kotlin/com/getyourguide/paparazzi/actions/group/GroupRecordAction.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ class GroupRecordAction : GroupAction(ACTION_NAME, AllIcons.Debugger.Db_set_brea
2424
val psiClass = uClass.javaPsi
2525
val testName = getQualifiedTestName(psiClass, null)
2626
runRecordTask(project, testName, modulePath, RecordTaskCallback(project, psiClass, null))
27-
} else {
28-
val psiDirectory = getPaparazziDirectory(e)
29-
if (psiDirectory != null) {
30-
val modulePath = project.modulePath(psiDirectory.virtualFile) ?: return
31-
val psiPackage = psiDirectory.getPackage() ?: return
32-
val testName = psiPackage.qualifiedName + ".*"
33-
runRecordTask(project, testName, modulePath)
34-
}
27+
return
28+
}
29+
val psiDirectory = getPaparazziDirectory(e)
30+
if (psiDirectory != null) {
31+
val modulePath = project.modulePath(psiDirectory.virtualFile) ?: return
32+
val psiPackage = psiDirectory.getPackage() ?: return
33+
val testName = psiPackage.qualifiedName + ".*"
34+
runRecordTask(project, testName, modulePath)
35+
return
36+
}
37+
val module = getPaparazziModule(e)
38+
if (module != null) {
39+
val modulePath = project.modulePath(module.virtualFile) ?: return
40+
runRecordTask(project, null, modulePath)
41+
return
3542
}
3643
}
3744

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
<li>View previously-recorded golden snapshots for the current test file opened in the editor</li>
1313
<li>View golden snapshots of the current focussed test method</li>
1414
<li>View failure diffs for the current test class or method</li>
15-
<li>Record, Verify and Delete snapshots for individual tests or for entire test class</li>
15+
<li>Record, Verify and Delete snapshots for individual tests or for entire test class, package or module</li>
1616
<li>Zoom options for Actual Size and Fit to Window</li>
1717
<li>Fully supported for test files written in Java or Kotlin</li>
1818
</ul>
1919
]]></description>
2020
<change-notes><![CDATA[
2121
<h3>1.2</h3>
2222
<ul>
23-
<li>Record and Delete snapshots for a test class or package from the Project View under the More Run/Debug menu</li>
23+
<li>Record and Delete snapshots for a test class, package or module from the Project View under the More Run/Debug menu</li>
2424
</ul>
2525
<h3>1.1</h3>
2626
<ul>

0 commit comments

Comments
 (0)