Python: close files after project generation

GitOrigin-RevId: 4d166e871d6be6eba86efbf9d72bf5585035ca61
This commit is contained in:
Ilya.Kazakevich
2025-01-31 13:31:06 +00:00
committed by intellij-monorepo-bot
parent d9d56054c0
commit f90034ed75
2 changed files with 23 additions and 1 deletions
@@ -31,5 +31,6 @@
<orderEntry type="module" module-name="intellij.python.community.plugin" scope="RUNTIME" />
<orderEntry type="module" module-name="intellij.community.wintools" scope="TEST" />
<orderEntry type="module" module-name="intellij.python.community.impl.poetry" />
<orderEntry type="module" module-name="intellij.platform.ide.impl" scope="TEST" />
</component>
</module>
@@ -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()
}