Skip to content

Commit

Permalink
Change the "mirror" command to completely mirror gCLI instead of prev…
Browse files Browse the repository at this point in the history
…ious usage
  • Loading branch information
libraryaddict committed Jul 30, 2022
1 parent 5dd54dc commit 89ba30b
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 11 deletions.
20 changes: 18 additions & 2 deletions lib/net/java/dev/spellcast/utilities/ChatBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,16 @@ public JScrollPane addDisplay( final JEditorPane displayPane )
* Sets the log file used to actively record messages that are being stored in the buffer.
*/

public void setLogFile( final File f )
{
public void setLogFile( final File f ) {
// If the new file is null but the existing file is not null
// We should treat this as a log writer close request
if (f == null && this.logFile != null) {
this.logWriter.close();
this.logWriter = null;
this.logFile = null;
return;
}

if ( f == null || this.title == null )
{
return;
Expand All @@ -150,6 +158,14 @@ public void setLogFile( final File f )
return;
}

// If the existing file is not null and would be using a different log writer
if (this.logFile != null && ChatBuffer.ACTIVE_LOG_FILES.get(filename) != this.logWriter) {
// Close the existing log writer to prevent a resource leak
this.logWriter.close();
this.logWriter = null;
this.logFile = null;
}

if ( ChatBuffer.ACTIVE_LOG_FILES.containsKey( filename ) )
{
this.logFile = f;
Expand Down
9 changes: 3 additions & 6 deletions src/net/sourceforge/kolmafia/RequestLogger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sourceforge.kolmafia;

import java.io.File;
import java.io.PrintStream;
import java.util.Date;
import java.util.List;
Expand All @@ -21,7 +22,6 @@ public class RequestLogger extends NullStream {
public static final RequestLogger INSTANCE = new RequestLogger();

private static PrintStream outputStream = KoLmafiaTUI.outputStream;
private static PrintStream mirrorStream = NullStream.INSTANCE;

private static PrintStream sessionStream = NullStream.INSTANCE;
private static PrintStream debugStream = NullStream.INSTANCE;
Expand Down Expand Up @@ -110,7 +110,6 @@ public static final void printLine(final MafiaState state, String message, boole
RequestLogger.previousUpdateString = message;

RequestLogger.outputStream.println(message);
RequestLogger.mirrorStream.println(message);
RequestLogger.debugStream.println(message);

if (StaticEntity.backtraceTrigger != null && message.contains(StaticEntity.backtraceTrigger)) {
Expand Down Expand Up @@ -211,13 +210,11 @@ public static final void closeCustom() {
}

public static final void openMirror(final String location) {
RequestLogger.mirrorStream =
RequestLogger.openStream(location, RequestLogger.mirrorStream, true);
KoLConstants.commandBuffer.setLogFile(new File(KoLConstants.ROOT_LOCATION, location));
}

public static final void closeMirror() {
RequestLogger.closeStream(RequestLogger.mirrorStream);
RequestLogger.mirrorStream = NullStream.INSTANCE;
KoLConstants.commandBuffer.setLogFile(null);
}

public static final PrintStream getSessionStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class MirrorLogCommand extends AbstractCommand {
public MirrorLogCommand() {
this.usage = " [<filename>] - stop [or start] logging to an additional file.";
this.usage = " [<filename>] - stop [or start] gCLI logging to an additional file.";
}

@Override
Expand All @@ -20,8 +20,8 @@ public void run(final String command, String parameters) {
RequestLogger.closeMirror();
KoLmafia.updateDisplay("Mirror stream closed.");
} else {
if (!parameters.endsWith(".txt")) {
parameters += ".txt";
if (!parameters.endsWith(".txt") && !parameters.endsWith(".html")) {
parameters += ".html";
}

RequestLogger.openMirror(parameters);
Expand Down
134 changes: 134 additions & 0 deletions test/net/sourceforge/kolmafia/textui/command/MirrorCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package net.sourceforge.kolmafia.textui.command;

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

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import net.sourceforge.kolmafia.KoLConstants;
import net.sourceforge.kolmafia.RequestLogger;
import net.sourceforge.kolmafia.utilities.FileUtilities;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.CsvSource;

public class MirrorCommandTest extends AbstractCommandTestBase {

public MirrorCommandTest() {
this.command = "mirror";
}

@BeforeEach
public void stopMirrorLogging() {
execute("");
}

private String getMirrorLog(String fileName) {
StringBuilder builder = new StringBuilder();

try (BufferedReader reader =
FileUtilities.getReader(new File(KoLConstants.ROOT_LOCATION, "chats/" + fileName))) {
String line;

// Read the full file to a string
while ((line = FileUtilities.readLine(reader)) != null) {
if (builder.length() > 0) {
builder.append("\n");
}

builder.append(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
}

return builder.toString();
}

@Test
public void testMirrorWritesHTML() {
// Open the mirror
execute("chats/test_writes_html");

// When `> ` is used as a prefix, the RequestLogger will colorize it assuming its a command
// input.
RequestLogger.printLine("> Fake command input");

// Log another line, this should be raw.
RequestLogger.printLine("Raw Line");

// Close the mirror
execute("");

String mirrorOutput = getMirrorLog("test_writes_html.html");

assertTrue(mirrorOutput.contains("<font color=olive>> Fake command input</font><br>"));
assertTrue(mirrorOutput.contains("Raw Line<br>"));
}

@Test
public void testMirrorClose() {
execute("chats/test_mirror_closes");

// Write to the mirror log
RequestLogger.printLine("Some input");

// Close the mirror
execute("");

// The first test, we're checking that CommandBuffer has no file
assertNull(KoLConstants.commandBuffer.getLogFile());

// The second test, write to the command buffer again
RequestLogger.printLine("Should not be seen");

// Now read the log and verify that it was not written
String mirrorOutput = getMirrorLog("test_mirror_closes.html");

// Test that the second line is not in the output
assertFalse(mirrorOutput.contains("Should not be seen"));
}

@Test
public void testMultipleMirrorsOpened() {
execute("chats/mirror_1");

// Write to the mirror log
RequestLogger.printLine("Mirror 1 Log");

// Switch mirrors to mirror_2
execute("chats/mirror_2");

// Write to the mirror log
RequestLogger.printLine("Mirror 2 Log");

// Close the mirror
execute("");

// Assert that mirror_1 only contains Mirror 1 Log
assertTrue(getMirrorLog("mirror_1.html").contains("Mirror 1 Log"));
assertFalse(getMirrorLog("mirror_2.html").contains("Mirror 1 Log"));

// Assert that mirror_2 only contains Mirror 2 Log
assertFalse(getMirrorLog("mirror_2.html").contains("Mirror 1 Log"));
assertTrue(getMirrorLog("mirror_2.html").contains("Mirror 2 Log"));
}

@CsvSource({
"file,file.html",
"file.html,file.html",
"file.txt,file.txt",
"file.csv,file.csv.html"
})
public void testMirrorFileNames(String mirrorName, String fileName) {
execute("chats/" + mirrorName);

RequestLogger.printLine("Filler Line");

execute("");

assertTrue(getMirrorLog(fileName).contains("Filler Line"));
}
}

0 comments on commit 89ba30b

Please sign in to comment.