Skip to content
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

Apply java and kotlin source formatting #20

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
// The Plugin Publish plugin will in turn auto-apply the Gradle Plugin Development Plugin (java-gradle-plugin)
// and the Maven Publish plugin (maven-publish)
id("com.gradle.plugin-publish") version "1.2.0"
id("com.diffplug.spotless") version "7.0.1"

`maven-publish`
}
Expand All @@ -32,28 +33,39 @@ dependencies {
gradlePlugin {
website.set("https://github.com/dafny-lang/dafny-gradle-plugin")
vcsUrl.set("https://github.com/dafny-lang/dafny-gradle-plugin")

// Define the plugin
plugins {
create("org.dafny.dafny") {
id = "org.dafny.dafny"
implementationClass = "org.dafny.gradle.plugin.DafnyPlugin"
displayName = "Gradle plugin for Dafny"
description = "This plugin offers tight integration of the " +
"Dafny verification-aware programming language with Java: " +
"automatically verifying Dafny source code and compiling it to Java source code, " +
"which the Java plugin will then build together with any hand-written Java in the project. " +
"It also provides a robust approach to distributing and managing Dafny dependencies " +
"through Gradle-supported repositories such as Maven Central."
"Dafny verification-aware programming language with Java: " +
"automatically verifying Dafny source code and compiling it to Java source code, " +
"which the Java plugin will then build together with any hand-written Java in the project. " +
"It also provides a robust approach to distributing and managing Dafny dependencies " +
"through Gradle-supported repositories such as Maven Central."
tags.set(listOf("dafny", "verification", "building"))
}
}
}

// Add a source set for the functional test suite
val functionalTestSourceSet = sourceSets.create("functionalTest") {
spotless {
java {
googleJavaFormat()
}
kotlin {
target("**/*.kts")
ktlint()
}
}

// Add a source set for the functional test suite
val functionalTestSourceSet =
sourceSets.create("functionalTest") {
}

configurations["functionalTestImplementation"].extendsFrom(configurations["testImplementation"])

// Add a task to run the functional tests
Expand Down
2 changes: 1 addition & 1 deletion examples/java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ repositories {

dafny {
dafnyVersion.set("4.9.1")
}
}
1 change: 1 addition & 0 deletions examples/java/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ dafny {
dafnyVersion.set("4.9.1")

optionsMap.put("unicode-char", true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ dafny {
dafnyVersion.set("4.9.1")

optionsMap.put("unicode-char", false)
}
}
2 changes: 1 addition & 1 deletion examples/multi-project-incompatible/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pluginManagement {
mavenLocal()
// Uncomment these to use the published version of the plugin from your preferred source.
// gradlePluginPortal()
mavenCentral()
mavenCentral()
}
}
2 changes: 1 addition & 1 deletion examples/multi-project/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pluginManagement {
mavenLocal()
// Uncomment these to use the published version of the plugin from your preferred source.
// gradlePluginPortal()
mavenCentral()
mavenCentral()
}
}
1 change: 1 addition & 0 deletions examples/no-dafny/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file
2 changes: 1 addition & 1 deletion examples/simple-verify/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ repositories {

dafny {
dafnyVersion.set("4.9.1")
}
}
1 change: 1 addition & 0 deletions examples/simple-verify/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file
1 change: 1 addition & 0 deletions examples/using-standard-libraries/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file
2 changes: 1 addition & 1 deletion examples/wrong-dafny-version/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ repositories {

dafny {
dafnyVersion.set("2.3.0")
}
}
1 change: 1 addition & 0 deletions examples/wrong-dafny-version/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file
Original file line number Diff line number Diff line change
Expand Up @@ -5,123 +5,129 @@

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.io.FileWriter;

import org.gradle.api.GradleException;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.*;

/**
* A simple functional test for the 'dafny.gradle.plugin.greeting' plugin.
*/
/** A simple functional test for the 'dafny.gradle.plugin.greeting' plugin. */
class DafnyPluginFunctionalTest {

@Test
void canVerify() throws IOException {
BuildResult result = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("build")
.withProjectDir(new File("examples/simple-verify"))
.buildAndFail();
@Test
void canVerify() throws IOException {
BuildResult result =
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("build")
.withProjectDir(new File("examples/simple-verify"))
.buildAndFail();

Assertions.assertTrue(result.getOutput().contains(
Assertions.assertTrue(
result
.getOutput()
.contains(
"examples/simple-verify/src/main/dafny/simple.dfy(2,9): Error: assertion might not hold"));
Assertions.assertTrue(result.getOutput().contains(
Assertions.assertTrue(
result
.getOutput()
.contains(
"examples/simple-verify/src/main/dafny/nested/simple.dfy(2,9): Error: assertion might not hold"));
}

@Test
void canReferenceDependencies() throws IOException {
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/multi-project"))
.build();
}
}

// Expected to fail because the producer and consumer use different values of
// --unicode-char
@Test
void failsOnIncompatibleDependencies() throws IOException {
BuildResult result = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/multi-project-incompatible"))
.buildAndFail();
Assertions.assertTrue(result.getOutput().contains(
"--unicode-char is set locally to True, but the library was built with False"));
}
@Test
void canReferenceDependencies() throws IOException {
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/multi-project"))
.build();
}

@Test
void succeedsWithNoDafnySourceFiles() throws IOException {
// Expected to fail because the producer and consumer use different values of
// --unicode-char
@Test
void failsOnIncompatibleDependencies() throws IOException {
BuildResult result =
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/no-dafny"))
.build();
}
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/multi-project-incompatible"))
.buildAndFail();
Assertions.assertTrue(
result
.getOutput()
.contains(
"--unicode-char is set locally to True, but the library was built with False"));
}

@Test
void failsOnWrongDafnyVersion() throws IOException {
BuildResult result = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/wrong-dafny-version"))
.buildAndFail();
Assertions.assertTrue(result.getOutput().contains(
"Incorrect Dafny version: expected 2.3.0, found"));
}
@Test
void succeedsWithNoDafnySourceFiles() throws IOException {
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/no-dafny"))
.build();
}

// Regression test: this previously failed because the standard libraries
// end up included in the .doo file,
// so passing `--standard-libraries` again when translating led to duplicate
// definitions.
// Dafny has addressed this by not including the standard libraries which
// building .doo files.
@Test
void supportsStandardLibraries() throws IOException {
@Test
void failsOnWrongDafnyVersion() throws IOException {
BuildResult result =
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/using-standard-libraries"))
.build();
}
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/wrong-dafny-version"))
.buildAndFail();
Assertions.assertTrue(
result.getOutput().contains("Incorrect Dafny version: expected 2.3.0, found"));
}

@Test
void convertsToJava() throws IOException {
var dir = new File("examples/java");
// N.B. This path is set in DafnyPlugin.java, then Dafny adds "-java" suffix
var expected = dir.toPath().resolve("build/generated/sources/fromDafny/java/main-java/Foo/Bar.java").toFile();
// Regression test: this previously failed because the standard libraries
// end up included in the .doo file,
// so passing `--standard-libraries` again when translating led to duplicate
// definitions.
// Dafny has addressed this by not including the standard libraries which
// building .doo files.
@Test
void supportsStandardLibraries() throws IOException {
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "build")
.withProjectDir(new File("examples/using-standard-libraries"))
.build();
}

// Clean build
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "translateDafnyToJava")
.withProjectDir(dir)
.build();
Assertions.assertTrue(expected.exists());
var firstTime = expected.lastModified();
@Test
void convertsToJava() throws IOException {
var dir = new File("examples/java");
// N.B. This path is set in DafnyPlugin.java, then Dafny adds "-java" suffix
var expected =
dir.toPath()
.resolve("build/generated/sources/fromDafny/java/main-java/Foo/Bar.java")
.toFile();

// (Force) an incremental re-build, and check the modified time moved
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("translateDafnyToJava", "--rerun-tasks")
.withProjectDir(dir)
.build();
Assertions.assertTrue(expected.lastModified() > firstTime);
}
// Clean build
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("clean", "translateDafnyToJava")
.withProjectDir(dir)
.build();
Assertions.assertTrue(expected.exists());
var firstTime = expected.lastModified();

// (Force) an incremental re-build, and check the modified time moved
GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("translateDafnyToJava", "--rerun-tasks")
.withProjectDir(dir)
.build();
Assertions.assertTrue(expected.lastModified() > firstTime);
}
}
Loading
Loading