Skip to content

Commit 7744f82

Browse files
committed
Enable RequireExplicitNullMarking
As part of this change, the assertions on the configuration of Error Prone have been split up. This was necessary as the ordering of the checks map is undefined which results in the toString() output of ErrorProneOptions that's used in the tests' build.gradle script being non-deterministic. Closes gh-23
1 parent 812181a commit 7744f82

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/main/java/io/spring/gradle/nullability/NullabilityOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private Map<String, String> checkOptions(Checking checking) {
7474

7575
private Map<String, CheckSeverity> checks(Checking checking) {
7676
if (checking != Checking.DISABLED) {
77-
return Map.of("NullAway", CheckSeverity.ERROR);
77+
return Map.of("NullAway", CheckSeverity.ERROR, "RequireExplicitNullMarking", CheckSeverity.ERROR);
7878
}
7979
return Collections.emptyMap();
8080
}

src/test/java/io/spring/gradle/nullability/NullabilityPluginIntegrationTests.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ void usesCustomNullAwayVersion() {
6767
@Test
6868
void configuresErrorProneOnCompileJava() {
6969
BuildResult result = this.gradleBuild.build("checkCompileJava");
70-
assertThat(result.getOutput())
71-
.contains("-XepDisableAllChecks -Xep:NullAway:ERROR -XepOpt:NullAway:OnlyNullMarked=true "
72-
+ "-XepOpt:NullAway:CustomContractAnnotations=org.springframework.lang.Contract "
73-
+ "-XepOpt:NullAway:JSpecifyMode=true");
70+
assertThat(result.getOutput()).contains("-XepDisableAllChecks")
71+
.contains("-Xep:NullAway:ERROR")
72+
.contains("-Xep:RequireExplicitNullMarking:ERROR")
73+
.contains("-XepOpt:NullAway:OnlyNullMarked=true")
74+
.contains("-XepOpt:NullAway:CustomContractAnnotations=org.springframework.lang.Contract")
75+
.contains("-XepOpt:NullAway:JSpecifyMode=true");
7476
}
7577

7678
@Test
@@ -82,10 +84,15 @@ void disablesErrorProneOnCompileTestJavaByDefault() {
8284
@Test
8385
void configuresErrorProneOnCompileTestJavaWhenEnabled() {
8486
BuildResult result = this.gradleBuild.build("checkCompileTestJava");
85-
assertThat(result.getOutput()).contains(
86-
"-XepDisableAllChecks -XepCompilingTestOnlyCode -Xep:NullAway:ERROR -XepOpt:NullAway:OnlyNullMarked=true "
87-
+ "-XepOpt:NullAway:CustomContractAnnotations=org.springframework.lang.Contract,org.assertj.core.internal.annotation.Contract "
88-
+ "-XepOpt:NullAway:JSpecifyMode=true -XepOpt:NullAway:HandleTestAssertionLibraries=true");
87+
assertThat(result.getOutput()).contains("-XepDisableAllChecks")
88+
.contains("-XepCompilingTestOnlyCode")
89+
.contains("-Xep:NullAway:ERROR")
90+
.contains("-Xep:RequireExplicitNullMarking:ERROR")
91+
.contains("-XepOpt:NullAway:OnlyNullMarked=true")
92+
.contains(
93+
"-XepOpt:NullAway:CustomContractAnnotations=org.springframework.lang.Contract,org.assertj.core.internal.annotation.Contract")
94+
.contains("-XepOpt:NullAway:JSpecifyMode=true")
95+
.contains("-XepOpt:NullAway:HandleTestAssertionLibraries=true");
8996
}
9097

9198
@Test
@@ -109,17 +116,48 @@ void compileFailsForNullabilityViolationInTestCodeWhenCheckingIsEnabled() throws
109116
assertThat(result.getOutput()).contains("[NullAway] assigning @Nullable expression to @NonNull field");
110117
}
111118

112-
private void writeSource(String sourceSetName) {
119+
@Test
120+
void compileFailsForCodeThatIsNotNullMarked() throws IOException {
121+
Path pkg = createSrcDirectories("main");
122+
writeExampleClass(pkg);
123+
BuildResult result = this.gradleBuild.prepareRunner("compileJava").buildAndFail();
124+
assertThat(result.getOutput()).contains("[RequireExplicitNullMarking]");
125+
}
126+
127+
private Path createSrcDirectories(String sourceSetName) {
113128
Path projectDir = this.gradleBuild.getProjectDir().toPath();
114129
Path pkg = projectDir.resolve("src/%s/java/com/example".formatted(sourceSetName));
115130
try {
116131
Files.createDirectories(pkg);
132+
}
133+
catch (IOException ex) {
134+
throw new UncheckedIOException(ex);
135+
}
136+
return pkg;
137+
}
138+
139+
private void writeSource(String sourceSetName) {
140+
Path pkg = createSrcDirectories(sourceSetName);
141+
writePackageInfo(pkg);
142+
writeExampleClass(pkg);
143+
}
144+
145+
private void writePackageInfo(Path pkg) {
146+
try {
117147
Files.writeString(pkg.resolve("package-info.java"), """
118148
@NullMarked
119149
package com.example;
120150
121151
import org.jspecify.annotations.NullMarked;
122152
""");
153+
}
154+
catch (IOException ex) {
155+
throw new UncheckedIOException(ex);
156+
}
157+
}
158+
159+
private void writeExampleClass(Path pkg) {
160+
try {
123161
Files.writeString(pkg.resolve("Example.java"), """
124162
package com.example;
125163

0 commit comments

Comments
 (0)