Skip to content

Commit

Permalink
GH-26: Add tests for $PATH resolution of protoc
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Feb 4, 2024
1 parent 7b74841 commit 96a7ffa
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 3 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,33 @@ jobs:
java-version: [11, 21]

steps:
- name: Checkout
- name: Checkout code
uses: actions/checkout@v4

# Install protoc onto the PATH so that we can test PATH resolution.
- name: Install protoc
shell: bash
run: |-
case "${{ matrix.os-name }}" in
macos-*) brew install protobuf ;;
ubuntu-*) sudo apt update -q && sudo apt install protobuf-compiler -qy ;;
windows-*) choco install protoc ;;
esac
protoc --version
- name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java-version }}
distribution: 'temurin'
cache: maven

- name: Build with Maven
- name: Build and test
shell: bash
run: ./mvnw -B verify

- name: Publish to codecov
- name: Publish code coverage
uses: codecov/codecov-action@v3
continue-on-error: true
if: always()
4 changes: 4 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Install protoc
shell: bash
run: sudo apt update -q && sudo apt install protocol-buffers -qy

- name: Set up JDK
uses: actions/setup-java@v4
with:
Expand Down
1 change: 1 addition & 0 deletions src/it/test-java-main-system-protoc/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean package
91 changes: 91 additions & 0 deletions src/it/test-java-main-system-protoc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2023 - 2024, Ashley Scopes.
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<junit.version>5.10.2</junit.version>
<protobuf.version>3.25.1</protobuf.version>

<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>3.2.2</maven-surefire-plugin.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>

<configuration>
<release>11</release>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>

<plugin>
<groupId>${plugin-group-id}</groupId>
<artifactId>${plugin-artifact-id}</artifactId>
<version>${plugin-version}</version>

<configuration>
<protocVersion>PATH</protocVersion>
</configuration>

<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (C) 2023 - 2024, Ashley Scopes.
//
// 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.
//

syntax = "proto3";

option java_multiple_files = true;
option java_package = "org.example.helloworld";

package org.example.helloworld;

message GreetingRequest {
string name = 1;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2023 - 2024, Ashley Scopes.
*
* 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 org.example.helloworld;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ProtobufTest {
@Test
void generatedProtobufSourcesAreFullMessages() throws Throwable {
// When
var superClasses = new ArrayList<String>();
Class<?> superClass = GreetingRequest.class;

do {
superClasses.add(superClass.getName());
superClass = superClass.getSuperclass();
} while (superClass != null);

// Then
assertTrue(superClasses.contains("com.google.protobuf.GeneratedMessageV3"));
}

@Test
void generatedProtobufSourcesAreValid() throws Throwable {
// Given
var expectedGreetingRequest = GreetingRequest
.newBuilder()
.setName("Ashley")
.build();

// When
var baos = new ByteArrayOutputStream();
expectedGreetingRequest.writeTo(baos);
var actualGreetingRequest = GreetingRequest.parseFrom(baos.toByteArray());

assertNotEquals(0, baos.toByteArray().length);

// Then
assertEquals(expectedGreetingRequest.getName(), actualGreetingRequest.getName());
}
}
41 changes: 41 additions & 0 deletions src/it/test-java-main-system-protoc/test.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2023 - 2024, Ashley Scopes.
*
* 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.
*/
import java.nio.file.Path

import static org.assertj.core.api.Assertions.assertThat

Path baseDirectory = basedir.toPath().toAbsolutePath()
def generatedSourcesDir = baseDirectory.resolve("target/generated-sources/protobuf")
def classesDir = baseDirectory.resolve("target/classes")
def expectedGeneratedFiles = [
"org/example/helloworld/Helloworld",
"org/example/helloworld/GreetingRequest",
"org/example/helloworld/GreetingRequestOrBuilder",
]

assertThat(generatedSourcesDir).isDirectory()

assertThat(classesDir).isDirectory()

expectedGeneratedFiles.forEach {
assertThat(generatedSourcesDir.resolve("${it}.java"))
.exists()
.isNotEmptyFile()
assertThat(classesDir.resolve("${it}.class"))
.exists()
.isNotEmptyFile()

}

0 comments on commit 96a7ffa

Please sign in to comment.