Skip to content

Commit

Permalink
- F Add logger of approved files
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottBob committed Oct 31, 2024
1 parent cc292a1 commit b23c767
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ __pycache__/
*.bak

.gitmessage
.approval_tests_temp/
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.approvaltests;

import com.spun.util.io.FileUtils;
import org.approvaltests.namer.ApprovalNamer;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.io.File;

public class ApprovedFileLogTest {
@Test
void testLogging() {
File file = ApprovedFileLog.get();
ApprovalNamer approvalNamer = Approvals.createApprovalNamer();
File approvedFile = approvalNamer.getApprovedFile(".txt");
String prelog = FileUtils.readFile(file);
Assert.assertFalse(prelog.contains(approvedFile.getAbsolutePath()));
Approvals.verify("anything");
String postlog = FileUtils.readFile(file);
Assert.assertTrue(postlog.contains(approvedFile.getAbsolutePath()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
anything
22 changes: 18 additions & 4 deletions approvaltests-util/src/main/java/com/spun/util/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -157,6 +154,23 @@ public static void copyFileToDirectory(String file, File tempDir)
throw ObjectUtils.throwAsError(t);
}
}

public static void appendToFile(File file, String text)
{
try
{
Asserts.assertNotNull("Writing to file: " + file, text);
file.getCanonicalFile().getParentFile().mkdirs();
try (BufferedWriter out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8, StandardOpenOption.APPEND))
{
out.write(text);
}
}
catch (Throwable t)
{
throw ObjectUtils.throwAsError(t);
}
}
public static void writeFile(File file, String text)
{
try
Expand Down
21 changes: 21 additions & 0 deletions approvaltests/src/main/java/org/approvaltests/ApprovedFileLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.approvaltests;

import com.spun.util.io.FileUtils;

import java.io.File;

public class ApprovedFileLog {
static {
FileUtils.writeFile(get(), "");
}
public static File get() {
File file = new File(".approval_tests_temp/.approved_files.log");
FileUtils.createIfNeeded(file.getAbsolutePath());
return file;
}

public static void log(File file) {
File log = get();
FileUtils.appendToFile(log, file.getAbsolutePath() + "\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.spun.util.ObjectUtils;
import com.spun.util.io.FileUtils;
import org.approvaltests.ApprovedFileLog;
import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.core.ApprovalReporterWithCleanUp;
import org.approvaltests.core.ApprovalWriter;
Expand Down Expand Up @@ -33,6 +34,7 @@ public FileApprover(ApprovalWriter writer, ApprovalNamer namer, Function2<File,
public VerifyResult approve()
{
tracker.assertUnique(approved.getAbsolutePath());
ApprovedFileLog.log(approved);
received = writer.writeReceivedFile(received);
return approver.call(received, approved);
}
Expand Down

0 comments on commit b23c767

Please sign in to comment.