Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Merge branch 'feature/fail-if-avpr-will-be-overwritten'
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmc24 committed Apr 22, 2021
2 parents a519def + 9b0a7d9 commit 4711f6f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 20 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
arguments: --no-daemon --info --stacktrace build
dependencies-cache-enabled: true
configuration-cache-enabled: true
- uses: codecov/codecov-action@v1
with:
file: ./build/reports/jacoco/test/jacocoTestReport.xml
fail_ci_if_error: true
# - uses: codecov/codecov-action@v1
# with:
# file: ./build/reports/jacoco/test/jacocoTestReport.xml
# fail_ci_if_error: true
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
* Avro 1.9.0-1.9.2 is supported again (no changed needed; just change in support policy and testing)
* `generateAvroProtocol` task fails if avpr file will get overwritten (due to multiple IDL files using the same namespace and protocol)

## 1.1.0
* Built using Avro 1.10.2
Expand Down
35 changes: 22 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ plugins {
id "checkstyle"
id "codenarc"
id "idea"
id "jacoco"
// id "jacoco"
id "maven-publish"
id "signing"
id "java-gradle-plugin"
id "org.nosphere.gradle.github.actions" version "1.2.0"
id "io.github.gradle-nexus.publish-plugin" version "1.0.0"
// id "pl.droidsonroids.jacoco.testkit" version "1.0.8"
}

// Gradle TestKit (which most of this plugins tests run using) runs the builds in a separate
// JVM than the tests. Thus, for Jacoco to pick up on it, you need to do some extra legwork.
// https://github.com/koral--/jacoco-gradle-testkit-plugin seeks to solve that
// However, Gradle 7.0-rc-1 doesn't currently support the combination of the configuration cache
// with a Java agent (such as Jacoco) and TestKit.
// Thus, for now, no coverage reporting, as I place a higher value on testing configuration cache
// support.

group = "com.github.davidmc24.gradle.plugin"
version = "1.1.1-SNAPSHOT"

Expand Down Expand Up @@ -201,7 +210,7 @@ test {
gradleVersion: gradle.gradleVersion,
kotlinPluginVersion: latestKotlinVersion,
]
finalizedBy jacocoTestReport // report is always generated after tests run
// finalizedBy jacocoTestReport // report is always generated after tests run
}

tasks.create(name: "testCompatibility", type: Test) {
Expand Down Expand Up @@ -239,18 +248,18 @@ tasks.create(name: "testKotlinPluginCompatibility", type: Test) {
}
}

jacoco {
// 0.8.7+ needed for Java 15 support
// See https://www.jacoco.org/jacoco/trunk/doc/changes.html
toolVersion = "0.8.7-SNAPSHOT"
}
//jacoco {
// // 0.8.7+ needed for Java 15 support
// // See https://www.jacoco.org/jacoco/trunk/doc/changes.html
// toolVersion = "0.8.7-SNAPSHOT"
//}

jacocoTestReport {
reports {
html.enabled true
xml.enabled true
}
}
//jacocoTestReport {
// reports {
// html.enabled true
// xml.enabled true
// }
//}

tasks.withType(Test) {
jvmArgs "-Xss320k"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.avro.Protocol;
import org.apache.avro.compiler.idl.Idl;
import org.apache.avro.compiler.idl.ParseException;
Expand All @@ -41,10 +43,12 @@
public class GenerateAvroProtocolTask extends OutputDirTask {

private FileCollection classpath;
private Set<String> processedFiles;

public GenerateAvroProtocolTask() {
super();
this.classpath = GradleCompatibility.createConfigurableFileCollection(getProject());
this.processedFiles = new HashSet<String>();
}

public void setClasspath(FileCollection classpath) {
Expand Down Expand Up @@ -88,12 +92,17 @@ private void processFiles() {
private void processIDLFile(File idlFile, ClassLoader loader) {
getLogger().info("Processing {}", idlFile);
try (Idl idl = new Idl(idlFile, loader)) {
File outputDir = getOutputDir().get().getAsFile();
Protocol protocol = idl.CompilationUnit();
File protoFile = new File(getOutputDir().get().getAsFile(), AvroUtils.assemblePath(protocol));
String filePath = AvroUtils.assemblePath(protocol);
if (!processedFiles.add(filePath)) {
throw new GradleException("File already processed with same namespace and protocol name.");
}
File protoFile = new File(outputDir, filePath);
String protoJson = protocol.toString(true);
FileUtils.writeJsonFile(protoFile, protoJson);
getLogger().debug("Wrote {}", protoFile.getPath());
} catch (IOException | ParseException ex) {
} catch (IOException | ParseException | GradleException ex) {
throw new GradleException(String.format("Failed to compile IDL file %s", idlFile), ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ abstract class FunctionalSpec extends Specification {
}

protected void copyResource(String name, File targetFolder) {
def file = new File(targetFolder, name)
copyResource(name, targetFolder, name)
}

protected void copyResource(String name, File targetFolder, String targetName) {
def resource = getClass().getResourceAsStream(name)
def file = new File(targetFolder, targetName)
if (resource == null) {
throw new FileNotFoundException("Could not resource with name ${name}")
}
file.parentFile.mkdirs()
file << getClass().getResourceAsStream(name)
}
Expand All @@ -121,6 +129,8 @@ abstract class FunctionalSpec extends Specification {
}

protected GradleRunner createGradleRunner() {
// // Set up code coverage reporting based on https://github.com/koral--/jacoco-gradle-testkit-plugin
// copyResource("/testkit-gradle.properties", testProjectDir, "gradle.properties")
return GradleRunner.create().withProjectDir(testProjectDir).withGradleVersion(gradleVersion.version).withPluginClasspath()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,20 @@ class GenerateAvroProtocolTaskFunctionalSpec extends FunctionalSpec {
projectFile("build/generated-main-avro-avpr/org/example/v1/TestProtocol.avpr").file
projectFile("build/generated-main-avro-avpr/org/example/v2/TestProtocol.avpr").file
}
def "fails if avpr will be overwritten"() {
given: "a project with two IDL files with the same protocol name and namespace"
applyAvroPlugin()
copyResource("namespaced-idl/v1/test.avdl", projectFolder("src/main/avro/v1"))
copyResource("namespaced-idl/v1/test_same_protocol.avdl", projectFolder("src/main/avro/v1"))
when: "running the task"
run("generateAvroProtocol")
then:
def ex = thrown(Exception)
ex.message.contains("Failed to compile IDL file")
ex.message.contains("File already processed with same namespace and protocol name.")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@namespace("org.example.v1")
protocol TestProtocol {
record SomeOtherTestRecord {
string field1;
}
}

0 comments on commit 4711f6f

Please sign in to comment.