Scripting: add check for 'kotlin' package similar to the regular sources

#KT-52598 fixed
This commit is contained in:
Ilya Chernikov
2022-05-31 15:55:47 +02:00
committed by teamcity
parent f5950ac8f3
commit a8c4ea04c8
6 changed files with 53 additions and 3 deletions
@@ -382,6 +382,29 @@ class ReplTest : TestCase() {
}
}
@Test
fun testKotlinPackage() {
val greeting = "Hello from script!"
val error = "Only the Kotlin standard library is allowed to use the 'kotlin' package"
val script = "package kotlin\n\"$greeting\""
checkEvaluateInReplDiags(
sequenceOf(script),
sequenceOf(
makeFailureResult(
error, path = "Line_0.simplescript.kts",
location = SourceCode.Location(SourceCode.Position(1, 1), SourceCode.Position(1, 15))
)
)
)
checkEvaluateInRepl(
sequenceOf(script),
sequenceOf(greeting),
simpleScriptCompilationConfiguration.with {
compilerOptions("-Xallow-kotlin-package")
}
)
}
companion object {
private fun positionsEqual(a: SourceCode.Position?, b: SourceCode.Position?): Boolean {
if (a == null || b == null) {
@@ -315,6 +315,25 @@ class ScriptingHostTest : TestCase() {
Assert.assertEquals(greeting, output)
}
@Test
fun testKotlinPackage() {
val greeting = "Hello from script!"
val error = "Only the Kotlin standard library is allowed to use the 'kotlin' package"
val script = "package kotlin\nprintln(\"$greeting\")"
val res0 = evalScript(script)
Assert.assertTrue(res0.reports.any { it.message == error })
Assert.assertTrue(res0 is ResultWithDiagnostics.Failure)
val output = captureOut {
val res1 = evalScriptWithConfiguration(script) {
compilerOptions("-Xallow-kotlin-package")
}
Assert.assertTrue(res1.reports.none { it.message == error })
Assert.assertTrue(res1 is ResultWithDiagnostics.Success)
}
Assert.assertEquals(greeting, output)
}
private fun doDiamondImportTest(evaluationConfiguration: ScriptEvaluationConfiguration? = null): List<String> {
val mainScript = "sharedVar += 1\nprintln(\"sharedVar == \$sharedVar\")".toScriptSource("main.kts")
val middleScript = File(TEST_DATA_DIR, "importTest/diamondImportMiddle.kts").toScriptSource()