Skip to content

Commit c7fb16a

Browse files
committed
. t dont set env variables globally to test EnvironmentVariableReporter
1 parent a80c919 commit c7fb16a

File tree

7 files changed

+102
-111
lines changed

7 files changed

+102
-111
lines changed

approvaltests-tests/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,6 @@
141141
<plugin>
142142
<artifactId>maven-surefire-plugin</artifactId>
143143
<version>3.3.1</version>
144-
<configuration>
145-
<environmentVariables>
146-
<APPROVAL_TESTS_USE_REPORTER>DiffReporter,QuietReporter</APPROVAL_TESTS_USE_REPORTER>
147-
</environmentVariables>
148-
</configuration>
149144
</plugin>
150145
<plugin>
151146
<groupId>org.apache.maven.plugins</groupId>
Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package org.approvaltests.reporters;
22

3+
import org.approvaltests.Approvals;
4+
import org.approvaltests.core.Options;
35
import org.junit.jupiter.api.Assertions;
46
import org.junit.jupiter.api.Test;
57

6-
public class EnvironmentVariableReporterTest {
7-
@Test
8-
public void testEnvironmentVariable() {
9-
var reporter = EnvironmentVariableReporter.INSTANCE.getReporter();
8+
import static org.junit.jupiter.api.Assertions.*;
109

11-
Assertions.assertInstanceOf(MultiReporter.class, reporter);
12-
var multiReporter = (MultiReporter) reporter;
13-
var reporters = multiReporter.getReporters();
14-
15-
Assertions.assertEquals(2, reporters.length);
16-
Assertions.assertInstanceOf(DiffReporter.class, reporters[0]);
17-
Assertions.assertInstanceOf(QuietReporter.class, reporters[1]);
10+
public class EnvironmentVariableReporterTest
11+
{
12+
@Test
13+
public void testEnvironmentVariable()
14+
{
15+
var expected = """
16+
DiffReporter, QuietReporter
17+
""";
18+
try
19+
{
20+
EnvironmentVariableReporter.ENVIRONMENT_VARIABLES = (s) -> "DiffReporter,QuietReporter";
21+
var reporter = new EnvironmentVariableReporter().getReporter();
22+
Approvals.verify(reporter, new Options().inline(expected));
23+
}
24+
finally
25+
{
26+
EnvironmentVariableReporter.ENVIRONMENT_VARIABLES = System::getenv;
1827
}
28+
}
1929
}

approvaltests/docs/how_to/ConfigureReporters.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ The environment variable can take any combination of the following values. Multi
121121
"WindowsDiffReporter",
122122
```
123123

124-
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.
124+
For example, setting `APPROVAL_TESTS_USE_REPORTER=AutoApproveReporter` allows you to approve all pending changes at once
125+
without modifying the source code and rebuilding the code to temporarily choose a different reporter. Similarly, setting
126+
`APPROVAL_TESTS_USE_REPORTER=MeldMergeReporter` allows you to explicitly choose a reporter you want to use locally,
127+
without influencing the default reporter priorities and setup for fellow developers.
125128

126129
See Also: [Reporters](../reference/Reporters.md)

approvaltests/src/main/java/org/approvaltests/reporters/BeyondCompareReporter.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
public class BeyondCompareReporter extends FirstWorkingReporter
66
{
7-
public static final BeyondCompareReporter INSTANCE = new BeyondCompareReporter();
8-
public BeyondCompareReporter()
9-
{
10-
super(
11-
org.approvaltests.reporters.windows.BeyondCompareReporter.INSTANCE,
12-
BeyondCompareMacReporter.INSTANCE
13-
);
14-
}
7+
public static final BeyondCompareReporter INSTANCE = new BeyondCompareReporter();
8+
public BeyondCompareReporter()
9+
{
10+
super(org.approvaltests.reporters.windows.BeyondCompareReporter.INSTANCE, BeyondCompareMacReporter.INSTANCE);
11+
}
1512
}

approvaltests/src/main/java/org/approvaltests/reporters/DefaultFrontLoadedReporter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ public DefaultFrontLoadedReporter()
77
{
88
super(
99
// begin-snippet: default_front_loaded_reporter
10-
PitReporter.INSTANCE,
11-
EnvironmentVariableReporter.INSTANCE
10+
PitReporter.INSTANCE, EnvironmentVariableReporter.INSTANCE
1211
// end-snippet
1312
);
1413
}

approvaltests/src/main/java/org/approvaltests/reporters/EnvironmentVariableReporter.java

Lines changed: 65 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,74 @@
88
import org.approvaltests.reporters.macosx.P4MergeReporter;
99
import org.approvaltests.reporters.macosx.TkDiffReporter;
1010
import org.approvaltests.reporters.windows.*;
11+
import org.lambda.functions.Function1;
1112

1213
import java.util.*;
1314

14-
public class EnvironmentVariableReporter implements ApprovalFailureReporter {
15-
private final ApprovalFailureReporter reporter;
16-
17-
private static final Map<String, Class<? extends ApprovalFailureReporter>> REPORTER_MAP = Map.ofEntries(
18-
Map.entry("AraxisMergeReporter", AraxisMergeReporter.class),
19-
Map.entry("AutoApproveReporter", AutoApproveReporter.class),
20-
Map.entry("AutoApproveWhenEmptyReporter", AutoApproveWhenEmptyReporter.class),
21-
Map.entry("BeyondCompareReporter", BeyondCompareReporter.class),
22-
Map.entry("ClipboardReporter", ClipboardReporter.class),
23-
Map.entry("CodeCompareReporter", CodeCompareReporter.class),
24-
Map.entry("DelayedClipboardReporter", DelayedClipboardReporter.class),
25-
Map.entry("DiffMergeReporter", DiffMergeReporter.class),
26-
Map.entry("DiffReporter", DiffReporter.class),
27-
Map.entry("FileCaptureReporter", FileCaptureReporter.class),
28-
Map.entry("ImageReporter", ImageReporter.class),
29-
Map.entry("ImageWebReporter", ImageWebReporter.class),
30-
Map.entry("IntelliJReporter", IntelliJReporter.class),
31-
Map.entry("JunitReporter", JunitReporter.class),
32-
Map.entry("KDiff3Reporter", KDiff3Reporter.class),
33-
Map.entry("KaleidoscopeDiffReporter", KaleidoscopeDiffReporter.class),
34-
Map.entry("MeldMergeReporter", MeldMergeReporter.class),
35-
Map.entry("P4MergeReporter", P4MergeReporter.class),
36-
Map.entry("PitReporter", PitReporter.class),
37-
Map.entry("QuietReporter", QuietReporter.class),
38-
Map.entry("TestNgReporter", TestNgReporter.class),
39-
Map.entry("TextWebReporter", TextWebReporter.class),
40-
Map.entry("TkDiffReporter", TkDiffReporter.class),
41-
Map.entry("TortoiseDiffReporter", TortoiseDiffReporter.class),
42-
Map.entry("VisualStudioCodeReporter", VisualStudioCodeReporter.class),
43-
Map.entry("WinMergeReporter", WinMergeReporter.class),
44-
Map.entry("WindowsDiffReporter", WindowsDiffReporter.class)
45-
);
46-
47-
public static final String ENVIRONMENT_VARIABLE_NAME = "APPROVAL_TESTS_USE_REPORTER";
48-
public static final EnvironmentVariableReporter INSTANCE = new EnvironmentVariableReporter();
49-
50-
public EnvironmentVariableReporter() {
51-
String environmentValue = System.getenv(ENVIRONMENT_VARIABLE_NAME);
52-
if(environmentValue == null) {
53-
reporter = null;
54-
return;
55-
}
56-
57-
var reporters = Arrays.stream(environmentValue.split(","))
58-
.distinct()
59-
.map(REPORTER_MAP::get)
60-
.filter(Objects::nonNull)
61-
.map(reporterType -> (ApprovalFailureReporter) ClassUtils.create(reporterType))
62-
.toList();
63-
64-
switch(reporters.size()) {
65-
case 0: {
66-
reporter = null;
67-
break;
68-
}
69-
case 1: {
70-
reporter = reporters.get(0);
71-
break;
72-
}
73-
default: {
74-
reporter = new MultiReporter(reporters);
75-
break;
76-
}
77-
}
15+
public class EnvironmentVariableReporter implements ApprovalFailureReporter
16+
{
17+
private final ApprovalFailureReporter reporter;
18+
private static final Map<String, Class<? extends ApprovalFailureReporter>> REPORTER_MAP = Map
19+
.ofEntries(Map.entry("AraxisMergeReporter", AraxisMergeReporter.class),
20+
Map.entry("AutoApproveReporter", AutoApproveReporter.class),
21+
Map.entry("AutoApproveWhenEmptyReporter", AutoApproveWhenEmptyReporter.class),
22+
Map.entry("BeyondCompareReporter", BeyondCompareReporter.class),
23+
Map.entry("ClipboardReporter", ClipboardReporter.class),
24+
Map.entry("CodeCompareReporter", CodeCompareReporter.class),
25+
Map.entry("DelayedClipboardReporter", DelayedClipboardReporter.class),
26+
Map.entry("DiffMergeReporter", DiffMergeReporter.class), Map.entry("DiffReporter", DiffReporter.class),
27+
Map.entry("FileCaptureReporter", FileCaptureReporter.class),
28+
Map.entry("ImageReporter", ImageReporter.class), Map.entry("ImageWebReporter", ImageWebReporter.class),
29+
Map.entry("IntelliJReporter", IntelliJReporter.class), Map.entry("JunitReporter", JunitReporter.class),
30+
Map.entry("KDiff3Reporter", KDiff3Reporter.class),
31+
Map.entry("KaleidoscopeDiffReporter", KaleidoscopeDiffReporter.class),
32+
Map.entry("MeldMergeReporter", MeldMergeReporter.class),
33+
Map.entry("P4MergeReporter", P4MergeReporter.class), Map.entry("PitReporter", PitReporter.class),
34+
Map.entry("QuietReporter", QuietReporter.class), Map.entry("TestNgReporter", TestNgReporter.class),
35+
Map.entry("TextWebReporter", TextWebReporter.class), Map.entry("TkDiffReporter", TkDiffReporter.class),
36+
Map.entry("TortoiseDiffReporter", TortoiseDiffReporter.class),
37+
Map.entry("VisualStudioCodeReporter", VisualStudioCodeReporter.class),
38+
Map.entry("WinMergeReporter", WinMergeReporter.class),
39+
Map.entry("WindowsDiffReporter", WindowsDiffReporter.class));
40+
public static final String ENVIRONMENT_VARIABLE_NAME = "APPROVAL_TESTS_USE_REPORTER";
41+
public static Function1<String, String> ENVIRONMENT_VARIABLES = System::getenv;
42+
public static final EnvironmentVariableReporter INSTANCE = new EnvironmentVariableReporter();
43+
public EnvironmentVariableReporter()
44+
{
45+
String environmentValue = ENVIRONMENT_VARIABLES.call(ENVIRONMENT_VARIABLE_NAME);
46+
if (environmentValue == null)
47+
{
48+
reporter = null;
49+
return;
7850
}
79-
80-
public ApprovalFailureReporter getReporter() {
81-
return reporter;
82-
}
83-
84-
@Override
85-
public boolean report(String received, String approved) {
86-
if(reporter == null) {
87-
return false;
88-
}
89-
90-
return reporter.report(received, approved);
51+
var reporters = Arrays.stream(environmentValue.split(",")).distinct().map(REPORTER_MAP::get)
52+
.filter(Objects::nonNull).map(reporterType -> (ApprovalFailureReporter) ClassUtils.create(reporterType))
53+
.toList();
54+
switch (reporters.size())
55+
{
56+
case 0 : {
57+
reporter = null;
58+
break;
59+
}
60+
case 1 : {
61+
reporter = reporters.get(0);
62+
break;
63+
}
64+
default : {
65+
reporter = new MultiReporter(reporters);
66+
break;
67+
}
9168
}
69+
}
70+
public ApprovalFailureReporter getReporter()
71+
{
72+
return reporter;
73+
}
74+
@Override
75+
public boolean report(String received, String approved)
76+
{
77+
if (reporter == null)
78+
{ return false; }
79+
return reporter.report(received, approved);
80+
}
9281
}

approvaltests/src/main/java/org/approvaltests/reporters/VisualStudioCodeReporter.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
public class VisualStudioCodeReporter extends FirstWorkingReporter
44
{
5-
public static final VisualStudioCodeReporter INSTANCE = new VisualStudioCodeReporter();
6-
public VisualStudioCodeReporter()
7-
{
8-
super(
9-
org.approvaltests.reporters.windows.VisualStudioCodeReporter.INSTANCE,
10-
org.approvaltests.reporters.macosx.VisualStudioCodeReporter.INSTANCE
11-
);
12-
}
5+
public static final VisualStudioCodeReporter INSTANCE = new VisualStudioCodeReporter();
6+
public VisualStudioCodeReporter()
7+
{
8+
super(org.approvaltests.reporters.windows.VisualStudioCodeReporter.INSTANCE,
9+
org.approvaltests.reporters.macosx.VisualStudioCodeReporter.INSTANCE);
10+
}
1311
}

0 commit comments

Comments
 (0)