Skip to content

Commit

Permalink
Fix Gradle build with Kotlin DSL assertions
Browse files Browse the repository at this point in the history
Closes gh-1448
  • Loading branch information
snicoll committed Jul 24, 2023
1 parent 6303ff7 commit 0be9637
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public SELF hasSourceCompatibility(String javaVersion) {
* @return {@code this} assertion object
*/
public SELF hasProperty(String name, String value) {
return contains(String.format("%s = '%s'", name, value));
return contains(String.format("%s = %s", name, quote(value)));
}

/**
Expand All @@ -71,10 +71,12 @@ public SELF containsOnlyExtProperties(String... values) {
throw new IllegalArgumentException("Size must be even, it is a set of property=value pairs");
}
for (int i = 0; i < values.length; i += 2) {
builder.append(String.format("\tset('%s', \"%s\")%n", values[i], values[i + 1]));
builder.append(String.format("\tset(%s, \"%s\")%n", quote(values[i]), values[i + 1]));
}
builder.append("}");
return contains(builder.toString());
}

protected abstract String quote(String value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ public GroovyDslGradleBuildAssert(Path buildGradleFile) {
this(TextTestUtils.readContent(buildGradleFile));
}

@Override
protected String quote(String value) {
return "'" + value + "'";
}

/**
* Assert {@code build.gradle} defines a plugin with the specified id and version.
* @param id the id of the plugin
* @param version the version of the plugin
* @return {@code this} assertion object
*/
public GroovyDslGradleBuildAssert hasPlugin(String id, String version) {
return contains(String.format("id '%s' version '%s'", id, version));
return contains(String.format("id %s version %s", quote(id), quote(version)));
}

/**
Expand All @@ -51,7 +56,7 @@ public GroovyDslGradleBuildAssert hasPlugin(String id, String version) {
* @return {@code this} assertion object
*/
public GroovyDslGradleBuildAssert hasPlugin(String id) {
return contains(String.format("id '%s'", id));
return contains(String.format("id %s", quote(id)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Simple assertions for a kotlin build using the kotlin DSL.
*
* @author Prithvi singh
* @author Stephane Nicoll
*/
public class KotlinDslGradleBuildAssert extends GradleBuildAssert<KotlinDslGradleBuildAssert> {

Expand All @@ -35,14 +36,19 @@ public KotlinDslGradleBuildAssert(Path buildGradleFile) {
this(TextTestUtils.readContent(buildGradleFile));
}

@Override
protected String quote(String value) {
return "\"" + value + "\"";
}

/**
* Assert {@code build.gradle.kts} defines a plugin with the specified id and version.
* @param id the id of the plugin
* @param version the version of the plugin
* @return {@code this} assertion object
*/
public KotlinDslGradleBuildAssert hasPlugin(String id, String version) {
return contains(String.format("id('%s') version '%s'", id, version));
return contains(String.format("id(%s) version %s", quote(id), quote(version)));
}

/**
Expand All @@ -51,7 +57,7 @@ public KotlinDslGradleBuildAssert hasPlugin(String id, String version) {
* @return {@code this} assertion object
*/
public KotlinDslGradleBuildAssert hasPlugin(String id) {
return contains(String.format("id('%s')", id));
return contains(String.format("id(%s)", quote(id)));
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
plugins {
id('com.example') version '1.0.0.RELEASE'
id('java')
id("com.example") version "1.0.0.RELEASE"
id("java")
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
group = "com.example"
version = "0.0.1-SNAPSHOT"
sourceCompatibility = "1.8"

repositories {
mavenCentral()
}

ext {
set('acmeVersion', "Brussels.SR2")
set("acmeVersion", "Brussels.SR2")
}

dependencies {
implementation 'com.example.acme:library'
testImplementation 'com.example.acme:library-test'
implementation "com.example.acme:library"
testImplementation "com.example.acme:library-test"
}

dependencyManagement {
Expand Down

0 comments on commit 0be9637

Please sign in to comment.