diff --git a/approvaltests-tests/pom.xml b/approvaltests-tests/pom.xml index 390dcb28..2f06d3b4 100644 --- a/approvaltests-tests/pom.xml +++ b/approvaltests-tests/pom.xml @@ -141,6 +141,11 @@ maven-surefire-plugin 3.3.1 + + + DiffReporter,QuietReporter + + org.apache.maven.plugins diff --git a/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java b/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java index 6a6f19d7..a9c7695b 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/PackageSettings.java @@ -2,6 +2,7 @@ import org.approvaltests.core.ApprovalFailureReporter; import org.approvaltests.reporters.DiffReporter; +import org.approvaltests.reporters.MultiReporter; public class PackageSettings { @@ -9,5 +10,6 @@ public static class ApprovalTestsPackageLevelReporter extends DiffReporter { } public static ApprovalFailureReporter UseReporter = new ApprovalTestsPackageLevelReporter(); + public static ApprovalFailureReporter FrontloadedReporter = new MultiReporter(); public static boolean AllowMultipleVerifyCallsPerTest = true; } diff --git a/approvaltests-tests/src/test/java/org/approvaltests/reporters/EnvironmentVariableReporterTest.java b/approvaltests-tests/src/test/java/org/approvaltests/reporters/EnvironmentVariableReporterTest.java new file mode 100644 index 00000000..f6560f4e --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/reporters/EnvironmentVariableReporterTest.java @@ -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]); + } +} diff --git a/approvaltests/docs/how_to/ConfigureReporters.md b/approvaltests/docs/how_to/ConfigureReporters.md index 6761dbb0..a4f91716 100644 --- a/approvaltests/docs/how_to/ConfigureReporters.md +++ b/approvaltests/docs/how_to/ConfigureReporters.md @@ -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) + * [Method 3: Package Level](#method-3-package-level) + * [Method 4: Environment Variables](#method-4-environment-variables) ## Problem Statement Reporters are the part of Approval Tests that launch diff tools when things do not match. @@ -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) diff --git a/approvaltests/docs/reference/FrontLoadedReporter.md b/approvaltests/docs/reference/FrontLoadedReporter.md index 8a4231a0..844ccc0c 100644 --- a/approvaltests/docs/reference/FrontLoadedReporter.md +++ b/approvaltests/docs/reference/FrontLoadedReporter.md @@ -23,7 +23,8 @@ Currently, the DefaultFrontLoadedReporter that comes with ApprovalTests is set u ```java -PitReporter.INSTANCE +PitReporter.INSTANCE, +EnvironmentVariableReporter.INSTANCE ``` snippet source | anchor diff --git a/approvaltests/pom.xml b/approvaltests/pom.xml index ce4c0d35..d2330f43 100644 --- a/approvaltests/pom.xml +++ b/approvaltests/pom.xml @@ -164,6 +164,14 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 17 + 17 + + diff --git a/approvaltests/src/main/java/org/approvaltests/reporters/BeyondCompareReporter.java b/approvaltests/src/main/java/org/approvaltests/reporters/BeyondCompareReporter.java new file mode 100644 index 00000000..7e2c3db6 --- /dev/null +++ b/approvaltests/src/main/java/org/approvaltests/reporters/BeyondCompareReporter.java @@ -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 + ); + } +} diff --git a/approvaltests/src/main/java/org/approvaltests/reporters/DefaultFrontLoadedReporter.java b/approvaltests/src/main/java/org/approvaltests/reporters/DefaultFrontLoadedReporter.java index 29895910..0d0867a4 100644 --- a/approvaltests/src/main/java/org/approvaltests/reporters/DefaultFrontLoadedReporter.java +++ b/approvaltests/src/main/java/org/approvaltests/reporters/DefaultFrontLoadedReporter.java @@ -7,7 +7,8 @@ public DefaultFrontLoadedReporter() { super( // begin-snippet: default_front_loaded_reporter - PitReporter.INSTANCE + PitReporter.INSTANCE, + EnvironmentVariableReporter.INSTANCE // end-snippet ); } diff --git a/approvaltests/src/main/java/org/approvaltests/reporters/EnvironmentVariableReporter.java b/approvaltests/src/main/java/org/approvaltests/reporters/EnvironmentVariableReporter.java new file mode 100644 index 00000000..361a5b2a --- /dev/null +++ b/approvaltests/src/main/java/org/approvaltests/reporters/EnvironmentVariableReporter.java @@ -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> 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); + } +} diff --git a/approvaltests/src/main/java/org/approvaltests/reporters/VisualStudioCodeReporter.java b/approvaltests/src/main/java/org/approvaltests/reporters/VisualStudioCodeReporter.java new file mode 100644 index 00000000..16d77a2c --- /dev/null +++ b/approvaltests/src/main/java/org/approvaltests/reporters/VisualStudioCodeReporter.java @@ -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 + ); + } +}