Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the "mirror" command to completely mirror gCLI #942

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
89ba30b
Change the "mirror" command to completely mirror gCLI instead of prev…
libraryaddict Jul 30, 2022
8f945d6
Partially revert "Change the "mirror" command to completely mirror gC…
libraryaddict Jul 31, 2022
bcddeb8
Use the existing mirror code, but ensure html is logged
libraryaddict Aug 1, 2022
a854e18
Merge branch 'main' into mirror-gcli
libraryaddict Aug 1, 2022
cb4dbc1
Create MirrorCommandTest.java
jaadams5 Aug 2, 2022
14a0f01
Update MirrorCommandTest.java
jaadams5 Aug 2, 2022
b567aad
Works
jaadams5 Aug 2, 2022
df9de9e
Merge branch 'mirror' into pr/942
jaadams5 Aug 2, 2022
abdb936
Update RequestLogger.java
jaadams5 Aug 2, 2022
7151b7a
tweaks
jaadams5 Aug 3, 2022
bb0e5c1
Add printList tests
libraryaddict Aug 4, 2022
f6f1560
Merge branch 'main' into pr/942
jaadams5 Aug 4, 2022
1450402
Update RequestLogger.java
jaadams5 Aug 4, 2022
fd392f4
Update MirrorCommandTest.java
jaadams5 Aug 4, 2022
f544e4b
Lint etc.
jaadams5 Aug 4, 2022
3473688
Use equals comparison instead of contains
libraryaddict Aug 5, 2022
3b91237
Stop mirror logging after each test as well
libraryaddict Aug 5, 2022
26da8e8
Initial
jaadams5 Aug 5, 2022
2a7b5d1
Update MirrorCommandTest.java
jaadams5 Aug 5, 2022
25d67b4
Merge branch 'main' into MirrorTest
jaadams5 Aug 9, 2022
b3bdbb7
Update MirrorCommandTest.java
jaadams5 Aug 9, 2022
4051670
Update MirrorCommandTest.java
jaadams5 Aug 9, 2022
80cbfef
Merge branch 'main' into MirrorTest
jaadams5 Aug 10, 2022
eca35f7
Update MirrorCommandTest.java
jaadams5 Aug 10, 2022
ec03f4d
Merge branch 'main' into MirrorTest
jaadams5 Aug 10, 2022
e9f3827
Merge branch 'main' into MirrorTest
jaadams5 Aug 11, 2022
8042682
Merge branch 'MirrorTest' into pr/942
jaadams5 Aug 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 ) {
libraryaddict marked this conversation as resolved.
Show resolved Hide resolved
// 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;
libraryaddict marked this conversation as resolved.
Show resolved Hide resolved

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";
gausie marked this conversation as resolved.
Show resolved Hide resolved
}

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"));
}
}