Skip to content

Commit

Permalink
. t dont set env variables globally to test EnvironmentVariableReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Aug 12, 2024
1 parent a80c919 commit c7fb16a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 111 deletions.
5 changes: 0 additions & 5 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,6 @@
<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
@@ -1,19 +1,29 @@
package org.approvaltests.reporters;

import org.approvaltests.Approvals;
import org.approvaltests.core.Options;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class EnvironmentVariableReporterTest {
@Test
public void testEnvironmentVariable() {
var reporter = EnvironmentVariableReporter.INSTANCE.getReporter();
import static org.junit.jupiter.api.Assertions.*;

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]);
public class EnvironmentVariableReporterTest
{
@Test
public void testEnvironmentVariable()
{
var expected = """
DiffReporter, QuietReporter
""";
try
{
EnvironmentVariableReporter.ENVIRONMENT_VARIABLES = (s) -> "DiffReporter,QuietReporter";
var reporter = new EnvironmentVariableReporter().getReporter();
Approvals.verify(reporter, new Options().inline(expected));
}
finally
{
EnvironmentVariableReporter.ENVIRONMENT_VARIABLES = System::getenv;
}
}
}
5 changes: 4 additions & 1 deletion approvaltests/docs/how_to/ConfigureReporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ The environment variable can take any combination of the following values. Multi
"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.
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)
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

public class BeyondCompareReporter extends FirstWorkingReporter
{
public static final BeyondCompareReporter INSTANCE = new BeyondCompareReporter();
public BeyondCompareReporter()
{
super(
org.approvaltests.reporters.windows.BeyondCompareReporter.INSTANCE,
BeyondCompareMacReporter.INSTANCE
);
}
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,8 +7,7 @@ public DefaultFrontLoadedReporter()
{
super(
// begin-snippet: default_front_loaded_reporter
PitReporter.INSTANCE,
EnvironmentVariableReporter.INSTANCE
PitReporter.INSTANCE, EnvironmentVariableReporter.INSTANCE
// end-snippet
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,74 @@
import org.approvaltests.reporters.macosx.P4MergeReporter;
import org.approvaltests.reporters.macosx.TkDiffReporter;
import org.approvaltests.reporters.windows.*;
import org.lambda.functions.Function1;

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 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 Function1<String, String> ENVIRONMENT_VARIABLES = System::getenv;
public static final EnvironmentVariableReporter INSTANCE = new EnvironmentVariableReporter();
public EnvironmentVariableReporter()
{
String environmentValue = ENVIRONMENT_VARIABLES.call(ENVIRONMENT_VARIABLE_NAME);
if (environmentValue == null)
{
reporter = null;
return;
}

public ApprovalFailureReporter getReporter() {
return reporter;
}

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

return reporter.report(received, approved);
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
Expand Up @@ -2,12 +2,10 @@

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
);
}
public static final VisualStudioCodeReporter INSTANCE = new VisualStudioCodeReporter();
public VisualStudioCodeReporter()
{
super(org.approvaltests.reporters.windows.VisualStudioCodeReporter.INSTANCE,
org.approvaltests.reporters.macosx.VisualStudioCodeReporter.INSTANCE);
}
}

0 comments on commit c7fb16a

Please sign in to comment.