From f90034ed75f047584316021bafc1ddf7644cd476 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Fri, 31 Jan 2025 12:55:43 +0100 Subject: [PATCH] Python: close files after project generation GitOrigin-RevId: 4d166e871d6be6eba86efbf9d72bf5585035ca61 --- ...python.community.junit5Tests.framework.iml | 1 + .../intellij/python/junit5Tests/TestTools.kt | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/python/junit5Tests-framework/intellij.python.community.junit5Tests.framework.iml b/python/junit5Tests-framework/intellij.python.community.junit5Tests.framework.iml index d2619ec7f59b..b658b3b0829a 100644 --- a/python/junit5Tests-framework/intellij.python.community.junit5Tests.framework.iml +++ b/python/junit5Tests-framework/intellij.python.community.junit5Tests.framework.iml @@ -31,5 +31,6 @@ + \ No newline at end of file diff --git a/python/junit5Tests-framework/src/com/intellij/python/junit5Tests/TestTools.kt b/python/junit5Tests-framework/src/com/intellij/python/junit5Tests/TestTools.kt index b135860b230a..5f8fcbd0f6da 100644 --- a/python/junit5Tests-framework/src/com/intellij/python/junit5Tests/TestTools.kt +++ b/python/junit5Tests-framework/src/com/intellij/python/junit5Tests/TestTools.kt @@ -1,10 +1,17 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.python.junit5Tests import com.intellij.execution.configurations.PathEnvironmentVariableUtil +import com.intellij.openapi.application.EDT +import com.intellij.openapi.application.writeIntentReadAction +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.fileEditor.impl.EditorHistoryManager +import com.intellij.openapi.project.Project import com.intellij.openapi.util.SystemInfoRt import com.jetbrains.python.PythonBinary import com.jetbrains.python.Result +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import org.junit.jupiter.api.Assertions import kotlin.io.path.Path @@ -28,4 +35,18 @@ fun Result<*, *>.assertFail() { is Result.Failure -> Assertions.assertNotNull(this.error, "No error") is Result.Success -> Assertions.fail("Unexpected success: ${this.result}") } +} + +/** + * Close all files opened in [project]. Might be useful to call in `finally` or in [org.junit.jupiter.api.AfterEach] + */ +suspend fun closeAllFiles(project: Project): Unit = withContext(Dispatchers.EDT) { + writeIntentReadAction { + FileEditorManager.getInstance(project).apply { + for (openFile in openFiles) { + closeFile(openFile) + } + } + } + EditorHistoryManager.getInstance(project).removeAllFiles() } \ No newline at end of file