Skip to content

Commit

Permalink
Add tests for configuration of java executable
Browse files Browse the repository at this point in the history
  • Loading branch information
wfhartford committed Mar 14, 2024
1 parent 35e4908 commit b729027
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ tasks.named('test') {
inputs.files fileTree("$projectDir/testProjectAndroidLibrary")
inputs.files fileTree("$projectDir/testProjectBase")
inputs.files fileTree("$projectDir/testProjectBuildTimeProto")
inputs.files fileTree("$projectDir/testProjectConfigureJavaExecutable")
inputs.files fileTree("$projectDir/testProjectCustomProtoDir")
inputs.files fileTree("$projectDir/testProjectDependent")
inputs.files fileTree("$projectDir/testProjectDependentApp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,96 @@ class ProtobufJavaPluginTest extends Specification {
limit == GenerateProtoTask.DEFAULT_CMD_LENGTH_LIMIT
}
void "test custom java executable in extension"() {
given: "a basic project"
Project project = setupBasicProject()
when: "a java executable is specified on the protobuf extension"
project.extensions.getByType(ProtobufExtension).javaExecutablePath.set("/custom-java.exe")
then: "all tasks get the custom executable"
assert project.extensions.getByType(ProtobufExtension).javaExecutablePath.get() == "/custom-java.exe"
assert ((GenerateProtoTask)project.tasks.generateProto).javaExecutablePath.get() == "/custom-java.exe"
assert ((GenerateProtoTask)project.tasks.generateTestProto).javaExecutablePath.get() == "/custom-java.exe"
}
void "test custom java executable in task"() {
given: "a basic project"
Project project = setupBasicProject()
when: "a java executable is specified on the generate proto task"
((GenerateProtoTask)project.tasks.generateProto).javaExecutablePath.set("/custom-java.exe")
then: "generate proto task uses configured executable"
assert ((GenerateProtoTask)project.tasks.generateProto).javaExecutablePath.get() == "/custom-java.exe"
and: "extension and test task use default executable"
assert project.extensions.getByType(ProtobufExtension).javaExecutablePath
.get() == ProtobufExtension.computeJavaExePath()
assert ((GenerateProtoTask)project.tasks.generateTestProto).javaExecutablePath
.get() == ProtobufExtension.computeJavaExePath()
}
void "test custom java executable in extension and task"() {
given: "a basic project"
Project project = setupBasicProject()
when: "a java executable is specified on the protobuf extension and generate proto task"
project.extensions.getByType(ProtobufExtension).javaExecutablePath.set("/ext-java.exe")
((GenerateProtoTask)project.tasks.generateProto).javaExecutablePath.set("/task-java.exe")
then: "extension and test task use executable specified on the extension"
assert project.extensions.getByType(ProtobufExtension).javaExecutablePath.get() == "/ext-java.exe"
assert ((GenerateProtoTask)project.tasks.generateTestProto).javaExecutablePath.get() == "/ext-java.exe"
and: "generate proto task uses executable specified on task"
assert ((GenerateProtoTask)project.tasks.generateProto).javaExecutablePath.get() == "/task-java.exe"
}
@Unroll
void "test proto generation fails when java executable is invalid [gradle #gradleVersion]"() {
given: "project from testProject"
File projectDir = ProtobufPluginTestHelper.projectBuilder('testProjectConfigureJavaExecutable')
.copyDirs('testProjectConfigureJavaExecutable')
.build()
when: "build is invoked using grpc plugin"
BuildResult result = ProtobufPluginTestHelper.getGradleRunner(
projectDir,
gradleVersion,
"build"
).build()
then: "it succeeds"
assert result.task(":build").outcome == TaskOutcome.SUCCESS
assert result.task(":generateProto").outcome == TaskOutcome.SUCCESS
// Since we don't know if there are multiple JDKs installed, and it would
// be challenging to determine which one was actually executed, we're
// going to test that the executable change works by setting to something
// invalid and ensuring that the build fails for the right reason.
when: "protobuf java executor is invalid and build runs again"
new File(projectDir, "build.gradle")
.append("""
protobuf {
javaExecutablePath.set("/nothing")
}""")
result = ProtobufPluginTestHelper.getGradleRunner(
projectDir,
gradleVersion,
"build"
).buildAndFail()
then: "generateProto FAILED"
result.task(":generateProto").outcome == TaskOutcome.FAILED
and: "the failure was caused by a missing executable"
result.output.contains("exec: /nothing: not found")
where:
gradleVersion << GRADLE_VERSIONS
}
private Project setupBasicProject() {
Project project = ProjectBuilder.builder().build()
project.apply plugin:'java'
Expand Down
25 changes: 25 additions & 0 deletions testProjectConfigureJavaExecutable/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id 'java'
id 'com.google.protobuf'
}
repositories { mavenCentral() }
dependencies {
implementation 'com.google.protobuf:protobuf-java:3.0.0'
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.0.3' }
grpcKotlin { artifact = 'io.grpc:protoc-gen-grpc-kotlin:1.4.1:jdk8@jar' }
}
generateProtoTasks {
all().configureEach { task ->
task.plugins {
grpc {}
grpcKotlin {}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

syntax = "proto3";

option java_package = "com.example.tutorial";
option java_outer_classname = "OuterSample";
option java_multiple_files = true;

message Msg {
string foo = 1;
SecondMsg blah = 2;
}

message SecondMsg {
int32 blah = 1;
}

0 comments on commit b729027

Please sign in to comment.