mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-12 00:21:30 +00:00
Add error reporting on the options parsing errors in scripting
also report standard parsing warnings also fix language version test, since it is not possible anymore to use version 1.0
This commit is contained in:
@@ -286,13 +286,13 @@ class ScriptingHostTest : TestCase() {
|
||||
|
||||
@Test
|
||||
fun testCompileOptionsLanguageVersion() {
|
||||
val script = "typealias MyInt = Int\nval x: MyInt = 3"
|
||||
val script = "fun interface FunInterface {\n fun invoke()\n}"
|
||||
val compilationConfiguration1 = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate> {
|
||||
compilerOptions("-language-version", "1.0")
|
||||
compilerOptions("-language-version", "1.3")
|
||||
}
|
||||
val res = BasicJvmScriptingHost().eval(script.toScriptSource(), compilationConfiguration1, null)
|
||||
assertTrue(res is ResultWithDiagnostics.Failure)
|
||||
res.reports.find { it.message.startsWith("The feature \"type aliases\" is only available since language version 1.1") }
|
||||
res.reports.find { it.message.startsWith("The feature \"functional interface conversion\" is only available since language version 1.4") }
|
||||
?: fail("Error report about language version not found. Reported:\n ${res.reports.joinToString("\n ") { it.message }}")
|
||||
}
|
||||
|
||||
@@ -320,6 +320,50 @@ class ScriptingHostTest : TestCase() {
|
||||
assertTrue(res2 is ResultWithDiagnostics.Success)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testErrorOnParsingOptions() {
|
||||
val script = "println(\"Hi\")"
|
||||
|
||||
val compilationConfiguration1 = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate> {
|
||||
compilerOptions("-jvm-target=1.8")
|
||||
}
|
||||
val res1 = BasicJvmScriptingHost().eval(script.toScriptSource(), compilationConfiguration1, null)
|
||||
assertTrue(res1 is ResultWithDiagnostics.Failure)
|
||||
assertNotNull(res1.reports.find { it.message == "Invalid argument: -jvm-target=1.8" })
|
||||
|
||||
val compilationConfiguration2 = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate> {
|
||||
refineConfiguration {
|
||||
beforeCompiling { ctx ->
|
||||
ScriptCompilationConfiguration(ctx.compilationConfiguration) {
|
||||
compilerOptions.append("-jvm-target=1.6")
|
||||
}.asSuccess()
|
||||
}
|
||||
}
|
||||
}
|
||||
val res2 = BasicJvmScriptingHost().eval(script.toScriptSource(), compilationConfiguration2, null)
|
||||
assertTrue(res2 is ResultWithDiagnostics.Failure)
|
||||
assertNotNull(res2.reports.find { it.message == "Invalid argument: -jvm-target=1.6" })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInvalidOptionsWarning() {
|
||||
val script = "1"
|
||||
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate> {
|
||||
compilerOptions("-Xunknown1")
|
||||
refineConfiguration {
|
||||
beforeCompiling { ctx ->
|
||||
ScriptCompilationConfiguration(ctx.compilationConfiguration) {
|
||||
compilerOptions.append("-Xunknown2")
|
||||
}.asSuccess()
|
||||
}
|
||||
}
|
||||
}
|
||||
val res = BasicJvmScriptingHost().eval(script.toScriptSource(), compilationConfiguration, null)
|
||||
assertTrue(res is ResultWithDiagnostics.Success)
|
||||
assertNotNull(res.reports.find { it.message == "Flag is not supported by this version of the compiler: -Xunknown1" })
|
||||
assertNotNull(res.reports.find { it.message == "Flag is not supported by this version of the compiler: -Xunknown2" })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIgnoredOptionsWarning() {
|
||||
val script = "println(\"Hi\")"
|
||||
|
||||
@@ -99,6 +99,8 @@ open class KJvmReplCompilerBase<AnalyzerT : ReplCodeAnalyzerBase> protected cons
|
||||
if (firstFailure != null)
|
||||
return firstFailure
|
||||
|
||||
if (messageCollector.hasErrors()) return failure(messageCollector)
|
||||
|
||||
if (history.isEmpty()) {
|
||||
val updatedConfiguration = ScriptDependenciesProvider.getInstance(context.environment.project)
|
||||
?.getScriptConfiguration(snippetKtFile)?.configuration
|
||||
|
||||
@@ -114,13 +114,17 @@ private fun compileImpl(
|
||||
)
|
||||
.valueOr { return it }
|
||||
|
||||
if (messageCollector.hasErrors()) return failure(messageCollector)
|
||||
|
||||
val (sourceFiles, sourceDependencies) = collectRefinedSourcesAndUpdateEnvironment(
|
||||
context,
|
||||
mainKtFile,
|
||||
messageCollector
|
||||
)
|
||||
|
||||
if (sourceDependencies.any { it.sourceDependencies is ResultWithDiagnostics.Failure }) return failure(messageCollector)
|
||||
if (messageCollector.hasErrors() || sourceDependencies.any { it.sourceDependencies is ResultWithDiagnostics.Failure }) {
|
||||
return failure(messageCollector)
|
||||
}
|
||||
|
||||
val dependenciesProvider = ScriptDependenciesProvider.getInstance(context.environment.project)
|
||||
val getScriptConfiguration = { ktFile: KtFile ->
|
||||
|
||||
@@ -9,8 +9,10 @@ import com.intellij.openapi.Disposable
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
|
||||
import org.jetbrains.kotlin.cli.common.arguments.validateArguments
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||
import org.jetbrains.kotlin.cli.common.reportArgumentParseProblems
|
||||
import org.jetbrains.kotlin.cli.common.setupCommonArguments
|
||||
import org.jetbrains.kotlin.cli.jvm.*
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
@@ -165,6 +167,13 @@ private fun CompilerConfiguration.updateWithCompilerOptions(
|
||||
val compilerArguments = K2JVMCompilerArguments()
|
||||
parseCommandLineArguments(compilerOptions, compilerArguments)
|
||||
|
||||
validateArguments(compilerArguments.errors)?.let {
|
||||
messageCollector.report(CompilerMessageSeverity.ERROR, it)
|
||||
return
|
||||
}
|
||||
|
||||
messageCollector.reportArgumentParseProblems(compilerArguments)
|
||||
|
||||
reportArgumentsIgnoredGenerally(
|
||||
compilerArguments,
|
||||
messageCollector,
|
||||
|
||||
Reference in New Issue
Block a user