<commit_before>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.testFramework.rules

import com.intellij.openapi.application.invokeAndWaitIfNeeded
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.application.runWriteActionAndWait
import com.intellij.openapi.module.EmptyModuleType
import com.intellij.openapi.module.ModifiableModuleModel
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.ex.ProjectManagerEx
import com.intellij.openapi.projectRoots.*
import com.intellij.openapi.rd.attach
import com.intellij.openapi.roots.ModuleRootModificationUtil
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.roots.impl.libraries.LibraryEx
import com.intellij.openapi.roots.libraries.Library
import com.intellij.openapi.roots.libraries.LibraryTable
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar
import com.intellij.openapi.util.Ref
import com.intellij.openapi.util.io.systemIndependentPath
import com.intellij.testFramework.DisposableRule
import com.intellij.testFramework.RuleChain
import com.intellij.testFramework.createHeavyProject
import com.intellij.testFramework.runInEdtAndWait
import org.junit.rules.ExternalResource
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import java.io.File

class ProjectModelRule : TestRule {
  val baseProjectDir = TempDirectory()
  private val projectDelegate = lazy { val project = createHeavyProject(baseProjectDir.root.toPath())
    invokeAndWaitIfNeeded {
      ProjectManagerEx.getInstanceEx().openTestProject(project)
    }
    project
  }
  private val disposableRule = DisposableRule()
  val project by projectDelegate
  private val closeProject = object : ExternalResource() {
    override fun after() {
      if (projectDelegate.isInitialized()) {
        runInEdtAndWait {
          ProjectManagerEx.getInstanceEx().forceCloseProject(project)
        }
      }
    }
  }
  private val ruleChain = RuleChain(baseProjectDir, closeProject, disposableRule)

  override fun apply(base: Statement, description: Description): Statement {
    return ruleChain.apply(base, description)
  }

  fun createModule(name: String = "module"): Module {
    val imlFile = File(baseProjectDir.root, "$name/$name.iml")
    val manager = moduleManager
    return runWriteActionAndWait {
      manager.newModule(imlFile.systemIndependentPath, EmptyModuleType.EMPTY_MODULE)
    }
  }

  fun createModule(name: String, moduleModel: ModifiableModuleModel): Module {
    val imlFile = baseProjectDir.newFile("$name/$name.iml")
    return moduleModel.newModule(imlFile.systemIndependentPath, EmptyModuleType.EMPTY_MODULE)
  }


  fun createSdk(name: String = "sdk"): Sdk {
    return ProjectJdkTable.getInstance().createSdk(name, sdkType)
  }

  fun addSdk(sdk: Sdk, setup: (SdkModificator) -> Unit = {}): Sdk {
    runWriteActionAndWait {<commit_msg>[project model tests] always initialize Project in ProjectModelRule before starting tests<commit_after>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.testFramework.rules

import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.application.runWriteActionAndWait
import com.intellij.openapi.module.EmptyModuleType
import com.intellij.openapi.module.ModifiableModuleModel
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ex.ProjectManagerEx
import com.intellij.openapi.projectRoots.*
import com.intellij.openapi.rd.attach
import com.intellij.openapi.roots.ModuleRootModificationUtil
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.roots.impl.libraries.LibraryEx
import com.intellij.openapi.roots.libraries.Library
import com.intellij.openapi.roots.libraries.LibraryTable
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar
import com.intellij.openapi.util.Ref
import com.intellij.openapi.util.io.systemIndependentPath
import com.intellij.testFramework.DisposableRule
import com.intellij.testFramework.RuleChain
import com.intellij.testFramework.createHeavyProject
import com.intellij.testFramework.runInEdtAndWait
import org.junit.rules.ExternalResource
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import java.io.File

class ProjectModelRule : TestRule {
  val baseProjectDir = TempDirectory()
  private val disposableRule = DisposableRule()
  lateinit var project: Project
  private val projectResource = object : ExternalResource() {
    override fun before() {
      project = createHeavyProject(baseProjectDir.root.toPath())
      runInEdtAndWait {
        ProjectManagerEx.getInstanceEx().openTestProject(project)
      }
    }

    override fun after() {
      runInEdtAndWait {
        ProjectManagerEx.getInstanceEx().forceCloseProject(project)
      }
    }
  }
  private val ruleChain = RuleChain(baseProjectDir, projectResource, disposableRule)

  override fun apply(base: Statement, description: Description): Statement {
    return ruleChain.apply(base, description)
  }

  fun createModule(name: String = "module"): Module {
    val imlFile = File(baseProjectDir.root, "$name/$name.iml")
    val manager = moduleManager
    return runWriteActionAndWait {
      manager.newModule(imlFile.systemIndependentPath, EmptyModuleType.EMPTY_MODULE)
    }
  }

  fun createModule(name: String, moduleModel: ModifiableModuleModel): Module {
    val imlFile = baseProjectDir.newFile("$name/$name.iml")
    return moduleModel.newModule(imlFile.systemIndependentPath, EmptyModuleType.EMPTY_MODULE)
  }


  fun createSdk(name: String = "sdk"): Sdk {
    return ProjectJdkTable.getInstance().createSdk(name, sdkType)
  }

  fun addSdk(sdk: Sdk, setup: (SdkModificator) -> Unit = {}): Sdk {
    runWriteActionAndWait {
{'fraction_non_alphanumeric': 0.0877221324717286, 'fraction_numerical': 0.0032310177705977385, 'mean_word_length': 5.147964250248262, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5220439', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1834, 'n_words': 359}