Skip to content

Commit ff929cc

Browse files
committed
Improve message for invalid .errors entries
1 parent f8a846d commit ff929cc

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

config/spotbugs/filter.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@
149149
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
150150
</Match>
151151

152+
<!-- This is a false positive. The value is guarded behind an Objects.requireNonNull -->
153+
<Match>
154+
<Class name="software.amazon.smithy.model.validation.testrunner.SmithyTestCase"/>
155+
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
156+
</Match>
157+
152158
<!-- We don't care about the return value of putIfAbsent in this cache -->
153159
<Match>
154160
<Class name="software.amazon.smithy.model.shapes.ShapeId$ShapeIdFactory"/>

smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
package software.amazon.smithy.model.validation.testrunner;
1717

18+
import static java.lang.String.format;
19+
20+
import java.nio.file.Paths;
1821
import java.util.Arrays;
1922
import java.util.Collection;
2023
import java.util.Collections;
@@ -164,16 +167,19 @@ private static String inferErrorFileLocation(String modelLocation) {
164167

165168
private static List<ValidationEvent> loadExpectedEvents(String errorsFileLocation) {
166169
String contents = IoUtils.readUtf8File(errorsFileLocation);
170+
String fileName = Objects.requireNonNull(Paths.get(errorsFileLocation).getFileName()).toString();
167171
return Arrays.stream(contents.split(System.lineSeparator()))
168172
.filter(line -> !line.trim().isEmpty())
169-
.map(SmithyTestCase::parseValidationEvent)
173+
.map(line -> parseValidationEvent(line, fileName))
170174
.collect(Collectors.toList());
171175
}
172176

173-
static ValidationEvent parseValidationEvent(String event) {
177+
static ValidationEvent parseValidationEvent(String event, String fileName) {
174178
Matcher matcher = EVENT_PATTERN.matcher(event);
175179
if (!matcher.find()) {
176-
throw new IllegalArgumentException("Invalid validation event: " + event);
180+
throw new IllegalArgumentException(format("Invalid validation event in file `%s`, the following event did "
181+
+ "not match the expected regular expression `%s`: %s",
182+
fileName, EVENT_PATTERN.pattern(), event));
177183
}
178184

179185
// Construct a dummy source location since we don't validate it.

smithy-model/src/test/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCaseTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.hamcrest.MatcherAssert.assertThat;
1919
import static org.hamcrest.Matchers.equalTo;
2020
import static org.hamcrest.Matchers.is;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2122

2223
import java.util.Collections;
2324
import org.junit.jupiter.api.Assertions;
@@ -32,14 +33,17 @@
3233
public class SmithyTestCaseTest {
3334
@Test
3435
public void validatesThatEventsAreValid() {
35-
Assertions.assertThrows(
36+
IllegalArgumentException e = Assertions.assertThrows(
3637
IllegalArgumentException.class,
37-
() -> SmithyTestCase.parseValidationEvent("[ERROR] - m"));
38+
() -> SmithyTestCase.parseValidationEvent("[ERROR] - m", "filename"));
39+
40+
assertTrue(e.getMessage().contains("`filename`"));
41+
assertTrue(e.getMessage().contains("SUPPRESSED|NOTE|WARNING|DANGER|ERROR"));
3842
}
3943

4044
@Test
4145
public void parsesValidEvents() {
42-
SmithyTestCase.parseValidationEvent("[ERROR] -: message | EventId /filename:0:0");
46+
SmithyTestCase.parseValidationEvent("[ERROR] -: message | EventId /filename:0:0", "filename");
4347
}
4448

4549
@Test

0 commit comments

Comments
 (0)