Scripting: support scripts starting with UTF-8 BOM

#KT-54705 fixed
This commit is contained in:
Ilya Chernikov
2023-01-04 17:48:14 +01:00
parent 989c1ec64d
commit 6c035bb5f4
4 changed files with 27 additions and 2 deletions
@@ -62,7 +62,7 @@ abstract class FileBasedScriptSource() : ExternalSourceCode {
*/
open class FileScriptSource(override val file: File, private val preloadedText: String? = null) : FileBasedScriptSource(), Serializable {
override val externalLocation: URL get() = file.toURI().toURL()
override val text: String by lazy { preloadedText ?: file.readText() }
override val text: String by lazy { preloadedText ?: file.readTextSkipUtf8Bom() }
override val name: String? get() = file.name
override val locationId: String? get() = file.path
@@ -81,7 +81,7 @@ open class FileScriptSource(override val file: File, private val preloadedText:
* The implementation of the SourceCode for a script location pointed by the URL
*/
open class UrlScriptSource(override val externalLocation: URL) : ExternalSourceCode, Serializable {
override val text: String by lazy { externalLocation.readText() }
override val text: String by lazy { externalLocation.readTextSkipUtf8Bom() }
override val name: String? get() = externalLocation.file
override val locationId: String? get() = externalLocation.toString()
@@ -133,3 +133,10 @@ private val ExternalSourceCode.textSafe: String?
} catch (e: Throwable) {
null
}
private const val UTF8_BOM = 0xfeff.toChar().toString()
private fun File.readTextSkipUtf8Bom(): String = readText().removePrefix(UTF8_BOM)
private fun URL.readTextSkipUtf8Bom(): String = readText().removePrefix(UTF8_BOM)
@@ -154,6 +154,13 @@ class MainKtsIT {
listOf("""\{"firstName":"James","lastName":"Bond"\}""", "User\\(firstName=James, lastName=Bond\\)")
)
}
@Test
fun testUtf8Bom() {
val scriptPath = "$TEST_DATA_ROOT/utf8bom.main.kts"
Assert.assertTrue("Expect file '$scriptPath' to start with UTF-8 BOM", File(scriptPath).readText().startsWith(UTF8_BOM))
runWithKotlincAndMainKts(scriptPath, listOf("Hello world"))
}
}
fun runWithKotlincAndMainKts(
@@ -204,3 +211,5 @@ fun runWithK2JVMCompilerAndMainKts(
}
}
internal const val UTF8_BOM = 0xfeff.toChar().toString()
@@ -266,6 +266,14 @@ class MainKtsTest {
)
}
@Test
fun testUtf8Bom() {
val scriptPath = "$TEST_DATA_ROOT/utf8bom.main.kts"
Assert.assertTrue("Expect file '$scriptPath' to start with UTF-8 BOM", File(scriptPath).readText().startsWith(UTF8_BOM))
val res = evalFile(File(scriptPath))
assertSucceeded(res)
}
private fun assertSucceeded(res: ResultWithDiagnostics<EvaluationResult>) {
Assert.assertTrue(
"test failed:\n ${res.reports.joinToString("\n ") { it.severity.name + ": " + it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
@@ -0,0 +1 @@
println("Hello world")