-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Test Framework For Parsers (#46)
Also include a failing test for #45 Will fix it in the next cl.
- Loading branch information
1 parent
168be46
commit 95259b8
Showing
11 changed files
with
1,532 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/build/ByteCodeDecompiler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2024 Romain Guy | ||
* | ||
* 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. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer.build | ||
|
||
import dev.romainguy.kotlin.explorer.process | ||
import java.nio.file.Path | ||
import kotlin.io.path.ExperimentalPathApi | ||
import kotlin.io.path.extension | ||
import kotlin.io.path.pathString | ||
import kotlin.io.path.walk | ||
|
||
class ByteCodeDecompiler { | ||
suspend fun decompile(directory: Path) = process(*buildJavapCommand(directory), directory = directory) | ||
|
||
@OptIn(ExperimentalPathApi::class) | ||
private fun buildJavapCommand(directory: Path): Array<String> { | ||
val command = mutableListOf("javap", "-p", "-l", "-c") | ||
val classFiles = directory.walk() | ||
.filter { path -> path.extension == "class" } | ||
.map { path -> directory.relativize(path).pathString } | ||
.sorted() | ||
command.addAll(classFiles) | ||
return command.toTypedArray() | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/build/KolinCompiler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2024 Romain Guy | ||
* | ||
* 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. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer.build | ||
|
||
import dev.romainguy.kotlin.explorer.ToolPaths | ||
import dev.romainguy.kotlin.explorer.process | ||
import java.nio.file.Path | ||
|
||
class KotlinCompiler(private val toolPaths: ToolPaths, private val outputDirectory: Path) { | ||
|
||
suspend fun compile(source: Path) = process(*buildCompileCommand(source), directory = outputDirectory) | ||
|
||
private fun buildCompileCommand(file: Path): Array<String> { | ||
val command = mutableListOf( | ||
toolPaths.kotlinc.toString(), | ||
file.toString(), | ||
"-Xmulti-platform", | ||
"-Xno-param-assertions", | ||
"-Xno-call-assertions", | ||
"-Xno-receiver-assertions", | ||
"-classpath", | ||
(toolPaths.kotlinLibs + toolPaths.platform).joinToString(":") { jar -> jar.toString() } | ||
) | ||
|
||
return command.toTypedArray() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/jvmTest/kotlin/dev/romainguy/kotlin/explorer/bytecode/ByteCodeParserTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2024 Romain Guy | ||
* | ||
* 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. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer.bytecode | ||
|
||
import dev.romainguy.kotlin.explorer.testing.Builder | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
|
||
class ByteCodeParserTest { | ||
@get:Rule | ||
val temporaryFolder = TemporaryFolder() | ||
|
||
private val builder by lazy { Builder.getInstance(temporaryFolder.root.toPath()) } | ||
private val byteCodeParser = ByteCodeParser() | ||
|
||
@Test | ||
fun issue_45() { | ||
val byteCode = builder.generateByteCode("Issue_45.kt") | ||
val content = byteCodeParser.parse(byteCode) | ||
println(content) | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
src/jvmTest/kotlin/dev/romainguy/kotlin/explorer/testing/Builder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (C) 2024 Romain Guy | ||
* | ||
* 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. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer.testing | ||
|
||
import dev.romainguy.kotlin.explorer.ToolPaths | ||
import dev.romainguy.kotlin.explorer.build.ByteCodeDecompiler | ||
import dev.romainguy.kotlin.explorer.build.KotlinCompiler | ||
import kotlinx.coroutines.runBlocking | ||
import java.nio.file.Path | ||
import java.util.* | ||
import kotlin.io.path.* | ||
import kotlin.test.fail | ||
|
||
interface Builder { | ||
fun generateByteCode(testFile: String): String | ||
|
||
companion object { | ||
fun getInstance(outputDirectory: Path) = | ||
try { | ||
LocalBuilder(outputDirectory) | ||
} catch (e: Throwable) { | ||
System.err.println("Failed to create local builder. Using Github builder") | ||
GithubBuilder() | ||
} | ||
} | ||
} | ||
|
||
class GithubBuilder : Builder { | ||
private val cwd = Path.of(System.getProperty("user.dir")) | ||
private val testData = cwd.resolve("src/jvmTest/kotlin/testData") | ||
|
||
override fun generateByteCode(testFile: String): String { | ||
return testData.resolve(testFile.replace(".kt", ".javap")).readText() | ||
} | ||
} | ||
|
||
class LocalBuilder(private val outputDirectory: Path) : Builder { | ||
private val cwd = Path.of(System.getProperty("user.dir")) | ||
private val testData = cwd.resolve("src/jvmTest/kotlin/testData") | ||
private val toolPaths = createToolsPath() | ||
private val kotlinCompiler = KotlinCompiler(toolPaths, outputDirectory) | ||
private val byteCodeDecompiler = ByteCodeDecompiler() | ||
|
||
override fun generateByteCode(testFile: String): String { | ||
val path = testData.resolve(testFile) | ||
if (path.notExists()) { | ||
fail("$path does not exists") | ||
} | ||
return runBlocking { | ||
kotlinCompile(path) | ||
val result = byteCodeDecompiler.decompile(outputDirectory) | ||
if (result.exitCode != 0) { | ||
System.err.println(result.output) | ||
fail("javap error") | ||
} | ||
|
||
// Save the fine under <project/build/testData> so we can examine it when needed | ||
val saveFile = testData.resolve(testFile.replace(".kt", ".javap")) | ||
if (saveFile.parent.notExists()) { | ||
saveFile.parent.createDirectory() | ||
} | ||
saveFile.writeText(result.output) | ||
|
||
result.output | ||
} | ||
} | ||
|
||
private suspend fun kotlinCompile(path: Path) { | ||
val result = kotlinCompiler.compile(path) | ||
if (result.exitCode != 0) { | ||
System.err.println(result.output) | ||
fail("kotlinc error") | ||
} | ||
} | ||
|
||
private fun createToolsPath(): ToolPaths { | ||
val properties = Properties() | ||
properties.load(cwd.resolve("local.properties").reader()) | ||
|
||
val kotlinHome = getKotlinHome(properties) | ||
val androidHome = getAndroidHome(properties) | ||
val toolPaths = ToolPaths(Path.of(""), androidHome, kotlinHome) | ||
if (toolPaths.isKotlinHomeValid && toolPaths.isAndroidHomeValid) { | ||
return toolPaths | ||
} | ||
throw IllegalStateException("Invalid ToolsPath: KOTLIN_HOME=${kotlinHome} ANDROID_HOME=$androidHome") | ||
} | ||
} | ||
|
||
private fun getKotlinHome(properties: Properties): Path { | ||
val pathString = System.getenv("KOTLIN_HOME") ?: properties.getProperty("kotlin.home") | ||
|
||
if (pathString == null) { | ||
throw IllegalStateException("Could not find Android SDK") | ||
} | ||
val path = Path.of(pathString) | ||
if (path.notExists()) { | ||
throw IllegalStateException("Could not find Android SDK") | ||
} | ||
return path | ||
} | ||
|
||
private fun getAndroidHome(properties: Properties): Path { | ||
val path = | ||
when (val androidHome: String? = System.getenv("ANDROID_HOME") ?: properties.getProperty("android.home")) { | ||
null -> Path.of(System.getProperty("user.home")).resolve("Android/Sdk") | ||
else -> Path.of(androidHome) | ||
} | ||
|
||
if (path.notExists()) { | ||
throw IllegalStateException("Could not find Android SDK") | ||
} | ||
return path | ||
} | ||
|
Oops, something went wrong.