-
Notifications
You must be signed in to change notification settings - Fork 236
Mixed source compile test reproducing issues/707 #710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
load("//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test") | ||
|
||
kt_jvm_library( | ||
name = "lib", | ||
srcs = glob( | ||
["*.kt"], | ||
exclude = ["*Test.kt"], | ||
), | ||
) | ||
|
||
kt_jvm_test( | ||
name = "WriteWorkspaceTest", | ||
size = "enormous", | ||
srcs = ["WriteWorkspaceTest.kt"], | ||
deps = [ | ||
":lib", | ||
"@com_github_jetbrains_kotlin//:kotlin-test", | ||
"@kotlin_rules_maven//:com_google_truth_truth", | ||
"@kotlin_rules_maven//:junit_junit", | ||
], | ||
) | ||
|
||
kt_jvm_test( | ||
name = "MixedSourceCompileTest", | ||
srcs = ["MixedSourceCompileTest.kt"], | ||
resources = [ | ||
"//:rules_kotlin_release.tgz", | ||
], | ||
tags = ["local"], # necessary for running Bazel in a test. | ||
test_class = "io.bazel.kotlin.integration.MixedSourceCompileTest", | ||
deps = [ | ||
":lib", | ||
"@com_github_jetbrains_kotlin//:kotlin-test", | ||
"@kotlin_rules_maven//:com_google_truth_truth", | ||
"@kotlin_rules_maven//:junit_junit", | ||
], | ||
) |
71 changes: 71 additions & 0 deletions
71
src/test/kotlin/io/bazel/kotlin/integration/MixedSourceCompileTest.kt
This file contains hidden or 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,71 @@ | ||
package io.bazel.kotlin.integration | ||
|
||
import com.google.common.truth.Truth | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary? |
||
import com.google.common.truth.Truth.assertWithMessage | ||
import io.bazel.kotlin.integration.RulesKotlinWorkspace.Companion.build | ||
import org.junit.Test | ||
import java.nio.file.Files | ||
|
||
class MixedSourceCompileTest { | ||
|
||
@Test | ||
fun `kotlin and java sources with dependency`() { | ||
val workspace = RulesKotlinWorkspace.write(rulesRepo = "rules_kotlin_release") { | ||
|
||
workspace { | ||
val (kt_register_toolchains) = load( | ||
"@rules_kotlin_release//kotlin:core.bzl", | ||
"kt_register_toolchains" | ||
) | ||
kt_register_toolchains() | ||
} | ||
|
||
"library" { | ||
build { | ||
val (ktJvmLibrary) = load("@rules_kotlin_release//kotlin:jvm.bzl", "kt_jvm_library") | ||
|
||
ktJvmLibrary( | ||
"name" to "library", | ||
"srcs" to !"['Library.kt']", | ||
"visibility" to !"['//:__subpackages__']" | ||
) | ||
} | ||
kotlin("Library.kt") { | ||
`package`("library") | ||
`class`("Library") {} | ||
} | ||
} | ||
|
||
"mixed" { | ||
build { | ||
val (ktJvmLibrary) = load("@rules_kotlin_release//kotlin:jvm.bzl", "kt_jvm_library") | ||
|
||
ktJvmLibrary( | ||
"name" to "mixed", | ||
"srcs".list("Main.java", "Dep.kt"), | ||
"visibility" to !"['//:__subpackages__']", | ||
"deps".list("//library") | ||
) | ||
} | ||
|
||
java("Main.java") { | ||
`package`("mixed") | ||
|
||
"public class Main" { | ||
"public void run()" { | ||
+"new Dep();" | ||
} | ||
} | ||
} | ||
|
||
kotlin("Dep.kt") { | ||
`package`("mixed") | ||
`class`("Dep") {} | ||
} | ||
} | ||
} | ||
|
||
val result = workspace.build("//mixed") | ||
assertWithMessage("failed with $result").that(result.exit).isEqualTo(0) | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/test/kotlin/io/bazel/kotlin/integration/RulesKotlinWorkspace.kt
This file contains hidden or 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,113 @@ | ||
package io.bazel.kotlin.integration | ||
|
||
import io.bazel.kotlin.integration.WriteWorkspace.BzlWorkspace | ||
import io.bazel.kotlin.integration.WriteWorkspace.Workspace | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.StandardOpenOption.CREATE_NEW | ||
import java.util.concurrent.TimeUnit | ||
|
||
class RulesKotlinWorkspace private constructor( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we start adding some class level kdocs, at least for new types please? |
||
private val rulesRepo: String, | ||
private val write: Workspace | ||
) : Workspace by write { | ||
|
||
companion object { | ||
fun write( | ||
rulesRepo: String = "rules_kotlin_release", | ||
contents: RulesKotlinWorkspace.() -> Unit | ||
): Path { | ||
return WriteWorkspace.using<RulesKotlinWorkspace> { | ||
RulesKotlinWorkspace(rulesRepo, this) | ||
.apply(contents) | ||
} | ||
} | ||
|
||
fun Path.build(vararg targets: String): BuildResult { | ||
val out = Files.createTempFile("out", "txt").toFile().apply { | ||
deleteOnExit() | ||
} | ||
val err = Files.createTempFile("err", "txt").toFile().apply { | ||
deleteOnExit() | ||
} | ||
|
||
return ProcessBuilder() | ||
.command( | ||
listOf( | ||
"bazel", | ||
"build", | ||
) + targets | ||
) | ||
.redirectOutput(out) | ||
.redirectError(err) | ||
.directory(this.toFile()) | ||
.start().runCatching { | ||
if (!waitFor(5, TimeUnit.MINUTES)) { | ||
error("build took too long:\nout: ${out.readText(Charsets.UTF_8)}\nerr: ${err.readText( | ||
Charsets.UTF_8 | ||
)}") | ||
} | ||
BuildResult( | ||
exit = exitValue(), | ||
out = out.readText(Charsets.UTF_8), | ||
err = err.readText(Charsets.UTF_8) | ||
) | ||
} | ||
.recover { exception -> | ||
BuildResult( | ||
exit = 1, | ||
out = "", | ||
err = exception.toString() | ||
) | ||
} | ||
.getOrThrow() | ||
} | ||
data class BuildResult( | ||
val exit: Int, | ||
val out: String, | ||
val err: String, | ||
) | ||
} | ||
|
||
override fun workspace(contents: BzlWorkspace.() -> Unit) { | ||
val rulesRepo = this.rulesRepo | ||
val (location, archive_repository) = defineArchiveRepository() | ||
val releaseArchive = writeReleaseArchive().toString() | ||
write.workspace { | ||
load(location, archive_repository) | ||
archive_repository( | ||
"name" to rulesRepo, | ||
"path" to releaseArchive | ||
) | ||
load("@$rulesRepo//kotlin:repositories.bzl", "kotlin_repositories") | ||
"kotlin_repositories"() | ||
apply(contents) | ||
} | ||
} | ||
|
||
private fun writeReleaseArchive(): Path { | ||
return RulesKotlinWorkspace::class.java.classLoader.getResourceAsStream("_release.tgz") | ||
?.let { stream -> | ||
Files.createTempDirectory("rules_kotlin_release").resolve("$rulesRepo.tgz").also { | ||
Files.newOutputStream(it, CREATE_NEW).buffered().write(stream.readAllBytes()) | ||
} | ||
} | ||
?: error("Cannot find release repo") | ||
} | ||
|
||
private fun defineArchiveRepository(): Pair<String, String> { | ||
starlark("archive_repository.bzl") { | ||
"def _archive_repository_impl"(!"repository_ctx") { | ||
"repository_ctx.extract"( | ||
"archive" to !"repository_ctx.attr.path", | ||
) | ||
} | ||
|
||
"archive_repository=repository_rule"( | ||
"implementation" to !"_archive_repository_impl", | ||
"attrs" to !"{'path': attr.string()}" | ||
) | ||
} | ||
return "@//:archive_repository.bzl" to "archive_repository" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh