From b23c767a27adbeb0ecabb4a8e9e6dc7941ee5610 Mon Sep 17 00:00:00 2001 From: Scott Wierschem Date: Thu, 31 Oct 2024 16:23:22 -0500 Subject: [PATCH] - F Add logger of approved files --- .gitignore | 1 + .../approvaltests/ApprovedFileLogTest.java | 22 +++++++++++++++++++ ...provedFileLogTest.testLogging.approved.txt | 1 + .../main/java/com/spun/util/io/FileUtils.java | 22 +++++++++++++++---- .../org/approvaltests/ApprovedFileLog.java | 21 ++++++++++++++++++ .../approvaltests/approvers/FileApprover.java | 2 ++ 6 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.java create mode 100644 approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.testLogging.approved.txt create mode 100644 approvaltests/src/main/java/org/approvaltests/ApprovedFileLog.java diff --git a/.gitignore b/.gitignore index c150a449..7886e2e6 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ __pycache__/ *.bak .gitmessage +.approval_tests_temp/ \ No newline at end of file diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.java b/approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.java new file mode 100644 index 00000000..9a284a02 --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.java @@ -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())); + } +} diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.testLogging.approved.txt b/approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.testLogging.approved.txt new file mode 100644 index 00000000..67aed78e --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/ApprovedFileLogTest.testLogging.approved.txt @@ -0,0 +1 @@ +anything \ No newline at end of file diff --git a/approvaltests-util/src/main/java/com/spun/util/io/FileUtils.java b/approvaltests-util/src/main/java/com/spun/util/io/FileUtils.java index 7768ff9b..d884ff37 100644 --- a/approvaltests-util/src/main/java/com/spun/util/io/FileUtils.java +++ b/approvaltests-util/src/main/java/com/spun/util/io/FileUtils.java @@ -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; @@ -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 diff --git a/approvaltests/src/main/java/org/approvaltests/ApprovedFileLog.java b/approvaltests/src/main/java/org/approvaltests/ApprovedFileLog.java new file mode 100644 index 00000000..ce8243f4 --- /dev/null +++ b/approvaltests/src/main/java/org/approvaltests/ApprovedFileLog.java @@ -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"); + } +} diff --git a/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java b/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java index a4b4ead4..2b9c6c81 100644 --- a/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java +++ b/approvaltests/src/main/java/org/approvaltests/approvers/FileApprover.java @@ -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; @@ -33,6 +34,7 @@ public FileApprover(ApprovalWriter writer, ApprovalNamer namer, Function2