-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use special hamcrest matcher for Gradle property non-mutable exception
Signed-off-by: Daniel Lacasse <daniel@lacasse.io>
- Loading branch information
Showing
11 changed files
with
164 additions
and
17 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
gradle/libraries/hamcrest-matchers/hamcrest-gradle/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
dependencies { | ||
def hamcrestVersion = 'latest.release' | ||
api "org.hamcrest:hamcrest:${hamcrestVersion}" | ||
compileOnly "dev.gradleplugins:gradle-api:${gradle.gradleVersion}" | ||
} |
91 changes: 91 additions & 0 deletions
91
...mcrest-gradle/src/main/java/dev/gradleplugins/hamcrest/gradle/GradleProviderMatchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 dev.gradleplugins.hamcrest.gradle; | ||
|
||
import org.hamcrest.Matcher; | ||
import org.hamcrest.Matchers; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public final class GradleProviderMatchers { | ||
private GradleProviderMatchers() {} | ||
|
||
/** | ||
* Matches any exception message variant thrown when disallowed change applied to Gradle property. | ||
* | ||
* @return an exception message matcher, never null. | ||
*/ | ||
public static Matcher<String> forChangesDisallowed() { | ||
return forChangesDisallowed(ON_ANY_TARGET); | ||
} | ||
|
||
private static final PropertyTarget ON_ANY_TARGET = new PropertyTarget() { | ||
@Override | ||
public String toString() { | ||
return ".+"; | ||
} | ||
}; | ||
|
||
public static Matcher<String> forChangesDisallowed(PropertyTarget target) { | ||
return Matchers.matchesPattern(exceptionMessagePattern(target.toString())); | ||
} | ||
|
||
public interface PropertyTarget { | ||
static PropertyTargetBuilder ofOwner(Object toString) { | ||
return new PropertyTargetBuilder() { | ||
@Override | ||
public PropertyTarget property(String name) { | ||
return new PropertyTarget() { | ||
@Override | ||
public String toString() { | ||
return toString + " property '" + name + "'"; | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
static PropertyTarget onProperty(String name) { | ||
return new PropertyTarget() { | ||
@Override | ||
public String toString() { | ||
return ".*property '" + name + "'"; | ||
} | ||
}; | ||
} | ||
|
||
interface PropertyTargetBuilder extends PropertyTarget { | ||
PropertyTarget property(String name); | ||
} | ||
} | ||
|
||
// Matches message such as: | ||
// - The value for this property cannot be changed any further. | ||
// - The value for property 'foo' cannot be changed any further. | ||
// - The value for this file collection cannot be changed. | ||
// Note that we implicitly check for finalized value as final value imply disallowed changes. | ||
// Notice the difference in the exception message for _file collection_. | ||
// Sample "The value for test suite 'functionalTest' property 'sourceSet' is final and cannot be changed any further." | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^^ | ||
// owner toString prop display name if final prop vs FC | ||
// | ||
// Sample "The value for property 'testedSourceSet' cannot be changed any further." | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ | ||
// prop display name prop vs FC | ||
private static Pattern exceptionMessagePattern(String targetPattern) { | ||
return Pattern.compile("The value for " + targetPattern + " (is final and )?cannot be changed( any further)?."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import org.gradle.api.initialization.resolve.RepositoriesMode | ||
|
||
include 'hamcrest-gradle' | ||
|
||
rootProject.name = 'hamcrest-matchers' | ||
|
||
gradle.allprojects { | ||
group = 'dev.gradleplugins' | ||
|
||
pluginManager.withPlugin('java-base') { | ||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
} | ||
|
||
dependencyResolutionManagement { | ||
repositories { | ||
mavenCentral() | ||
} | ||
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters