Skip to content

Commit

Permalink
#11 Be more failsafe w.r.t. annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbechtold committed Feb 3, 2020
1 parent a80b326 commit f02ed3c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ If you change the tested code in a manner you consider *valid*, you can approve

## Requirements
Java 8

## Pitfalls
*Always* annotate test classes with `@GoldenMasterTest`, and use *only* `@GoldenMasterRun` for the test methods (cf. this [issue](https://github.com/maxbechtold/golden-master/issues/11))
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String resolveApprovalIdFor(ExtensionContext context) {
}

private String getApprovalId(ExtensionContext context) {
if (!GoldenMasterRun.AUTO_ID.equals(goldenMasterRunAnnotation.id())) {
if (goldenMasterRunAnnotation != null && !GoldenMasterRun.AUTO_ID.equals(goldenMasterRunAnnotation.id())) {
return goldenMasterRunAnnotation.id();
}
return context.getRequiredTestMethod().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public RunInvocationContextProvider() throws IOException {
outputFile = File.createTempFile("goldenmaster_recording_" + System.currentTimeMillis(), ".txt");
}

// FIXME #11 Currently, the harness fails with multiple TestDescriptors, see warning when using @GoldenMasterRun with @Test
@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
return getSupportedAnnotation(context) != null;
}

@Override
Expand All @@ -55,7 +56,7 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
}

private int determineRepetitions(ExtensionContext context) {
GoldenMasterRun goldenMasterAnnotation = getAnnotation(context);
GoldenMasterRun goldenMasterAnnotation = getSupportedAnnotation(context);
if (goldenMasterAnnotation == null) {
return GoldenMasterRun.DEFAULT_REPETITIONS;
}
Expand All @@ -66,8 +67,8 @@ private int determineRepetitions(ExtensionContext context) {
return repetitions;
}

private GoldenMasterRun getAnnotation(ExtensionContext context) {
return context.getElement().get().getAnnotation(GoldenMasterRun.class);
private GoldenMasterRun getSupportedAnnotation(ExtensionContext context) {
return context.getElement().map(element -> element.getAnnotation(GoldenMasterRun.class)).orElse(null);
}

@Override
Expand All @@ -81,7 +82,7 @@ public void beforeAll(ExtensionContext context) throws Exception {

@Override
public void beforeEach(ExtensionContext context) throws Exception {
String runId = new ApprovalIdResolver(getAnnotation(context)).resolveRunIdFor(context);
String runId = new ApprovalIdResolver(getSupportedAnnotation(context)).resolveRunIdFor(context);
Path basePath = Paths.get("src", "test", "resources", "approved");
pathMapper = new TemplatedTestPathMapper<>(context, basePath, runId);
}
Expand All @@ -93,7 +94,7 @@ public void afterEach(ExtensionContext context) throws Exception {
.withConverter(new FileConverter())//
.withReporter(get(context, Reporter.class, REPORTER_KEY)).build();

ApprovalIdResolver approvalIdResolver = new ApprovalIdResolver(getAnnotation(context));
ApprovalIdResolver approvalIdResolver = new ApprovalIdResolver(getSupportedAnnotation(context));

String fileName = approvalIdResolver.resolveApprovalIdFor(context) + ".approved";
approval.verify(outputFile, Paths.get(fileName));
Expand All @@ -112,7 +113,12 @@ public void afterAll(ExtensionContext context) throws Exception {
}

private <T> T get(ExtensionContext context, Class<T> type, String key) {
return getStore(context).get(key, type);
T element = getStore(context).get(key, type);
if (element == null) {
throw new RuntimeException(String.format("Did not find element for key '%s', did you annotate your class with @%s?",
key, GoldenMasterTest.class.getSimpleName()));
}
return element;
}

private Store getStore(ExtensionContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ void setUp() {
Mockito.doReturn("[123]").when(context).getDisplayName();
}

@Test
void noExceptionIfNoAnnotationPresent() throws Exception {
ApprovalIdResolver resolver = new ApprovalIdResolver(null);

String approvalId = resolver.resolveApprovalIdFor(context);

assertThat(approvalId).isEqualTo(someMethod().getName());
}
@Test
void resolvesIdIfSpecified() throws Exception {
String testId = "test-id";
Expand Down

0 comments on commit f02ed3c

Please sign in to comment.