[Test] Move java generation utils to :compiler:tests-compiler-utils module

This commit is contained in:
Dmitriy Novozhilov
2020-12-23 11:22:19 +03:00
committed by TeamCityServer
parent cb7b1652e7
commit 8689fc43cd
15 changed files with 78 additions and 97 deletions
@@ -8,47 +8,9 @@ package org.jetbrains.kotlin
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.junit.Assert
import java.io.File
import java.io.PrintStream
import java.io.PrintWriter
import java.util.regex.Matcher
import java.util.regex.Pattern
enum class TestsExceptionType(val postfix: String) {
COMPILER_ERROR("compiler"),
COMPILETIME_ERROR("compiletime"),
RUNTIME_ERROR("runtime"),
INFRASTRUCTURE_ERROR("infrastructure");
companion object {
private val map = values().associateBy(TestsExceptionType::postfix)
fun fromValue(type: String) = map[type]
}
}
sealed class TestsError(val original: Throwable, val type: TestsExceptionType) : Error() {
override fun toString(): String = original.toString()
override fun getStackTrace(): Array<out StackTraceElement> = original.stackTrace
override fun initCause(cause: Throwable?): Throwable = original.initCause(cause)
override val cause: Throwable? get() = original.cause
// This function is called in the constructor of Throwable, where original is not yet initialized
override fun fillInStackTrace(): Throwable? = @Suppress("UNNECESSARY_SAFE_CALL") original?.fillInStackTrace()
override fun setStackTrace(stackTrace: Array<out StackTraceElement>?) {
original.stackTrace = stackTrace
}
override fun printStackTrace() = original.printStackTrace()
override fun printStackTrace(s: PrintStream?) = original.printStackTrace(s)
override fun printStackTrace(s: PrintWriter?) = original.printStackTrace(s)
}
class TestsCompilerError(original: Throwable) : TestsError(original, TestsExceptionType.COMPILER_ERROR)
class TestsInfrastructureError(original: Throwable) : TestsError(original, TestsExceptionType.INFRASTRUCTURE_ERROR)
class TestsCompiletimeError(original: Throwable) : TestsError(original, TestsExceptionType.COMPILETIME_ERROR)
class TestsRuntimeError(original: Throwable) : TestsError(original, TestsExceptionType.RUNTIME_ERROR)
private enum class ExceptionType {
ANALYZING_EXPRESSION,
UNKNOWN
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.checkers
import org.jetbrains.kotlin.codegen.CodegenTestUtil
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.util.JUnit4Assertions
import org.jetbrains.kotlin.test.util.KtTestUtil
import java.io.File
@@ -34,9 +35,10 @@ abstract class AbstractForeignAnnotationsNoAnnotationInClasspathTest : AbstractF
val additionalClasspath = (foreignAnnotations + testAnnotations).map { it.path }
CodegenTestUtil.compileJava(
CodegenTestUtil.findJavaSourcesInDirectory(javaFilesDir),
additionalClasspath, emptyList(),
compiledJavaPath
CodegenTestUtil.findJavaSourcesInDirectory(javaFilesDir),
additionalClasspath, emptyList(),
compiledJavaPath,
JUnit4Assertions
)
return listOf(compiledJavaPath) + testAnnotations
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoot
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.test.util.JUnit4Assertions
import java.io.File
abstract class AbstractBlackBoxAgainstJavaCodegenTest : AbstractBlackBoxCodegenTest() {
@@ -35,6 +36,7 @@ abstract class AbstractBlackBoxAgainstJavaCodegenTest : AbstractBlackBoxCodegenT
jvmTargets.firstOrNull(),
enablePreview,
),
JUnit4Assertions
)
}
@@ -33,8 +33,6 @@ import org.jetbrains.kotlin.psi.KtExpression;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfoFactory;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactoryImpl;
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
import org.jetbrains.kotlin.resolve.scopes.ImportingScope;
@@ -56,13 +56,10 @@ import org.jetbrains.kotlin.test.util.StringUtilsKt;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import org.junit.Assert;
import javax.tools.*;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -22,6 +22,7 @@ dependencies {
testCompile(project(":compiler:cli"))
testCompile(project(":compiler:cli-js"))
testCompile(project(":compiler:serialization"))
testCompile(project(":compiler:fir:entrypoint"))
testCompile(projectTests(":compiler:test-infrastructure-utils"))
testCompile(project(":kotlin-preloader"))
testCompileOnly(intellijCoreDep()) { includeJars("intellij-core") }
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin
import java.io.PrintStream
import java.io.PrintWriter
enum class TestsExceptionType(val postfix: String) {
COMPILER_ERROR("compiler"),
COMPILETIME_ERROR("compiletime"),
RUNTIME_ERROR("runtime"),
INFRASTRUCTURE_ERROR("infrastructure");
companion object {
private val map = values().associateBy(TestsExceptionType::postfix)
fun fromValue(type: String) = map[type]
}
}
sealed class TestsError(val original: Throwable, val type: TestsExceptionType) : Error() {
override fun toString(): String = original.toString()
override fun getStackTrace(): Array<out StackTraceElement> = original.stackTrace
override fun initCause(cause: Throwable?): Throwable = original.initCause(cause)
override val cause: Throwable? get() = original.cause
// This function is called in the constructor of Throwable, where original is not yet initialized
override fun fillInStackTrace(): Throwable? = @Suppress("UNNECESSARY_SAFE_CALL") original?.fillInStackTrace()
override fun setStackTrace(stackTrace: Array<out StackTraceElement>?) {
original.stackTrace = stackTrace
}
override fun printStackTrace() = original.printStackTrace()
override fun printStackTrace(s: PrintStream?) = original.printStackTrace(s)
override fun printStackTrace(s: PrintWriter?) = original.printStackTrace(s)
}
class TestsCompilerError(original: Throwable) : TestsError(original, TestsExceptionType.COMPILER_ERROR)
class TestsInfrastructureError(original: Throwable) : TestsError(original, TestsExceptionType.INFRASTRUCTURE_ERROR)
class TestsCompiletimeError(original: Throwable) : TestsError(original, TestsExceptionType.COMPILETIME_ERROR)
class TestsRuntimeError(original: Throwable) : TestsError(original, TestsExceptionType.RUNTIME_ERROR)
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen;
@@ -24,7 +13,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.Assertions;
import org.jetbrains.kotlin.test.JvmCompilationUtils;
import org.jetbrains.kotlin.test.KtAssert;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import org.jetbrains.kotlin.utils.StringsKt;
@@ -37,8 +28,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class CodegenTestUtil {
private CodegenTestUtil() {
}
@@ -58,7 +47,7 @@ public class CodegenTestUtil {
catch (InvocationTargetException ex) {
caught = exceptionClass.isInstance(ex.getTargetException());
}
assertTrue(caught);
KtAssert.assertTrue(String.format("Exception of class %s must be thrown", exceptionClass.getName()), caught);
}
@NotNull
@@ -83,11 +72,12 @@ public class CodegenTestUtil {
public static File compileJava(
@NotNull List<String> fileNames,
@NotNull List<String> additionalClasspath,
@NotNull List<String> additionalOptions
@NotNull List<String> additionalOptions,
@NotNull Assertions assertions
) {
try {
File directory = KtTestUtil.tmpDir("java-classes");
compileJava(fileNames, additionalClasspath, additionalOptions, directory);
compileJava(fileNames, additionalClasspath, additionalOptions, directory, assertions);
return directory;
}
catch (IOException e) {
@@ -99,11 +89,12 @@ public class CodegenTestUtil {
@NotNull List<String> fileNames,
@NotNull List<String> additionalClasspath,
@NotNull List<String> additionalOptions,
@NotNull File outDirectory
@NotNull File outDirectory,
@NotNull Assertions assertions
) {
try {
List<String> options = prepareJavacOptions(additionalClasspath, additionalOptions, outDirectory);
KotlinTestUtils.compileJavaFiles(CollectionsKt.map(fileNames, File::new), options);
JvmCompilationUtils.compileJavaFiles(CollectionsKt.map(fileNames, File::new), options, assertions);
}
catch (IOException e) {
throw ExceptionUtilsKt.rethrow(e);
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.lazy
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.backend.common.output.OutputFile;
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.util.JUnit4Assertions;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.utils.StringsKt;
import org.jetbrains.org.objectweb.asm.ClassReader;
@@ -158,7 +159,8 @@ public class OuterClassGenTest extends CodegenTestCase {
File javaOut = CodegenTestUtil.compileJava(
Collections.singletonList(KtTestUtil.getTestDataPathBase() + "/codegen/" + getPrefix() + "/" + testDataFile + ".java"),
Collections.emptyList(),
Collections.emptyList()
Collections.emptyList(),
JUnit4Assertions.INSTANCE
);
String javaClassPath = javaClassName.replace('.', File.separatorChar) + ".class";
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.KotlinBaseTest.TestFile
import org.jetbrains.kotlin.test.MockLibraryUtil
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import org.jetbrains.kotlin.test.util.JUnit4Assertions
import java.io.File
class DebuggerTestCompilerFacility(
@@ -109,7 +110,8 @@ class DebuggerTestCompilerFacility(
java.map { File(srcDir, it.name).absolutePath },
mavenArtifacts + classesDir.absolutePath,
listOf("-g"),
classesDir
classesDir,
JUnit4Assertions
)
}
}
@@ -152,7 +154,8 @@ class DebuggerTestCompilerFacility(
java.map { File(srcDir, it.name).absolutePath },
getClasspath(module) + listOf(classesDir.absolutePath),
listOf("-g"),
classesDir
classesDir,
JUnit4Assertions
)
}
@@ -252,4 +255,4 @@ private fun splitByLanguage(files: List<TestFile>): TestFilesByLanguage {
}
return TestFilesByLanguage(kotlin = kotlin, java = java, resources = resources)
}
}