Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.approvaltests.reporters;

import com.spun.util.io.FileUtils;
import org.approvaltests.Approvals;
import org.approvaltests.core.Options;
import org.approvaltests.namer.NamerWrapper;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AutoApproveWhenPropertySetReporterTest
{
@Test
void testWhenPropertyIsSet()
{
NamerWrapper namer = new NamerWrapper(Approvals.createApprovalNamer());
cleanupFiles(namer);
System.setProperty("org.approvaltests.reporters.autoapprove", "true");
Approvals.verify("applesauce", new Options().withReporter(new AutoApproveWhenPropertySetReporter()));
assertTrue(namer.getApprovalFile(".txt").exists());
assertFalse(namer.getReceivedFile((".txt")).exists());
cleanupFiles(namer);
}
@Test
void testWhenPropertyNotSet()
{
System.clearProperty("org.approvaltests.reporters.autoapprove");
NamerWrapper namer = new NamerWrapper(Approvals.createApprovalNamer());
cleanupFiles(namer);
try
{
Approvals.verify("applesauce",
new Options().withReporter(new AutoApproveWhenPropertySetReporter(new UseReporterTest.TestReporter(),
"org.approvaltests.reporters.autoapprove")));
}
catch (Throwable t)
{
assertTrue(t.getMessage().startsWith("Failed Approval"));
}
assertFalse(namer.getApprovalFile(".txt").exists());
assertTrue(namer.getReceivedFile(".txt").exists());
cleanupFiles(namer);
}
private static void cleanupFiles(NamerWrapper namer)
{
FileUtils.delete(namer.getApprovalFile(".txt"));
FileUtils.delete(namer.getReceivedFile((".txt")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.approvaltests.reporters;

import com.spun.util.io.FileUtils;
import org.approvaltests.Approvals;
import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.core.VerifyResult;

import java.io.File;

public class AutoApproveWhenPropertySetReporter implements ReporterWithApprovalPower
{
private final ApprovalFailureReporter reporter;
private final String property;
private VerifyResult result;
public AutoApproveWhenPropertySetReporter()
{
this(Approvals.getReporter(), "org.approvaltests.reporters.autoapprove");
}
public AutoApproveWhenPropertySetReporter(ApprovalFailureReporter reporter, String property)
{
this.reporter = reporter;
this.property = property;
}
@Override
public boolean report(String received, String approved)
{
String v = System.getProperty(property);
if ("".equals(v) || "true".equalsIgnoreCase(v))
{
FileUtils.copyFile(new File(received), new File(approved));
result = VerifyResult.SUCCESS;
}
else
{
reporter.report(received, approved);
result = VerifyResult.FAILURE;
}
return true;
}
@Override
public VerifyResult approveWhenReported()
{
return result;
}
}