From ab4dce12269f85c48224ec4627dcdbea66524477 Mon Sep 17 00:00:00 2001 From: "Maxim.Kolmakov" Date: Fri, 22 Mar 2024 19:06:01 +0100 Subject: [PATCH] Add command to assert current file name GitOrigin-RevId: 9c7f128eb50036c0613f6ec212bd534fc0c0bb56 --- .../commands/generalCommandChain.kt | 7 ++++ .../BaseCommandProvider.java | 3 +- .../commands/AssertCurrentFileCommand.kt | 37 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/commands/AssertCurrentFileCommand.kt diff --git a/plugins/performanceTesting/commands-model/src/com/intellij/tools/ide/performanceTesting/commands/generalCommandChain.kt b/plugins/performanceTesting/commands-model/src/com/intellij/tools/ide/performanceTesting/commands/generalCommandChain.kt index ee4175742371..d2a749c1d4da 100644 --- a/plugins/performanceTesting/commands-model/src/com/intellij/tools/ide/performanceTesting/commands/generalCommandChain.kt +++ b/plugins/performanceTesting/commands-model/src/com/intellij/tools/ide/performanceTesting/commands/generalCommandChain.kt @@ -1033,4 +1033,11 @@ fun T.disableKotlinNotification(): T = apply { */ fun T.assertCaretPosition(line: Int, column: Int): T = apply { addCommand("${CMD_PREFIX}assertCaretPosition $line $column") +} + +/** + * Assert the current file in editor. + */ +fun T.assertCurrentFile(name: String): T = apply { + addCommand("${CMD_PREFIX}assertCurrentFile $name") } \ No newline at end of file diff --git a/plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/BaseCommandProvider.java b/plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/BaseCommandProvider.java index 09f7c5773780..988efad31cca 100644 --- a/plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/BaseCommandProvider.java +++ b/plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/BaseCommandProvider.java @@ -112,7 +112,8 @@ public final class BaseCommandProvider implements CommandProvider { Map.entry(AddContentRootToModule.PREFIX, AddContentRootToModule::new), Map.entry(DisableKotlinNotificationCommand.PREFIX, DisableKotlinNotificationCommand::new), Map.entry(RequestHeavyScanningOnNextStartCommand.PREFIX, RequestHeavyScanningOnNextStartCommand::new), - Map.entry(AssertCaretPositionCommand.PREFIX, AssertCaretPositionCommand::new) + Map.entry(AssertCaretPositionCommand.PREFIX, AssertCaretPositionCommand::new), + Map.entry(AssertCurrentFileCommand.PREFIX, AssertCurrentFileCommand::new) ); } } diff --git a/plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/commands/AssertCurrentFileCommand.kt b/plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/commands/AssertCurrentFileCommand.kt new file mode 100644 index 000000000000..9d6f6ee97d19 --- /dev/null +++ b/plugins/performanceTesting/core/src/com/jetbrains/performancePlugin/commands/AssertCurrentFileCommand.kt @@ -0,0 +1,37 @@ +package com.jetbrains.performancePlugin.commands + +import com.intellij.openapi.application.readAction +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.ui.playback.PlaybackContext +import org.jetbrains.annotations.NonNls + +/** + * Assert the current file in editor. + * + * Syntax: `%assertCurrentFile ` + * + * Example: `%assertCurrentFile AssertCurrentFileCommand.kt` + */ +class AssertCurrentFileCommand(text: String, line: Int) : PerformanceCommandCoroutineAdapter(text, line) { + companion object { + const val NAME: @NonNls String = "assertCurrentFile" + const val PREFIX: String = CMD_PREFIX + NAME + } + + override suspend fun doExecute(context: PlaybackContext) { + val fileName = extractCommandArgument(PREFIX).split(" ").filterNot { it.trim() == "" }.singleOrNull() + if (fileName == null) { + throw IllegalArgumentException("File name is not provided") + } + + readAction { + val editor = checkNotNull(FileEditorManager.getInstance(context.project).selectedTextEditor) + val currentFileName = editor.virtualFile.name + if (fileName != currentFileName) { + throw Exception("Current file name is $currentFileName, expected $fileName") + } + } + } + + override fun getName(): String = NAME +} \ No newline at end of file