Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for environment variable-based reporters #520

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<environmentVariables>
<APPROVAL_TESTS_USE_REPORTER>DiffReporter,QuietReporter</APPROVAL_TESTS_USE_REPORTER>
</environmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.reporters.DiffReporter;
import org.approvaltests.reporters.MultiReporter;

public class PackageSettings
{
public static class ApprovalTestsPackageLevelReporter extends DiffReporter
{
}
public static ApprovalFailureReporter UseReporter = new ApprovalTestsPackageLevelReporter();
public static ApprovalFailureReporter FrontloadedReporter = new MultiReporter();
public static boolean AllowMultipleVerifyCallsPerTest = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.approvaltests.reporters;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class EnvironmentVariableReporterTest {
@Test
public void testEnvironmentVariable() {
var reporter = EnvironmentVariableReporter.INSTANCE.getReporter();

Assertions.assertInstanceOf(MultiReporter.class, reporter);
var multiReporter = (MultiReporter) reporter;
var reporters = multiReporter.getReporters();

Assertions.assertEquals(2, reporters.length);
Assertions.assertInstanceOf(DiffReporter.class, reporters[0]);
Assertions.assertInstanceOf(QuietReporter.class, reporters[1]);
}
}
41 changes: 40 additions & 1 deletion approvaltests/docs/how_to/ConfigureReporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* [Method 1: Via Options](#method-1-via-options)
* [Method 2: Class and Method Level Annotations](#method-2-class-and-method-level-annotations)
* [Single Reporter](#single-reporter)
* [Method 3: Package Level](#method-3-package-level)<!-- endToc -->
* [Method 3: Package Level](#method-3-package-level)
* [Method 4: Environment Variables](#method-4-environment-variables)<!-- endToc -->

## Problem Statement
Reporters are the part of Approval Tests that launch diff tools when things do not match.
Expand Down Expand Up @@ -84,4 +85,42 @@ public class PackageSettings

You can find out more about [Package Level settings here](../reference/PackageSettings.md#top)

### Method 4: Environment Variables

Lastly, it is possible to set the `APPROVAL_TESTS_USE_REPORTER` environment variable to influence the reporters. Setting a reporter through the environment overrides all other reporters - so use this sparingly and only to change reporter behaviour for individual runs or on individual machines.

The environment variable can take any combination of the following values. Multiple values should be separated by a comma, without whitespace.

```java
"AraxisMergeReporter",
"AutoApproveReporter",
"AutoApproveWhenEmptyReporter",
"BeyondCompareReporter",
"ClipboardReporter",
"CodeCompareReporter",
"DelayedClipboardReporter",
"DiffMergeReporter",
"DiffReporter",
"FileCaptureReporter",
"ImageReporter",
"ImageWebReporter",
"IntelliJReporter",
"JunitReporter",
"KDiff3Reporter",
"KaleidoscopeDiffReporter",
"MeldMergeReporter",
"P4MergeReporter",
"PitReporter",
"QuietReporter",
"TestNgReporter",
"TextWebReporter",
"TkDiffReporter",
"TortoiseDiffReporter",
"VisualStudioCodeReporter",
"WinMergeReporter",
"WindowsDiffReporter",
```

For example, setting `APPROVAL_TESTS_USE_REPORTER=AutoApproveReporter` allows you to approve all pending changes at once without modifying the source code and rebuilding the code to temporarily choose a different reporter. Similarly, setting `APPROVAL_TESTS_USE_REPORTER=MeldMergeReporter` allows you to explicitly choose a reporter you want to use locally, without influencing the default reporter priorities and setup for fellow developers.

See Also: [Reporters](../reference/Reporters.md)
3 changes: 2 additions & 1 deletion approvaltests/docs/reference/FrontLoadedReporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Currently, the DefaultFrontLoadedReporter that comes with ApprovalTests is set u
<!-- snippet: default_front_loaded_reporter -->
<a id='snippet-default_front_loaded_reporter'></a>
```java
PitReporter.INSTANCE
PitReporter.INSTANCE,
EnvironmentVariableReporter.INSTANCE
```
<sup><a href='/approvaltests/src/main/java/org/approvaltests/reporters/DefaultFrontLoadedReporter.java#L9-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-default_front_loaded_reporter' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Expand Down
8 changes: 8 additions & 0 deletions approvaltests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.approvaltests.reporters;

import org.approvaltests.reporters.macosx.BeyondCompareMacReporter;

public class BeyondCompareReporter extends FirstWorkingReporter
{
public static final BeyondCompareReporter INSTANCE = new BeyondCompareReporter();
public BeyondCompareReporter()
{
super(
org.approvaltests.reporters.windows.BeyondCompareReporter.INSTANCE,
BeyondCompareMacReporter.INSTANCE
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public DefaultFrontLoadedReporter()
{
super(
// begin-snippet: default_front_loaded_reporter
PitReporter.INSTANCE
PitReporter.INSTANCE,
EnvironmentVariableReporter.INSTANCE
// end-snippet
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.approvaltests.reporters;

import com.spun.util.ClassUtils;
import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.reporters.intellij.IntelliJReporter;
import org.approvaltests.reporters.linux.MeldMergeReporter;
import org.approvaltests.reporters.macosx.KaleidoscopeDiffReporter;
import org.approvaltests.reporters.macosx.P4MergeReporter;
import org.approvaltests.reporters.macosx.TkDiffReporter;
import org.approvaltests.reporters.windows.*;

import java.util.*;

public class EnvironmentVariableReporter implements ApprovalFailureReporter {
private final ApprovalFailureReporter reporter;

private static final Map<String, Class<? extends ApprovalFailureReporter>> REPORTER_MAP = Map.ofEntries(
Map.entry("AraxisMergeReporter", AraxisMergeReporter.class),
Map.entry("AutoApproveReporter", AutoApproveReporter.class),
Map.entry("AutoApproveWhenEmptyReporter", AutoApproveWhenEmptyReporter.class),
Map.entry("BeyondCompareReporter", BeyondCompareReporter.class),
Map.entry("ClipboardReporter", ClipboardReporter.class),
Map.entry("CodeCompareReporter", CodeCompareReporter.class),
Map.entry("DelayedClipboardReporter", DelayedClipboardReporter.class),
Map.entry("DiffMergeReporter", DiffMergeReporter.class),
Map.entry("DiffReporter", DiffReporter.class),
Map.entry("FileCaptureReporter", FileCaptureReporter.class),
Map.entry("ImageReporter", ImageReporter.class),
Map.entry("ImageWebReporter", ImageWebReporter.class),
Map.entry("IntelliJReporter", IntelliJReporter.class),
Map.entry("JunitReporter", JunitReporter.class),
Map.entry("KDiff3Reporter", KDiff3Reporter.class),
Map.entry("KaleidoscopeDiffReporter", KaleidoscopeDiffReporter.class),
Map.entry("MeldMergeReporter", MeldMergeReporter.class),
Map.entry("P4MergeReporter", P4MergeReporter.class),
Map.entry("PitReporter", PitReporter.class),
Map.entry("QuietReporter", QuietReporter.class),
Map.entry("TestNgReporter", TestNgReporter.class),
Map.entry("TextWebReporter", TextWebReporter.class),
Map.entry("TkDiffReporter", TkDiffReporter.class),
Map.entry("TortoiseDiffReporter", TortoiseDiffReporter.class),
Map.entry("VisualStudioCodeReporter", VisualStudioCodeReporter.class),
Map.entry("WinMergeReporter", WinMergeReporter.class),
Map.entry("WindowsDiffReporter", WindowsDiffReporter.class)
);

public static final String ENVIRONMENT_VARIABLE_NAME = "APPROVAL_TESTS_USE_REPORTER";
public static final EnvironmentVariableReporter INSTANCE = new EnvironmentVariableReporter();

public EnvironmentVariableReporter() {
String environmentValue = System.getenv(ENVIRONMENT_VARIABLE_NAME);
if(environmentValue == null) {
reporter = null;
return;
}

var reporters = Arrays.stream(environmentValue.split(","))
.distinct()
.map(REPORTER_MAP::get)
.filter(Objects::nonNull)
.map(reporterType -> (ApprovalFailureReporter) ClassUtils.create(reporterType))
.toList();

switch(reporters.size()) {
case 0: {
reporter = null;
break;
}
case 1: {
reporter = reporters.get(0);
break;
}
default: {
reporter = new MultiReporter(reporters);
break;
}
}
}

public ApprovalFailureReporter getReporter() {
return reporter;
}

@Override
public boolean report(String received, String approved) {
if(reporter == null) {
return false;
}

return reporter.report(received, approved);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.approvaltests.reporters;

public class VisualStudioCodeReporter extends FirstWorkingReporter
{
public static final VisualStudioCodeReporter INSTANCE = new VisualStudioCodeReporter();
public VisualStudioCodeReporter()
{
super(
org.approvaltests.reporters.windows.VisualStudioCodeReporter.INSTANCE,
org.approvaltests.reporters.macosx.VisualStudioCodeReporter.INSTANCE
);
}
}
Loading